1 /* |
1 /* |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, 2015 Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
33 typedef bool (*equals_fn)(K const&, K const&); |
33 typedef bool (*equals_fn)(K const&, K const&); |
34 }; |
34 }; |
35 |
35 |
36 template<typename K> unsigned primitive_hash(const K& k) { |
36 template<typename K> unsigned primitive_hash(const K& k) { |
37 unsigned hash = (unsigned)((uintptr_t)k); |
37 unsigned hash = (unsigned)((uintptr_t)k); |
38 return hash ^ (hash > 3); // just in case we're dealing with aligned ptrs |
38 return hash ^ (hash >> 3); // just in case we're dealing with aligned ptrs |
39 } |
39 } |
40 |
40 |
41 template<typename K> bool primitive_equals(const K& k0, const K& k1) { |
41 template<typename K> bool primitive_equals(const K& k0, const K& k1) { |
42 return k0 == k1; |
42 return k0 == k1; |
43 } |
43 } |
48 // http://stackoverflow.com/questions/8532961/template-argument-of-type-that-is-defined-by-inner-typedef-from-other-template-c |
48 // http://stackoverflow.com/questions/8532961/template-argument-of-type-that-is-defined-by-inner-typedef-from-other-template-c |
49 //typename ResourceHashtableFns<K>::hash_fn HASH = primitive_hash<K>, |
49 //typename ResourceHashtableFns<K>::hash_fn HASH = primitive_hash<K>, |
50 //typename ResourceHashtableFns<K>::equals_fn EQUALS = primitive_equals<K>, |
50 //typename ResourceHashtableFns<K>::equals_fn EQUALS = primitive_equals<K>, |
51 unsigned (*HASH) (K const&) = primitive_hash<K>, |
51 unsigned (*HASH) (K const&) = primitive_hash<K>, |
52 bool (*EQUALS)(K const&, K const&) = primitive_equals<K>, |
52 bool (*EQUALS)(K const&, K const&) = primitive_equals<K>, |
53 unsigned SIZE = 256 |
53 unsigned SIZE = 256, |
|
54 ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA, |
|
55 MEMFLAGS MEM_TYPE = mtInternal |
54 > |
56 > |
55 class ResourceHashtable : public ResourceObj { |
57 class ResourceHashtable : public ResourceObj { |
56 private: |
58 private: |
57 |
59 |
58 class Node : public ResourceObj { |
60 class Node : public ResourceObj { |
89 } |
91 } |
90 |
92 |
91 public: |
93 public: |
92 ResourceHashtable() { memset(_table, 0, SIZE * sizeof(Node*)); } |
94 ResourceHashtable() { memset(_table, 0, SIZE * sizeof(Node*)); } |
93 |
95 |
|
96 ~ResourceHashtable() { |
|
97 if (ALLOC_TYPE == C_HEAP) { |
|
98 Node* const* bucket = _table; |
|
99 while (bucket < &_table[SIZE]) { |
|
100 Node* node = *bucket; |
|
101 while (node != NULL) { |
|
102 Node* cur = node; |
|
103 node = node->_next; |
|
104 delete cur; |
|
105 } |
|
106 ++bucket; |
|
107 } |
|
108 } |
|
109 } |
|
110 |
94 bool contains(K const& key) const { |
111 bool contains(K const& key) const { |
95 return get(key) != NULL; |
112 return get(key) != NULL; |
96 } |
113 } |
97 |
114 |
98 V* get(K const& key) const { |
115 V* get(K const& key) const { |
103 } else { |
120 } else { |
104 return NULL; |
121 return NULL; |
105 } |
122 } |
106 } |
123 } |
107 |
124 |
108 // Inserts or replaces a value in the table |
125 /** |
109 void put(K const& key, V const& value) { |
126 * Inserts or replaces a value in the table. |
|
127 * @return: true: if a new item is added |
|
128 * false: if the item already existed and the value is updated |
|
129 */ |
|
130 bool put(K const& key, V const& value) { |
110 unsigned hv = HASH(key); |
131 unsigned hv = HASH(key); |
111 Node** ptr = lookup_node(hv, key); |
132 Node** ptr = lookup_node(hv, key); |
112 if (*ptr != NULL) { |
133 if (*ptr != NULL) { |
113 (*ptr)->_value = value; |
134 (*ptr)->_value = value; |
|
135 return false; |
114 } else { |
136 } else { |
115 *ptr = new Node(hv, key, value); |
137 *ptr = new (ALLOC_TYPE, MEM_TYPE) Node(hv, key, value); |
|
138 return true; |
116 } |
139 } |
|
140 } |
|
141 |
|
142 bool remove(K const& key) { |
|
143 unsigned hv = HASH(key); |
|
144 Node** ptr = lookup_node(hv, key); |
|
145 |
|
146 Node* node = *ptr; |
|
147 if (node != NULL) { |
|
148 *ptr = node->_next; |
|
149 if (ALLOC_TYPE == C_HEAP) { |
|
150 delete node; |
|
151 } |
|
152 return true; |
|
153 } |
|
154 return false; |
117 } |
155 } |
118 |
156 |
119 // ITER contains bool do_entry(K const&, V const&), which will be |
157 // ITER contains bool do_entry(K const&, V const&), which will be |
120 // called for each entry in the table. If do_entry() returns false, |
158 // called for each entry in the table. If do_entry() returns false, |
121 // the iteration is cancelled. |
159 // the iteration is cancelled. |