| 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 #ifndef PLATFORM_HASHMAP_H_ | 5 #ifndef PLATFORM_HASHMAP_H_ |
| 6 #define PLATFORM_HASHMAP_H_ | 6 #define PLATFORM_HASHMAP_H_ |
| 7 | 7 |
| 8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 class HashMap { | 12 class HashMap { |
| 13 public: | 13 public: |
| 14 typedef bool (*MatchFun) (void* key1, void* key2); | 14 typedef bool (*MatchFun) (void* key1, void* key2); |
| 15 | 15 |
| 16 typedef void (*ClearFun) (void* value); | |
| 17 | |
| 18 static bool SamePointerValue(void* key1, void* key2) { | 16 static bool SamePointerValue(void* key1, void* key2) { |
| 19 return key1 == key2; | 17 return key1 == key2; |
| 20 } | 18 } |
| 21 | 19 |
| 22 static uint32_t StringHash(char* key) { | 20 static uint32_t StringHash(char* key) { |
| 23 uint32_t hash_ = 0; | 21 uint32_t hash_ = 0; |
| 24 if (key == NULL) return hash_; | 22 if (key == NULL) return hash_; |
| 25 int len = strlen(key); | 23 int len = strlen(key); |
| 26 for (int i = 0; i < len; i++) { | 24 for (int i = 0; i < len; i++) { |
| 27 hash_ += key[i]; | 25 hash_ += key[i]; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 43 // initial_capacity is the size of the initial hash map; | 41 // initial_capacity is the size of the initial hash map; |
| 44 // it must be a power of 2 (and thus must not be 0). | 42 // it must be a power of 2 (and thus must not be 0). |
| 45 HashMap(MatchFun match, uint32_t initial_capacity); | 43 HashMap(MatchFun match, uint32_t initial_capacity); |
| 46 | 44 |
| 47 ~HashMap(); | 45 ~HashMap(); |
| 48 | 46 |
| 49 // HashMap entries are (key, value, hash) triplets. | 47 // HashMap entries are (key, value, hash) triplets. |
| 50 // Some clients may not need to use the value slot | 48 // Some clients may not need to use the value slot |
| 51 // (e.g. implementers of sets, where the key is the value). | 49 // (e.g. implementers of sets, where the key is the value). |
| 52 struct Entry { | 50 struct Entry { |
| 53 Entry() : key(NULL), value(NULL), hash(0) {} | |
| 54 void* key; | 51 void* key; |
| 55 void* value; | 52 void* value; |
| 56 uint32_t hash; // The full hash value for key. | 53 uint32_t hash; // The full hash value for key. |
| 57 }; | 54 }; |
| 58 | 55 |
| 59 // If an entry with matching key is found, Lookup() | 56 // If an entry with matching key is found, Lookup() |
| 60 // returns that entry. If no matching entry is found, | 57 // returns that entry. If no matching entry is found, |
| 61 // but insert is set, a new entry is inserted with | 58 // but insert is set, a new entry is inserted with |
| 62 // corresponding key, key hash, and NULL value. | 59 // corresponding key, key hash, and NULL value. |
| 63 // Otherwise, NULL is returned. | 60 // Otherwise, NULL is returned. |
| 64 Entry* Lookup(void* key, uint32_t hash, bool insert); | 61 Entry* Lookup(void* key, uint32_t hash, bool insert); |
| 65 | 62 |
| 66 // Removes the entry with matching key. | 63 // Removes the entry with matching key. |
| 67 void Remove(void* key, uint32_t hash); | 64 void Remove(void* key, uint32_t hash); |
| 68 | 65 |
| 69 // Empties the hash map (occupancy() == 0), and calls the function 'clear' on | 66 // Empties the hash map (occupancy() == 0). |
| 70 // each of the values if given. | 67 void Clear(); |
| 71 void Clear(ClearFun clear = NULL); | |
| 72 | |
| 73 // The number of entries stored in the table. | |
| 74 intptr_t size() const { return occupancy_; } | |
| 75 | 68 |
| 76 // The capacity of the table. The implementation | 69 // The capacity of the table. The implementation |
| 77 // makes sure that occupancy is at most 80% of | 70 // makes sure that occupancy is at most 80% of |
| 78 // the table capacity. | 71 // the table capacity. |
| 79 intptr_t capacity() const { return capacity_; } | 72 intptr_t capacity() const { return capacity_; } |
| 80 | 73 |
| 81 // Iteration | 74 // Iteration |
| 82 // | 75 // |
| 83 // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) { | 76 // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) { |
| 84 // ... | 77 // ... |
| (...skipping 14 matching lines...) Expand all Loading... |
| 99 Entry* Probe(void* key, uint32_t hash); | 92 Entry* Probe(void* key, uint32_t hash); |
| 100 void Initialize(uint32_t capacity); | 93 void Initialize(uint32_t capacity); |
| 101 void Resize(); | 94 void Resize(); |
| 102 | 95 |
| 103 friend class IntSet; // From hashmap_test.cc | 96 friend class IntSet; // From hashmap_test.cc |
| 104 }; | 97 }; |
| 105 | 98 |
| 106 } // namespace dart | 99 } // namespace dart |
| 107 | 100 |
| 108 #endif // PLATFORM_HASHMAP_H_ | 101 #endif // PLATFORM_HASHMAP_H_ |
| OLD | NEW |