Chromium Code Reviews| 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 #ifndef BIN_HASHMAP_H_ | |
| 6 #define BIN_HASHMAP_H_ | |
| 7 | |
| 8 #include "platform/globals.h" | |
| 9 | |
| 10 class HashMap { | |
| 11 public: | |
| 12 typedef bool (*MatchFun) (void* key1, void* key2); | |
| 13 | |
| 14 static bool SamePointerValue(void* key1, void* key2) { | |
| 15 return key1 == key2; | |
| 16 } | |
| 17 | |
| 18 // initial_capacity is the size of the initial hash map; | |
| 19 // it must be a power of 2 (and thus must not be 0). | |
| 20 explicit HashMap(MatchFun match = &SamePointerValue, | |
|
Ivan Posva
2012/01/14 01:19:02
Is explicit needed here?
Søren Gjesse
2012/01/16 14:14:58
No - forgot to remove it when I added a default va
| |
| 21 uint32_t initial_capacity = 8); | |
| 22 | |
| 23 ~HashMap(); | |
| 24 | |
| 25 // HashMap entries are (key, value, hash) triplets. | |
| 26 // Some clients may not need to use the value slot | |
| 27 // (e.g. implementers of sets, where the key is the value). | |
| 28 struct Entry { | |
| 29 void* key; | |
| 30 void* value; | |
| 31 uint32_t hash; // The full hash value for key. | |
| 32 }; | |
| 33 | |
| 34 // If an entry with matching key is found, Lookup() | |
| 35 // returns that entry. If no matching entry is found, | |
| 36 // but insert is set, a new entry is inserted with | |
| 37 // corresponding key, key hash, and NULL value. | |
| 38 // Otherwise, NULL is returned. | |
| 39 Entry* Lookup(void* key, uint32_t hash, bool insert); | |
| 40 | |
| 41 // Removes the entry with matching key. | |
| 42 void Remove(void* key, uint32_t hash); | |
| 43 | |
| 44 // Empties the hash map (occupancy() == 0). | |
| 45 void Clear(); | |
| 46 | |
| 47 // The number of (non-empty) entries in the table. | |
| 48 uint32_t occupancy() const { return occupancy_; } | |
| 49 | |
| 50 // The capacity of the table. The implementation | |
| 51 // makes sure that occupancy is at most 80% of | |
| 52 // the table capacity. | |
| 53 uint32_t capacity() const { return capacity_; } | |
| 54 | |
| 55 // Iteration | |
| 56 // | |
| 57 // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) { | |
| 58 // ... | |
| 59 // } | |
| 60 // | |
| 61 // If entries are inserted during iteration, the effect of | |
| 62 // calling Next() is undefined. | |
| 63 Entry* Start() const; | |
| 64 Entry* Next(Entry* p) const; | |
| 65 | |
| 66 private: | |
| 67 MatchFun match_; | |
| 68 Entry* map_; | |
| 69 uint32_t capacity_; | |
| 70 uint32_t occupancy_; | |
| 71 | |
| 72 Entry* map_end() const { return map_ + capacity_; } | |
| 73 Entry* Probe(void* key, uint32_t hash); | |
| 74 void Initialize(uint32_t capacity); | |
| 75 void Resize(); | |
| 76 }; | |
| 77 | |
| 78 #endif // BIN_HASHMAP_H_ | |
| OLD | NEW |