X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/b37bf2e156556c589aea3e1f58a377f2b1189665..4e4e5a6f2694187498445a6ac6f1634ce8141119:/wtf/HashTraits.h diff --git a/wtf/HashTraits.h b/wtf/HashTraits.h index 5f35278..c8d40f7 100644 --- a/wtf/HashTraits.h +++ b/wtf/HashTraits.h @@ -1,7 +1,5 @@ -// -*- mode: c++; c-basic-offset: 4 -*- /* - * This file is part of the KDE libraries - * Copyright (C) 2005, 2006 Apple Computer, Inc. + * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -23,8 +21,8 @@ #ifndef WTF_HashTraits_h #define WTF_HashTraits_h -#include "Assertions.h" #include "HashFunctions.h" +#include "TypeTraits.h" #include #include @@ -33,219 +31,82 @@ namespace WTF { using std::pair; using std::make_pair; - template struct IsInteger { static const bool value = false; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - template<> struct IsInteger { static const bool value = true; }; - - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_bool_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_char_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_signed_char_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_unsigned_char_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_short_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_unsigned_short_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_int_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_unsigned_int_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_long_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_unsigned_long_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_long_long_true); - COMPILE_ASSERT(IsInteger::value, WTF_IsInteger_unsigned_long_long_true); - - COMPILE_ASSERT(!IsInteger::value, WTF_IsInteger_char_pointer_false); - COMPILE_ASSERT(!IsInteger::value, WTF_IsInteger_const_char_pointer_false); - COMPILE_ASSERT(!IsInteger::value, WTF_IsInteger_volatile_char_pointer__false); - COMPILE_ASSERT(!IsInteger::value, WTF_IsInteger_double_false); - COMPILE_ASSERT(!IsInteger::value, WTF_IsInteger_float_false); - template struct HashTraits; template struct GenericHashTraitsBase; - template struct GenericHashTraitsBase { - typedef T TraitType; - typedef HashTraits::SignedType> StorageTraits; - static const bool emptyValueIsZero = true; - static const bool needsDestruction = false; - }; + template struct GenericHashTraitsBase { - typedef T TraitType; - typedef HashTraits StorageTraits; static const bool emptyValueIsZero = false; static const bool needsDestruction = true; }; + // Default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned). + template struct GenericHashTraitsBase { + static const bool emptyValueIsZero = true; + static const bool needsDestruction = false; + static void constructDeletedValue(T& slot) { slot = static_cast(-1); } + static bool isDeletedValue(T value) { return value == static_cast(-1); } + }; + template struct GenericHashTraits : GenericHashTraitsBase::value, T> { + typedef T TraitType; static T emptyValue() { return T(); } - static const bool needsRef = false; }; template struct HashTraits : GenericHashTraits { }; - // signed integer traits may not be appropriate for all uses since they disallow 0 and -1 as keys - template<> struct HashTraits : GenericHashTraits { - static signed char deletedValue() { return -1; } - }; - template<> struct HashTraits : GenericHashTraits { - static short deletedValue() { return -1; } - }; - template<> struct HashTraits : GenericHashTraits { - static int deletedValue() { return -1; } - }; - template<> struct HashTraits : GenericHashTraits { - static unsigned int deletedValue() { return static_cast(-1); } - }; - template<> struct HashTraits : GenericHashTraits { - static long deletedValue() { return -1; } - }; - template<> struct HashTraits : GenericHashTraits { - static unsigned long deletedValue() { return static_cast(-1); } - }; - template<> struct HashTraits : GenericHashTraits { - static long long deletedValue() { return -1; } - }; - template<> struct HashTraits : GenericHashTraits { - static unsigned long long deletedValue() { return static_cast(-1); } - }; - - template struct FloatHashTraits { - typedef T TraitType; - typedef HashTraits StorageTraits; + template struct FloatHashTraits : GenericHashTraits { + static const bool needsDestruction = false; static T emptyValue() { return std::numeric_limits::infinity(); } - static T deletedValue() { return -std::numeric_limits::infinity(); } + static void constructDeletedValue(T& slot) { slot = -std::numeric_limits::infinity(); } + static bool isDeletedValue(T value) { return value == -std::numeric_limits::infinity(); } + }; + + template<> struct HashTraits : FloatHashTraits { }; + template<> struct HashTraits : FloatHashTraits { }; + + // Default unsigned traits disallow both 0 and max as keys -- use these traits to allow zero and disallow max - 1. + template struct UnsignedWithZeroKeyHashTraits : GenericHashTraits { static const bool emptyValueIsZero = false; static const bool needsDestruction = false; - static const bool needsRef = false; - }; - template<> struct HashTraits : FloatHashTraits { - }; - template<> struct HashTraits : FloatHashTraits { + static T emptyValue() { return std::numeric_limits::max(); } + static void constructDeletedValue(T& slot) { slot = std::numeric_limits::max() - 1; } + static bool isDeletedValue(T value) { return value == std::numeric_limits::max() - 1; } }; template struct HashTraits : GenericHashTraits { - typedef HashTraits::SignedType> StorageTraits; static const bool emptyValueIsZero = true; static const bool needsDestruction = false; - static P* deletedValue() { return reinterpret_cast(-1); } + static void constructDeletedValue(P*& slot) { slot = reinterpret_cast(-1); } + static bool isDeletedValue(P* value) { return value == reinterpret_cast(-1); } }; template struct HashTraits > : GenericHashTraits > { - typedef HashTraits::SignedType> StorageTraits; - typedef typename StorageTraits::TraitType StorageType; static const bool emptyValueIsZero = true; - static const bool needsRef = true; - - typedef union { - P* m_p; - StorageType m_s; - } UnionType; - - static void ref(const StorageType& s) - { - if (const P* p = reinterpret_cast(&s)->m_p) - const_cast(p)->ref(); - } - static void deref(const StorageType& s) - { - if (const P* p = reinterpret_cast(&s)->m_p) - const_cast(p)->deref(); - } - }; - - // template to set deleted values - - template struct DeletedValueAssigner { - static void assignDeletedValue(typename Traits::TraitType& location) { location = Traits::deletedValue(); } + static void constructDeletedValue(RefPtr

& slot) { new (&slot) RefPtr

(HashTableDeletedValue); } + static bool isDeletedValue(const RefPtr

& value) { return value.isHashTableDeletedValue(); } }; - template inline void assignDeleted(T& location) - { - DeletedValueAssigner::assignDeletedValue(location); - } - // special traits for pairs, helpful for their use in HashMap implementation - template struct PairHashTraits; - template - struct PairBaseHashTraits : GenericHashTraits > { + struct PairHashTraits : GenericHashTraits > { typedef FirstTraitsArg FirstTraits; typedef SecondTraitsArg SecondTraits; typedef pair TraitType; - typedef PairHashTraits StorageTraits; - static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; - - static TraitType emptyValue() - { - return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); - } - }; - - template - struct PairHashTraits : PairBaseHashTraits { - typedef pair TraitType; + static TraitType emptyValue() { return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; - static TraitType deletedValue() - { - return TraitType(FirstTraits::deletedValue(), SecondTraits::emptyValue()); - } - - static void assignDeletedValue(TraitType& location) - { - assignDeleted(location.first); - location.second = SecondTraits::emptyValue(); - } + static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); } + static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); } }; template struct HashTraits > : public PairHashTraits, HashTraits > { }; - template - struct DeletedValueAssigner > - { - static void assignDeletedValue(pair& location) - { - PairHashTraits::assignDeletedValue(location); - } - }; - - template - struct DeletedValueAssigner > > - { - static void assignDeletedValue(pair& location) - { - HashTraits >::assignDeletedValue(location); - } - }; - - // hash functions and traits that are equivalent (for code sharing) - - template struct HashKeyStorageTraits { - typedef HashArg Hash; - typedef TraitsArg Traits; - }; - template struct HashKeyStorageTraits, HashTraits > { - typedef typename IntTypes::SignedType IntType; - typedef IntHash Hash; - typedef HashTraits Traits; - }; - template struct HashKeyStorageTraits >, HashTraits > > { - typedef typename IntTypes::SignedType IntType; - typedef IntHash Hash; - typedef HashTraits Traits; - }; - } // namespace WTF using WTF::HashTraits;