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* candidate = Probe(key, hash); | |
53 if (candidate->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 // "candidate" is the candidate entry to clear. "next" is used to scan | |
75 // forwards. | |
76 Entry* next = candidate; // Start at the entry to remove. | |
77 while (true) { | |
78 // Move "next" to the next entry. Wrap when at the end of the array. | |
79 next = next + 1; | |
80 if (next == map_end()) { | |
81 next = map_; | |
82 } | |
83 | |
84 // All entries between "candidate" and "next" have their initial position | |
85 // between candidate and entry and the entry candidate can be cleared | |
86 // without breaking the search for these entries. | |
87 if (next->key == NULL) { | |
88 break; | |
89 } | |
90 | |
91 // Find the initial position for the entry at position "next". That is | |
92 // the entry where searching for the entry at position "next" will | |
93 // actually start. | |
94 Entry* start = map_ + (next->hash & (capacity_ - 1)); | |
95 | |
96 // If the entry at position "next" has its initial position outside the | |
97 // range between "candidate" and "next" it can be moved forward to | |
98 // position "candidate" and will still be found. There is now the new | |
99 // candidate entry for clearing. | |
100 if ((next > candidate && (start <= candidate || start > next)) || | |
101 (next < candidate && (start <= candidate && start > next))) { | |
102 *candidate = *next; | |
103 candidate = next; | |
104 } | |
105 } | |
106 | |
107 // Clear the candidate which will not break searching the hash table. | |
108 candidate->key = NULL; | |
109 occupancy_--; | |
110 } | |
111 | |
112 | |
113 void HashMap::Clear() { | |
114 // Mark all entries as empty. | |
115 const Entry* end = map_end(); | |
116 for (Entry* p = map_; p < end; p++) { | |
117 p->key = NULL; | |
118 } | |
119 occupancy_ = 0; | |
120 } | |
121 | |
122 | |
123 HashMap::Entry* HashMap::Start() const { | |
124 return Next(map_ - 1); | |
125 } | |
126 | |
127 | |
128 HashMap::Entry* HashMap::Next(Entry* p) const { | |
129 const Entry* end = map_end(); | |
130 ASSERT(map_ - 1 <= p && p < end); | |
131 for (p++; p < end; p++) { | |
132 if (p->key != NULL) { | |
133 return p; | |
134 } | |
135 } | |
136 return NULL; | |
137 } | |
138 | |
139 | |
140 HashMap::Entry* HashMap::Probe(void* key, uint32_t hash) { | |
141 ASSERT(key != NULL); | |
142 | |
143 ASSERT(dart::Utils::IsPowerOfTwo(capacity_)); | |
144 Entry* p = map_ + (hash & (capacity_ - 1)); | |
145 const Entry* end = map_end(); | |
146 ASSERT(map_ <= p && p < end); | |
147 | |
148 ASSERT(occupancy_ < capacity_); // Guarantees loop termination. | |
149 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { | |
150 p++; | |
151 if (p >= end) { | |
152 p = map_; | |
153 } | |
154 } | |
155 | |
156 return p; | |
157 } | |
158 | |
159 | |
160 void HashMap::Initialize(uint32_t capacity) { | |
161 ASSERT(dart::Utils::IsPowerOfTwo(capacity)); | |
162 map_ = reinterpret_cast<Entry*>(malloc(capacity * sizeof(Entry))); | |
163 if (map_ == NULL) { | |
164 // TODO(sgjesse): Handle out of memory. | |
165 FATAL("Cannot allocate memory for hashmap"); | |
166 return; | |
167 } | |
168 capacity_ = capacity; | |
169 Clear(); | |
170 } | |
171 | |
172 | |
173 void HashMap::Resize() { | |
174 Entry* map = map_; | |
175 uint32_t n = occupancy_; | |
176 | |
177 // Allocate larger map. | |
178 Initialize(capacity_ * 2); | |
179 | |
180 // Rehash all current entries. | |
181 for (Entry* p = map; n > 0; p++) { | |
182 if (p->key != NULL) { | |
183 Lookup(p->key, p->hash, true)->value = p->value; | |
184 n--; | |
185 } | |
186 } | |
187 | |
188 // Delete old map. | |
189 free(map); | |
190 } | |
OLD | NEW |