OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserv
ed. | 2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserv
ed. |
3 * Copyright (C) 2008 David Levin <levin@chromium.org> | 3 * Copyright (C) 2008 David Levin <levin@chromium.org> |
4 * | 4 * |
5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
9 * | 9 * |
10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 78 |
79 friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, Key
Traits, Allocator>; | 79 friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, Key
Traits, Allocator>; |
80 friend class HashTableIterator<Key, Value, Extractor, HashFunctions, Tra
its, KeyTraits, Allocator>; | 80 friend class HashTableIterator<Key, Value, Extractor, HashFunctions, Tra
its, KeyTraits, Allocator>; |
81 | 81 |
82 void skipEmptyBuckets() | 82 void skipEmptyBuckets() |
83 { | 83 { |
84 while (m_position != m_endPosition && HashTableType::isEmptyOrDelete
dBucket(*m_position)) | 84 while (m_position != m_endPosition && HashTableType::isEmptyOrDelete
dBucket(*m_position)) |
85 ++m_position; | 85 ++m_position; |
86 } | 86 } |
87 | 87 |
88 HashTableConstIterator(PointerType position, PointerType endPosition) | 88 HashTableConstIterator(PointerType position, PointerType endPosition, co
nst HashTableType* container) |
89 : m_position(position), m_endPosition(endPosition) | 89 : m_position(position) |
| 90 , m_endPosition(endPosition) |
| 91 #ifdef ASSERT_ENABLED |
| 92 , m_container(container) |
| 93 , m_containerModifications(container->modifications()) |
| 94 #endif |
90 { | 95 { |
91 skipEmptyBuckets(); | 96 skipEmptyBuckets(); |
92 } | 97 } |
93 | 98 |
94 HashTableConstIterator(PointerType position, PointerType endPosition, Ha
shItemKnownGoodTag) | 99 HashTableConstIterator(PointerType position, PointerType endPosition, co
nst HashTableType* container, HashItemKnownGoodTag) |
95 : m_position(position), m_endPosition(endPosition) | 100 : m_position(position) |
| 101 , m_endPosition(endPosition) |
| 102 #ifdef ASSERT_ENABLED |
| 103 , m_container(container) |
| 104 , m_containerModifications(container->modifications()) |
| 105 #endif |
96 { | 106 { |
97 } | 107 } |
98 | 108 |
| 109 void checkModifications() const |
| 110 { |
| 111 // HashTable and collections that build on it do not support |
| 112 // modifications while there is an iterator in use. The exception |
| 113 // is ListHashSet, which has its own iterators that tolerate |
| 114 // modification of the underlying set. |
| 115 ASSERT(m_containerModifications == m_container->modifications()); |
| 116 } |
| 117 |
99 public: | 118 public: |
100 HashTableConstIterator() | 119 HashTableConstIterator() |
101 { | 120 { |
102 } | 121 } |
103 | 122 |
104 GetType get() const | 123 GetType get() const |
105 { | 124 { |
| 125 checkModifications(); |
106 return m_position; | 126 return m_position; |
107 } | 127 } |
108 typename Traits::IteratorConstReferenceType operator*() const { return T
raits::getToReferenceConstConversion(get()); } | 128 typename Traits::IteratorConstReferenceType operator*() const { return T
raits::getToReferenceConstConversion(get()); } |
109 GetType operator->() const { return get(); } | 129 GetType operator->() const { return get(); } |
110 | 130 |
111 const_iterator& operator++() | 131 const_iterator& operator++() |
112 { | 132 { |
113 ASSERT(m_position != m_endPosition); | 133 ASSERT(m_position != m_endPosition); |
| 134 checkModifications(); |
114 ++m_position; | 135 ++m_position; |
115 skipEmptyBuckets(); | 136 skipEmptyBuckets(); |
116 return *this; | 137 return *this; |
117 } | 138 } |
118 | 139 |
119 // postfix ++ intentionally omitted | 140 // postfix ++ intentionally omitted |
120 | 141 |
121 // Comparison. | 142 // Comparison. |
122 bool operator==(const const_iterator& other) const | 143 bool operator==(const const_iterator& other) const |
123 { | 144 { |
124 return m_position == other.m_position; | 145 return m_position == other.m_position; |
125 } | 146 } |
126 bool operator!=(const const_iterator& other) const | 147 bool operator!=(const const_iterator& other) const |
127 { | 148 { |
128 return m_position != other.m_position; | 149 return m_position != other.m_position; |
129 } | 150 } |
130 bool operator==(const iterator& other) const | 151 bool operator==(const iterator& other) const |
131 { | 152 { |
132 return *this == static_cast<const_iterator>(other); | 153 return *this == static_cast<const_iterator>(other); |
133 } | 154 } |
134 bool operator!=(const iterator& other) const | 155 bool operator!=(const iterator& other) const |
135 { | 156 { |
136 return *this != static_cast<const_iterator>(other); | 157 return *this != static_cast<const_iterator>(other); |
137 } | 158 } |
138 | 159 |
139 private: | 160 private: |
140 PointerType m_position; | 161 PointerType m_position; |
141 PointerType m_endPosition; | 162 PointerType m_endPosition; |
| 163 #ifdef ASSERT_ENABLED |
| 164 const HashTableType* m_container; |
| 165 int64_t m_containerModifications; |
| 166 #endif |
142 }; | 167 }; |
143 | 168 |
144 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 169 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
145 class HashTableIterator { | 170 class HashTableIterator { |
146 private: | 171 private: |
| 172 typedef HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTrait
s, Allocator> HashTableType; |
147 typedef HashTableIterator<Key, Value, Extractor, HashFunctions, Traits,
KeyTraits, Allocator> iterator; | 173 typedef HashTableIterator<Key, Value, Extractor, HashFunctions, Traits,
KeyTraits, Allocator> iterator; |
148 typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Tra
its, KeyTraits, Allocator> const_iterator; | 174 typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Tra
its, KeyTraits, Allocator> const_iterator; |
149 typedef Value ValueType; | 175 typedef Value ValueType; |
150 typedef typename Traits::IteratorGetType GetType; | 176 typedef typename Traits::IteratorGetType GetType; |
151 typedef ValueType* PointerType; | 177 typedef ValueType* PointerType; |
152 | 178 |
153 friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, Key
Traits, Allocator>; | 179 friend class HashTable<Key, Value, Extractor, HashFunctions, Traits, Key
Traits, Allocator>; |
154 | 180 |
155 HashTableIterator(PointerType pos, PointerType end) : m_iterator(pos, en
d) { } | 181 HashTableIterator(PointerType pos, PointerType end, const HashTableType*
container) : m_iterator(pos, end, container) { } |
156 HashTableIterator(PointerType pos, PointerType end, HashItemKnownGoodTag
tag) : m_iterator(pos, end, tag) { } | 182 HashTableIterator(PointerType pos, PointerType end, const HashTableType*
container, HashItemKnownGoodTag tag) : m_iterator(pos, end, container, tag) { } |
157 | 183 |
158 public: | 184 public: |
159 HashTableIterator() { } | 185 HashTableIterator() { } |
160 | 186 |
161 // default copy, assignment and destructor are OK | 187 // default copy, assignment and destructor are OK |
162 | 188 |
163 GetType get() const { return const_cast<GetType>(m_iterator.get()); } | 189 GetType get() const { return const_cast<GetType>(m_iterator.get()); } |
164 typename Traits::IteratorReferenceType operator*() const { return Traits
::getToReferenceConversion(get()); } | 190 typename Traits::IteratorReferenceType operator*() const { return Traits
::getToReferenceConversion(get()); } |
165 GetType operator->() const { return get(); } | 191 GetType operator->() const { return get(); } |
166 | 192 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 368 |
343 static bool isEmptyBucket(const ValueType& value) { return isHashTraitsE
mptyValue<KeyTraits>(Extractor::extract(value)); } | 369 static bool isEmptyBucket(const ValueType& value) { return isHashTraitsE
mptyValue<KeyTraits>(Extractor::extract(value)); } |
344 static bool isDeletedBucket(const ValueType& value) { return KeyTraits::
isDeletedValue(Extractor::extract(value)); } | 370 static bool isDeletedBucket(const ValueType& value) { return KeyTraits::
isDeletedValue(Extractor::extract(value)); } |
345 static bool isEmptyOrDeletedBucket(const ValueType& value) { return Hash
TableHelper<ValueType, Extractor, KeyTraits>:: isEmptyOrDeletedBucket(value); } | 371 static bool isEmptyOrDeletedBucket(const ValueType& value) { return Hash
TableHelper<ValueType, Extractor, KeyTraits>:: isEmptyOrDeletedBucket(value); } |
346 | 372 |
347 ValueType* lookup(KeyPeekInType key) { return lookup<IdentityTranslatorT
ype>(key); } | 373 ValueType* lookup(KeyPeekInType key) { return lookup<IdentityTranslatorT
ype>(key); } |
348 template<typename HashTranslator, typename T> ValueType* lookup(const T&
); | 374 template<typename HashTranslator, typename T> ValueType* lookup(const T&
); |
349 | 375 |
350 void trace(typename Allocator::Visitor*); | 376 void trace(typename Allocator::Visitor*); |
351 | 377 |
| 378 #ifdef ASSERT_ENABLED |
| 379 int64_t modifications() const { return m_modifications; } |
| 380 void registerModification() { m_modifications++; } |
| 381 #else |
| 382 int64_t modifications() const { return 0; } |
| 383 void registerModification() { } |
| 384 #endif |
| 385 |
352 private: | 386 private: |
353 static ValueType* allocateTable(unsigned size); | 387 static ValueType* allocateTable(unsigned size); |
354 static void deallocateTable(ValueType* table, unsigned size); | 388 static void deallocateTable(ValueType* table, unsigned size); |
355 | 389 |
356 typedef std::pair<ValueType*, bool> LookupType; | 390 typedef std::pair<ValueType*, bool> LookupType; |
357 typedef std::pair<LookupType, unsigned> FullLookupType; | 391 typedef std::pair<LookupType, unsigned> FullLookupType; |
358 | 392 |
359 LookupType lookupForWriting(const Key& key) { return lookupForWriting<Id
entityTranslatorType>(key); }; | 393 LookupType lookupForWriting(const Key& key) { return lookupForWriting<Id
entityTranslatorType>(key); }; |
360 template<typename HashTranslator, typename T> FullLookupType fullLookupF
orWriting(const T&); | 394 template<typename HashTranslator, typename T> FullLookupType fullLookupF
orWriting(const T&); |
361 template<typename HashTranslator, typename T> LookupType lookupForWritin
g(const T&); | 395 template<typename HashTranslator, typename T> LookupType lookupForWritin
g(const T&); |
362 | 396 |
363 void remove(ValueType*); | 397 void remove(ValueType*); |
364 | 398 |
365 bool shouldExpand() const { return (m_keyCount + m_deletedCount) * m_max
Load >= m_tableSize; } | 399 bool shouldExpand() const { return (m_keyCount + m_deletedCount) * m_max
Load >= m_tableSize; } |
366 bool mustRehashInPlace() const { return m_keyCount * m_minLoad < m_table
Size * 2; } | 400 bool mustRehashInPlace() const { return m_keyCount * m_minLoad < m_table
Size * 2; } |
367 bool shouldShrink() const { return m_keyCount * m_minLoad < m_tableSize
&& m_tableSize > KeyTraits::minimumTableSize; } | 401 bool shouldShrink() const { return m_keyCount * m_minLoad < m_tableSize
&& m_tableSize > KeyTraits::minimumTableSize; } |
368 void expand(); | 402 void expand(); |
369 void shrink() { rehash(m_tableSize / 2); } | 403 void shrink() { rehash(m_tableSize / 2); } |
370 | 404 |
371 void rehash(unsigned newTableSize); | 405 void rehash(unsigned newTableSize); |
372 void reinsert(ValueType&); | 406 void reinsert(ValueType&); |
373 | 407 |
374 static void initializeBucket(ValueType& bucket); | 408 static void initializeBucket(ValueType& bucket); |
375 static void deleteBucket(ValueType& bucket) { bucket.~ValueType(); Trait
s::constructDeletedValue(bucket); } | 409 static void deleteBucket(ValueType& bucket) { bucket.~ValueType(); Trait
s::constructDeletedValue(bucket); } |
376 | 410 |
377 FullLookupType makeLookupResult(ValueType* position, bool found, unsigne
d hash) | 411 FullLookupType makeLookupResult(ValueType* position, bool found, unsigne
d hash) |
378 { return FullLookupType(LookupType(position, found), hash); } | 412 { return FullLookupType(LookupType(position, found), hash); } |
379 | 413 |
380 iterator makeIterator(ValueType* pos) { return iterator(pos, m_table + m
_tableSize); } | 414 iterator makeIterator(ValueType* pos) { return iterator(pos, m_table + m
_tableSize, this); } |
381 const_iterator makeConstIterator(ValueType* pos) const { return const_it
erator(pos, m_table + m_tableSize); } | 415 const_iterator makeConstIterator(ValueType* pos) const { return const_it
erator(pos, m_table + m_tableSize, this); } |
382 iterator makeKnownGoodIterator(ValueType* pos) { return iterator(pos, m_
table + m_tableSize, HashItemKnownGood); } | 416 iterator makeKnownGoodIterator(ValueType* pos) { return iterator(pos, m_
table + m_tableSize, this, HashItemKnownGood); } |
383 const_iterator makeKnownGoodConstIterator(ValueType* pos) const { return
const_iterator(pos, m_table + m_tableSize, HashItemKnownGood); } | 417 const_iterator makeKnownGoodConstIterator(ValueType* pos) const { return
const_iterator(pos, m_table + m_tableSize, this, HashItemKnownGood); } |
384 | 418 |
385 static const unsigned m_maxLoad = 2; | 419 static const unsigned m_maxLoad = 2; |
386 static const unsigned m_minLoad = 6; | 420 static const unsigned m_minLoad = 6; |
387 | 421 |
388 ValueType* m_table; | 422 ValueType* m_table; |
389 unsigned m_tableSize; | 423 unsigned m_tableSize; |
390 unsigned m_tableSizeMask; | 424 unsigned m_tableSizeMask; |
391 unsigned m_keyCount; | 425 unsigned m_keyCount; |
392 unsigned m_deletedCount; | 426 unsigned m_deletedCount; |
| 427 #ifdef ASSERT_ENABLED |
| 428 unsigned m_modifications; |
| 429 #endif |
393 | 430 |
394 #if DUMP_HASHTABLE_STATS_PER_TABLE | 431 #if DUMP_HASHTABLE_STATS_PER_TABLE |
395 public: | 432 public: |
396 mutable OwnPtr<Stats> m_stats; | 433 mutable OwnPtr<Stats> m_stats; |
397 #endif | 434 #endif |
398 | 435 |
399 template<bool x, typename T, typename U, typename V, typename W, typenam
e X, typename Y, typename Z> friend struct WeakProcessingHashTableHelper; | 436 template<bool x, typename T, typename U, typename V, typename W, typenam
e X, typename Y, typename Z> friend struct WeakProcessingHashTableHelper; |
400 }; | 437 }; |
401 | 438 |
402 // Set all the bits to one after the most significant bit: 00110101010 -> 00
111111111. | 439 // Set all the bits to one after the most significant bit: 00110101010 -> 00
111111111. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 COMPILE_ASSERT(value > (2 * size), HashTableCapacityHoldsContentSize); | 474 COMPILE_ASSERT(value > (2 * size), HashTableCapacityHoldsContentSize); |
438 }; | 475 }; |
439 | 476 |
440 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 477 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
441 inline HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Al
locator>::HashTable() | 478 inline HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Al
locator>::HashTable() |
442 : m_table(0) | 479 : m_table(0) |
443 , m_tableSize(0) | 480 , m_tableSize(0) |
444 , m_tableSizeMask(0) | 481 , m_tableSizeMask(0) |
445 , m_keyCount(0) | 482 , m_keyCount(0) |
446 , m_deletedCount(0) | 483 , m_deletedCount(0) |
| 484 #ifdef ASSERT_ENABLED |
| 485 , m_modifications(0) |
| 486 #endif |
447 #if DUMP_HASHTABLE_STATS_PER_TABLE | 487 #if DUMP_HASHTABLE_STATS_PER_TABLE |
448 , m_stats(adoptPtr(new Stats)) | 488 , m_stats(adoptPtr(new Stats)) |
449 #endif | 489 #endif |
450 { | 490 { |
451 } | 491 } |
452 | 492 |
453 inline unsigned doubleHash(unsigned key) | 493 inline unsigned doubleHash(unsigned key) |
454 { | 494 { |
455 key = ~key + (key >> 23); | 495 key = ~key + (key >> 23); |
456 key ^= (key << 12); | 496 key ^= (key << 12); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 #if DUMP_HASHTABLE_STATS_PER_TABLE | 762 #if DUMP_HASHTABLE_STATS_PER_TABLE |
723 ++perTableProbeCount; | 763 ++perTableProbeCount; |
724 m_stats->recordCollisionAtCount(perTableProbeCount); | 764 m_stats->recordCollisionAtCount(perTableProbeCount); |
725 #endif | 765 #endif |
726 | 766 |
727 if (!k) | 767 if (!k) |
728 k = 1 | doubleHash(h); | 768 k = 1 | doubleHash(h); |
729 i = (i + k) & sizeMask; | 769 i = (i + k) & sizeMask; |
730 } | 770 } |
731 | 771 |
| 772 registerModification(); |
| 773 |
732 if (deletedEntry) { | 774 if (deletedEntry) { |
733 initializeBucket(*deletedEntry); | 775 initializeBucket(*deletedEntry); |
734 entry = deletedEntry; | 776 entry = deletedEntry; |
735 --m_deletedCount; | 777 --m_deletedCount; |
736 } | 778 } |
737 | 779 |
738 HashTranslator::translate(*entry, key, extra); | 780 HashTranslator::translate(*entry, key, extra); |
739 | 781 |
740 ++m_keyCount; | 782 ++m_keyCount; |
741 | 783 |
(...skipping 22 matching lines...) Expand all Loading... |
764 | 806 |
765 FullLookupType lookupResult = fullLookupForWriting<HashTranslator>(key); | 807 FullLookupType lookupResult = fullLookupForWriting<HashTranslator>(key); |
766 | 808 |
767 ValueType* entry = lookupResult.first.first; | 809 ValueType* entry = lookupResult.first.first; |
768 bool found = lookupResult.first.second; | 810 bool found = lookupResult.first.second; |
769 unsigned h = lookupResult.second; | 811 unsigned h = lookupResult.second; |
770 | 812 |
771 if (found) | 813 if (found) |
772 return AddResult(entry, false); | 814 return AddResult(entry, false); |
773 | 815 |
| 816 registerModification(); |
| 817 |
774 if (isDeletedBucket(*entry)) { | 818 if (isDeletedBucket(*entry)) { |
775 initializeBucket(*entry); | 819 initializeBucket(*entry); |
776 --m_deletedCount; | 820 --m_deletedCount; |
777 } | 821 } |
778 | 822 |
779 HashTranslator::translate(*entry, key, extra, h); | 823 HashTranslator::translate(*entry, key, extra, h); |
780 ++m_keyCount; | 824 ++m_keyCount; |
781 if (shouldExpand()) { | 825 if (shouldExpand()) { |
782 // FIXME: This makes an extra copy on expand. Probably not that bad
since | 826 // FIXME: This makes an extra copy on expand. Probably not that bad
since |
783 // expand is rare, but would be better to have a version of expand t
hat can | 827 // expand is rare, but would be better to have a version of expand t
hat can |
784 // follow a pivot entry and return the new position. | 828 // follow a pivot entry and return the new position. |
785 typename WTF::RemoveReference<KeyPassInType>::Type enteredKey = Extr
actor::extract(*entry); | 829 typename WTF::RemoveReference<KeyPassInType>::Type enteredKey = Extr
actor::extract(*entry); |
786 expand(); | 830 expand(); |
787 iterator findResult = find(enteredKey); | 831 iterator findResult = find(enteredKey); |
788 ASSERT(findResult != end()); | 832 ASSERT(findResult != end()); |
789 ValueType* resultValue = findResult.get(); | 833 ValueType* resultValue = findResult.get(); |
790 AddResult result(resultValue, true); | 834 AddResult result(resultValue, true); |
791 return result; | 835 return result; |
792 } | 836 } |
793 | 837 |
794 return AddResult(entry, true); | 838 return AddResult(entry, true); |
795 } | 839 } |
796 | 840 |
797 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 841 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
798 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTrait
s, Allocator>::reinsert(ValueType& entry) | 842 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTrait
s, Allocator>::reinsert(ValueType& entry) |
799 { | 843 { |
800 ASSERT(m_table); | 844 ASSERT(m_table); |
| 845 registerModification(); |
801 ASSERT(!lookupForWriting(Extractor::extract(entry)).second); | 846 ASSERT(!lookupForWriting(Extractor::extract(entry)).second); |
802 ASSERT(!isDeletedBucket(*(lookupForWriting(Extractor::extract(entry)).fi
rst))); | 847 ASSERT(!isDeletedBucket(*(lookupForWriting(Extractor::extract(entry)).fi
rst))); |
803 #if DUMP_HASHTABLE_STATS | 848 #if DUMP_HASHTABLE_STATS |
804 atomicIncrement(&HashTableStats::numReinserts); | 849 atomicIncrement(&HashTableStats::numReinserts); |
805 #endif | 850 #endif |
806 #if DUMP_HASHTABLE_STATS_PER_TABLE | 851 #if DUMP_HASHTABLE_STATS_PER_TABLE |
807 ++m_stats->numReinserts; | 852 ++m_stats->numReinserts; |
808 #endif | 853 #endif |
809 | 854 |
810 Mover<ValueType, Traits::needsDestruction>::move(entry, *lookupForWritin
g(Extractor::extract(entry)).first); | 855 Mover<ValueType, Traits::needsDestruction>::move(entry, *lookupForWritin
g(Extractor::extract(entry)).first); |
(...skipping 24 matching lines...) Expand all Loading... |
835 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 880 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
836 template <typename HashTranslator, typename T> | 881 template <typename HashTranslator, typename T> |
837 bool HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::contains(const T& key) const | 882 bool HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::contains(const T& key) const |
838 { | 883 { |
839 return const_cast<HashTable*>(this)->lookup<HashTranslator>(key); | 884 return const_cast<HashTable*>(this)->lookup<HashTranslator>(key); |
840 } | 885 } |
841 | 886 |
842 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 887 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
843 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::remove(ValueType* pos) | 888 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::remove(ValueType* pos) |
844 { | 889 { |
| 890 registerModification(); |
845 #if DUMP_HASHTABLE_STATS | 891 #if DUMP_HASHTABLE_STATS |
846 atomicIncrement(&HashTableStats::numRemoves); | 892 atomicIncrement(&HashTableStats::numRemoves); |
847 #endif | 893 #endif |
848 #if DUMP_HASHTABLE_STATS_PER_TABLE | 894 #if DUMP_HASHTABLE_STATS_PER_TABLE |
849 ++m_stats->numRemoves; | 895 ++m_stats->numRemoves; |
850 #endif | 896 #endif |
851 | 897 |
852 deleteBucket(*pos); | 898 deleteBucket(*pos); |
853 ++m_deletedCount; | 899 ++m_deletedCount; |
854 --m_keyCount; | 900 --m_keyCount; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 reinsert(oldTable[i]); | 999 reinsert(oldTable[i]); |
954 | 1000 |
955 m_deletedCount = 0; | 1001 m_deletedCount = 0; |
956 | 1002 |
957 deallocateTable(oldTable, oldTableSize); | 1003 deallocateTable(oldTable, oldTableSize); |
958 } | 1004 } |
959 | 1005 |
960 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 1006 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
961 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::clear() | 1007 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::clear() |
962 { | 1008 { |
| 1009 registerModification(); |
963 if (!m_table) | 1010 if (!m_table) |
964 return; | 1011 return; |
965 | 1012 |
966 deallocateTable(m_table, m_tableSize); | 1013 deallocateTable(m_table, m_tableSize); |
967 m_table = 0; | 1014 m_table = 0; |
968 m_tableSize = 0; | 1015 m_tableSize = 0; |
969 m_tableSizeMask = 0; | 1016 m_tableSizeMask = 0; |
970 m_keyCount = 0; | 1017 m_keyCount = 0; |
971 } | 1018 } |
972 | 1019 |
973 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 1020 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
974 HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allocator
>::HashTable(const HashTable& other) | 1021 HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allocator
>::HashTable(const HashTable& other) |
975 : m_table(0) | 1022 : m_table(0) |
976 , m_tableSize(0) | 1023 , m_tableSize(0) |
977 , m_tableSizeMask(0) | 1024 , m_tableSizeMask(0) |
978 , m_keyCount(0) | 1025 , m_keyCount(0) |
979 , m_deletedCount(0) | 1026 , m_deletedCount(0) |
| 1027 #ifdef ASSERT_ENABLED |
| 1028 , m_modifications(0) |
| 1029 #endif |
980 #if DUMP_HASHTABLE_STATS_PER_TABLE | 1030 #if DUMP_HASHTABLE_STATS_PER_TABLE |
981 , m_stats(adoptPtr(new Stats(*other.m_stats))) | 1031 , m_stats(adoptPtr(new Stats(*other.m_stats))) |
982 #endif | 1032 #endif |
983 { | 1033 { |
984 // Copy the hash table the dumb way, by adding each element to the new t
able. | 1034 // Copy the hash table the dumb way, by adding each element to the new t
able. |
985 // It might be more efficient to copy the table slots, but it's not clea
r that efficiency is needed. | 1035 // It might be more efficient to copy the table slots, but it's not clea
r that efficiency is needed. |
986 const_iterator end = other.end(); | 1036 const_iterator end = other.end(); |
987 for (const_iterator it = other.begin(); it != end; ++it) | 1037 for (const_iterator it = other.begin(); it != end; ++it) |
988 add(*it); | 1038 add(*it); |
989 } | 1039 } |
990 | 1040 |
991 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 1041 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
992 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::swap(HashTable& other) | 1042 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allo
cator>::swap(HashTable& other) |
993 { | 1043 { |
994 std::swap(m_table, other.m_table); | 1044 std::swap(m_table, other.m_table); |
995 std::swap(m_tableSize, other.m_tableSize); | 1045 std::swap(m_tableSize, other.m_tableSize); |
996 std::swap(m_tableSizeMask, other.m_tableSizeMask); | 1046 std::swap(m_tableSizeMask, other.m_tableSizeMask); |
997 std::swap(m_keyCount, other.m_keyCount); | 1047 std::swap(m_keyCount, other.m_keyCount); |
998 std::swap(m_deletedCount, other.m_deletedCount); | 1048 std::swap(m_deletedCount, other.m_deletedCount); |
999 | 1049 |
| 1050 #ifdef ASSERT_ENABLED |
| 1051 std::swap(m_modifications, other.m_modifications); |
| 1052 #endif |
| 1053 |
1000 #if DUMP_HASHTABLE_STATS_PER_TABLE | 1054 #if DUMP_HASHTABLE_STATS_PER_TABLE |
1001 m_stats.swap(other.m_stats); | 1055 m_stats.swap(other.m_stats); |
1002 #endif | 1056 #endif |
1003 } | 1057 } |
1004 | 1058 |
1005 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> | 1059 template<typename Key, typename Value, typename Extractor, typename HashFunc
tions, typename Traits, typename KeyTraits, typename Allocator> |
1006 HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allocator
>& HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allocator>
::operator=(const HashTable& other) | 1060 HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allocator
>& HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits, Allocator>
::operator=(const HashTable& other) |
1007 { | 1061 { |
1008 HashTable tmp(other); | 1062 HashTable tmp(other); |
1009 swap(tmp); | 1063 swap(tmp); |
(...skipping 17 matching lines...) Expand all Loading... |
1027 if (table->m_table) { | 1081 if (table->m_table) { |
1028 // This just marks it live and does not push anything onto the | 1082 // This just marks it live and does not push anything onto the |
1029 // marking stack. | 1083 // marking stack. |
1030 Allocator::markNoTracing(visitor, table->m_table); | 1084 Allocator::markNoTracing(visitor, table->m_table); |
1031 // Now perform weak processing (this is a no-op if the backing | 1085 // Now perform weak processing (this is a no-op if the backing |
1032 // was accessible through an iterator and was already marked | 1086 // was accessible through an iterator and was already marked |
1033 // strongly). | 1087 // strongly). |
1034 for (typename HashTableType::ValueType* element = table->m_table
+ table->m_tableSize - 1; element >= table->m_table; element--) { | 1088 for (typename HashTableType::ValueType* element = table->m_table
+ table->m_tableSize - 1; element >= table->m_table; element--) { |
1035 if (!HashTableType::isEmptyOrDeletedBucket(*element)) { | 1089 if (!HashTableType::isEmptyOrDeletedBucket(*element)) { |
1036 if (Allocator::hasDeadMember(visitor, *element)) { | 1090 if (Allocator::hasDeadMember(visitor, *element)) { |
| 1091 table->registerModification(); |
1037 HashTableType::deleteBucket(*element); // Also calls
the destructor. | 1092 HashTableType::deleteBucket(*element); // Also calls
the destructor. |
1038 table->m_deletedCount++; | 1093 table->m_deletedCount++; |
1039 table->m_keyCount--; | 1094 table->m_keyCount--; |
1040 // We don't rehash the backing until the next add | 1095 // We don't rehash the backing until the next add |
1041 // or delete, because that would cause allocation | 1096 // or delete, because that would cause allocation |
1042 // during GC. | 1097 // during GC. |
1043 } | 1098 } |
1044 } | 1099 } |
1045 } | 1100 } |
1046 } | 1101 } |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1166 inline bool operator!=(const HashTableIteratorAdapter<T, U>& a, const HashTa
bleConstIteratorAdapter<T, U>& b) | 1221 inline bool operator!=(const HashTableIteratorAdapter<T, U>& a, const HashTa
bleConstIteratorAdapter<T, U>& b) |
1167 { | 1222 { |
1168 return a.m_impl != b.m_impl; | 1223 return a.m_impl != b.m_impl; |
1169 } | 1224 } |
1170 | 1225 |
1171 } // namespace WTF | 1226 } // namespace WTF |
1172 | 1227 |
1173 #include "wtf/HashIterators.h" | 1228 #include "wtf/HashIterators.h" |
1174 | 1229 |
1175 #endif // WTF_HashTable_h | 1230 #endif // WTF_HashTable_h |
OLD | NEW |