0N/A/*
1879N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_LIBADT_DICT_HPP
1879N/A#define SHARE_VM_LIBADT_DICT_HPP
1879N/A
1879N/A#include "libadt/port.hpp"
1879N/A
0N/A// Dictionaries - An Abstract Data Type
0N/A//INTERFACE
0N/Aclass ostream;
0N/Aclass Dict;
0N/A
0N/A// These dictionaries define a key-value mapping. They can be inserted to,
0N/A// searched or deleted from. They grow and shrink as needed. The key is a
0N/A// pointer to something (or anything which can be stored in a pointer). A
0N/A// key comparison routine determines if two keys are equal or not. A hash
0N/A// function can be provided; if it's not provided the key itself is used
0N/A// instead. A nice string hash function is included.
0N/Atypedef int32 (*CmpKey)(const void *key1, const void *key2);
0N/Atypedef int (*Hash)(const void *key);
0N/Atypedef void (*FuncDict)(const void *key, const void *val, Dict *d);
0N/A
0N/Aclass Dict : public ResourceObj { // Dictionary structure
0N/A private:
0N/A class Arena *_arena; // Where to draw storage from
0N/A class bucket *_bin; // Hash table is array of buckets
0N/A uint _size; // Size (# of slots) in hash table
0N/A uint32 _cnt; // Number of key-value pairs in hash table
0N/A const Hash _hash; // Hashing function
0N/A const CmpKey _cmp; // Key comparison function
0N/A void doubhash( void ); // Double hash table size
0N/A
0N/A public:
0N/A friend class DictI; // Friendly iterator function
0N/A
0N/A // cmp is a key comparision routine. hash is a routine to hash a key.
0N/A Dict( CmpKey cmp, Hash hash );
0N/A Dict( CmpKey cmp, Hash hash, Arena *arena, int size=16 );
0N/A ~Dict();
0N/A
0N/A Dict( const Dict & ); // Deep-copy guts
0N/A Dict &operator =( const Dict & );
0N/A
0N/A // Zap to empty; ready for re-use
0N/A void Clear();
0N/A
0N/A // Return # of key-value pairs in dict
0N/A uint32 Size(void) const { return _cnt; }
0N/A
0N/A // Insert inserts the given key-value pair into the dictionary. The prior
0N/A // value of the key is returned; NULL if the key was not previously defined.
0N/A void *Insert(void *key, void *val, bool replace = true); // A new key-value
0N/A void *Delete(void *key); // Delete & return old
0N/A
0N/A // Find finds the value of a given key; or NULL if not found.
0N/A // The dictionary is NOT changed.
0N/A void *operator [](const void *key) const; // Do a lookup
0N/A
0N/A // == compares two dictionaries; they must have the same keys (their keys
0N/A // must match using CmpKey) and they must have the same values (pointer
0N/A // comparison). If so 1 is returned, if not 0 is returned.
0N/A int32 operator ==(const Dict &d) const; // Compare dictionaries for equal
0N/A
0N/A // Print out the dictionary contents as key-value pairs
0N/A void print();
0N/A};
0N/A
0N/A// Hashing functions
0N/Aint hashstr(const void *s); // Nice string hash
605N/A// Slimey cheap hash function; no guaranteed performance. Better than the
0N/A// default for pointers, especially on MS-DOS machines.
0N/Aint hashptr(const void *key);
605N/A// Slimey cheap hash function; no guaranteed performance.
0N/Aint hashkey(const void *key);
0N/A
0N/A// Key comparators
0N/Aint32 cmpstr(const void *k1, const void *k2);
0N/A// Slimey cheap key comparator.
0N/Aint32 cmpkey(const void *key1, const void *key2);
0N/A
0N/A//------------------------------Iteration--------------------------------------
0N/A// The class of dictionary iterators. Fails in the presences of modifications
0N/A// to the dictionary during iteration (including searches).
0N/A// Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}
0N/Aclass DictI {
0N/A private:
0N/A const Dict *_d; // Dictionary being iterated over
0N/A uint _i; // Counter over the bins
0N/A uint _j; // Counter inside each bin
0N/A public:
0N/A const void *_key, *_value; // Easy access to the key-value pair
0N/A DictI( const Dict *d ) {reset(d);}; // Create a new iterator
0N/A void reset( const Dict *dict ); // Reset existing iterator
0N/A void operator ++(void); // Increment iterator
0N/A int test(void) { return _i<_d->_size;} // Test for end of iteration
0N/A};
0N/A
1879N/A#endif // SHARE_VM_LIBADT_DICT_HPP