Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/libadt/dict.hpp
32285 views
/*1* Copyright (c) 1997, 2010, 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_VM_LIBADT_DICT_HPP25#define SHARE_VM_LIBADT_DICT_HPP2627#include "libadt/port.hpp"2829// Dictionaries - An Abstract Data Type30//INTERFACE31class ostream;32class Dict;3334// These dictionaries define a key-value mapping. They can be inserted to,35// searched or deleted from. They grow and shrink as needed. The key is a36// pointer to something (or anything which can be stored in a pointer). A37// key comparison routine determines if two keys are equal or not. A hash38// function can be provided; if it's not provided the key itself is used39// instead. A nice string hash function is included.40typedef int32 (*CmpKey)(const void *key1, const void *key2);41typedef int (*Hash)(const void *key);42typedef void (*FuncDict)(const void *key, const void *val, Dict *d);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 _cnt; // Number of key-value pairs in hash table50const Hash _hash; // Hashing function51const CmpKey _cmp; // Key comparison function52void doubhash( void ); // 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 );60~Dict();6162Dict( const Dict & ); // Deep-copy guts63Dict &operator =( const Dict & );6465// Zap to empty; ready for re-use66void Clear();6768// Return # of key-value pairs in dict69uint32 Size(void) const { return _cnt; }7071// Insert inserts the given key-value pair into the dictionary. The prior72// value of the key is returned; NULL if the key was not previously defined.73void *Insert(void *key, void *val, bool replace = true); // A new key-value74void *Delete(void *key); // Delete & return old7576// Find finds the value of a given key; or NULL if not found.77// The dictionary is NOT changed.78void *operator [](const void *key) const; // Do a lookup7980// == compares two dictionaries; they must have the same keys (their keys81// must match using CmpKey) and they must have the same values (pointer82// comparison). If so 1 is returned, if not 0 is returned.83int32 operator ==(const Dict &d) const; // Compare dictionaries for equal8485// Print out the dictionary contents as key-value pairs86void print();87};8889// Hashing functions90int hashstr(const void *s); // Nice string hash91// Slimey cheap hash function; no guaranteed performance. Better than the92// default for pointers, especially on MS-DOS machines.93int hashptr(const void *key);94// Slimey cheap hash function; no guaranteed performance.95int hashkey(const void *key);9697// Key comparators98int32 cmpstr(const void *k1, const void *k2);99// Slimey cheap key comparator.100int32 cmpkey(const void *key1, const void *key2);101102//------------------------------Iteration--------------------------------------103// The class of dictionary iterators. Fails in the presences of modifications104// to the dictionary during iteration (including searches).105// Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}106class DictI {107private:108const Dict *_d; // Dictionary being iterated over109uint _i; // Counter over the bins110uint _j; // Counter inside each bin111public:112const void *_key, *_value; // Easy access to the key-value pair113DictI( const Dict *d ) {reset(d);}; // Create a new iterator114void reset( const Dict *dict ); // Reset existing iterator115void operator ++(void); // Increment iterator116int test(void) { return _i<_d->_size;} // Test for end of iteration117};118119#endif // SHARE_VM_LIBADT_DICT_HPP120121122