2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 David Levin <levin@chromium.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef WTF_HashTable_h
23 #define WTF_HashTable_h
25 #include <wtf/Alignment.h>
26 #include <wtf/Assertions.h>
27 #include <wtf/DataLog.h>
28 #include <wtf/FastMalloc.h>
29 #include <wtf/HashTraits.h>
30 #include <wtf/StdLibExtras.h>
31 #include <wtf/Threading.h>
32 #include <wtf/ValueCheck.h>
35 // Required for CHECK_HASHTABLE_ITERATORS.
36 #include <wtf/OwnPtr.h>
37 #include <wtf/PassOwnPtr.h>
42 #define DUMP_HASHTABLE_STATS 0
43 #define DUMP_HASHTABLE_STATS_PER_TABLE 0
45 // Enables internal WTF consistency checks that are invoked automatically. Non-WTF callers can call checkTableConsistency() even if internal checks are disabled.
46 #define CHECK_HASHTABLE_CONSISTENCY 0
49 #define CHECK_HASHTABLE_ITERATORS 0
50 #define CHECK_HASHTABLE_USE_AFTER_DESTRUCTION 0
52 #define CHECK_HASHTABLE_ITERATORS 1
53 #define CHECK_HASHTABLE_USE_AFTER_DESTRUCTION 1
56 #if DUMP_HASHTABLE_STATS
58 struct HashTableStats {
59 // The following variables are all atomically incremented when modified.
60 WTF_EXPORTDATA static int numAccesses;
61 WTF_EXPORTDATA static int numRehashes;
62 WTF_EXPORTDATA static int numRemoves;
63 WTF_EXPORTDATA static int numReinserts;
65 // The following variables are only modified in the recordCollisionAtCount method within a mutex.
66 WTF_EXPORTDATA static int maxCollisions;
67 WTF_EXPORTDATA static int numCollisions;
68 WTF_EXPORTDATA static int collisionGraph[4096];
70 WTF_EXPORT_PRIVATE static void recordCollisionAtCount(int count);
71 WTF_EXPORT_PRIVATE static void dumpStats();
76 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
78 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
79 class HashTableIterator;
80 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
81 class HashTableConstIterator;
83 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
84 void addIterator(const HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>*,
85 HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>*);
87 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
88 void removeIterator(HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>*);
90 #if !CHECK_HASHTABLE_ITERATORS
92 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
93 inline void addIterator(const HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>*,
94 HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>*) { }
96 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
97 inline void removeIterator(HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>*) { }
101 typedef enum { HashItemKnownGood } HashItemKnownGoodTag;
103 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
104 class HashTableConstIterator {
106 typedef HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> HashTableType;
107 typedef HashTableIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> iterator;
108 typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> const_iterator;
109 typedef Value ValueType;
110 typedef const ValueType& ReferenceType;
111 typedef const ValueType* PointerType;
113 friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>;
114 friend class HashTableIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>;
116 void skipEmptyBuckets()
118 while (m_position != m_endPosition && HashTableType::isEmptyOrDeletedBucket(*m_position))
122 HashTableConstIterator(const HashTableType* table, PointerType position, PointerType endPosition)
123 : m_position(position), m_endPosition(endPosition)
125 addIterator(table, this);
129 HashTableConstIterator(const HashTableType* table, PointerType position, PointerType endPosition, HashItemKnownGoodTag)
130 : m_position(position), m_endPosition(endPosition)
132 addIterator(table, this);
136 HashTableConstIterator()
138 addIterator(static_cast<const HashTableType*>(0), this);
141 // default copy, assignment and destructor are OK if CHECK_HASHTABLE_ITERATORS is 0
143 #if CHECK_HASHTABLE_ITERATORS
144 ~HashTableConstIterator()
146 removeIterator(this);
149 HashTableConstIterator(const const_iterator& other)
150 : m_position(other.m_position), m_endPosition(other.m_endPosition)
152 addIterator(other.m_table, this);
155 const_iterator& operator=(const const_iterator& other)
157 m_position = other.m_position;
158 m_endPosition = other.m_endPosition;
160 removeIterator(this);
161 addIterator(other.m_table, this);
167 PointerType get() const
172 ReferenceType operator*() const { return *get(); }
173 PointerType operator->() const { return get(); }
175 const_iterator& operator++()
178 ASSERT(m_position != m_endPosition);
184 // postfix ++ intentionally omitted
187 bool operator==(const const_iterator& other) const
189 checkValidity(other);
190 return m_position == other.m_position;
192 bool operator!=(const const_iterator& other) const
194 checkValidity(other);
195 return m_position != other.m_position;
197 bool operator==(const iterator& other) const
199 return *this == static_cast<const_iterator>(other);
201 bool operator!=(const iterator& other) const
203 return *this != static_cast<const_iterator>(other);
207 void checkValidity() const
209 #if CHECK_HASHTABLE_ITERATORS
215 #if CHECK_HASHTABLE_ITERATORS
216 void checkValidity(const const_iterator& other) const
219 ASSERT_UNUSED(other, other.m_table);
220 ASSERT(m_table == other.m_table);
223 void checkValidity(const const_iterator&) const { }
226 PointerType m_position;
227 PointerType m_endPosition;
229 #if CHECK_HASHTABLE_ITERATORS
231 // Any modifications of the m_next or m_previous of an iterator that is in a linked list of a HashTable::m_iterator,
232 // should be guarded with m_table->m_mutex.
233 mutable const HashTableType* m_table;
234 mutable const_iterator* m_next;
235 mutable const_iterator* m_previous;
239 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
240 class HashTableIterator {
242 typedef HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> HashTableType;
243 typedef HashTableIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> iterator;
244 typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> const_iterator;
245 typedef Value ValueType;
246 typedef ValueType& ReferenceType;
247 typedef ValueType* PointerType;
249 friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>;
251 HashTableIterator(HashTableType* table, PointerType pos, PointerType end) : m_iterator(table, pos, end) { }
252 HashTableIterator(HashTableType* table, PointerType pos, PointerType end, HashItemKnownGoodTag tag) : m_iterator(table, pos, end, tag) { }
255 HashTableIterator() { }
257 // default copy, assignment and destructor are OK
259 PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
260 ReferenceType operator*() const { return *get(); }
261 PointerType operator->() const { return get(); }
263 iterator& operator++() { ++m_iterator; return *this; }
265 // postfix ++ intentionally omitted
268 bool operator==(const iterator& other) const { return m_iterator == other.m_iterator; }
269 bool operator!=(const iterator& other) const { return m_iterator != other.m_iterator; }
270 bool operator==(const const_iterator& other) const { return m_iterator == other; }
271 bool operator!=(const const_iterator& other) const { return m_iterator != other; }
273 operator const_iterator() const { return m_iterator; }
276 const_iterator m_iterator;
281 // Work around MSVC's standard library, whose swap for pairs does not swap by component.
282 template<typename T> inline void hashTableSwap(T& a, T& b)
287 template<typename T, typename U> inline void hashTableSwap(KeyValuePair<T, U>& a, KeyValuePair<T, U>& b)
289 swap(a.first, b.first);
290 swap(a.second, b.second);
293 template<typename T, bool useSwap> struct Mover;
294 template<typename T> struct Mover<T, true> { static void move(T& from, T& to) { hashTableSwap(from, to); } };
295 template<typename T> struct Mover<T, false> { static void move(T& from, T& to) { to = from; } };
297 template<typename HashFunctions> class IdentityHashTranslator {
299 template<typename T> static unsigned hash(const T& key) { return HashFunctions::hash(key); }
300 template<typename T> static bool equal(const T& a, const T& b) { return HashFunctions::equal(a, b); }
301 template<typename T, typename U> static void translate(T& location, const U&, const T& value) { location = value; }
304 template<typename IteratorType> struct HashTableAddResult {
305 HashTableAddResult(IteratorType iter, bool isNewEntry) : iterator(iter), isNewEntry(isNewEntry) { }
306 IteratorType iterator;
310 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
313 typedef HashTableIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> iterator;
314 typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> const_iterator;
315 typedef Traits ValueTraits;
317 typedef Value ValueType;
318 typedef IdentityHashTranslator<HashFunctions> IdentityTranslatorType;
319 typedef HashTableAddResult<iterator> AddResult;
321 #if DUMP_HASHTABLE_STATS_PER_TABLE
341 int collisionGraph[4096];
343 void recordCollisionAtCount(int count)
345 if (count > maxCollisions)
346 maxCollisions = count;
348 collisionGraph[count]++;
353 dataLog("\nWTF::HashTable::Stats dump\n\n");
354 dataLog("%d accesses\n", numAccesses);
355 dataLog("%d total collisions, average %.2f probes per access\n", numCollisions, 1.0 * (numAccesses + numCollisions) / numAccesses);
356 dataLog("longest collision chain: %d\n", maxCollisions);
357 for (int i = 1; i <= maxCollisions; i++) {
358 dataLog(" %d lookups with exactly %d collisions (%.2f%% , %.2f%% with this many or more)\n", collisionGraph[i], i, 100.0 * (collisionGraph[i] - collisionGraph[i+1]) / numAccesses, 100.0 * collisionGraph[i] / numAccesses);
360 dataLog("%d rehashes\n", numRehashes);
361 dataLog("%d reinserts\n", numReinserts);
369 invalidateIterators();
371 deallocateTable(m_table, m_tableSize);
372 #if CHECK_HASHTABLE_USE_AFTER_DESTRUCTION
373 m_table = (ValueType*)(uintptr_t)0xbbadbeef;
377 HashTable(const HashTable&);
378 void swap(HashTable&);
379 HashTable& operator=(const HashTable&);
381 // When the hash table is empty, just return the same iterator for end as for begin.
382 // This is more efficient because we don't have to skip all the empty and deleted
383 // buckets, and iterating an empty table is a common case that's worth optimizing.
384 iterator begin() { return isEmpty() ? end() : makeIterator(m_table); }
385 iterator end() { return makeKnownGoodIterator(m_table + m_tableSize); }
386 const_iterator begin() const { return isEmpty() ? end() : makeConstIterator(m_table); }
387 const_iterator end() const { return makeKnownGoodConstIterator(m_table + m_tableSize); }
389 int size() const { return m_keyCount; }
390 int capacity() const { return m_tableSize; }
391 bool isEmpty() const { return !m_keyCount; }
393 AddResult add(const ValueType& value) { return add<IdentityTranslatorType>(Extractor::extract(value), value); }
395 // A special version of add() that finds the object by hashing and comparing
396 // with some other type, to avoid the cost of type conversion if the object is already
398 template<typename HashTranslator, typename T, typename Extra> AddResult add(const T& key, const Extra&);
399 template<typename HashTranslator, typename T, typename Extra> AddResult addPassingHashCode(const T& key, const Extra&);
401 iterator find(const KeyType& key) { return find<IdentityTranslatorType>(key); }
402 const_iterator find(const KeyType& key) const { return find<IdentityTranslatorType>(key); }
403 bool contains(const KeyType& key) const { return contains<IdentityTranslatorType>(key); }
405 template<typename HashTranslator, typename T> iterator find(const T&);
406 template<typename HashTranslator, typename T> const_iterator find(const T&) const;
407 template<typename HashTranslator, typename T> bool contains(const T&) const;
409 void remove(const KeyType&);
410 void remove(iterator);
411 void removeWithoutEntryConsistencyCheck(iterator);
412 void removeWithoutEntryConsistencyCheck(const_iterator);
415 static bool isEmptyBucket(const ValueType& value) { return isHashTraitsEmptyValue<KeyTraits>(Extractor::extract(value)); }
416 static bool isDeletedBucket(const ValueType& value) { return KeyTraits::isDeletedValue(Extractor::extract(value)); }
417 static bool isEmptyOrDeletedBucket(const ValueType& value) { return isEmptyBucket(value) || isDeletedBucket(value); }
419 ValueType* lookup(const Key& key) { return lookup<IdentityTranslatorType>(key); }
420 template<typename HashTranslator, typename T> ValueType* lookup(const T&);
423 void checkTableConsistency() const;
425 static void checkTableConsistency() { }
427 #if CHECK_HASHTABLE_CONSISTENCY
428 void internalCheckTableConsistency() const { checkTableConsistency(); }
429 void internalCheckTableConsistencyExceptSize() const { checkTableConsistencyExceptSize(); }
431 static void internalCheckTableConsistencyExceptSize() { }
432 static void internalCheckTableConsistency() { }
436 static ValueType* allocateTable(int size);
437 static void deallocateTable(ValueType* table, int size);
439 typedef std::pair<ValueType*, bool> LookupType;
440 typedef std::pair<LookupType, unsigned> FullLookupType;
442 LookupType lookupForWriting(const Key& key) { return lookupForWriting<IdentityTranslatorType>(key); };
443 template<typename HashTranslator, typename T> FullLookupType fullLookupForWriting(const T&);
444 template<typename HashTranslator, typename T> LookupType lookupForWriting(const T&);
446 template<typename HashTranslator, typename T> void checkKey(const T&);
448 void removeAndInvalidateWithoutEntryConsistencyCheck(ValueType*);
449 void removeAndInvalidate(ValueType*);
450 void remove(ValueType*);
452 bool shouldExpand() const { return (m_keyCount + m_deletedCount) * m_maxLoad >= m_tableSize; }
453 bool mustRehashInPlace() const { return m_keyCount * m_minLoad < m_tableSize * 2; }
454 bool shouldShrink() const { return m_keyCount * m_minLoad < m_tableSize && m_tableSize > KeyTraits::minimumTableSize; }
456 void shrink() { rehash(m_tableSize / 2); }
458 void rehash(int newTableSize);
459 void reinsert(ValueType&);
461 static void initializeBucket(ValueType& bucket);
462 static void deleteBucket(ValueType& bucket) { bucket.~ValueType(); Traits::constructDeletedValue(bucket); }
464 FullLookupType makeLookupResult(ValueType* position, bool found, unsigned hash)
465 { return FullLookupType(LookupType(position, found), hash); }
467 iterator makeIterator(ValueType* pos) { return iterator(this, pos, m_table + m_tableSize); }
468 const_iterator makeConstIterator(ValueType* pos) const { return const_iterator(this, pos, m_table + m_tableSize); }
469 iterator makeKnownGoodIterator(ValueType* pos) { return iterator(this, pos, m_table + m_tableSize, HashItemKnownGood); }
470 const_iterator makeKnownGoodConstIterator(ValueType* pos) const { return const_iterator(this, pos, m_table + m_tableSize, HashItemKnownGood); }
473 void checkTableConsistencyExceptSize() const;
475 static void checkTableConsistencyExceptSize() { }
478 #if CHECK_HASHTABLE_ITERATORS
479 void invalidateIterators();
481 static void invalidateIterators() { }
484 static const int m_maxLoad = 2;
485 static const int m_minLoad = 6;
493 #if CHECK_HASHTABLE_ITERATORS
495 // All access to m_iterators should be guarded with m_mutex.
496 mutable const_iterator* m_iterators;
497 // Use OwnPtr so HashTable can still be memmove'd or memcpy'ed.
498 mutable OwnPtr<Mutex> m_mutex;
501 #if DUMP_HASHTABLE_STATS_PER_TABLE
503 mutable OwnPtr<Stats> m_stats;
507 // Set all the bits to one after the most significant bit: 00110101010 -> 00111111111.
508 template<unsigned size> struct OneifyLowBits;
510 struct OneifyLowBits<0> {
511 static const unsigned value = 0;
513 template<unsigned number>
514 struct OneifyLowBits {
515 static const unsigned value = number | OneifyLowBits<(number >> 1)>::value;
517 // Compute the first power of two integer that is an upper bound of the parameter 'number'.
518 template<unsigned number>
519 struct UpperPowerOfTwoBound {
520 static const unsigned value = (OneifyLowBits<number - 1>::value + 1) * 2;
523 // Because power of two numbers are the limit of maxLoad, their capacity is twice the
524 // UpperPowerOfTwoBound, or 4 times their values.
525 template<unsigned size, bool isPowerOfTwo> struct HashTableCapacityForSizeSplitter;
526 template<unsigned size>
527 struct HashTableCapacityForSizeSplitter<size, true> {
528 static const unsigned value = size * 4;
530 template<unsigned size>
531 struct HashTableCapacityForSizeSplitter<size, false> {
532 static const unsigned value = UpperPowerOfTwoBound<size>::value;
535 // HashTableCapacityForSize computes the upper power of two capacity to hold the size parameter.
536 // This is done at compile time to initialize the HashTraits.
537 template<unsigned size>
538 struct HashTableCapacityForSize {
539 static const unsigned value = HashTableCapacityForSizeSplitter<size, !(size & (size - 1))>::value;
540 COMPILE_ASSERT(size > 0, HashTableNonZeroMinimumCapacity);
541 COMPILE_ASSERT(!static_cast<int>(value >> 31), HashTableNoCapacityOverflow);
542 COMPILE_ASSERT(value > (2 * size), HashTableCapacityHoldsContentSize);
545 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
546 inline HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::HashTable()
552 #if CHECK_HASHTABLE_ITERATORS
554 , m_mutex(adoptPtr(new Mutex))
556 #if DUMP_HASHTABLE_STATS_PER_TABLE
557 , m_stats(adoptPtr(new Stats))
562 inline unsigned doubleHash(unsigned key)
564 key = ~key + (key >> 23);
574 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
575 template<typename HashTranslator, typename T>
576 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::checkKey(const T&)
582 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
583 template<typename HashTranslator, typename T>
584 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::checkKey(const T& key)
586 if (!HashFunctions::safeToCompareToEmptyOrDeleted)
588 ASSERT(!HashTranslator::equal(KeyTraits::emptyValue(), key));
589 AlignedBuffer<sizeof(ValueType), WTF_ALIGN_OF(ValueType)> deletedValueBuffer;
590 ValueType* deletedValuePtr = reinterpret_cast_ptr<ValueType*>(deletedValueBuffer.buffer);
591 ValueType& deletedValue = *deletedValuePtr;
592 Traits::constructDeletedValue(deletedValue);
593 ASSERT(!HashTranslator::equal(Extractor::extract(deletedValue), key));
598 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
599 template<typename HashTranslator, typename T>
600 inline Value* HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::lookup(const T& key)
602 checkKey<HashTranslator>(key);
605 int sizeMask = m_tableSizeMask;
606 ValueType* table = m_table;
607 unsigned h = HashTranslator::hash(key);
608 int i = h & sizeMask;
613 #if DUMP_HASHTABLE_STATS
614 atomicIncrement(&HashTableStats::numAccesses);
618 #if DUMP_HASHTABLE_STATS_PER_TABLE
619 ++m_stats->numAccesses;
620 int perTableProbeCount = 0;
624 ValueType* entry = table + i;
626 // we count on the compiler to optimize out this branch
627 if (HashFunctions::safeToCompareToEmptyOrDeleted) {
628 if (HashTranslator::equal(Extractor::extract(*entry), key))
631 if (isEmptyBucket(*entry))
634 if (isEmptyBucket(*entry))
637 if (!isDeletedBucket(*entry) && HashTranslator::equal(Extractor::extract(*entry), key))
640 #if DUMP_HASHTABLE_STATS
642 HashTableStats::recordCollisionAtCount(probeCount);
645 #if DUMP_HASHTABLE_STATS_PER_TABLE
646 ++perTableProbeCount;
647 m_stats->recordCollisionAtCount(perTableProbeCount);
651 k = 1 | doubleHash(h);
652 i = (i + k) & sizeMask;
656 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
657 template<typename HashTranslator, typename T>
658 inline typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::LookupType HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::lookupForWriting(const T& key)
661 checkKey<HashTranslator>(key);
664 ValueType* table = m_table;
665 int sizeMask = m_tableSizeMask;
666 unsigned h = HashTranslator::hash(key);
667 int i = h & sizeMask;
669 #if DUMP_HASHTABLE_STATS
670 atomicIncrement(&HashTableStats::numAccesses);
674 #if DUMP_HASHTABLE_STATS_PER_TABLE
675 ++m_stats->numAccesses;
676 int perTableProbeCount = 0;
679 ValueType* deletedEntry = 0;
682 ValueType* entry = table + i;
684 // we count on the compiler to optimize out this branch
685 if (HashFunctions::safeToCompareToEmptyOrDeleted) {
686 if (isEmptyBucket(*entry))
687 return LookupType(deletedEntry ? deletedEntry : entry, false);
689 if (HashTranslator::equal(Extractor::extract(*entry), key))
690 return LookupType(entry, true);
692 if (isDeletedBucket(*entry))
693 deletedEntry = entry;
695 if (isEmptyBucket(*entry))
696 return LookupType(deletedEntry ? deletedEntry : entry, false);
698 if (isDeletedBucket(*entry))
699 deletedEntry = entry;
700 else if (HashTranslator::equal(Extractor::extract(*entry), key))
701 return LookupType(entry, true);
703 #if DUMP_HASHTABLE_STATS
705 HashTableStats::recordCollisionAtCount(probeCount);
708 #if DUMP_HASHTABLE_STATS_PER_TABLE
709 ++perTableProbeCount;
710 m_stats->recordCollisionAtCount(perTableProbeCount);
714 k = 1 | doubleHash(h);
715 i = (i + k) & sizeMask;
719 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
720 template<typename HashTranslator, typename T>
721 inline typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::FullLookupType HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::fullLookupForWriting(const T& key)
724 checkKey<HashTranslator>(key);
727 ValueType* table = m_table;
728 int sizeMask = m_tableSizeMask;
729 unsigned h = HashTranslator::hash(key);
730 int i = h & sizeMask;
732 #if DUMP_HASHTABLE_STATS
733 atomicIncrement(&HashTableStats::numAccesses);
737 #if DUMP_HASHTABLE_STATS_PER_TABLE
738 ++m_stats->numAccesses;
739 int perTableProbeCount = 0;
742 ValueType* deletedEntry = 0;
745 ValueType* entry = table + i;
747 // we count on the compiler to optimize out this branch
748 if (HashFunctions::safeToCompareToEmptyOrDeleted) {
749 if (isEmptyBucket(*entry))
750 return makeLookupResult(deletedEntry ? deletedEntry : entry, false, h);
752 if (HashTranslator::equal(Extractor::extract(*entry), key))
753 return makeLookupResult(entry, true, h);
755 if (isDeletedBucket(*entry))
756 deletedEntry = entry;
758 if (isEmptyBucket(*entry))
759 return makeLookupResult(deletedEntry ? deletedEntry : entry, false, h);
761 if (isDeletedBucket(*entry))
762 deletedEntry = entry;
763 else if (HashTranslator::equal(Extractor::extract(*entry), key))
764 return makeLookupResult(entry, true, h);
766 #if DUMP_HASHTABLE_STATS
768 HashTableStats::recordCollisionAtCount(probeCount);
771 #if DUMP_HASHTABLE_STATS_PER_TABLE
772 ++perTableProbeCount;
773 m_stats->recordCollisionAtCount(perTableProbeCount);
777 k = 1 | doubleHash(h);
778 i = (i + k) & sizeMask;
782 template<bool emptyValueIsZero> struct HashTableBucketInitializer;
784 template<> struct HashTableBucketInitializer<false> {
785 template<typename Traits, typename Value> static void initialize(Value& bucket)
787 new (NotNull, &bucket) Value(Traits::emptyValue());
791 template<> struct HashTableBucketInitializer<true> {
792 template<typename Traits, typename Value> static void initialize(Value& bucket)
794 // This initializes the bucket without copying the empty value.
795 // That makes it possible to use this with types that don't support copying.
796 // The memset to 0 looks like a slow operation but is optimized by the compilers.
797 memset(&bucket, 0, sizeof(bucket));
801 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
802 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::initializeBucket(ValueType& bucket)
804 HashTableBucketInitializer<Traits::emptyValueIsZero>::template initialize<Traits>(bucket);
807 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
808 template<typename HashTranslator, typename T, typename Extra>
809 inline typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::AddResult HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::add(const T& key, const Extra& extra)
811 checkKey<HashTranslator>(key);
813 invalidateIterators();
818 internalCheckTableConsistency();
823 ValueType* table = m_table;
824 int sizeMask = m_tableSizeMask;
825 unsigned h = HashTranslator::hash(key);
826 int i = h & sizeMask;
828 #if DUMP_HASHTABLE_STATS
829 atomicIncrement(&HashTableStats::numAccesses);
833 #if DUMP_HASHTABLE_STATS_PER_TABLE
834 ++m_stats->numAccesses;
835 int perTableProbeCount = 0;
838 ValueType* deletedEntry = 0;
843 // we count on the compiler to optimize out this branch
844 if (HashFunctions::safeToCompareToEmptyOrDeleted) {
845 if (isEmptyBucket(*entry))
848 if (HashTranslator::equal(Extractor::extract(*entry), key))
849 return AddResult(makeKnownGoodIterator(entry), false);
851 if (isDeletedBucket(*entry))
852 deletedEntry = entry;
854 if (isEmptyBucket(*entry))
857 if (isDeletedBucket(*entry))
858 deletedEntry = entry;
859 else if (HashTranslator::equal(Extractor::extract(*entry), key))
860 return AddResult(makeKnownGoodIterator(entry), false);
862 #if DUMP_HASHTABLE_STATS
864 HashTableStats::recordCollisionAtCount(probeCount);
867 #if DUMP_HASHTABLE_STATS_PER_TABLE
868 ++perTableProbeCount;
869 m_stats->recordCollisionAtCount(perTableProbeCount);
873 k = 1 | doubleHash(h);
874 i = (i + k) & sizeMask;
878 initializeBucket(*deletedEntry);
879 entry = deletedEntry;
883 HashTranslator::translate(*entry, key, extra);
887 if (shouldExpand()) {
888 // FIXME: This makes an extra copy on expand. Probably not that bad since
889 // expand is rare, but would be better to have a version of expand that can
890 // follow a pivot entry and return the new position.
891 KeyType enteredKey = Extractor::extract(*entry);
893 AddResult result(find(enteredKey), true);
894 ASSERT(result.iterator != end());
898 internalCheckTableConsistency();
900 return AddResult(makeKnownGoodIterator(entry), true);
903 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
904 template<typename HashTranslator, typename T, typename Extra>
905 inline typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::AddResult HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::addPassingHashCode(const T& key, const Extra& extra)
907 checkKey<HashTranslator>(key);
909 invalidateIterators();
914 internalCheckTableConsistency();
916 FullLookupType lookupResult = fullLookupForWriting<HashTranslator>(key);
918 ValueType* entry = lookupResult.first.first;
919 bool found = lookupResult.first.second;
920 unsigned h = lookupResult.second;
923 return AddResult(makeKnownGoodIterator(entry), false);
925 if (isDeletedBucket(*entry)) {
926 initializeBucket(*entry);
930 HashTranslator::translate(*entry, key, extra, h);
932 if (shouldExpand()) {
933 // FIXME: This makes an extra copy on expand. Probably not that bad since
934 // expand is rare, but would be better to have a version of expand that can
935 // follow a pivot entry and return the new position.
936 KeyType enteredKey = Extractor::extract(*entry);
938 AddResult result(find(enteredKey), true);
939 ASSERT(result.iterator != end());
943 internalCheckTableConsistency();
945 return AddResult(makeKnownGoodIterator(entry), true);
948 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
949 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::reinsert(ValueType& entry)
952 ASSERT(!lookupForWriting(Extractor::extract(entry)).second);
953 ASSERT(!isDeletedBucket(*(lookupForWriting(Extractor::extract(entry)).first)));
954 #if DUMP_HASHTABLE_STATS
955 atomicIncrement(&HashTableStats::numReinserts);
957 #if DUMP_HASHTABLE_STATS_PER_TABLE
958 ++m_stats->numReinserts;
961 Mover<ValueType, Traits::needsDestruction>::move(entry, *lookupForWriting(Extractor::extract(entry)).first);
964 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
965 template <typename HashTranslator, typename T>
966 typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::iterator HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::find(const T& key)
971 ValueType* entry = lookup<HashTranslator>(key);
975 return makeKnownGoodIterator(entry);
978 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
979 template <typename HashTranslator, typename T>
980 typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::const_iterator HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::find(const T& key) const
985 ValueType* entry = const_cast<HashTable*>(this)->lookup<HashTranslator>(key);
989 return makeKnownGoodConstIterator(entry);
992 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
993 template <typename HashTranslator, typename T>
994 bool HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::contains(const T& key) const
999 return const_cast<HashTable*>(this)->lookup<HashTranslator>(key);
1002 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1003 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::removeAndInvalidateWithoutEntryConsistencyCheck(ValueType* pos)
1005 invalidateIterators();
1009 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1010 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::removeAndInvalidate(ValueType* pos)
1012 invalidateIterators();
1013 internalCheckTableConsistency();
1017 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1018 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(ValueType* pos)
1020 #if DUMP_HASHTABLE_STATS
1021 atomicIncrement(&HashTableStats::numRemoves);
1023 #if DUMP_HASHTABLE_STATS_PER_TABLE
1024 ++m_stats->numRemoves;
1034 internalCheckTableConsistency();
1037 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1038 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(iterator it)
1043 removeAndInvalidate(const_cast<ValueType*>(it.m_iterator.m_position));
1046 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1047 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::removeWithoutEntryConsistencyCheck(iterator it)
1052 removeAndInvalidateWithoutEntryConsistencyCheck(const_cast<ValueType*>(it.m_iterator.m_position));
1055 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1056 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::removeWithoutEntryConsistencyCheck(const_iterator it)
1061 removeAndInvalidateWithoutEntryConsistencyCheck(const_cast<ValueType*>(it.m_position));
1064 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1065 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(const KeyType& key)
1070 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1071 Value* HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::allocateTable(int size)
1073 // would use a template member function with explicit specializations here, but
1074 // gcc doesn't appear to support that
1075 if (Traits::emptyValueIsZero)
1076 return static_cast<ValueType*>(fastZeroedMalloc(size * sizeof(ValueType)));
1077 ValueType* result = static_cast<ValueType*>(fastMalloc(size * sizeof(ValueType)));
1078 for (int i = 0; i < size; i++)
1079 initializeBucket(result[i]);
1083 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1084 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::deallocateTable(ValueType* table, int size)
1086 if (Traits::needsDestruction) {
1087 for (int i = 0; i < size; ++i) {
1088 if (!isDeletedBucket(table[i]))
1089 table[i].~ValueType();
1095 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1096 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::expand()
1099 if (m_tableSize == 0)
1100 newSize = KeyTraits::minimumTableSize;
1101 else if (mustRehashInPlace())
1102 newSize = m_tableSize;
1104 newSize = m_tableSize * 2;
1109 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1110 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::rehash(int newTableSize)
1112 internalCheckTableConsistencyExceptSize();
1114 int oldTableSize = m_tableSize;
1115 ValueType* oldTable = m_table;
1117 #if DUMP_HASHTABLE_STATS
1118 if (oldTableSize != 0)
1119 atomicIncrement(&HashTableStats::numRehashes);
1122 #if DUMP_HASHTABLE_STATS_PER_TABLE
1123 if (oldTableSize != 0)
1124 ++m_stats->numRehashes;
1127 m_tableSize = newTableSize;
1128 m_tableSizeMask = newTableSize - 1;
1129 m_table = allocateTable(newTableSize);
1131 for (int i = 0; i != oldTableSize; ++i)
1132 if (!isEmptyOrDeletedBucket(oldTable[i]))
1133 reinsert(oldTable[i]);
1137 deallocateTable(oldTable, oldTableSize);
1139 internalCheckTableConsistency();
1142 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1143 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::clear()
1145 invalidateIterators();
1149 deallocateTable(m_table, m_tableSize);
1152 m_tableSizeMask = 0;
1156 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1157 HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::HashTable(const HashTable& other)
1160 , m_tableSizeMask(0)
1163 #if CHECK_HASHTABLE_ITERATORS
1165 , m_mutex(adoptPtr(new Mutex))
1167 #if DUMP_HASHTABLE_STATS_PER_TABLE
1168 , m_stats(adoptPtr(new Stats(*other.m_stats)))
1171 // Copy the hash table the dumb way, by adding each element to the new table.
1172 // It might be more efficient to copy the table slots, but it's not clear that efficiency is needed.
1173 const_iterator end = other.end();
1174 for (const_iterator it = other.begin(); it != end; ++it)
1178 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1179 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::swap(HashTable& other)
1181 invalidateIterators();
1182 other.invalidateIterators();
1184 ValueType* tmp_table = m_table;
1185 m_table = other.m_table;
1186 other.m_table = tmp_table;
1188 int tmp_tableSize = m_tableSize;
1189 m_tableSize = other.m_tableSize;
1190 other.m_tableSize = tmp_tableSize;
1192 int tmp_tableSizeMask = m_tableSizeMask;
1193 m_tableSizeMask = other.m_tableSizeMask;
1194 other.m_tableSizeMask = tmp_tableSizeMask;
1196 int tmp_keyCount = m_keyCount;
1197 m_keyCount = other.m_keyCount;
1198 other.m_keyCount = tmp_keyCount;
1200 int tmp_deletedCount = m_deletedCount;
1201 m_deletedCount = other.m_deletedCount;
1202 other.m_deletedCount = tmp_deletedCount;
1204 #if DUMP_HASHTABLE_STATS_PER_TABLE
1205 m_stats.swap(other.m_stats);
1209 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1210 HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>& HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::operator=(const HashTable& other)
1212 HashTable tmp(other);
1217 #if !ASSERT_DISABLED
1219 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1220 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::checkTableConsistency() const
1222 checkTableConsistencyExceptSize();
1223 ASSERT(!m_table || !shouldExpand());
1224 ASSERT(!shouldShrink());
1227 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1228 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::checkTableConsistencyExceptSize() const
1234 int deletedCount = 0;
1235 for (int j = 0; j < m_tableSize; ++j) {
1236 ValueType* entry = m_table + j;
1237 if (isEmptyBucket(*entry))
1240 if (isDeletedBucket(*entry)) {
1245 const_iterator it = find(Extractor::extract(*entry));
1246 ASSERT(entry == it.m_position);
1249 ValueCheck<Key>::checkConsistency(it->first);
1252 ASSERT(count == m_keyCount);
1253 ASSERT(deletedCount == m_deletedCount);
1254 ASSERT(m_tableSize >= KeyTraits::minimumTableSize);
1255 ASSERT(m_tableSizeMask);
1256 ASSERT(m_tableSize == m_tableSizeMask + 1);
1259 #endif // ASSERT_DISABLED
1261 #if CHECK_HASHTABLE_ITERATORS
1263 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1264 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::invalidateIterators()
1266 MutexLocker lock(*m_mutex);
1267 const_iterator* next;
1268 for (const_iterator* p = m_iterators; p; p = next) {
1277 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1278 void addIterator(const HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>* table,
1279 HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>* it)
1281 it->m_table = table;
1284 // Insert iterator at head of doubly-linked list of iterators.
1288 MutexLocker lock(*table->m_mutex);
1289 ASSERT(table->m_iterators != it);
1290 it->m_next = table->m_iterators;
1291 table->m_iterators = it;
1293 ASSERT(!it->m_next->m_previous);
1294 it->m_next->m_previous = it;
1299 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1300 void removeIterator(HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>* it)
1302 typedef HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> HashTableType;
1303 typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> const_iterator;
1305 // Delete iterator from doubly-linked list of iterators.
1307 ASSERT(!it->m_next);
1308 ASSERT(!it->m_previous);
1310 MutexLocker lock(*it->m_table->m_mutex);
1312 ASSERT(it->m_next->m_previous == it);
1313 it->m_next->m_previous = it->m_previous;
1315 if (it->m_previous) {
1316 ASSERT(it->m_table->m_iterators != it);
1317 ASSERT(it->m_previous->m_next == it);
1318 it->m_previous->m_next = it->m_next;
1320 ASSERT(it->m_table->m_iterators == it);
1321 it->m_table->m_iterators = it->m_next;
1330 #endif // CHECK_HASHTABLE_ITERATORS
1332 // iterator adapters
1334 template<typename HashTableType, typename ValueType> struct HashTableConstIteratorAdapter {
1335 HashTableConstIteratorAdapter() {}
1336 HashTableConstIteratorAdapter(const typename HashTableType::const_iterator& impl) : m_impl(impl) {}
1338 const ValueType* get() const { return (const ValueType*)m_impl.get(); }
1339 const ValueType& operator*() const { return *get(); }
1340 const ValueType* operator->() const { return get(); }
1342 HashTableConstIteratorAdapter& operator++() { ++m_impl; return *this; }
1343 // postfix ++ intentionally omitted
1345 typename HashTableType::const_iterator m_impl;
1348 template<typename HashTableType, typename ValueType> struct HashTableIteratorAdapter {
1349 HashTableIteratorAdapter() {}
1350 HashTableIteratorAdapter(const typename HashTableType::iterator& impl) : m_impl(impl) {}
1352 ValueType* get() const { return (ValueType*)m_impl.get(); }
1353 ValueType& operator*() const { return *get(); }
1354 ValueType* operator->() const { return get(); }
1356 HashTableIteratorAdapter& operator++() { ++m_impl; return *this; }
1357 // postfix ++ intentionally omitted
1359 operator HashTableConstIteratorAdapter<HashTableType, ValueType>() {
1360 typename HashTableType::const_iterator i = m_impl;
1364 typename HashTableType::iterator m_impl;
1367 template<typename T, typename U>
1368 inline bool operator==(const HashTableConstIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1370 return a.m_impl == b.m_impl;
1373 template<typename T, typename U>
1374 inline bool operator!=(const HashTableConstIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1376 return a.m_impl != b.m_impl;
1379 template<typename T, typename U>
1380 inline bool operator==(const HashTableIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b)
1382 return a.m_impl == b.m_impl;
1385 template<typename T, typename U>
1386 inline bool operator!=(const HashTableIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b)
1388 return a.m_impl != b.m_impl;
1391 // All 4 combinations of ==, != and Const,non const.
1392 template<typename T, typename U>
1393 inline bool operator==(const HashTableConstIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b)
1395 return a.m_impl == b.m_impl;
1398 template<typename T, typename U>
1399 inline bool operator!=(const HashTableConstIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b)
1401 return a.m_impl != b.m_impl;
1404 template<typename T, typename U>
1405 inline bool operator==(const HashTableIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1407 return a.m_impl == b.m_impl;
1410 template<typename T, typename U>
1411 inline bool operator!=(const HashTableIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1413 return a.m_impl != b.m_impl;
1418 #include <wtf/HashIterators.h>
1420 #endif // WTF_HashTable_h