diff -r 000000000000 -r 4f2f89ce4247 JavaScriptCore/wtf/HashTraits.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/JavaScriptCore/wtf/HashTraits.h Fri Sep 17 09:02:29 2010 +0300 @@ -0,0 +1,115 @@ +/* + * 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 + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef WTF_HashTraits_h +#define WTF_HashTraits_h + +#include "HashFunctions.h" +#include "TypeTraits.h" +#include +#include + +namespace WTF { + + using std::pair; + using std::make_pair; + + template struct HashTraits; + + template struct GenericHashTraitsBase; + + template struct GenericHashTraitsBase { + 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(); } + }; + + template struct HashTraits : GenericHashTraits { }; + + template struct FloatHashTraits : GenericHashTraits { + static const bool needsDestruction = false; + static T emptyValue() { 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 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 { + static const bool emptyValueIsZero = true; + static const bool needsDestruction = false; + static void constructDeletedValue(P*& slot) { slot = reinterpret_cast(-1); } + static bool isDeletedValue(P* value) { return value == reinterpret_cast(-1); } + }; + + template struct HashTraits > : GenericHashTraits > { + static const bool emptyValueIsZero = true; + static void constructDeletedValue(RefPtr

& slot) { new (&slot) RefPtr

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

& value) { return value.isHashTableDeletedValue(); } + }; + + // special traits for pairs, helpful for their use in HashMap implementation + + template + struct PairHashTraits : GenericHashTraits > { + typedef FirstTraitsArg FirstTraits; + typedef SecondTraitsArg SecondTraits; + typedef pair TraitType; + + static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; + static TraitType emptyValue() { return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } + + static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; + + 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 > { }; + +} // namespace WTF + +using WTF::HashTraits; +using WTF::PairHashTraits; + +#endif // WTF_HashTraits_h