Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/adlc/dict2.hpp
32285 views
/*1* Copyright (c) 1998, 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_ADLC_DICT2_HPP25#define SHARE_VM_ADLC_DICT2_HPP2627// Dictionaries - An Abstract Data Type282930class Dict;3132// These dictionaries define a key-value mapping. They can be inserted to,33// searched or deleted from. They grow and shrink as needed. The key is a34// pointer to something (or anything which can be stored in a pointer). A35// key comparison routine determines if two keys are equal or not. A hash36// function can be provided; if it's not provided the key itself is used37// instead. A nice string hash function is included.38typedef int (*CmpKey)(const void *key1, const void *key2);39typedef int (*Hash)(const void *key);40typedef void (*PrintKeyOrValue)(const void *key_or_value);41typedef void (*FuncDict)(const void *key, const void *val, Dict *d);4243class Dict { // Dictionary structure44private:45class Arena *_arena; // Where to draw storage from46class bucket *_bin; // Hash table is array of buckets47int _size; // Size (# of slots) in hash table48int _cnt; // Number of key-value pairs in hash table49const Hash _hash; // Hashing function50const CmpKey _cmp; // Key comparison function51void doubhash( void ); // Double hash table size5253public:54friend class DictI; // Friendly iterator function5556// cmp is a key comparision routine. hash is a routine to hash a key.57Dict( CmpKey cmp, Hash hash );58Dict( CmpKey cmp, Hash hash, Arena *arena );59void init();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 dict69int 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.73const void *Insert(const void *key, const void *val); // A new key-value74const void *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.78const void *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.83int operator ==(const Dict &d) const; // Compare dictionaries for equal8485// Print out the dictionary contents as key-value pairs86void print();87void print(PrintKeyOrValue print_key, PrintKeyOrValue print_value);88};8990// Hashing functions91int hashstr(const void *s); // Nice string hash92// Slimey cheap hash function; no guaranteed performance. Better than the93// default for pointers, especially on MS-DOS machines.94int hashptr(const void *key);95// Slimey cheap hash function; no guaranteed performance.96int hashkey(const void *key);9798// Key comparators99int cmpstr(const void *k1, const void *k2);100// Slimey cheap key comparator.101int cmpkey(const void *key1, const void *key2);102103//------------------------------Iteration--------------------------------------104// The class of dictionary iterators. Fails in the presences of modifications105// to the dictionary during iteration (including searches).106// Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}107class DictI {108private:109const Dict *_d; // Dictionary being iterated over110int _i; // Counter over the bins111int _j; // Counter inside each bin112public:113const void *_key, *_value; // Easy access to the key-value pair114DictI( const Dict *d ) {reset(d);}; // Create a new iterator115void reset( const Dict *dict ); // Reset existing iterator116void operator ++(void); // Increment iterator117int test(void) { return _i<_d->_size;} // Test for end of iteration118};119120#endif // SHARE_VM_ADLC_DICT2_HPP121122123