Path: blob/master/src/hotspot/share/libadt/dict.hpp
40951 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_LIBADT_DICT_HPP25#define SHARE_LIBADT_DICT_HPP2627// Dictionaries - An Abstract Data Type2829#include "memory/allocation.hpp"30#include "memory/resourceArea.hpp"31#include "runtime/thread.hpp"3233class Dict;3435// These dictionaries define a key-value mapping. They can be inserted to,36// searched or deleted from. They grow and shrink as needed. The key is a37// pointer to something (or anything which can be stored in a pointer). A38// key comparison routine determines if two keys are equal or not. A hash39// function can be provided; if it's not provided the key itself is used40// instead. A nice string hash function is included.41typedef int32_t (*CmpKey)(const void* key1, const void* key2);42typedef int (*Hash)(const void* key);4344class Dict : public ResourceObj { // Dictionary structure45private:46class Arena* _arena; // Where to draw storage from47class bucket* _bin; // Hash table is array of buckets48uint _size; // Size (# of slots) in hash table49uint32_t _cnt; // Number of key-value pairs in hash table50const Hash _hash; // Hashing function51const CmpKey _cmp; // Key comparison function52void doubhash(); // Double hash table size5354public:55friend class DictI; // Friendly iterator function5657// cmp is a key comparision routine. hash is a routine to hash a key.58Dict(CmpKey cmp, Hash hash);59Dict(CmpKey cmp, Hash hash, Arena* arena, int size = 16);60Dict(const Dict &base, Arena* arena); // Deep-copy61~Dict();6263// Return # of key-value pairs in dict64uint32_t Size(void) const { return _cnt; }6566// Insert inserts the given key-value pair into the dictionary. The prior67// value of the key is returned; NULL if the key was not previously defined.68void* Insert(void* key, void* val, bool replace = true); // A new key-value69void* Delete(void* key); // Delete & return old7071// Find finds the value of a given key; or NULL if not found.72// The dictionary is NOT changed.73void* operator [](const void* key) const; // Do a lookup7475// Print out the dictionary contents as key-value pairs76void print();77};7879// Hashing functions80int hashstr(const void* s); // Nice string hash81// Slimey cheap hash function; no guaranteed performance. Better than the82// default for pointers, especially on MS-DOS machines.83int hashptr(const void* key);84// Slimey cheap hash function; no guaranteed performance.85int hashkey(const void* key);8687// Key comparators88int32_t cmpstr(const void* k1, const void* k2);89// Slimey cheap key comparator.90int32_t cmpkey(const void* key1, const void* key2);9192//------------------------------Iteration--------------------------------------93// The class of dictionary iterators. Fails in the presences of modifications94// to the dictionary during iteration (including searches).95// Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}96class DictI {97private:98const Dict* _d; // Dictionary being iterated over99uint _i; // Counter over the bins100uint _j; // Counter inside each bin101public:102const void* _key;103const void* _value;104DictI(const Dict* d) { reset(d); }; // Create a new iterator105void reset(const Dict* dict); // Reset existing iterator106void operator ++(void); // Increment iterator107int test(void) { return _i < _d->_size; } // Test for end of iteration108};109110#endif // SHARE_LIBADT_DICT_HPP111112113