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