Index: src/hashmap.h |
diff --git a/src/hashmap.h b/src/hashmap.h |
index 91843b81b81244ba4cdec3bd3ef3fb391ee0da44..78e58d3eeb522943a7a201362dad7d174ca845e6 100644 |
--- a/src/hashmap.h |
+++ b/src/hashmap.h |
@@ -42,7 +42,8 @@ class TemplateHashMapImpl { |
// initial_capacity is the size of the initial hash map; |
// it must be a power of 2 (and thus must not be 0). |
- TemplateHashMapImpl(MatchFun match, uint32_t initial_capacity = 8); |
+ TemplateHashMapImpl(MatchFun match, uint32_t capacity = 8, |
danno
2012/06/04 10:46:46
Turn default value into a constant so that you can
sanjoy
2012/06/04 11:05:42
Done.
|
+ AllocationPolicy allocator = AllocationPolicy()); |
~TemplateHashMapImpl(); |
@@ -60,7 +61,8 @@ class TemplateHashMapImpl { |
// but insert is set, a new entry is inserted with |
// corresponding key, key hash, and NULL value. |
// Otherwise, NULL is returned. |
- Entry* Lookup(void* key, uint32_t hash, bool insert); |
+ Entry* Lookup(void* key, uint32_t hash, bool insert, |
+ AllocationPolicy allocator = AllocationPolicy()); |
// Removes the entry with matching key. |
// It returns the value of the deleted entry |
@@ -97,29 +99,30 @@ class TemplateHashMapImpl { |
Entry* map_end() const { return map_ + capacity_; } |
Entry* Probe(void* key, uint32_t hash); |
- void Initialize(uint32_t capacity); |
- void Resize(); |
+ void Initialize(uint32_t capacity, AllocationPolicy allocator); |
+ void Resize(AllocationPolicy allocator); |
}; |
typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap; |
-template<class P> |
-TemplateHashMapImpl<P>::TemplateHashMapImpl(MatchFun match, |
- uint32_t initial_capacity) { |
+template<class AllocationPolicy> |
+TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl( |
+ MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { |
match_ = match; |
- Initialize(initial_capacity); |
+ Initialize(initial_capacity, allocator); |
} |
-template<class P> |
-TemplateHashMapImpl<P>::~TemplateHashMapImpl() { |
- P::Delete(map_); |
+template<class AllocationPolicy> |
+TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() { |
+ AllocationPolicy::Delete(map_); |
} |
-template<class P> |
-typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup( |
- void* key, uint32_t hash, bool insert) { |
+template<class AllocationPolicy> |
+typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
+TemplateHashMapImpl<AllocationPolicy>::Lookup( |
+ void* key, uint32_t hash, bool insert, AllocationPolicy allocator) { |
// Find a matching entry. |
Entry* p = Probe(key, hash); |
if (p->key != NULL) { |
@@ -135,7 +138,7 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup( |
// Grow the map if we reached >= 80% occupancy. |
if (occupancy_ + occupancy_/4 >= capacity_) { |
- Resize(); |
+ Resize(allocator); |
p = Probe(key, hash); |
} |
@@ -147,8 +150,8 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup( |
} |
-template<class P> |
-void* TemplateHashMapImpl<P>::Remove(void* key, uint32_t hash) { |
+template<class AllocationPolicy> |
+void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) { |
// Lookup the entry for the key to remove. |
Entry* p = Probe(key, hash); |
if (p->key == NULL) { |
@@ -209,8 +212,8 @@ void* TemplateHashMapImpl<P>::Remove(void* key, uint32_t hash) { |
} |
-template<class P> |
-void TemplateHashMapImpl<P>::Clear() { |
+template<class AllocationPolicy> |
+void TemplateHashMapImpl<AllocationPolicy>::Clear() { |
// Mark all entries as empty. |
const Entry* end = map_end(); |
for (Entry* p = map_; p < end; p++) { |
@@ -220,15 +223,16 @@ void TemplateHashMapImpl<P>::Clear() { |
} |
-template<class P> |
-typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Start() const { |
+template<class AllocationPolicy> |
+typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
+ TemplateHashMapImpl<AllocationPolicy>::Start() const { |
return Next(map_ - 1); |
} |
-template<class P> |
-typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Next(Entry* p) |
- const { |
+template<class AllocationPolicy> |
+typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
+ TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const { |
const Entry* end = map_end(); |
ASSERT(map_ - 1 <= p && p < end); |
for (p++; p < end; p++) { |
@@ -240,9 +244,9 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Next(Entry* p) |
} |
-template<class P> |
-typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Probe(void* key, |
- uint32_t hash) { |
+template<class AllocationPolicy> |
+typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
+ TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) { |
ASSERT(key != NULL); |
ASSERT(IsPowerOf2(capacity_)); |
@@ -262,10 +266,11 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Probe(void* key, |
} |
-template<class P> |
-void TemplateHashMapImpl<P>::Initialize(uint32_t capacity) { |
+template<class AllocationPolicy> |
+void TemplateHashMapImpl<AllocationPolicy>::Initialize( |
+ uint32_t capacity, AllocationPolicy allocator) { |
ASSERT(IsPowerOf2(capacity)); |
- map_ = reinterpret_cast<Entry*>(P::New(capacity * sizeof(Entry))); |
+ map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry))); |
if (map_ == NULL) { |
v8::internal::FatalProcessOutOfMemory("HashMap::Initialize"); |
return; |
@@ -275,13 +280,13 @@ void TemplateHashMapImpl<P>::Initialize(uint32_t capacity) { |
} |
-template<class P> |
-void TemplateHashMapImpl<P>::Resize() { |
+template<class AllocationPolicy> |
+void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) { |
Entry* map = map_; |
uint32_t n = occupancy_; |
// Allocate larger map. |
- Initialize(capacity_ * 2); |
+ Initialize(capacity_ * 2, allocator); |
// Rehash all current entries. |
for (Entry* p = map; n > 0; p++) { |
@@ -292,7 +297,7 @@ void TemplateHashMapImpl<P>::Resize() { |
} |
// Delete old map. |
- P::Delete(map); |
+ AllocationPolicy::Delete(map); |
} |
@@ -329,8 +334,9 @@ class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> { |
}; |
TemplateHashMap( |
- typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match) |
- : TemplateHashMapImpl<AllocationPolicy>(match) { } |
+ typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match, |
+ AllocationPolicy allocator = AllocationPolicy()) |
+ : TemplateHashMapImpl<AllocationPolicy>(match, 8, allocator) { } |
danno
2012/06/04 10:46:46
As discussed above, use constant for this.
sanjoy
2012/06/04 11:05:42
Done.
|
Iterator begin() const { return Iterator(this, this->Start()); } |
Iterator end() const { return Iterator(this, NULL); } |