annotate src/share/vm/libadt/dict.hpp @ 605:98cb887364d3

6810672: Comment typos Summary: I have collected some typos I have found while looking at the code. Reviewed-by: kvn, never
author twisti
date Fri, 27 Feb 2009 13:27:09 -0800
parents a61af66fc99e
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #ifndef _DICT_
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #define _DICT_
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // Dictionaries - An Abstract Data Type
a61af66fc99e Initial load
duke
parents:
diff changeset
28 //INTERFACE
a61af66fc99e Initial load
duke
parents:
diff changeset
29 class ostream;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 class Dict;
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 // These dictionaries define a key-value mapping. They can be inserted to,
a61af66fc99e Initial load
duke
parents:
diff changeset
33 // searched or deleted from. They grow and shrink as needed. The key is a
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // pointer to something (or anything which can be stored in a pointer). A
a61af66fc99e Initial load
duke
parents:
diff changeset
35 // key comparison routine determines if two keys are equal or not. A hash
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // function can be provided; if it's not provided the key itself is used
a61af66fc99e Initial load
duke
parents:
diff changeset
37 // instead. A nice string hash function is included.
a61af66fc99e Initial load
duke
parents:
diff changeset
38 typedef int32 (*CmpKey)(const void *key1, const void *key2);
a61af66fc99e Initial load
duke
parents:
diff changeset
39 typedef int (*Hash)(const void *key);
a61af66fc99e Initial load
duke
parents:
diff changeset
40 typedef void (*FuncDict)(const void *key, const void *val, Dict *d);
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 class Dict : public ResourceObj { // Dictionary structure
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
44 class Arena *_arena; // Where to draw storage from
a61af66fc99e Initial load
duke
parents:
diff changeset
45 class bucket *_bin; // Hash table is array of buckets
a61af66fc99e Initial load
duke
parents:
diff changeset
46 uint _size; // Size (# of slots) in hash table
a61af66fc99e Initial load
duke
parents:
diff changeset
47 uint32 _cnt; // Number of key-value pairs in hash table
a61af66fc99e Initial load
duke
parents:
diff changeset
48 const Hash _hash; // Hashing function
a61af66fc99e Initial load
duke
parents:
diff changeset
49 const CmpKey _cmp; // Key comparison function
a61af66fc99e Initial load
duke
parents:
diff changeset
50 void doubhash( void ); // Double hash table size
a61af66fc99e Initial load
duke
parents:
diff changeset
51
a61af66fc99e Initial load
duke
parents:
diff changeset
52 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
53 friend class DictI; // Friendly iterator function
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // cmp is a key comparision routine. hash is a routine to hash a key.
a61af66fc99e Initial load
duke
parents:
diff changeset
56 Dict( CmpKey cmp, Hash hash );
a61af66fc99e Initial load
duke
parents:
diff changeset
57 Dict( CmpKey cmp, Hash hash, Arena *arena, int size=16 );
a61af66fc99e Initial load
duke
parents:
diff changeset
58 ~Dict();
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 Dict( const Dict & ); // Deep-copy guts
a61af66fc99e Initial load
duke
parents:
diff changeset
61 Dict &operator =( const Dict & );
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // Zap to empty; ready for re-use
a61af66fc99e Initial load
duke
parents:
diff changeset
64 void Clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // Return # of key-value pairs in dict
a61af66fc99e Initial load
duke
parents:
diff changeset
67 uint32 Size(void) const { return _cnt; }
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // Insert inserts the given key-value pair into the dictionary. The prior
a61af66fc99e Initial load
duke
parents:
diff changeset
70 // value of the key is returned; NULL if the key was not previously defined.
a61af66fc99e Initial load
duke
parents:
diff changeset
71 void *Insert(void *key, void *val, bool replace = true); // A new key-value
a61af66fc99e Initial load
duke
parents:
diff changeset
72 void *Delete(void *key); // Delete & return old
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 // Find finds the value of a given key; or NULL if not found.
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // The dictionary is NOT changed.
a61af66fc99e Initial load
duke
parents:
diff changeset
76 void *operator [](const void *key) const; // Do a lookup
a61af66fc99e Initial load
duke
parents:
diff changeset
77
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // == compares two dictionaries; they must have the same keys (their keys
a61af66fc99e Initial load
duke
parents:
diff changeset
79 // must match using CmpKey) and they must have the same values (pointer
a61af66fc99e Initial load
duke
parents:
diff changeset
80 // comparison). If so 1 is returned, if not 0 is returned.
a61af66fc99e Initial load
duke
parents:
diff changeset
81 int32 operator ==(const Dict &d) const; // Compare dictionaries for equal
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // Print out the dictionary contents as key-value pairs
a61af66fc99e Initial load
duke
parents:
diff changeset
84 void print();
a61af66fc99e Initial load
duke
parents:
diff changeset
85 };
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 // Hashing functions
a61af66fc99e Initial load
duke
parents:
diff changeset
88 int hashstr(const void *s); // Nice string hash
605
98cb887364d3 6810672: Comment typos
twisti
parents: 0
diff changeset
89 // Slimey cheap hash function; no guaranteed performance. Better than the
0
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // default for pointers, especially on MS-DOS machines.
a61af66fc99e Initial load
duke
parents:
diff changeset
91 int hashptr(const void *key);
605
98cb887364d3 6810672: Comment typos
twisti
parents: 0
diff changeset
92 // Slimey cheap hash function; no guaranteed performance.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
93 int hashkey(const void *key);
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 // Key comparators
a61af66fc99e Initial load
duke
parents:
diff changeset
96 int32 cmpstr(const void *k1, const void *k2);
a61af66fc99e Initial load
duke
parents:
diff changeset
97 // Slimey cheap key comparator.
a61af66fc99e Initial load
duke
parents:
diff changeset
98 int32 cmpkey(const void *key1, const void *key2);
a61af66fc99e Initial load
duke
parents:
diff changeset
99
a61af66fc99e Initial load
duke
parents:
diff changeset
100 //------------------------------Iteration--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
101 // The class of dictionary iterators. Fails in the presences of modifications
a61af66fc99e Initial load
duke
parents:
diff changeset
102 // to the dictionary during iteration (including searches).
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}
a61af66fc99e Initial load
duke
parents:
diff changeset
104 class DictI {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
106 const Dict *_d; // Dictionary being iterated over
a61af66fc99e Initial load
duke
parents:
diff changeset
107 uint _i; // Counter over the bins
a61af66fc99e Initial load
duke
parents:
diff changeset
108 uint _j; // Counter inside each bin
a61af66fc99e Initial load
duke
parents:
diff changeset
109 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
110 const void *_key, *_value; // Easy access to the key-value pair
a61af66fc99e Initial load
duke
parents:
diff changeset
111 DictI( const Dict *d ) {reset(d);}; // Create a new iterator
a61af66fc99e Initial load
duke
parents:
diff changeset
112 void reset( const Dict *dict ); // Reset existing iterator
a61af66fc99e Initial load
duke
parents:
diff changeset
113 void operator ++(void); // Increment iterator
a61af66fc99e Initial load
duke
parents:
diff changeset
114 int test(void) { return _i<_d->_size;} // Test for end of iteration
a61af66fc99e Initial load
duke
parents:
diff changeset
115 };
a61af66fc99e Initial load
duke
parents:
diff changeset
116
a61af66fc99e Initial load
duke
parents:
diff changeset
117 #endif // _DICT_