Unreviewed, rolling out r126914.
[WebKit-https.git] / Source / WTF / wtf / HashTable.h
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 David Levin <levin@chromium.org>
4  *
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.
9  *
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.
14  *
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.
19  *
20  */
21
22 #ifndef WTF_HashTable_h
23 #define WTF_HashTable_h
24
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>
33
34 #ifndef NDEBUG
35 // Required for CHECK_HASHTABLE_ITERATORS.
36 #include <wtf/OwnPtr.h>
37 #include <wtf/PassOwnPtr.h>
38 #endif
39
40 namespace WTF {
41
42 #define DUMP_HASHTABLE_STATS 0
43 #define DUMP_HASHTABLE_STATS_PER_TABLE 0
44
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
47
48 #ifdef NDEBUG
49 #define CHECK_HASHTABLE_ITERATORS 0
50 #define CHECK_HASHTABLE_USE_AFTER_DESTRUCTION 0
51 #else
52 #define CHECK_HASHTABLE_ITERATORS 1
53 #define CHECK_HASHTABLE_USE_AFTER_DESTRUCTION 1
54 #endif
55
56 #if DUMP_HASHTABLE_STATS
57
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;
64
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];
69
70         WTF_EXPORT_PRIVATE static void recordCollisionAtCount(int count);
71         WTF_EXPORT_PRIVATE static void dumpStats();
72     };
73
74 #endif
75
76     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
77     class HashTable;
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;
82
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>*);
86
87     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
88     void removeIterator(HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>*);
89
90 #if !CHECK_HASHTABLE_ITERATORS
91
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>*) { }
95
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>*) { }
98
99 #endif
100
101     typedef enum { HashItemKnownGood } HashItemKnownGoodTag;
102
103     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
104     class HashTableConstIterator {
105     private:
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;
112
113         friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>;
114         friend class HashTableIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>;
115
116         void skipEmptyBuckets()
117         {
118             while (m_position != m_endPosition && HashTableType::isEmptyOrDeletedBucket(*m_position))
119                 ++m_position;
120         }
121
122         HashTableConstIterator(const HashTableType* table, PointerType position, PointerType endPosition)
123             : m_position(position), m_endPosition(endPosition)
124         {
125             addIterator(table, this);
126             skipEmptyBuckets();
127         }
128
129         HashTableConstIterator(const HashTableType* table, PointerType position, PointerType endPosition, HashItemKnownGoodTag)
130             : m_position(position), m_endPosition(endPosition)
131         {
132             addIterator(table, this);
133         }
134
135     public:
136         HashTableConstIterator()
137         {
138             addIterator(static_cast<const HashTableType*>(0), this);
139         }
140
141         // default copy, assignment and destructor are OK if CHECK_HASHTABLE_ITERATORS is 0
142
143 #if CHECK_HASHTABLE_ITERATORS
144         ~HashTableConstIterator()
145         {
146             removeIterator(this);
147         }
148
149         HashTableConstIterator(const const_iterator& other)
150             : m_position(other.m_position), m_endPosition(other.m_endPosition)
151         {
152             addIterator(other.m_table, this);
153         }
154
155         const_iterator& operator=(const const_iterator& other)
156         {
157             m_position = other.m_position;
158             m_endPosition = other.m_endPosition;
159
160             removeIterator(this);
161             addIterator(other.m_table, this);
162
163             return *this;
164         }
165 #endif
166
167         PointerType get() const
168         {
169             checkValidity();
170             return m_position;
171         }
172         ReferenceType operator*() const { return *get(); }
173         PointerType operator->() const { return get(); }
174
175         const_iterator& operator++()
176         {
177             checkValidity();
178             ASSERT(m_position != m_endPosition);
179             ++m_position;
180             skipEmptyBuckets();
181             return *this;
182         }
183
184         // postfix ++ intentionally omitted
185
186         // Comparison.
187         bool operator==(const const_iterator& other) const
188         {
189             checkValidity(other);
190             return m_position == other.m_position;
191         }
192         bool operator!=(const const_iterator& other) const
193         {
194             checkValidity(other);
195             return m_position != other.m_position;
196         }
197         bool operator==(const iterator& other) const
198         {
199             return *this == static_cast<const_iterator>(other);
200         }
201         bool operator!=(const iterator& other) const
202         {
203             return *this != static_cast<const_iterator>(other);
204         }
205
206     private:
207         void checkValidity() const
208         {
209 #if CHECK_HASHTABLE_ITERATORS
210             ASSERT(m_table);
211 #endif
212         }
213
214
215 #if CHECK_HASHTABLE_ITERATORS
216         void checkValidity(const const_iterator& other) const
217         {
218             ASSERT(m_table);
219             ASSERT_UNUSED(other, other.m_table);
220             ASSERT(m_table == other.m_table);
221         }
222 #else
223         void checkValidity(const const_iterator&) const { }
224 #endif
225
226         PointerType m_position;
227         PointerType m_endPosition;
228
229 #if CHECK_HASHTABLE_ITERATORS
230     public:
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;
236 #endif
237     };
238
239     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
240     class HashTableIterator {
241     private:
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;
248
249         friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>;
250
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) { }
253
254     public:
255         HashTableIterator() { }
256
257         // default copy, assignment and destructor are OK
258
259         PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
260         ReferenceType operator*() const { return *get(); }
261         PointerType operator->() const { return get(); }
262
263         iterator& operator++() { ++m_iterator; return *this; }
264
265         // postfix ++ intentionally omitted
266
267         // Comparison.
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; }
272
273         operator const_iterator() const { return m_iterator; }
274
275     private:
276         const_iterator m_iterator;
277     };
278
279     using std::swap;
280
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)
283     {
284         swap(a, b);
285     }
286
287     template<typename T, typename U> inline void hashTableSwap(KeyValuePair<T, U>& a, KeyValuePair<T, U>& b)
288     {
289         swap(a.first, b.first);
290         swap(a.second, b.second);
291     }
292
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; } };
296
297     template<typename HashFunctions> class IdentityHashTranslator {
298     public:
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; }
302     };
303
304     template<typename IteratorType> struct HashTableAddResult {
305         HashTableAddResult(IteratorType iter, bool isNewEntry) : iterator(iter), isNewEntry(isNewEntry) { }
306         IteratorType iterator;
307         bool isNewEntry;
308     };
309
310     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
311     class HashTable {
312     public:
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;
316         typedef Key KeyType;
317         typedef Value ValueType;
318         typedef IdentityHashTranslator<HashFunctions> IdentityTranslatorType;
319         typedef HashTableAddResult<iterator> AddResult;
320
321 #if DUMP_HASHTABLE_STATS_PER_TABLE
322         struct Stats {
323             Stats()
324                 : numAccesses(0)
325                 , numRehashes(0)
326                 , numRemoves(0)
327                 , numReinserts(0)
328                 , maxCollisions(0)
329                 , numCollisions(0)
330                 , collisionGraph()
331             {
332             }
333
334             int numAccesses;
335             int numRehashes;
336             int numRemoves;
337             int numReinserts;
338
339             int maxCollisions;
340             int numCollisions;
341             int collisionGraph[4096];
342
343             void recordCollisionAtCount(int count)
344             {
345                 if (count > maxCollisions)
346                     maxCollisions = count;
347                 numCollisions++;
348                 collisionGraph[count]++;
349             }
350
351             void dumpStats()
352             {
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);
359                 }
360                 dataLog("%d rehashes\n", numRehashes);
361                 dataLog("%d reinserts\n", numReinserts);
362             }
363         };
364 #endif
365
366         HashTable();
367         ~HashTable() 
368         {
369             invalidateIterators(); 
370             if (m_table)
371                 deallocateTable(m_table, m_tableSize);
372 #if CHECK_HASHTABLE_USE_AFTER_DESTRUCTION
373             m_table = (ValueType*)(uintptr_t)0xbbadbeef;
374 #endif
375         }
376
377         HashTable(const HashTable&);
378         void swap(HashTable&);
379         HashTable& operator=(const HashTable&);
380
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); }
388
389         int size() const { return m_keyCount; }
390         int capacity() const { return m_tableSize; }
391         bool isEmpty() const { return !m_keyCount; }
392
393         AddResult add(const ValueType& value) { return add<IdentityTranslatorType>(Extractor::extract(value), value); }
394
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
397         // in the table.
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&);
400
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); }
404
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;
408
409         void remove(const KeyType&);
410         void remove(iterator);
411         void removeWithoutEntryConsistencyCheck(iterator);
412         void removeWithoutEntryConsistencyCheck(const_iterator);
413         void clear();
414
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); }
418
419         ValueType* lookup(const Key& key) { return lookup<IdentityTranslatorType>(key); }
420         template<typename HashTranslator, typename T> ValueType* lookup(const T&);
421
422 #if !ASSERT_DISABLED
423         void checkTableConsistency() const;
424 #else
425         static void checkTableConsistency() { }
426 #endif
427 #if CHECK_HASHTABLE_CONSISTENCY
428         void internalCheckTableConsistency() const { checkTableConsistency(); }
429         void internalCheckTableConsistencyExceptSize() const { checkTableConsistencyExceptSize(); }
430 #else
431         static void internalCheckTableConsistencyExceptSize() { }
432         static void internalCheckTableConsistency() { }
433 #endif
434
435     private:
436         static ValueType* allocateTable(int size);
437         static void deallocateTable(ValueType* table, int size);
438
439         typedef std::pair<ValueType*, bool> LookupType;
440         typedef std::pair<LookupType, unsigned> FullLookupType;
441
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&);
445
446         template<typename HashTranslator, typename T> void checkKey(const T&);
447
448         void removeAndInvalidateWithoutEntryConsistencyCheck(ValueType*);
449         void removeAndInvalidate(ValueType*);
450         void remove(ValueType*);
451
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; }
455         void expand();
456         void shrink() { rehash(m_tableSize / 2); }
457
458         void rehash(int newTableSize);
459         void reinsert(ValueType&);
460
461         static void initializeBucket(ValueType& bucket);
462         static void deleteBucket(ValueType& bucket) { bucket.~ValueType(); Traits::constructDeletedValue(bucket); }
463
464         FullLookupType makeLookupResult(ValueType* position, bool found, unsigned hash)
465             { return FullLookupType(LookupType(position, found), hash); }
466
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); }
471
472 #if !ASSERT_DISABLED
473         void checkTableConsistencyExceptSize() const;
474 #else
475         static void checkTableConsistencyExceptSize() { }
476 #endif
477
478 #if CHECK_HASHTABLE_ITERATORS
479         void invalidateIterators();
480 #else
481         static void invalidateIterators() { }
482 #endif
483
484         static const int m_maxLoad = 2;
485         static const int m_minLoad = 6;
486
487         ValueType* m_table;
488         int m_tableSize;
489         int m_tableSizeMask;
490         int m_keyCount;
491         int m_deletedCount;
492
493 #if CHECK_HASHTABLE_ITERATORS
494     public:
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;
499 #endif
500
501 #if DUMP_HASHTABLE_STATS_PER_TABLE
502     public:
503         mutable OwnPtr<Stats> m_stats;
504 #endif
505     };
506
507     // Set all the bits to one after the most significant bit: 00110101010 -> 00111111111.
508     template<unsigned size> struct OneifyLowBits;
509     template<>
510     struct OneifyLowBits<0> {
511         static const unsigned value = 0;
512     };
513     template<unsigned number>
514     struct OneifyLowBits {
515         static const unsigned value = number | OneifyLowBits<(number >> 1)>::value;
516     };
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;
521     };
522
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;
529     };
530     template<unsigned size>
531     struct HashTableCapacityForSizeSplitter<size, false> {
532         static const unsigned value = UpperPowerOfTwoBound<size>::value;
533     };
534
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);
543     };
544
545     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
546     inline HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::HashTable()
547         : m_table(0)
548         , m_tableSize(0)
549         , m_tableSizeMask(0)
550         , m_keyCount(0)
551         , m_deletedCount(0)
552 #if CHECK_HASHTABLE_ITERATORS
553         , m_iterators(0)
554         , m_mutex(adoptPtr(new Mutex))
555 #endif
556 #if DUMP_HASHTABLE_STATS_PER_TABLE
557         , m_stats(adoptPtr(new Stats))
558 #endif
559     {
560     }
561
562     inline unsigned doubleHash(unsigned key)
563     {
564         key = ~key + (key >> 23);
565         key ^= (key << 12);
566         key ^= (key >> 7);
567         key ^= (key << 2);
568         key ^= (key >> 20);
569         return key;
570     }
571
572 #if ASSERT_DISABLED
573
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&)
577     {
578     }
579
580 #else
581
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)
585     {
586         if (!HashFunctions::safeToCompareToEmptyOrDeleted)
587             return;
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));
594     }
595
596 #endif
597
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)
601     {
602         checkKey<HashTranslator>(key);
603
604         int k = 0;
605         int sizeMask = m_tableSizeMask;
606         ValueType* table = m_table;
607         unsigned h = HashTranslator::hash(key);
608         int i = h & sizeMask;
609
610         if (!table)
611             return 0;
612
613 #if DUMP_HASHTABLE_STATS
614         atomicIncrement(&HashTableStats::numAccesses);
615         int probeCount = 0;
616 #endif
617
618 #if DUMP_HASHTABLE_STATS_PER_TABLE
619         ++m_stats->numAccesses;
620         int perTableProbeCount = 0;
621 #endif
622
623         while (1) {
624             ValueType* entry = table + i;
625                 
626             // we count on the compiler to optimize out this branch
627             if (HashFunctions::safeToCompareToEmptyOrDeleted) {
628                 if (HashTranslator::equal(Extractor::extract(*entry), key))
629                     return entry;
630                 
631                 if (isEmptyBucket(*entry))
632                     return 0;
633             } else {
634                 if (isEmptyBucket(*entry))
635                     return 0;
636                 
637                 if (!isDeletedBucket(*entry) && HashTranslator::equal(Extractor::extract(*entry), key))
638                     return entry;
639             }
640 #if DUMP_HASHTABLE_STATS
641             ++probeCount;
642             HashTableStats::recordCollisionAtCount(probeCount);
643 #endif
644
645 #if DUMP_HASHTABLE_STATS_PER_TABLE
646             ++perTableProbeCount;
647             m_stats->recordCollisionAtCount(perTableProbeCount);
648 #endif
649
650             if (k == 0)
651                 k = 1 | doubleHash(h);
652             i = (i + k) & sizeMask;
653         }
654     }
655
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)
659     {
660         ASSERT(m_table);
661         checkKey<HashTranslator>(key);
662
663         int k = 0;
664         ValueType* table = m_table;
665         int sizeMask = m_tableSizeMask;
666         unsigned h = HashTranslator::hash(key);
667         int i = h & sizeMask;
668
669 #if DUMP_HASHTABLE_STATS
670         atomicIncrement(&HashTableStats::numAccesses);
671         int probeCount = 0;
672 #endif
673
674 #if DUMP_HASHTABLE_STATS_PER_TABLE
675         ++m_stats->numAccesses;
676         int perTableProbeCount = 0;
677 #endif
678
679         ValueType* deletedEntry = 0;
680
681         while (1) {
682             ValueType* entry = table + i;
683             
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);
688                 
689                 if (HashTranslator::equal(Extractor::extract(*entry), key))
690                     return LookupType(entry, true);
691                 
692                 if (isDeletedBucket(*entry))
693                     deletedEntry = entry;
694             } else {
695                 if (isEmptyBucket(*entry))
696                     return LookupType(deletedEntry ? deletedEntry : entry, false);
697             
698                 if (isDeletedBucket(*entry))
699                     deletedEntry = entry;
700                 else if (HashTranslator::equal(Extractor::extract(*entry), key))
701                     return LookupType(entry, true);
702             }
703 #if DUMP_HASHTABLE_STATS
704             ++probeCount;
705             HashTableStats::recordCollisionAtCount(probeCount);
706 #endif
707
708 #if DUMP_HASHTABLE_STATS_PER_TABLE
709             ++perTableProbeCount;
710             m_stats->recordCollisionAtCount(perTableProbeCount);
711 #endif
712
713             if (k == 0)
714                 k = 1 | doubleHash(h);
715             i = (i + k) & sizeMask;
716         }
717     }
718
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)
722     {
723         ASSERT(m_table);
724         checkKey<HashTranslator>(key);
725
726         int k = 0;
727         ValueType* table = m_table;
728         int sizeMask = m_tableSizeMask;
729         unsigned h = HashTranslator::hash(key);
730         int i = h & sizeMask;
731
732 #if DUMP_HASHTABLE_STATS
733         atomicIncrement(&HashTableStats::numAccesses);
734         int probeCount = 0;
735 #endif
736
737 #if DUMP_HASHTABLE_STATS_PER_TABLE
738         ++m_stats->numAccesses;
739         int perTableProbeCount = 0;
740 #endif
741
742         ValueType* deletedEntry = 0;
743
744         while (1) {
745             ValueType* entry = table + i;
746             
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);
751                 
752                 if (HashTranslator::equal(Extractor::extract(*entry), key))
753                     return makeLookupResult(entry, true, h);
754                 
755                 if (isDeletedBucket(*entry))
756                     deletedEntry = entry;
757             } else {
758                 if (isEmptyBucket(*entry))
759                     return makeLookupResult(deletedEntry ? deletedEntry : entry, false, h);
760             
761                 if (isDeletedBucket(*entry))
762                     deletedEntry = entry;
763                 else if (HashTranslator::equal(Extractor::extract(*entry), key))
764                     return makeLookupResult(entry, true, h);
765             }
766 #if DUMP_HASHTABLE_STATS
767             ++probeCount;
768             HashTableStats::recordCollisionAtCount(probeCount);
769 #endif
770
771 #if DUMP_HASHTABLE_STATS_PER_TABLE
772             ++perTableProbeCount;
773             m_stats->recordCollisionAtCount(perTableProbeCount);
774 #endif
775
776             if (k == 0)
777                 k = 1 | doubleHash(h);
778             i = (i + k) & sizeMask;
779         }
780     }
781
782     template<bool emptyValueIsZero> struct HashTableBucketInitializer;
783
784     template<> struct HashTableBucketInitializer<false> {
785         template<typename Traits, typename Value> static void initialize(Value& bucket)
786         {
787             new (NotNull, &bucket) Value(Traits::emptyValue());
788         }
789     };
790
791     template<> struct HashTableBucketInitializer<true> {
792         template<typename Traits, typename Value> static void initialize(Value& bucket)
793         {
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));
798         }
799     };
800     
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)
803     {
804         HashTableBucketInitializer<Traits::emptyValueIsZero>::template initialize<Traits>(bucket);
805     }
806
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)
810     {
811         checkKey<HashTranslator>(key);
812
813         invalidateIterators();
814
815         if (!m_table)
816             expand();
817
818         internalCheckTableConsistency();
819
820         ASSERT(m_table);
821
822         int k = 0;
823         ValueType* table = m_table;
824         int sizeMask = m_tableSizeMask;
825         unsigned h = HashTranslator::hash(key);
826         int i = h & sizeMask;
827
828 #if DUMP_HASHTABLE_STATS
829         atomicIncrement(&HashTableStats::numAccesses);
830         int probeCount = 0;
831 #endif
832
833 #if DUMP_HASHTABLE_STATS_PER_TABLE
834         ++m_stats->numAccesses;
835         int perTableProbeCount = 0;
836 #endif
837
838         ValueType* deletedEntry = 0;
839         ValueType* entry;
840         while (1) {
841             entry = table + i;
842             
843             // we count on the compiler to optimize out this branch
844             if (HashFunctions::safeToCompareToEmptyOrDeleted) {
845                 if (isEmptyBucket(*entry))
846                     break;
847                 
848                 if (HashTranslator::equal(Extractor::extract(*entry), key))
849                     return AddResult(makeKnownGoodIterator(entry), false);
850                 
851                 if (isDeletedBucket(*entry))
852                     deletedEntry = entry;
853             } else {
854                 if (isEmptyBucket(*entry))
855                     break;
856             
857                 if (isDeletedBucket(*entry))
858                     deletedEntry = entry;
859                 else if (HashTranslator::equal(Extractor::extract(*entry), key))
860                     return AddResult(makeKnownGoodIterator(entry), false);
861             }
862 #if DUMP_HASHTABLE_STATS
863             ++probeCount;
864             HashTableStats::recordCollisionAtCount(probeCount);
865 #endif
866
867 #if DUMP_HASHTABLE_STATS_PER_TABLE
868             ++perTableProbeCount;
869             m_stats->recordCollisionAtCount(perTableProbeCount);
870 #endif
871
872             if (k == 0)
873                 k = 1 | doubleHash(h);
874             i = (i + k) & sizeMask;
875         }
876
877         if (deletedEntry) {
878             initializeBucket(*deletedEntry);
879             entry = deletedEntry;
880             --m_deletedCount; 
881         }
882
883         HashTranslator::translate(*entry, key, extra);
884
885         ++m_keyCount;
886         
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);
892             expand();
893             AddResult result(find(enteredKey), true);
894             ASSERT(result.iterator != end());
895             return result;
896         }
897         
898         internalCheckTableConsistency();
899         
900         return AddResult(makeKnownGoodIterator(entry), true);
901     }
902
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)
906     {
907         checkKey<HashTranslator>(key);
908
909         invalidateIterators();
910
911         if (!m_table)
912             expand();
913
914         internalCheckTableConsistency();
915
916         FullLookupType lookupResult = fullLookupForWriting<HashTranslator>(key);
917
918         ValueType* entry = lookupResult.first.first;
919         bool found = lookupResult.first.second;
920         unsigned h = lookupResult.second;
921         
922         if (found)
923             return AddResult(makeKnownGoodIterator(entry), false);
924         
925         if (isDeletedBucket(*entry)) {
926             initializeBucket(*entry);
927             --m_deletedCount;
928         }
929         
930         HashTranslator::translate(*entry, key, extra, h);
931         ++m_keyCount;
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);
937             expand();
938             AddResult result(find(enteredKey), true);
939             ASSERT(result.iterator != end());
940             return result;
941         }
942
943         internalCheckTableConsistency();
944
945         return AddResult(makeKnownGoodIterator(entry), true);
946     }
947
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)
950     {
951         ASSERT(m_table);
952         ASSERT(!lookupForWriting(Extractor::extract(entry)).second);
953         ASSERT(!isDeletedBucket(*(lookupForWriting(Extractor::extract(entry)).first)));
954 #if DUMP_HASHTABLE_STATS
955         atomicIncrement(&HashTableStats::numReinserts);
956 #endif
957 #if DUMP_HASHTABLE_STATS_PER_TABLE
958         ++m_stats->numReinserts;
959 #endif
960
961         Mover<ValueType, Traits::needsDestruction>::move(entry, *lookupForWriting(Extractor::extract(entry)).first);
962     }
963
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)
967     {
968         if (!m_table)
969             return end();
970
971         ValueType* entry = lookup<HashTranslator>(key);
972         if (!entry)
973             return end();
974
975         return makeKnownGoodIterator(entry);
976     }
977
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
981     {
982         if (!m_table)
983             return end();
984
985         ValueType* entry = const_cast<HashTable*>(this)->lookup<HashTranslator>(key);
986         if (!entry)
987             return end();
988
989         return makeKnownGoodConstIterator(entry);
990     }
991
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
995     {
996         if (!m_table)
997             return false;
998
999         return const_cast<HashTable*>(this)->lookup<HashTranslator>(key);
1000     }
1001
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)
1004     {
1005         invalidateIterators();
1006         remove(pos);
1007     }
1008
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)
1011     {
1012         invalidateIterators();
1013         internalCheckTableConsistency();
1014         remove(pos);
1015     }
1016
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)
1019     {
1020 #if DUMP_HASHTABLE_STATS
1021         atomicIncrement(&HashTableStats::numRemoves);
1022 #endif
1023 #if DUMP_HASHTABLE_STATS_PER_TABLE
1024         ++m_stats->numRemoves;
1025 #endif
1026
1027         deleteBucket(*pos);
1028         ++m_deletedCount;
1029         --m_keyCount;
1030
1031         if (shouldShrink())
1032             shrink();
1033
1034         internalCheckTableConsistency();
1035     }
1036
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)
1039     {
1040         if (it == end())
1041             return;
1042
1043         removeAndInvalidate(const_cast<ValueType*>(it.m_iterator.m_position));
1044     }
1045
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)
1048     {
1049         if (it == end())
1050             return;
1051
1052         removeAndInvalidateWithoutEntryConsistencyCheck(const_cast<ValueType*>(it.m_iterator.m_position));
1053     }
1054
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)
1057     {
1058         if (it == end())
1059             return;
1060
1061         removeAndInvalidateWithoutEntryConsistencyCheck(const_cast<ValueType*>(it.m_position));
1062     }
1063
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)
1066     {
1067         remove(find(key));
1068     }
1069
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)
1072     {
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]);
1080         return result;
1081     }
1082
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)
1085     {
1086         if (Traits::needsDestruction) {
1087             for (int i = 0; i < size; ++i) {
1088                 if (!isDeletedBucket(table[i]))
1089                     table[i].~ValueType();
1090             }
1091         }
1092         fastFree(table);
1093     }
1094
1095     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1096     void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::expand()
1097     {
1098         int newSize;
1099         if (m_tableSize == 0)
1100             newSize = KeyTraits::minimumTableSize;
1101         else if (mustRehashInPlace())
1102             newSize = m_tableSize;
1103         else
1104             newSize = m_tableSize * 2;
1105
1106         rehash(newSize);
1107     }
1108
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)
1111     {
1112         internalCheckTableConsistencyExceptSize();
1113
1114         int oldTableSize = m_tableSize;
1115         ValueType* oldTable = m_table;
1116
1117 #if DUMP_HASHTABLE_STATS
1118         if (oldTableSize != 0)
1119             atomicIncrement(&HashTableStats::numRehashes);
1120 #endif
1121
1122 #if DUMP_HASHTABLE_STATS_PER_TABLE
1123         if (oldTableSize != 0)
1124             ++m_stats->numRehashes;
1125 #endif
1126
1127         m_tableSize = newTableSize;
1128         m_tableSizeMask = newTableSize - 1;
1129         m_table = allocateTable(newTableSize);
1130
1131         for (int i = 0; i != oldTableSize; ++i)
1132             if (!isEmptyOrDeletedBucket(oldTable[i]))
1133                 reinsert(oldTable[i]);
1134
1135         m_deletedCount = 0;
1136
1137         deallocateTable(oldTable, oldTableSize);
1138
1139         internalCheckTableConsistency();
1140     }
1141
1142     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1143     void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::clear()
1144     {
1145         invalidateIterators();
1146         if (!m_table)
1147             return;
1148
1149         deallocateTable(m_table, m_tableSize);
1150         m_table = 0;
1151         m_tableSize = 0;
1152         m_tableSizeMask = 0;
1153         m_keyCount = 0;
1154     }
1155
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)
1158         : m_table(0)
1159         , m_tableSize(0)
1160         , m_tableSizeMask(0)
1161         , m_keyCount(0)
1162         , m_deletedCount(0)
1163 #if CHECK_HASHTABLE_ITERATORS
1164         , m_iterators(0)
1165         , m_mutex(adoptPtr(new Mutex))
1166 #endif
1167 #if DUMP_HASHTABLE_STATS_PER_TABLE
1168         , m_stats(adoptPtr(new Stats(*other.m_stats)))
1169 #endif
1170     {
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)
1175             add(*it);
1176     }
1177
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)
1180     {
1181         invalidateIterators();
1182         other.invalidateIterators();
1183
1184         ValueType* tmp_table = m_table;
1185         m_table = other.m_table;
1186         other.m_table = tmp_table;
1187
1188         int tmp_tableSize = m_tableSize;
1189         m_tableSize = other.m_tableSize;
1190         other.m_tableSize = tmp_tableSize;
1191
1192         int tmp_tableSizeMask = m_tableSizeMask;
1193         m_tableSizeMask = other.m_tableSizeMask;
1194         other.m_tableSizeMask = tmp_tableSizeMask;
1195
1196         int tmp_keyCount = m_keyCount;
1197         m_keyCount = other.m_keyCount;
1198         other.m_keyCount = tmp_keyCount;
1199
1200         int tmp_deletedCount = m_deletedCount;
1201         m_deletedCount = other.m_deletedCount;
1202         other.m_deletedCount = tmp_deletedCount;
1203
1204 #if DUMP_HASHTABLE_STATS_PER_TABLE
1205         m_stats.swap(other.m_stats);
1206 #endif
1207     }
1208
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)
1211     {
1212         HashTable tmp(other);
1213         swap(tmp);
1214         return *this;
1215     }
1216
1217 #if !ASSERT_DISABLED
1218
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
1221     {
1222         checkTableConsistencyExceptSize();
1223         ASSERT(!m_table || !shouldExpand());
1224         ASSERT(!shouldShrink());
1225     }
1226
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
1229     {
1230         if (!m_table)
1231             return;
1232
1233         int count = 0;
1234         int deletedCount = 0;
1235         for (int j = 0; j < m_tableSize; ++j) {
1236             ValueType* entry = m_table + j;
1237             if (isEmptyBucket(*entry))
1238                 continue;
1239
1240             if (isDeletedBucket(*entry)) {
1241                 ++deletedCount;
1242                 continue;
1243             }
1244
1245             const_iterator it = find(Extractor::extract(*entry));
1246             ASSERT(entry == it.m_position);
1247             ++count;
1248
1249             ValueCheck<Key>::checkConsistency(it->first);
1250         }
1251
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);
1257     }
1258
1259 #endif // ASSERT_DISABLED
1260
1261 #if CHECK_HASHTABLE_ITERATORS
1262
1263     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
1264     void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::invalidateIterators()
1265     {
1266         MutexLocker lock(*m_mutex);
1267         const_iterator* next;
1268         for (const_iterator* p = m_iterators; p; p = next) {
1269             next = p->m_next;
1270             p->m_table = 0;
1271             p->m_next = 0;
1272             p->m_previous = 0;
1273         }
1274         m_iterators = 0;
1275     }
1276
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)
1280     {
1281         it->m_table = table;
1282         it->m_previous = 0;
1283
1284         // Insert iterator at head of doubly-linked list of iterators.
1285         if (!table) {
1286             it->m_next = 0;
1287         } else {
1288             MutexLocker lock(*table->m_mutex);
1289             ASSERT(table->m_iterators != it);
1290             it->m_next = table->m_iterators;
1291             table->m_iterators = it;
1292             if (it->m_next) {
1293                 ASSERT(!it->m_next->m_previous);
1294                 it->m_next->m_previous = it;
1295             }
1296         }
1297     }
1298
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)
1301     {
1302         typedef HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> HashTableType;
1303         typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> const_iterator;
1304
1305         // Delete iterator from doubly-linked list of iterators.
1306         if (!it->m_table) {
1307             ASSERT(!it->m_next);
1308             ASSERT(!it->m_previous);
1309         } else {
1310             MutexLocker lock(*it->m_table->m_mutex);
1311             if (it->m_next) {
1312                 ASSERT(it->m_next->m_previous == it);
1313                 it->m_next->m_previous = it->m_previous;
1314             }
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;
1319             } else {
1320                 ASSERT(it->m_table->m_iterators == it);
1321                 it->m_table->m_iterators = it->m_next;
1322             }
1323         }
1324
1325         it->m_table = 0;
1326         it->m_next = 0;
1327         it->m_previous = 0;
1328     }
1329
1330 #endif // CHECK_HASHTABLE_ITERATORS
1331
1332     // iterator adapters
1333
1334     template<typename HashTableType, typename ValueType> struct HashTableConstIteratorAdapter {
1335         HashTableConstIteratorAdapter() {}
1336         HashTableConstIteratorAdapter(const typename HashTableType::const_iterator& impl) : m_impl(impl) {}
1337
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(); }
1341
1342         HashTableConstIteratorAdapter& operator++() { ++m_impl; return *this; }
1343         // postfix ++ intentionally omitted
1344
1345         typename HashTableType::const_iterator m_impl;
1346     };
1347
1348     template<typename HashTableType, typename ValueType> struct HashTableIteratorAdapter {
1349         HashTableIteratorAdapter() {}
1350         HashTableIteratorAdapter(const typename HashTableType::iterator& impl) : m_impl(impl) {}
1351
1352         ValueType* get() const { return (ValueType*)m_impl.get(); }
1353         ValueType& operator*() const { return *get(); }
1354         ValueType* operator->() const { return get(); }
1355
1356         HashTableIteratorAdapter& operator++() { ++m_impl; return *this; }
1357         // postfix ++ intentionally omitted
1358
1359         operator HashTableConstIteratorAdapter<HashTableType, ValueType>() {
1360             typename HashTableType::const_iterator i = m_impl;
1361             return i;
1362         }
1363
1364         typename HashTableType::iterator m_impl;
1365     };
1366
1367     template<typename T, typename U>
1368     inline bool operator==(const HashTableConstIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1369     {
1370         return a.m_impl == b.m_impl;
1371     }
1372
1373     template<typename T, typename U>
1374     inline bool operator!=(const HashTableConstIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1375     {
1376         return a.m_impl != b.m_impl;
1377     }
1378
1379     template<typename T, typename U>
1380     inline bool operator==(const HashTableIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b)
1381     {
1382         return a.m_impl == b.m_impl;
1383     }
1384
1385     template<typename T, typename U>
1386     inline bool operator!=(const HashTableIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b)
1387     {
1388         return a.m_impl != b.m_impl;
1389     }
1390
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)
1394     {
1395         return a.m_impl == b.m_impl;
1396     }
1397
1398     template<typename T, typename U>
1399     inline bool operator!=(const HashTableConstIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b)
1400     {
1401         return a.m_impl != b.m_impl;
1402     }
1403
1404     template<typename T, typename U>
1405     inline bool operator==(const HashTableIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1406     {
1407         return a.m_impl == b.m_impl;
1408     }
1409
1410     template<typename T, typename U>
1411     inline bool operator!=(const HashTableIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b)
1412     {
1413         return a.m_impl != b.m_impl;
1414     }
1415
1416 } // namespace WTF
1417
1418 #include <wtf/HashIterators.h>
1419
1420 #endif // WTF_HashTable_h