OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "bin/hashmap.h" | |
6 | |
7 #include "bin/builtin.h" | |
8 #include "platform/utils.h" | |
9 | |
10 HashMap::HashMap(MatchFun match, | |
11 uint32_t initial_capacity) { | |
12 match_ = match; | |
13 Initialize(initial_capacity); | |
14 } | |
15 | |
16 | |
17 HashMap::~HashMap() { | |
18 free(map_); | |
19 } | |
20 | |
21 | |
22 HashMap::Entry* HashMap::Lookup(void* key, uint32_t hash, bool insert) { | |
23 // Find a matching entry. | |
24 Entry* p = Probe(key, hash); | |
25 if (p->key != NULL) { | |
26 return p; | |
27 } | |
28 | |
29 // No entry found; insert one if necessary. | |
30 if (insert) { | |
31 p->key = key; | |
32 p->value = NULL; | |
33 p->hash = hash; | |
34 occupancy_++; | |
35 | |
36 // Grow the map if we reached >= 80% occupancy. | |
37 if (occupancy_ + occupancy_ / 4 >= capacity_) { | |
38 Resize(); | |
39 p = Probe(key, hash); | |
40 } | |
41 | |
42 return p; | |
43 } | |
44 | |
45 // No entry found and none inserted. | |
46 return NULL; | |
47 } | |
48 | |
49 | |
50 void HashMap::Remove(void* key, uint32_t hash) { | |
51 // Lookup the entry for the key to remove. | |
52 Entry* p = Probe(key, hash); | |
53 if (p->key == NULL) { | |
54 // Key not found nothing to remove. | |
55 return; | |
56 } | |
57 | |
58 // To remove an entry we need to ensure that it does not create an empty | |
59 // entry that will cause the search for another entry to stop too soon. If all | |
60 // the entries between the entry to remove and the next empty slot have their | |
61 // initial position inside this interval, clearing the entry to remove will | |
62 // not break the search. If, while searching for the next empty entry, an | |
63 // entry is encountered which does not have its initial position between the | |
64 // entry to remove and the position looked at, then this entry can be moved to | |
65 // the place of the entry to remove without breaking the search for it. The | |
66 // entry made vacant by this move is now the entry to remove and the process | |
67 // starts over. | |
68 // Algorithm from http://en.wikipedia.org/wiki/Open_addressing. | |
69 | |
70 // This guarantees loop termination as there is at least one empty entry so | |
71 // eventually the removed entry will have an empty entry after it. | |
72 ASSERT(occupancy_ < capacity_); | |
73 | |
74 // p is the candidate entry to clear. q is used to scan forwards. | |
75 Entry* q = p; // Start at the entry to remove. | |
Ivan Posva
2012/01/17 07:57:41
Personally I find it really hard to follow these a
Søren Gjesse
2012/01/17 09:28:04
Changed names p, q and r to candidate, next and st
| |
76 while (true) { | |
77 // Move q to the next entry. | |
78 q = q + 1; | |
79 if (q == map_end()) { | |
80 q = map_; | |
81 } | |
82 | |
83 // All entries between p and q have their initial position between p and q | |
84 // and the entry p can be cleared without breaking the search for these | |
85 // entries. | |
86 if (q->key == NULL) { | |
87 break; | |
88 } | |
89 | |
90 // Find the initial position for the entry at position q. | |
91 Entry* r = map_ + (q->hash & (capacity_ - 1)); | |
92 | |
93 // If the entry at position q has its initial position outside the range | |
94 // between p and q it can be moved forward to position p and will still be | |
95 // found. There is now a new candidate entry for clearing. | |
96 if ((q > p && (r <= p || r > q)) || | |
97 (q < p && (r <= p && r > q))) { | |
98 *p = *q; | |
99 p = q; | |
100 } | |
101 } | |
102 | |
103 // Clear the entry which is allowed to en emptied. | |
Ivan Posva
2012/01/17 07:57:41
This comment does not parse.
Søren Gjesse
2012/01/17 09:28:04
Rephrased.
| |
104 p->key = NULL; | |
105 occupancy_--; | |
106 } | |
107 | |
108 | |
109 void HashMap::Clear() { | |
110 // Mark all entries as empty. | |
111 const Entry* end = map_end(); | |
112 for (Entry* p = map_; p < end; p++) { | |
113 p->key = NULL; | |
114 } | |
115 occupancy_ = 0; | |
116 } | |
117 | |
118 | |
119 HashMap::Entry* HashMap::Start() const { | |
120 return Next(map_ - 1); | |
121 } | |
122 | |
123 | |
124 HashMap::Entry* HashMap::Next(Entry* p) const { | |
125 const Entry* end = map_end(); | |
126 ASSERT(map_ - 1 <= p && p < end); | |
127 for (p++; p < end; p++) { | |
128 if (p->key != NULL) { | |
129 return p; | |
130 } | |
131 } | |
132 return NULL; | |
133 } | |
134 | |
135 | |
136 HashMap::Entry* HashMap::Probe(void* key, uint32_t hash) { | |
137 ASSERT(key != NULL); | |
138 | |
139 ASSERT(dart::Utils::IsPowerOfTwo(capacity_)); | |
140 Entry* p = map_ + (hash & (capacity_ - 1)); | |
141 const Entry* end = map_end(); | |
142 ASSERT(map_ <= p && p < end); | |
143 | |
144 ASSERT(occupancy_ < capacity_); // Guarantees loop termination. | |
145 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { | |
146 p++; | |
147 if (p >= end) { | |
148 p = map_; | |
149 } | |
150 } | |
151 | |
152 return p; | |
153 } | |
154 | |
155 | |
156 void HashMap::Initialize(uint32_t capacity) { | |
157 ASSERT(dart::Utils::IsPowerOfTwo(capacity)); | |
158 map_ = reinterpret_cast<Entry*>(malloc(capacity * sizeof(Entry))); | |
159 if (map_ == NULL) { | |
160 // TODO(sgjesse): Handle out of memory. | |
161 FATAL("Cannot allocate memory for hashmap"); | |
162 return; | |
163 } | |
164 capacity_ = capacity; | |
165 Clear(); | |
166 } | |
167 | |
168 | |
169 void HashMap::Resize() { | |
170 Entry* map = map_; | |
171 uint32_t n = occupancy_; | |
172 | |
173 // Allocate larger map. | |
174 Initialize(capacity_ * 2); | |
175 | |
176 // Rehash all current entries. | |
177 for (Entry* p = map; n > 0; p++) { | |
178 if (p->key != NULL) { | |
179 Lookup(p->key, p->hash, true)->value = p->value; | |
180 n--; | |
181 } | |
182 } | |
183 | |
184 // Delete old map. | |
185 free(map); | |
186 } | |
OLD | NEW |