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 static bool SamePointerValue(void* key1, void* key2) { | 16 static bool SamePointerValue(void* key1, void* key2) { |
17 return key1 == key2; | 17 return key1 == key2; |
18 } | 18 } |
19 | 19 |
| 20 static uint32_t StringHash(char* key) { |
| 21 uint32_t hash_ = 0; |
| 22 if (key == NULL) return hash_; |
| 23 int len = strlen(key); |
| 24 for (int i = 0; i < len; i++) { |
| 25 hash_ += key[i]; |
| 26 hash_ += hash_ << 10; |
| 27 hash_ ^= hash_ >> 6; |
| 28 } |
| 29 hash_ += hash_ << 3; |
| 30 hash_ ^= hash_ >> 11; |
| 31 hash_ += hash_ << 15; |
| 32 return hash_ == 0 ? 1 : hash_; |
| 33 } |
| 34 |
| 35 static bool SameStringValue(void* key1, void* key2) { |
| 36 return strcmp(reinterpret_cast<char*>(key1), |
| 37 reinterpret_cast<char*>(key2)) == 0; |
| 38 } |
| 39 |
| 40 |
20 // initial_capacity is the size of the initial hash map; | 41 // initial_capacity is the size of the initial hash map; |
21 // 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). |
22 HashMap(MatchFun match, uint32_t initial_capacity); | 43 HashMap(MatchFun match, uint32_t initial_capacity); |
23 | 44 |
24 ~HashMap(); | 45 ~HashMap(); |
25 | 46 |
26 // HashMap entries are (key, value, hash) triplets. | 47 // HashMap entries are (key, value, hash) triplets. |
27 // Some clients may not need to use the value slot | 48 // Some clients may not need to use the value slot |
28 // (e.g. implementers of sets, where the key is the value). | 49 // (e.g. implementers of sets, where the key is the value). |
29 struct Entry { | 50 struct Entry { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 Entry* Probe(void* key, uint32_t hash); | 92 Entry* Probe(void* key, uint32_t hash); |
72 void Initialize(uint32_t capacity); | 93 void Initialize(uint32_t capacity); |
73 void Resize(); | 94 void Resize(); |
74 | 95 |
75 friend class IntSet; // From hashmap_test.cc | 96 friend class IntSet; // From hashmap_test.cc |
76 }; | 97 }; |
77 | 98 |
78 } // namespace dart | 99 } // namespace dart |
79 | 100 |
80 #endif // PLATFORM_HASHMAP_H_ | 101 #endif // PLATFORM_HASHMAP_H_ |
OLD | NEW |