dict.hpp revision 0
1N/A * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved. 1N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1N/A * This code is free software; you can redistribute it and/or modify it 1N/A * under the terms of the GNU General Public License version 2 only, as 1N/A * published by the Free Software Foundation. 1N/A * This code is distributed in the hope that it will be useful, but WITHOUT 1N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or // Dictionaries - An Abstract Data Type // These dictionaries define a key-value mapping. They can be inserted to, // searched or deleted from. They grow and shrink as needed. The key is a // pointer to something (or anything which can be stored in a pointer). A // key comparison routine determines if two keys are equal or not. A hash // function can be provided; if it's not provided the key itself is used // instead. A nice string hash function is included. typedef int (*
Hash)(
const void *
key);
class bucket *
_bin;
// Hash table is array of buckets uint _size;
// Size (# of slots) in hash table uint32 _cnt;
// Number of key-value pairs in hash table void doubhash(
void );
// Double hash table size friend class DictI;
// Friendly iterator function // cmp is a key comparision routine. hash is a routine to hash a key. // Zap to empty; ready for re-use // Return # of key-value pairs in dict // Insert inserts the given key-value pair into the dictionary. The prior // value of the key is returned; NULL if the key was not previously defined. void *
Delete(
void *
key);
// Delete & return old // Find finds the value of a given key; or NULL if not found. // The dictionary is NOT changed. void *
operator [](
const void *
key)
const;
// Do a lookup // == compares two dictionaries; they must have the same keys (their keys // must match using CmpKey) and they must have the same values (pointer // comparison). If so 1 is returned, if not 0 is returned. int32 operator ==(
const Dict &d)
const;
// Compare dictionaries for equal // Print out the dictionary contents as key-value pairs int hashstr(
const void *s);
// Nice string hash // Slimey cheap hash function; no guarenteed performance. Better than the // default for pointers, especially on MS-DOS machines. // Slimey cheap hash function; no guarenteed performance. // Slimey cheap key comparator. //------------------------------Iteration-------------------------------------- // The class of dictionary iterators. Fails in the presences of modifications // to the dictionary during iteration (including searches). // Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;} const Dict *
_d;
// Dictionary being iterated over uint _i;
// Counter over the bins uint _j;
// Counter inside each bin const void *
_key, *
_value;
// Easy access to the key-value pair void operator ++(
void);
// Increment iterator int test(
void) {
return _i<
_d->
_size;}
// Test for end of iteration