Chromium Code Reviews| Index: runtime/vm/hash_map.h |
| =================================================================== |
| --- runtime/vm/hash_map.h (revision 42801) |
| +++ runtime/vm/hash_map.h (working copy) |
| @@ -31,8 +31,8 @@ |
| void Clear() { |
| if (!IsEmpty()) { |
| count_ = 0; |
| - memset(array_, 0, sizeof(HashMapListElement) * array_size_); |
| - memset(lists_, 0, sizeof(HashMapListElement) * lists_size_); |
| + InitArray(array_, array_size_); |
| + InitArray(lists_, lists_size_); |
| lists_[0].next = kNil; |
| for (intptr_t i = 1; i < lists_size_; ++i) { |
| lists_[i].next = i - 1; |
| @@ -44,11 +44,18 @@ |
| protected: |
| // A linked list of T values. Stored in arrays. |
| struct HashMapListElement { |
| + HashMapListElement() : kv(KeyValueTrait::NoValue()), next(kNil) { } |
| typename KeyValueTrait::Pair kv; |
| intptr_t next; // Index in the array of the next list element. |
| }; |
| static const intptr_t kNil = -1; // The end of a linked list |
| + static void InitArray(HashMapListElement* array, intptr_t size) { |
| + for (intptr_t i = 0; i < size; ++i) { |
| + array[i] = HashMapListElement(); |
| + } |
| + } |
| + |
| // Must be a power of 2. |
| static const intptr_t kInitialSize = 16; |
| @@ -70,8 +77,7 @@ |
| typename KeyValueTrait::Value |
| DirectChainedHashMap<KeyValueTrait>:: |
| Lookup(typename KeyValueTrait::Key key) const { |
| - const typename KeyValueTrait::Value kNoValue = |
| - static_cast<typename KeyValueTrait::Value>(0); |
| + const typename KeyValueTrait::Value kNoValue = KeyValueTrait::NoValue(); |
| uword hash = static_cast<uword>(KeyValueTrait::Hashcode(key)); |
| uword pos = Bound(hash); |
| @@ -111,8 +117,7 @@ |
| template <typename KeyValueTrait> |
| void DirectChainedHashMap<KeyValueTrait>::Resize(intptr_t new_size) { |
| - const typename KeyValueTrait::Value kNoValue = |
| - static_cast<typename KeyValueTrait::Value>(0); |
| + const typename KeyValueTrait::Value kNoValue = KeyValueTrait::NoValue(); |
|
Vyacheslav Egorov (Google)
2015/01/13 13:47:30
You can probably use ValueOf(KeyValueTrait::Pair()
Florian Schneider
2015/01/13 14:09:08
Done. KeyValueTrait::ValueOf(typename KeyValueTra
|
| ASSERT(new_size > count_); |
| // Hashing the values into the new array has no more collisions than in the |
| @@ -125,7 +130,7 @@ |
| HashMapListElement* new_array = |
| Isolate::Current()->current_zone()->Alloc<HashMapListElement>(new_size); |
| - memset(new_array, 0, sizeof(HashMapListElement) * new_size); |
| + InitArray(new_array, new_size); |
| HashMapListElement* old_array = array_; |
| intptr_t old_size = array_size_; |
| @@ -164,7 +169,7 @@ |
| HashMapListElement* new_lists = |
| Isolate::Current()->current_zone()-> |
| Alloc<HashMapListElement>(new_size); |
| - memset(new_lists, 0, sizeof(HashMapListElement) * new_size); |
| + InitArray(new_lists, new_size); |
| HashMapListElement* old_lists = lists_; |
| intptr_t old_size = lists_size_; |
| @@ -185,8 +190,7 @@ |
| template <typename KeyValueTrait> |
| void DirectChainedHashMap<KeyValueTrait>:: |
| Insert(typename KeyValueTrait::Pair kv) { |
| - const typename KeyValueTrait::Value kNoValue = |
| - static_cast<typename KeyValueTrait::Value>(0); |
| + const typename KeyValueTrait::Value kNoValue = KeyValueTrait::NoValue(); |
| ASSERT(KeyValueTrait::ValueOf(kv) != kNoValue); |
| // Resizing when half of the hashtable is filled up. |
| @@ -229,6 +233,8 @@ |
| return kv; |
| } |
| + static Value NoValue() { return NULL; } |
| + |
| static inline intptr_t Hashcode(Key key) { |
| return key->Hashcode(); |
| } |