| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 #pragma once | 1 #pragma once |
| 2 | 2 |
| 3 #include <cstddef> | 3 #include <cstddef> |
| 4 #include <cmath> | 4 #include <cmath> |
| 5 #include <initializer_list> | 5 #include <initializer_list> |
| 6 #include <memory> | 6 #include <memory> |
| 7 | 7 |
| 8 #include "String.h" | 8 #include "String.h" |
| 9 #include "debug.h" | 9 #include "debug.h" |
| 10 | 10 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 template<typename Entry> | 84 template<typename Entry> |
| 85 class HashContainer | 85 class HashContainer |
| 86 { | 86 { |
| 87 public: | 87 public: |
| 88 typedef Entry entry_type; | 88 typedef Entry entry_type; |
| 89 typedef size_t size_type; | 89 typedef size_t size_type; |
| 90 typedef HashContainerIterator<Entry> const_iterator; | 90 typedef HashContainerIterator<Entry> const_iterator; |
| 91 typedef HashContainerReference<const Entry> const_reference; | 91 typedef HashContainerReference<const Entry> const_reference; |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 HashContainer(const HashContainer& other); | 94 explicit HashContainer(const HashContainer& other); |
| 95 void operator=(const HashContainer& other); | 95 void operator=(const HashContainer& other); |
| 96 | 96 |
| 97 protected: | 97 protected: |
| 98 static constexpr size_type MIN_BUCKETS = 1; | 98 static constexpr size_type MIN_BUCKETS = 1; |
| 99 static constexpr double LOAD_FACTOR = 0.8; | 99 static constexpr double LOAD_FACTOR = 0.8; |
| 100 std::unique_ptr<entry_type[]> mBuckets; | 100 std::unique_ptr<entry_type[]> mBuckets; |
| 101 size_type mBucketCount; | 101 size_type mBucketCount; |
| 102 size_type mEntryCount; | 102 size_type mEntryCount; |
| 103 | 103 |
| 104 #if defined(DEBUG) | 104 #if defined(DEBUG) |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 130 entry_type* find_bucket(const String& key) const | 130 entry_type* find_bucket(const String& key) const |
| 131 { | 131 { |
| 132 size_type h = hash(key); | 132 size_type h = hash(key); |
| 133 | 133 |
| 134 // This does quadratic probing, effectively the following formula is used: | 134 // This does quadratic probing, effectively the following formula is used: |
| 135 // pos = (hash + 1/2 i + 1/2 i ^ 2) mod bucketCount | 135 // pos = (hash + 1/2 i + 1/2 i ^ 2) mod bucketCount |
| 136 for (size_type i = 0; ; ++i) | 136 for (size_type i = 0; ; ++i) |
| 137 { | 137 { |
| 138 // mBucketCount is 2^n so (h & mBucketCount - 1) is equivalent to | 138 // mBucketCount is 2^n so (h & mBucketCount - 1) is equivalent to |
| 139 // h % mBucketCount but significantly faster. | 139 // h % mBucketCount but significantly faster. |
| 140 entry_type* entry = &mBuckets[h & mBucketCount - 1]; | 140 entry_type* entry = &mBuckets[h & (mBucketCount - 1)]; |
|
sergei
2017/01/10 15:58:04
Could we put "h & (mBucketCount - 1)" to hide comp
Wladimir Palant
2017/03/13 17:42:23
Done.
| |
| 141 if (entry->first.is_invalid() || entry->first.equals(key)) | 141 if (entry->first.is_invalid() || entry->first.equals(key)) |
| 142 return entry; | 142 return entry; |
| 143 h += i; | 143 h += i; |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 | 146 |
| 147 void resize(size_type bucketCount) | 147 void resize(size_type bucketCount) |
| 148 { | 148 { |
| 149 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets)); | 149 std::unique_ptr<entry_type[]> oldBuckets(std::move(mBuckets)); |
| 150 size_type oldCount = mBucketCount; | 150 size_type oldCount = mBucketCount; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 const_reference find(const String& key) const | 326 const_reference find(const String& key) const |
| 327 { | 327 { |
| 328 return super::find(key); | 328 return super::find(key); |
| 329 } | 329 } |
| 330 | 330 |
| 331 reference find(const String& key) | 331 reference find(const String& key) |
| 332 { | 332 { |
| 333 return reference(this, key, super::find_bucket(key)); | 333 return reference(this, key, super::find_bucket(key)); |
| 334 } | 334 } |
| 335 }; | 335 }; |
| LEFT | RIGHT |