Chromium Code Reviews| Index: src/base/hashmap.h |
| diff --git a/src/libsampler/hashmap.h b/src/base/hashmap.h |
| similarity index 29% |
| rename from src/libsampler/hashmap.h |
| rename to src/base/hashmap.h |
| index e4b3cc6009b977756f173d01fca426386358b499..736ddf46aa4c7b7bb82770bab0cf3cd1bca6fdeb 100644 |
| --- a/src/libsampler/hashmap.h |
| +++ b/src/base/hashmap.h |
| @@ -2,31 +2,27 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -// This file is ported from src/hashmap.h |
| - |
| -#ifndef V8_LIBSAMPLER_HASHMAP_H_ |
| -#define V8_LIBSAMPLER_HASHMAP_H_ |
| +#ifndef V8_BASE_HASHMAP_H_ |
| +#define V8_BASE_HASHMAP_H_ |
| #include "src/base/bits.h" |
| #include "src/base/logging.h" |
| -#include "src/libsampler/utils.h" |
| namespace v8 { |
| -namespace sampler { |
| +namespace base { |
| class HashMapImpl { |
|
Yang
2016/05/30 09:19:04
Imo this name is misleading. "Impl" stands for imp
|
| public: |
| - typedef bool (*MatchFun) (void* key1, void* key2); |
| + typedef bool (*MatchFun)(void* key1, void* key2); |
| - // The default capacity. |
| + // The default capacity. This is used by the call sites which want |
| + // to pass in a non-default AllocationPolicy but want to use the |
| + // default value of capacity specified by the implementation. |
| static const uint32_t kDefaultHashMapCapacity = 8; |
| - // initial_capacity is the size of the initial hash map; |
| - // it must be a power of 2 (and thus must not be 0). |
| - HashMapImpl(MatchFun match, |
| - uint32_t capacity = kDefaultHashMapCapacity); |
| + HashMapImpl() {} |
| - ~HashMapImpl(); |
| + virtual ~HashMapImpl() {} |
| // HashMap entries are (key, value, hash) triplets. |
| // Some clients may not need to use the value slot |
| @@ -35,7 +31,7 @@ class HashMapImpl { |
| void* key; |
| void* value; |
| uint32_t hash; // The full hash value for key |
| - int order; // If you never remove entries this is the insertion order. |
| + int order; // If you never remove entries this is the insertion order. |
| }; |
| // If an entry with matching key is found, returns that entry. |
| @@ -75,204 +71,33 @@ class HashMapImpl { |
| Entry* Next(Entry* p) const; |
| // Some match functions defined for convenience. |
| - static bool PointersMatch(void* key1, void* key2) { |
| - return key1 == key2; |
| - } |
| + static bool PointersMatch(void* key1, void* key2) { return key1 == key2; } |
| - private: |
| + protected: |
| MatchFun match_; |
| Entry* map_; |
| uint32_t capacity_; |
| uint32_t occupancy_; |
| - Entry* map_end() const { return map_ + capacity_; } |
| Entry* Probe(void* key, uint32_t hash) const; |
| - void Initialize(uint32_t capacity); |
| - void Resize(); |
| + virtual void Resize() = 0; |
| + Entry* map_end() const { return map_ + capacity_; } |
| }; |
| -typedef HashMapImpl HashMap; |
| - |
| -HashMapImpl::HashMapImpl(MatchFun match, uint32_t initial_capacity) { |
| - match_ = match; |
| - Initialize(initial_capacity); |
| -} |
| - |
| - |
| -HashMapImpl::~HashMapImpl() { |
| - Malloced::Delete(map_); |
| -} |
| - |
| - |
| -HashMapImpl::Entry* HashMapImpl::Lookup(void* key, uint32_t hash) const { |
| - Entry* p = Probe(key, hash); |
| - return p->key != NULL ? p : NULL; |
| -} |
| - |
| - |
| -HashMapImpl::Entry* HashMapImpl::LookupOrInsert(void* key, uint32_t hash) { |
| - // Find a matching entry. |
| - Entry* p = Probe(key, hash); |
| - if (p->key != NULL) { |
| - return p; |
| - } |
| - |
| - // No entry found; insert one. |
| - p->key = key; |
| - p->value = NULL; |
| - p->hash = hash; |
| - p->order = occupancy_; |
| - occupancy_++; |
| - |
| - // Grow the map if we reached >= 80% occupancy. |
| - if (occupancy_ + occupancy_ / 4 >= capacity_) { |
| - Resize(); |
| - p = Probe(key, hash); |
| - } |
| - |
| - return p; |
| -} |
| - |
| - |
| -void* HashMapImpl::Remove(void* key, uint32_t hash) { |
| - // Lookup the entry for the key to remove. |
| - Entry* p = Probe(key, hash); |
| - if (p->key == NULL) { |
| - // Key not found nothing to remove. |
| - return NULL; |
| - } |
| - |
| - void* value = p->value; |
| - // To remove an entry we need to ensure that it does not create an empty |
| - // entry that will cause the search for another entry to stop too soon. If all |
| - // the entries between the entry to remove and the next empty slot have their |
| - // initial position inside this interval, clearing the entry to remove will |
| - // not break the search. If, while searching for the next empty entry, an |
| - // entry is encountered which does not have its initial position between the |
| - // entry to remove and the position looked at, then this entry can be moved to |
| - // the place of the entry to remove without breaking the search for it. The |
| - // entry made vacant by this move is now the entry to remove and the process |
| - // starts over. |
| - // Algorithm from http://en.wikipedia.org/wiki/Open_addressing. |
| - |
| - // This guarantees loop termination as there is at least one empty entry so |
| - // eventually the removed entry will have an empty entry after it. |
| - DCHECK(occupancy_ < capacity_); |
| - |
| - // p is the candidate entry to clear. q is used to scan forwards. |
| - Entry* q = p; // Start at the entry to remove. |
| - while (true) { |
| - // Move q to the next entry. |
| - q = q + 1; |
| - if (q == map_end()) { |
| - q = map_; |
| - } |
| - |
| - // All entries between p and q have their initial position between p and q |
| - // and the entry p can be cleared without breaking the search for these |
| - // entries. |
| - if (q->key == NULL) { |
| - break; |
| - } |
| - |
| - // Find the initial position for the entry at position q. |
| - Entry* r = map_ + (q->hash & (capacity_ - 1)); |
| - |
| - // If the entry at position q has its initial position outside the range |
| - // between p and q it can be moved forward to position p and will still be |
| - // found. There is now a new candidate entry for clearing. |
| - if ((q > p && (r <= p || r > q)) || |
| - (q < p && (r <= p && r > q))) { |
| - *p = *q; |
| - p = q; |
| - } |
| - } |
| - |
| - // Clear the entry which is allowed to en emptied. |
| - p->key = NULL; |
| - occupancy_--; |
| - return value; |
| -} |
| - |
| - |
| -void HashMapImpl::Clear() { |
| - // Mark all entries as empty. |
| - const Entry* end = map_end(); |
| - for (Entry* p = map_; p < end; p++) { |
| - p->key = NULL; |
| - } |
| - occupancy_ = 0; |
| -} |
| - |
| - |
| -HashMapImpl::Entry* HashMapImpl::Start() const { |
| - return Next(map_ - 1); |
| -} |
| - |
| - |
| -HashMapImpl::Entry* HashMapImpl::Next(Entry* p) const { |
| - const Entry* end = map_end(); |
| - DCHECK(map_ - 1 <= p && p < end); |
| - for (p++; p < end; p++) { |
| - if (p->key != NULL) { |
| - return p; |
| - } |
| - } |
| - return NULL; |
| -} |
| - |
| - |
| -HashMapImpl::Entry* HashMapImpl::Probe(void* key, uint32_t hash) const { |
| - DCHECK(key != NULL); |
| - |
| - DCHECK(base::bits::IsPowerOfTwo32(capacity_)); |
| - Entry* p = map_ + (hash & (capacity_ - 1)); |
| - const Entry* end = map_end(); |
| - DCHECK(map_ <= p && p < end); |
| - |
| - DCHECK(occupancy_ < capacity_); // Guarantees loop termination. |
| - while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { |
| - p++; |
| - if (p >= end) { |
| - p = map_; |
| - } |
| - } |
| - |
| - return p; |
| -} |
| - |
| - |
| -void HashMapImpl::Initialize(uint32_t capacity) { |
| - DCHECK(base::bits::IsPowerOfTwo32(capacity)); |
| - map_ = reinterpret_cast<Entry*>(Malloced::New(capacity * sizeof(Entry))); |
| - CHECK(map_ != NULL); |
| - capacity_ = capacity; |
| - Clear(); |
| -} |
| - |
| - |
| -void HashMapImpl::Resize() { |
| - Entry* map = map_; |
| - uint32_t n = occupancy_; |
| - |
| - // Allocate larger map. |
| - Initialize(capacity_ * 2); |
| +class HashMap : public HashMapImpl { |
| + public: |
| + // initial_capacity is the size of the initial hash map; |
| + // it must be a power of 2 (and thus must not be 0). |
| + explicit HashMap(MatchFun match, uint32_t capacity = kDefaultHashMapCapacity); |
| - // Rehash all current entries. |
| - for (Entry* p = map; n > 0; p++) { |
| - if (p->key != NULL) { |
| - Entry* entry = LookupOrInsert(p->key, p->hash); |
| - entry->value = p->value; |
| - entry->order = p->order; |
| - n--; |
| - } |
| - } |
| + ~HashMap() override; |
| - // Delete old map. |
| - Malloced::Delete(map); |
| -} |
| + private: |
| + void Initialize(uint32_t capacity); |
| + void Resize() override; |
| +}; |
| -} // namespace sampler |
| +} // namespace base |
| } // namespace v8 |
| -#endif // V8_LIBSAMPLER_HASHMAP_H_ |
| +#endif // V8_BASE_HASHMAP_H_ |