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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 159 } |
160 | 160 |
161 return p; | 161 return p; |
162 } | 162 } |
163 | 163 |
164 | 164 |
165 void HashMap::Initialize(uint32_t capacity) { | 165 void HashMap::Initialize(uint32_t capacity) { |
166 ASSERT(dart::Utils::IsPowerOfTwo(capacity)); | 166 ASSERT(dart::Utils::IsPowerOfTwo(capacity)); |
167 map_ = new Entry[capacity]; | 167 map_ = new Entry[capacity]; |
168 if (map_ == NULL) { | 168 if (map_ == NULL) { |
169 // TODO(sgjesse): Handle out of memory. | 169 OUT_OF_MEMORY(); |
170 FATAL("Cannot allocate memory for hashmap"); | |
171 return; | |
172 } | 170 } |
173 capacity_ = capacity; | 171 capacity_ = capacity; |
174 occupancy_ = 0; | 172 occupancy_ = 0; |
175 } | 173 } |
176 | 174 |
177 | 175 |
178 void HashMap::Resize() { | 176 void HashMap::Resize() { |
179 Entry* map = map_; | 177 Entry* map = map_; |
180 uint32_t n = occupancy_; | 178 uint32_t n = occupancy_; |
181 | 179 |
182 // Allocate larger map. | 180 // Allocate larger map. |
183 Initialize(capacity_ * 2); | 181 Initialize(capacity_ * 2); |
184 | 182 |
185 // Rehash all current entries. | 183 // Rehash all current entries. |
186 for (Entry* p = map; n > 0; p++) { | 184 for (Entry* p = map; n > 0; p++) { |
187 if (p->key != NULL) { | 185 if (p->key != NULL) { |
188 Lookup(p->key, p->hash, true)->value = p->value; | 186 Lookup(p->key, p->hash, true)->value = p->value; |
189 n--; | 187 n--; |
190 } | 188 } |
191 } | 189 } |
192 | 190 |
193 // Delete old map. | 191 // Delete old map. |
194 delete[] map; | 192 delete[] map; |
195 } | 193 } |
196 | 194 |
197 } // namespace dart | 195 } // namespace dart |
OLD | NEW |