OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/hashmap.h" | 5 #include "platform/hashmap.h" |
6 | 6 |
7 #include "platform/utils.h" | 7 #include "platform/utils.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 if ((next > candidate && (start <= candidate || start > next)) || | 100 if ((next > candidate && (start <= candidate || start > next)) || |
101 (next < candidate && (start <= candidate && start > next))) { | 101 (next < candidate && (start <= candidate && start > next))) { |
102 *candidate = *next; | 102 *candidate = *next; |
103 candidate = next; | 103 candidate = next; |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 // Clear the candidate which will not break searching the hash table. | 107 // Clear the candidate which will not break searching the hash table. |
108 candidate->key = NULL; | 108 candidate->key = NULL; |
109 candidate->value = NULL; | 109 candidate->value = NULL; |
110 candidate->hash = 0; | |
Florian Schneider
2016/11/30 18:08:41
Why clear the hash code?
kustermann
2016/11/30 21:27:15
For consistency:
The [key] *needs* to be cleared
Florian Schneider
2016/11/30 21:51:48
Agree, on the other hand, it may be useful to exam
| |
110 occupancy_--; | 111 occupancy_--; |
111 } | 112 } |
112 | 113 |
113 | 114 |
115 HashMap::Entry* HashMap::Remove(Entry* entry) { | |
116 // Save the key of the entry we would like to remove, since the removal itself | |
117 // might do a left-rotation of elements following this one, thereby overriding | |
118 // `entry`. | |
119 void* key = entry->key; | |
Florian Schneider
2016/11/30 18:08:41
Where is the saved key used after calling Remove?
kustermann
2016/11/30 21:27:15
Sorry that was a left-over. The indication whether
| |
120 Remove(key, entry->hash); | |
121 | |
122 // A key can only exist once in the map and we just removed `key`. This means | |
123 // that either a left-rotation has happened (in which case `entry` points | |
124 // already to the next element (in terms of iteration order)) or alternatively | |
125 // we can use the normal `Next()` call to move in iteration order. | |
126 if (entry->key != NULL) { | |
127 // A left-rotation happened. `entry` points already to the next element in | |
128 // iteration order. | |
129 return entry; | |
130 } else { | |
131 return Next(entry); | |
132 } | |
133 } | |
134 | |
135 | |
114 void HashMap::Clear(ClearFun clear) { | 136 void HashMap::Clear(ClearFun clear) { |
115 // Mark all entries as empty. | 137 // Mark all entries as empty. |
116 const Entry* end = map_end(); | 138 const Entry* end = map_end(); |
117 for (Entry* p = map_; p < end; p++) { | 139 for (Entry* p = map_; p < end; p++) { |
118 if ((clear != NULL) && (p->key != NULL)) { | 140 if ((clear != NULL) && (p->key != NULL)) { |
119 clear(p->value); | 141 clear(p->value); |
120 } | 142 } |
121 p->value = NULL; | 143 p->value = NULL; |
122 p->key = NULL; | 144 p->key = NULL; |
145 p->hash = 0; | |
Florian Schneider
2016/11/30 18:08:41
Same here. Seems wasted work.
kustermann
2016/11/30 21:27:15
Same answer.
| |
123 } | 146 } |
124 occupancy_ = 0; | 147 occupancy_ = 0; |
125 } | 148 } |
126 | 149 |
127 | 150 |
128 HashMap::Entry* HashMap::Start() const { | 151 HashMap::Entry* HashMap::Start() const { |
129 return Next(map_ - 1); | 152 return Next(map_ - 1); |
130 } | 153 } |
131 | 154 |
132 | 155 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 Lookup(p->key, p->hash, true)->value = p->value; | 209 Lookup(p->key, p->hash, true)->value = p->value; |
187 n--; | 210 n--; |
188 } | 211 } |
189 } | 212 } |
190 | 213 |
191 // Delete old map. | 214 // Delete old map. |
192 delete[] map; | 215 delete[] map; |
193 } | 216 } |
194 | 217 |
195 } // namespace dart | 218 } // namespace dart |
OLD | NEW |