1 //%2005////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to
14 // deal in the Software without restriction, including without limitation the
15 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 // sell copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
19 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
20 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
21 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
22 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //==============================================================================
30 // Author: Mike Brasher (mbrasher@bmc.com)
32 // Modified By: Carol Ann Krug Graves, Hewlett-Packard Company
33 // (carolann_graves@hp.com)
35 //%/////////////////////////////////////////////////////////////////////////////
37 #ifndef Pegasus_HashTable_h
38 #define Pegasus_HashTable_h
40 #include <Pegasus/Common/Config.h>
41 #include <Pegasus/Common/String.h>
42 #include <Pegasus/Common/CIMObjectPath.h>
43 #include <Pegasus/Common/Linkage.h>
45 PEGASUS_NAMESPACE_BEGIN
47 /* This is the default hash function object used by the HashTable template.
48 Specializations are provided for common types.
55 PEGASUS_TEMPLATE_SPECIALIZATION struct PEGASUS_COMMON_LINKAGE HashFunc<String>
57 static Uint32 hash(const String& str);
60 PEGASUS_TEMPLATE_SPECIALIZATION struct HashFunc<Uint32>
62 static Uint32 hash(Uint32 x) { return x + 13; }
65 PEGASUS_TEMPLATE_SPECIALIZATION struct HashFunc <CIMObjectPath>
67 static Uint32 hash (const CIMObjectPath & path)
69 return path.makeHashCode ();
74 Hash function object that converts to lowercase.
76 This function can be used for hash table keys constructed from strings that
77 should be treated as case insensitive (e.g. class names, namespace names,
80 Note: this function converts to lower case based on the process locale.
82 struct HashLowerCaseFunc
84 static Uint32 hash (const String & str)
89 for (Uint32 i = 0, n = cpy.size (); i < n; i++)
95 /* This is a function object used by the HashTable to compare keys. This is
96 the default implementation. Others may be defined and passed in the
97 template argument list to perform other kinds of comparisons.
102 static Boolean equal(const K& x, const K& y)
108 PEGASUS_TEMPLATE_SPECIALIZATION struct EqualFunc <CIMObjectPath>
110 static Boolean equal (const CIMObjectPath & x, const CIMObjectPath & y)
112 return x.identical (y);
117 Equal function object that can be used by HashTable to compare keys that
118 should be treated as case insensitive.
120 This function can be used for hash table keys constructed from strings that
121 should be treated as case insensitive (e.g. class names, namespace names,
124 Note: this function compares Strings based on the process locale.
126 struct EqualNoCaseFunc
128 static Boolean equal (const String & x, const String & y)
130 return (0 == String::compareNoCase (x, y));
134 /* Representation for a bucket. The HashTable class derives from this
135 bucket to append a key and value. This base class just defines
136 the pointer to the next bucket in the chain.
138 class PEGASUS_COMMON_LINKAGE _BucketBase
142 /* Default constructor. */
143 _BucketBase() : next(0) { }
145 /* Virtual destructor to ensure destruction of derived class
148 virtual ~_BucketBase();
150 /* returns true if the key pointed to by the key argument is equal
151 to the internal key of this bucket. This method must be overridden
152 by the derived class.
154 virtual Boolean equal(const void* key) const = 0;
156 /* Clone this bucket. */
157 virtual _BucketBase* clone() const = 0;
164 /* This class implements a simple hash table forward iterator. */
165 class PEGASUS_COMMON_LINKAGE _HashTableIteratorBase
169 _HashTableIteratorBase() : _first(0), _last(0), _bucket(0) { }
171 operator int() const { return _bucket != 0; }
173 _HashTableIteratorBase operator++(int);
175 _HashTableIteratorBase& operator++();
177 _HashTableIteratorBase(_BucketBase** first, _BucketBase** last);
181 _BucketBase** _first;
183 _BucketBase* _bucket;
184 friend class _HashTableRep;
187 // ATTN: reorganization not supported yet.
189 /*- The _HashTableRep class is the representation class used by HashTable.
191 This code is primarily an internal class used to implement the HashTable.
192 But there may be occasions to use it directly.
194 _HashTableRep parcels out much of the large code so that that code is not
195 instantiated by the HashTable template class many times. This scheme helps
196 reduce code bloat caused by templates. The HashTable template class below
197 acts as kind of a wrapper around this class.
199 _HashTableRep is implemented as an array of pointers to chains of hash
200 buckets. The table initially allocates some number of chains (which can
201 be controlled by the constructor) and then may increase the number of
202 chains later (resulting in a reorganization of the hash table).
204 class PEGASUS_COMMON_LINKAGE _HashTableRep
208 /*- This constructor allocates an array of pointers to chains of buckets,
209 which of course are all empty at this time. The numChains argument
210 If the numChains argument is less than eight, then eight chains will
212 @param numChains specifies the initial number of chains.
214 _HashTableRep(Uint32 numChains);
216 /*- Copy constructor. */
217 _HashTableRep(const _HashTableRep& x);
222 /*- Assignment operator. */
223 _HashTableRep& operator=(const _HashTableRep& x);
225 /*- Returns the size of this hash table (the number of entries). */
226 Uint32 size() const { return _size; }
228 /*- Clears the contents of this hash table. After this is called, the
229 size() method returns zero.
233 /*- Inserts new key-value pair into hash table. Deletes the bucket on
234 failure so caller need not.
235 @param hashCode hash code generated by caller's hash function.
236 @param bucket bucket to be inserted.
237 @param key pointer to key.
238 @return true if insertion successful; false if duplicate key.
240 Boolean insert(Uint32 hashCode, _BucketBase* bucket, const void* key);
242 /*- Finds the bucket with the given key. This method uses the
243 _BucketBase::equal() method to compare keys.
244 @param hashCode hash code generated by caller's hash function.
245 @param key void pointer to key.
246 @return pointer to bucket with that key or zero otherwise.
248 const _BucketBase* lookup(Uint32 hashCode, const void* key) const;
250 /*- Removes the bucket with the given key. This method uses the
251 _BucketBase::equal() method to compare keys.
252 @param hashCode hash code generated by caller's hash function.
253 @param key void pointer to key.
254 @return true if entry found and removed and false otherwise.
256 Boolean remove(Uint32 hashCode, const void* key);
258 _BucketBase** getChains() const { return _chains; }
260 Uint32 getNumChains() const { return _numChains; }
266 _BucketBase** _chains;
269 /* The _Bucket class is used to implement the HashTable class.
271 template<class K, class V, class E>
272 class _Bucket : public _BucketBase
276 _Bucket(const K& key, const V& value) : _key(key), _value(value) { }
280 virtual Boolean equal(const void* key) const;
282 virtual _BucketBase* clone() const;
284 K& getKey() { return _key; }
286 V& getValue() { return _value; }
294 template<class K, class V, class E>
295 Boolean _Bucket<K, V, E>::equal(const void* key) const
297 return E::equal(*((K*)key), _key);
300 template<class K, class V, class E>
301 _Bucket<K, V, E>::~_Bucket()
306 template<class K, class V, class E>
307 _BucketBase* _Bucket<K, V, E>::clone() const
309 return new _Bucket<K, V, E>(_key, _value);
312 /* Iterator for HashTable class. */
313 template<class K, class V, class E>
314 class _HashTableIterator : public _HashTableIteratorBase
319 : _HashTableIteratorBase() { }
321 _HashTableIterator(_BucketBase** first, _BucketBase** last)
322 : _HashTableIteratorBase(first, last) { }
324 const K& key() const { return ((_Bucket<K, V, E>*)_bucket)->getKey(); }
326 const V& value() const { return ((_Bucket<K, V, E>*)_bucket)->getValue(); }
329 /** The HashTable class provides a simple hash table implementation which
330 associates key-value pairs.
332 This implementation minimizes template bloat considerably by factoring out
333 most of the code into a common non-template class (see _HashTableRep).
334 The HashTable class is mostly a wrapper to add proper type semantics to the
335 use of its representation class.
337 Hashing as always is O(1).
339 HashTable uses the most popular hash table implementation which utilizes
340 an array of pointers to bucket chains. This is organized as follows:
345 0 | ----->| key | value |
348 | | +-----+-------+ +-----+-------+ +-----+-------+
349 1 | ----->| key | value |-->| key | value |-->| key | value |
350 | | +-----+-------+ +-----+-------+ +-----+-------+
356 | | +-----+-------+ +-----+-------+
357 N-1| ----->| key | value |-->| key | value |
358 | | +-----+-------+ +-----+-------+
362 To locate an item a hash function is applied to the key to produce an
363 integer value. Then the modulo of that integer is taken with N to select
364 a chain (as shown above). Then the chain is searched for a bucket whose
365 key value is the same as the target key.
367 The number of chains default to DEFAULT_NUM_CHAINS but should be about
368 one-third the number of expected entries (so that the average chain
369 will be three long). Making the number of chains too large will waste
370 space causing the hash table to be very sparse. But for optimal efficiency,
371 one might set the number of chains to be the same as the expected number
374 This implementation does have NOT an adaptive growth algorithm yet which
375 would allow it to increase the number of chains periodically based on some
376 statistic (e.g., when the number of entries is more than three times the
377 number of chains; this would keep the average chain length below three).
379 The following example shows how to instantiate a HashTable which associates
380 String keys with Uint32 values.
383 typedef HashTable<String, Uint32> HT;
387 Some of the template arguments are defaulted in the above example (the
388 third and forth). The instantiation is explicitly qualified like this
389 (which by the way has exactly the same effect).
392 typedef HashTable<String, Uint32, EqualFunc<String>, HashFunc<String>> HT;
395 The third and forth arguments are described more in detail later.
397 Then, entries may be inserted like this:
400 ht.insert("Red", 111);
401 ht.insert("Green", 222);
402 ht.insert("Blue", 222);
405 And entries may be looked up as follows:
409 ht.lookup("Red", value);
412 And entries may be removed like this:
418 Iteration is done like this:
421 for (HT::Iterator i = ht.start(); i; i++)
423 // To access the key call i.key()!
424 // To access the value call i.value()!
428 Note that only forward iteration is supported (no backwards iteration),
429 AND that the hashtable MUST NOT be modified during the iteration!!!
431 Equality of keys is determined using the EqualFunc class which is
432 the default third argument of the template argument list. A new function
433 object may be defined and passed to modify the behavior (for example, one
434 might define equality of strings to ignore whitespace). Here is how to
435 define and use a new equality function object:
440 static Boolean equal(const String& x, const String& y)
442 // do something here to test for equality!
448 EqualFunc<String, Uint32, MyEqualFunc> ht;
451 When the lookup(), insert(), and remove() methods are called, the
452 MyEqualFunc::equal() method will be used to determine equality.
454 Hash functions are provided for common types (as part of the default
455 HashFunc class). For other types it is possible to define a custom function
461 static Uint32 hash(const String& x)
463 // Do some hashing here!
469 EqualFunc<String, Uint32, MyEqualFunc, MyHashFunc> ht;
472 As always, the hash function should provide a reasonably uniform
473 distrubtion so that all of the entries don't get crowded into a few
474 chains. Note that a hash function which returns zero, would force
475 the pathalogical case in which all entries are placed in the first
478 template<class K, class V, class E , class H >
483 typedef _HashTableIterator<K, V, E> Iterator;
485 /* By default, we create this many chains initially */
486 enum { DEFAULT_NUM_CHAINS = 32 };
489 @param numChains number of chains to create.
491 HashTable(Uint32 numChains = DEFAULT_NUM_CHAINS) : _rep(numChains)
496 /** Copy constructor. */
497 HashTable(const HashTable<K,V,E,H>& x) : _rep(x._rep)
502 /** Assignment operator. */
503 HashTable<K,V,E,H>& operator=(const HashTable<K,V,E,H>& x)
510 /** Returns the size of this hash table (the number of entries). */
511 Uint32 size() const { return _rep.size(); }
513 /** Clears the contents of this hash table. After this is called, the
514 size() method returns zero.
516 void clear() { _rep.clear(); }
518 /** Inserts new key-value pair into hash table.
519 @param key key component.
520 @param value value component.
521 @return true on success; false if duplicate key.
523 Boolean insert(const K& key, const V& value)
526 H::hash(key), new _Bucket<K, V, E>(key, value), &key);
529 /** Checks to see if hash table contains an entry with the given key.
530 @param key key to be searched for
531 @return true if hash table contains an entry with the given key.
533 Boolean contains(const K& key) const
536 return lookup(key, value);
539 /** Looks up the entry with the given key.
540 @param key key of entry to be located.
541 @param value output value.
542 @return true if found; false otherwise.
544 Boolean lookup(const K& key, V& value) const;
546 /** Removes the entry with the given key.
547 @param key key of entry to be removed.
548 @return true on success; false otherwise.
550 Boolean remove(const K& key)
552 return _rep.remove(H::hash(key), &key);
555 /** Obtains an iterator for this object. */
556 Iterator start() const
559 _rep.getChains(), _rep.getChains() + _rep.getNumChains());
567 template<class K, class V, class E, class H>
568 inline Boolean HashTable<K, V, E, H>::lookup(const K& key, V& value) const
570 _Bucket<K, V, E>* bucket
571 = (_Bucket<K, V, E>*)_rep.lookup(H::hash(key), &key);
575 value = bucket->getValue();
582 PEGASUS_NAMESPACE_END
584 #endif /* Pegasus_HashTable_h */