(file) Return to HashTable.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.22 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.11 //
  3 karl  1.17 // 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 karl  1.15 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.17 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.19 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.22 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.11 //
 14            // Permission is hereby granted, free of charge, to any person obtaining a copy
 15 kumpf 1.13 // of this software and associated documentation files (the "Software"), to
 16            // deal in the Software without restriction, including without limitation the
 17            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18 mike  1.11 // sell copies of the Software, and to permit persons to whom the Software is
 19            // furnished to do so, subject to the following conditions:
 20 karl  1.19 // 
 21 kumpf 1.13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.11 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24 kumpf 1.13 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27 mike  1.11 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29            //
 30            //==============================================================================
 31            //
 32            //%/////////////////////////////////////////////////////////////////////////////
 33            
 34            #ifndef Pegasus_HashTable_h
 35            #define Pegasus_HashTable_h
 36            
 37            #include <Pegasus/Common/Config.h>
 38            #include <Pegasus/Common/String.h>
 39 carolann.graves 1.20 #include <Pegasus/Common/CIMObjectPath.h>
 40 kumpf           1.14 #include <Pegasus/Common/Linkage.h>
 41 mike            1.11 
 42                      PEGASUS_NAMESPACE_BEGIN
 43                      
 44                      /*  This is the default hash function object used by the HashTable template.
 45                          Specializations are provided for common types.
 46                      */
 47                      template<class K>
 48                      struct HashFunc
 49                      {
 50                      };
 51                      
 52                      PEGASUS_TEMPLATE_SPECIALIZATION struct PEGASUS_COMMON_LINKAGE HashFunc<String>
 53                      {
 54                          static Uint32 hash(const String& str);
 55                      };
 56                      
 57                      PEGASUS_TEMPLATE_SPECIALIZATION struct HashFunc<Uint32>
 58                      {
 59                          static Uint32 hash(Uint32 x) { return x + 13; }
 60                      };
 61                      
 62 carolann.graves 1.20 PEGASUS_TEMPLATE_SPECIALIZATION struct HashFunc <CIMObjectPath>
 63                      {
 64                          static Uint32 hash (const CIMObjectPath & path)
 65                          {
 66                              return path.makeHashCode ();
 67                          }
 68                      };
 69                      
 70 mike            1.21 //
 71                      // Computes a hash code for a string without regard to case. For example, it
 72                      // yields the same hash code for "AB", "ab", "Ab", and "aB".
 73                      //
 74                      struct PEGASUS_COMMON_LINKAGE HashLowerCaseFunc
 75 kumpf           1.16 {
 76 mike            1.21     static Uint32 hash(const String& str);
 77 kumpf           1.16 };
 78                      
 79 mike            1.11 /*  This is a function object used by the HashTable to compare keys. This is
 80 r.kieninger     1.18     the default implementation. Others may be defined and passed in the
 81 mike            1.11     template argument list to perform other kinds of comparisons.
 82                      */
 83                      template<class K>
 84                      struct EqualFunc
 85                      {
 86                          static Boolean equal(const K& x, const K& y)
 87                          {
 88 kumpf           1.23         return x == y;
 89 kumpf           1.16     }
 90                      };
 91                      
 92 carolann.graves 1.20 PEGASUS_TEMPLATE_SPECIALIZATION struct EqualFunc <CIMObjectPath>
 93                      {
 94                          static Boolean equal (const CIMObjectPath & x, const CIMObjectPath & y)
 95                          {
 96                              return x.identical (y);
 97                          }
 98                      };
 99                      
100 kumpf           1.16 /*
101 r.kieninger     1.18     Equal function object that can be used by HashTable to compare keys that
102                          should be treated as case insensitive.
103 kumpf           1.16 
104                          This function can be used for hash table keys constructed from strings that
105 r.kieninger     1.18     should be treated as case insensitive (e.g. class names, namespace names,
106 kumpf           1.16     system names).
107                      
108                          Note: this function compares Strings based on the process locale.
109                      */
110                      struct EqualNoCaseFunc
111                      {
112                          static Boolean equal (const String & x, const String & y)
113                          {
114                              return (0 == String::compareNoCase (x, y));
115 mike            1.11     }
116                      };
117                      
118                      /*  Representation for a bucket. The HashTable class derives from this
119                          bucket to append a key and value. This base class just defines
120                          the pointer to the next bucket in the chain.
121                      */
122                      class PEGASUS_COMMON_LINKAGE _BucketBase
123                      {
124                      public:
125                      
126                          /* Default constructor. */
127                          _BucketBase() : next(0) { }
128                      
129 r.kieninger     1.18     /* Virtual destructor to ensure destruction of derived class
130 kumpf           1.23         elements.
131 mike            1.11     */
132                          virtual ~_BucketBase();
133                      
134                          /* returns true if the key pointed to by the key argument is equal
135 kumpf           1.23         to the internal key of this bucket. This method must be overridden
136                              by the derived class.
137 mike            1.11     */
138                          virtual Boolean equal(const void* key) const = 0;
139                      
140                          /* Clone this bucket. */
141                          virtual _BucketBase* clone() const = 0;
142                      
143                          _BucketBase* next;
144                      };
145                      
146                      class _HashTableRep;
147                      
148                      /* This class implements a simple hash table forward iterator. */
149                      class PEGASUS_COMMON_LINKAGE _HashTableIteratorBase
150                      {
151                      public:
152 r.kieninger     1.18 
153 mike            1.11     _HashTableIteratorBase() : _first(0), _last(0), _bucket(0) { }
154                      
155                          operator int() const { return _bucket != 0; }
156                      
157                          _HashTableIteratorBase operator++(int);
158                      
159                          _HashTableIteratorBase& operator++();
160                      
161                          _HashTableIteratorBase(_BucketBase** first, _BucketBase** last);
162                      
163                      protected:
164                      
165                          _BucketBase** _first;
166                          _BucketBase** _last;
167                          _BucketBase* _bucket;
168                          friend class _HashTableRep;
169                      };
170                      
171                      // ATTN: reorganization not supported yet.
172                      
173                      /*- The _HashTableRep class is the representation class used by HashTable.
174 mike            1.11 
175                          This code is primarily an internal class used to implement the HashTable.
176                          But there may be occasions to use it directly.
177                      
178 r.kieninger     1.18     _HashTableRep parcels out much of the large code so that that code is not
179                          instantiated by the HashTable template class many times. This scheme helps
180 mike            1.11     reduce code bloat caused by templates. The HashTable template class below
181                          acts as kind of a wrapper around this class.
182                      
183                          _HashTableRep is implemented as an array of pointers to chains of hash
184                          buckets. The table initially allocates some number of chains (which can
185                          be controlled by the constructor) and then may increase the number of
186                          chains later (resulting in a reorganization of the hash table).
187                      */
188                      class PEGASUS_COMMON_LINKAGE _HashTableRep
189                      {
190                      public:
191                      
192                          /*- This constructor allocates an array of pointers to chains of buckets,
193 kumpf           1.23         which of course are all empty at this time. The numChains argument
194                              If the numChains argument is less than eight, then eight chains will
195                              be created.
196                              @param numChains specifies the initial number of chains.
197 mike            1.11     */
198                          _HashTableRep(Uint32 numChains);
199                      
200                          /*- Copy constructor. */
201                          _HashTableRep(const _HashTableRep& x);
202                      
203                          /*- Destructor. */
204                          ~_HashTableRep();
205                      
206                          /*- Assignment operator. */
207                          _HashTableRep& operator=(const _HashTableRep& x);
208                      
209                          /*- Returns the size of this hash table (the number of entries). */
210                          Uint32 size() const { return _size; }
211                      
212                          /*- Clears the contents of this hash table. After this is called, the
213 kumpf           1.23         size() method returns zero.
214 mike            1.11     */
215                          void clear();
216                      
217                          /*- Inserts new key-value pair into hash table. Deletes the bucket on
218 kumpf           1.23         failure so caller need not.
219                              @param hashCode hash code generated by caller's hash function.
220                              @param bucket bucket to be inserted.
221                              @param key pointer to key.
222                              @return true if insertion successful; false if duplicate key.
223 mike            1.11     */
224                          Boolean insert(Uint32 hashCode, _BucketBase* bucket, const void* key);
225                      
226                          /*- Finds the bucket with the given key. This method uses the
227 kumpf           1.23         _BucketBase::equal() method to compare keys.
228                              @param hashCode hash code generated by caller's hash function.
229                              @param key void pointer to key.
230                              @return pointer to bucket with that key or zero otherwise.
231 mike            1.11     */
232 mike            1.12     const _BucketBase* lookup(Uint32 hashCode, const void* key) const;
233 mike            1.11 
234                          /*- Removes the bucket with the given key. This method uses the
235 kumpf           1.23         _BucketBase::equal() method to compare keys.
236                              @param hashCode hash code generated by caller's hash function.
237                              @param key void pointer to key.
238                              @return true if entry found and removed and false otherwise.
239 mike            1.11     */
240                          Boolean remove(Uint32 hashCode, const void* key);
241                      
242                          _BucketBase** getChains() const { return _chains; }
243                      
244                          Uint32 getNumChains() const { return _numChains; }
245                      
246                      protected:
247                      
248                          Uint32 _size;
249                          Uint32 _numChains;
250                          _BucketBase** _chains;
251                      };
252                      
253                      /* The _Bucket class is used to implement the HashTable class.
254                      */
255                      template<class K, class V, class E>
256                      class _Bucket : public _BucketBase
257                      {
258                      public:
259                      
260 mike            1.11     _Bucket(const K& key, const V& value) : _key(key), _value(value) { }
261                      
262                          virtual ~_Bucket();
263                      
264                          virtual Boolean equal(const void* key) const;
265                      
266                          virtual _BucketBase* clone() const;
267                      
268                          K& getKey() { return _key; }
269                      
270                          V& getValue() { return _value; }
271                      
272                      private:
273                      
274                          K _key;
275                          V _value;
276                      };
277                      
278                      template<class K, class V, class E>
279                      Boolean _Bucket<K, V, E>::equal(const void* key) const
280                      {
281 mike            1.11     return E::equal(*((K*)key), _key);
282                      }
283                      
284                      template<class K, class V, class E>
285                      _Bucket<K, V, E>::~_Bucket()
286                      {
287                      
288                      }
289                      
290                      template<class K, class V, class E>
291                      _BucketBase* _Bucket<K, V, E>::clone() const
292                      {
293                          return new _Bucket<K, V, E>(_key, _value);
294                      }
295                      
296                      /* Iterator for HashTable class. */
297                      template<class K, class V, class E>
298                      class _HashTableIterator : public _HashTableIteratorBase
299                      {
300                      public:
301                      
302 r.kieninger     1.18     _HashTableIterator()
303 kumpf           1.23         : _HashTableIteratorBase() { }
304 mike            1.11 
305                          _HashTableIterator(_BucketBase** first, _BucketBase** last)
306 kumpf           1.23         : _HashTableIteratorBase(first, last) { }
307 mike            1.11 
308                          const K& key() const { return ((_Bucket<K, V, E>*)_bucket)->getKey(); }
309                      
310                          const V& value() const { return ((_Bucket<K, V, E>*)_bucket)->getValue(); }
311                      };
312                      
313 r.kieninger     1.18 /** The HashTable class provides a simple hash table implementation which
314 mike            1.11     associates key-value pairs.
315                      
316                          This implementation minimizes template bloat considerably by factoring out
317                          most of the code into a common non-template class (see _HashTableRep).
318                          The HashTable class is mostly a wrapper to add proper type semantics to the
319                          use of its representation class.
320                      
321                          Hashing as always is O(1).
322                      
323 r.kieninger     1.18     HashTable uses the most popular hash table implementation which utilizes
324 mike            1.11     an array of pointers to bucket chains. This is organized as follows:
325                      
326                              <pre>
327 r.kieninger     1.18            +---+
328 mike            1.11            |   |   +-----+-------+
329                               0 | ----->| key | value |
330                                 |   |   +-----+-------+
331                                 +---+
332                                 |   |   +-----+-------+   +-----+-------+   +-----+-------+
333                               1 | ----->| key | value |-->| key | value |-->| key | value |
334                                 |   |   +-----+-------+   +-----+-------+   +-----+-------+
335                                 +---+
336                                   .
337                                   .
338                                   .
339                                 +---+
340                                 |   |   +-----+-------+   +-----+-------+
341                              N-1| ----->| key | value |-->| key | value |
342                                 |   |   +-----+-------+   +-----+-------+
343                                 +---+
344                              </pre>
345                      
346 r.kieninger     1.18     To locate an item a hash function is applied to the key to produce an
347                          integer value. Then the modulo of that integer is taken with N to select
348 mike            1.11     a chain (as shown above). Then the chain is searched for a bucket whose
349                          key value is the same as the target key.
350                      
351                          The number of chains default to DEFAULT_NUM_CHAINS but should be about
352                          one-third the number of expected entries (so that the average chain
353                          will be three long). Making the number of chains too large will waste
354                          space causing the hash table to be very sparse. But for optimal efficiency,
355                          one might set the number of chains to be the same as the expected number
356                          of entries.
357                      
358 r.kieninger     1.18     This implementation does have NOT an adaptive growth algorithm yet which
359 mike            1.11     would allow it to increase the number of chains periodically based on some
360                          statistic (e.g., when the number of entries is more than three times the
361                          number of chains; this would keep the average chain length below three).
362                      
363                          The following example shows how to instantiate a HashTable which associates
364                          String keys with Uint32 values.
365                      
366 kumpf           1.23         <pre>
367                              typedef HashTable&lt;String, Uint32&gt; HT;
368                              HT ht;
369                              </pre>
370 mike            1.11 
371 r.kieninger     1.18     Some of the template arguments are defaulted in the above example (the
372 mike            1.11     third and forth). The instantiation is explicitly qualified like this
373                          (which by the way has exactly the same effect).
374                      
375 kumpf           1.23         <pre>
376                              typedef HashTable&lt;String, Uint32,
377                                  EqualFunc&lt;String&gt;, HashFunc&lt;String&gt;&gt; HT;
378                              </pre>
379 mike            1.11 
380 kumpf           1.23     The third and fourth arguments are described more in detail later.
381 mike            1.11 
382                          Then, entries may be inserted like this:
383                      
384 kumpf           1.23         <pre>
385                              ht.insert("Red", 111);
386                              ht.insert("Green", 222);
387                              ht.insert("Blue", 222);
388                              </pre>
389 mike            1.11 
390                          And entries may be looked up as follows:
391                      
392 kumpf           1.23         <pre>
393                              Uint32 value;
394                              ht.lookup("Red", value);
395                              </pre>
396 mike            1.11 
397                          And entries may be removed like this:
398                      
399 kumpf           1.23         <pre>
400                              h.remove("Red");
401                              </pre>
402 mike            1.11 
403                          Iteration is done like this:
404                      
405 kumpf           1.23         <pre>
406                              for (HT::Iterator i = ht.start(); i; i++)
407                              {
408                                  // To access the key call i.key()!
409                                  // To access the value call i.value()!
410                              }
411                              </pre>
412 mike            1.11 
413 r.kieninger     1.18     Note that only forward iteration is supported (no backwards iteration),
414                          AND that the hashtable MUST NOT be modified during the iteration!!!
415 mike            1.11 
416                          Equality of keys is determined using the EqualFunc class which is
417 r.kieninger     1.18     the default third argument of the template argument list. A new function
418                          object may be defined and passed to modify the behavior (for example, one
419                          might define equality of strings to ignore whitespace). Here is how to
420 mike            1.11     define and use a new equality function object:
421                      
422 kumpf           1.23         <pre>
423                              struct MyEqualFunc
424                              {
425                                  static Boolean equal(const String& x, const String& y)
426                                  {
427                                      // do something here to test for equality!
428                                  }
429                              };
430 mike            1.11 
431 kumpf           1.23         ...
432 mike            1.11 
433 kumpf           1.23         EqualFunc&lt;String, Uint32, MyEqualFunc&gt; ht;
434                              </pre>
435 mike            1.11 
436 r.kieninger     1.18     When the lookup(), insert(), and remove() methods are called, the
437 mike            1.11     MyEqualFunc::equal() method will be used to determine equality.
438                      
439                          Hash functions are provided for common types (as part of the default
440                          HashFunc class). For other types it is possible to define a custom function
441                          object as follows:
442                      
443 kumpf           1.23         <pre>
444                              struct MyHashFunc
445                              {
446                                  static Uint32 hash(const String& x)
447                                  {
448                                      // Do some hashing here!
449                                  }
450                              };
451 mike            1.11 
452 kumpf           1.23         ...
453 mike            1.11 
454 kumpf           1.23         EqualFunc&lt;String, Uint32, MyEqualFunc, MyHashFunc&gt; ht;
455                              </pre>
456 mike            1.11 
457 r.kieninger     1.18     As always, the hash function should provide a reasonably uniform
458 mike            1.11     distrubtion so that all of the entries don't get crowded into a few
459                          chains. Note that a hash function which returns zero, would force
460                          the pathalogical case in which all entries are placed in the first
461                          chain.
462                      */
463                      template<class K, class V, class E , class H >
464                      class HashTable
465                      {
466                      public:
467                      
468                          typedef _HashTableIterator<K, V, E> Iterator;
469                      
470                          /* By default, we create this many chains initially */
471                          enum { DEFAULT_NUM_CHAINS = 32 };
472                      
473                          /** Constructor.
474 kumpf           1.23         @param numChains number of chains to create.
475 mike            1.11     */
476                          HashTable(Uint32 numChains = DEFAULT_NUM_CHAINS) : _rep(numChains)
477                          {
478                      
479                          }
480                      
481                          /** Copy constructor. */
482                          HashTable(const HashTable<K,V,E,H>& x) : _rep(x._rep)
483                          {
484                      
485                          }
486                      
487                          /** Assignment operator. */
488                          HashTable<K,V,E,H>& operator=(const HashTable<K,V,E,H>& x)
489                          {
490 kumpf           1.23         if (this != &x)
491                                  _rep = x._rep;
492                              return *this;
493 mike            1.11     }
494                      
495                          /** Returns the size of this hash table (the number of entries). */
496                          Uint32 size() const { return _rep.size(); }
497                      
498                          /** Clears the contents of this hash table. After this is called, the
499 kumpf           1.23         size() method returns zero.
500 mike            1.11     */
501                          void clear() { _rep.clear(); }
502                      
503                          /** Inserts new key-value pair into hash table.
504 kumpf           1.23         @param key key component.
505                              @param value value component.
506                              @return true on success; false if duplicate key.
507 mike            1.11     */
508                          Boolean insert(const K& key, const V& value)
509                          {
510 kumpf           1.23         return _rep.insert(
511                                  H::hash(key), new _Bucket<K, V, E>(key, value), &key);
512 mike            1.11     }
513                      
514                          /** Checks to see if hash table contains an entry with the given key.
515 kumpf           1.23         @param key key to be searched for
516                              @return true if hash table contains an entry with the given key.
517 mike            1.11     */
518 mike            1.12     Boolean contains(const K& key) const
519 mike            1.11     {
520 kumpf           1.23         V value;
521                              return lookup(key, value);
522 mike            1.11     }
523                      
524                          /** Looks up the entry with the given key.
525 kumpf           1.23         @param key key of entry to be located.
526                              @param value output value.
527                              @return true if found; false otherwise.
528 mike            1.11     */
529 mike            1.12     Boolean lookup(const K& key, V& value) const;
530 mike            1.11 
531                          /** Removes the entry with the given key.
532 kumpf           1.23         @param key key of entry to be removed.
533                              @return true on success; false otherwise.
534 mike            1.11     */
535                          Boolean remove(const K& key)
536                          {
537 kumpf           1.23         return _rep.remove(H::hash(key), &key);
538 mike            1.11     }
539                      
540                          /** Obtains an iterator for this object. */
541 r.kieninger     1.18     Iterator start() const
542 mike            1.11     {
543 kumpf           1.23         return Iterator(
544                                  _rep.getChains(), _rep.getChains() + _rep.getNumChains());
545 mike            1.11     }
546                      
547                      private:
548                      
549                          _HashTableRep _rep;
550                      };
551                      
552                      template<class K, class V, class E, class H>
553 mike            1.12 inline Boolean HashTable<K, V, E, H>::lookup(const K& key, V& value) const
554 mike            1.11 {
555 r.kieninger     1.18     _Bucket<K, V, E>* bucket
556 kumpf           1.23         = (_Bucket<K, V, E>*)_rep.lookup(H::hash(key), &key);
557 mike            1.11 
558                          if (bucket)
559                          {
560 kumpf           1.23         value = bucket->getValue();
561                              return true;
562 mike            1.11     }
563                      
564                          return false;
565                      }
566                      
567                      PEGASUS_NAMESPACE_END
568                      
569                      #endif /* Pegasus_HashTable_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2