1 /*
2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
284 KVHashtableEntry* next() {
285 return (KVHashtableEntry*)BasicHashtableEntry<F>::next();
286 }
287 };
288
289 protected:
290 KVHashtableEntry* bucket(int i) const {
291 return (KVHashtableEntry*)BasicHashtable<F>::bucket(i);
292 }
293
294 KVHashtableEntry* new_entry(unsigned int hashValue, K key, V value) {
295 KVHashtableEntry* entry = (KVHashtableEntry*)BasicHashtable<F>::new_entry(hashValue);
296 entry->_key = key;
297 entry->_value = value;
298 return entry;
299 }
300
301 public:
302 KVHashtable(int table_size) : BasicHashtable<F>(table_size, sizeof(KVHashtableEntry)) {}
303
304 void add(K key, V value) {
305 unsigned int hash = HASH(key);
306 KVHashtableEntry* entry = new_entry(hash, key, value);
307 BasicHashtable<F>::add_entry(BasicHashtable<F>::hash_to_index(hash), entry);
308 }
309
310 V* lookup(K key) {
311 unsigned int hash = HASH(key);
312 int index = BasicHashtable<F>::hash_to_index(hash);
313 for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
314 if (e->hash() == hash && e->_key == key) {
315 return &(e->_value);
316 }
317 }
318 return NULL;
319 }
320 };
321
322
323 #endif // SHARE_UTILITIES_HASHTABLE_HPP
|
1 /*
2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
284 KVHashtableEntry* next() {
285 return (KVHashtableEntry*)BasicHashtableEntry<F>::next();
286 }
287 };
288
289 protected:
290 KVHashtableEntry* bucket(int i) const {
291 return (KVHashtableEntry*)BasicHashtable<F>::bucket(i);
292 }
293
294 KVHashtableEntry* new_entry(unsigned int hashValue, K key, V value) {
295 KVHashtableEntry* entry = (KVHashtableEntry*)BasicHashtable<F>::new_entry(hashValue);
296 entry->_key = key;
297 entry->_value = value;
298 return entry;
299 }
300
301 public:
302 KVHashtable(int table_size) : BasicHashtable<F>(table_size, sizeof(KVHashtableEntry)) {}
303
304 V* add(K key, V value) {
305 unsigned int hash = HASH(key);
306 KVHashtableEntry* entry = new_entry(hash, key, value);
307 BasicHashtable<F>::add_entry(BasicHashtable<F>::hash_to_index(hash), entry);
308 return &(entry->_value);
309 }
310
311 V* lookup(K key) const {
312 unsigned int hash = HASH(key);
313 int index = BasicHashtable<F>::hash_to_index(hash);
314 for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
315 if (e->hash() == hash && e->_key == key) {
316 return &(e->_value);
317 }
318 }
319 return NULL;
320 }
321
322 int table_size() const {
323 return BasicHashtable<F>::table_size();
324 }
325
326 // ITER contains bool do_entry(K, V const&), which will be
327 // called for each entry in the table. If do_entry() returns false,
328 // the iteration is cancelled.
329 template<class ITER>
330 void iterate(ITER* iter) const {
331 for (int index = 0; index < table_size(); index++) {
332 for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
333 bool cont = iter->do_entry(e->_key, &e->_value);
334 if (!cont) { return; }
335 }
336 }
337 }
338 };
339
340
341 #endif // SHARE_UTILITIES_HASHTABLE_HPP
|