//%///////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // //============================================================================== // // Author: Mike Brasher (mbrasher@bmc.com) // // Modified By: // //%///////////////////////////////////////////////////////////////////////////// #ifndef Pegasus_HashTable_h #define Pegasus_HashTable_h #include #include PEGASUS_NAMESPACE_BEGIN PEGASUS_COMMON_LINKAGE inline Uint32 Hash(const String& str); /** Representation for a bucket. The HashTable class derives from this bucket to append a key and value. This base class just defines the pointer to the next bucket in the chain. */ class PEGASUS_COMMON_LINKAGE BucketBase { public: /** Virtual destructor to ensure destruction of derived class elements. */ virtual ~BucketBase(); /** returns true if the key pointed to by the key argument is equal to the internal key of this bucket. This method must be overridden by the derived class. */ virtual Boolean equal(const void* key) const = 0; BucketBase* next; }; class HashTableBase; /** This class implements a simple hash table forward iterator. */ class PEGASUS_COMMON_LINKAGE HashTableIteratorBase { public: HashTableIteratorBase() : _first(0), _last(0), _bucket(0) { } operator Boolean() const { return _bucket != 0; } HashTableIteratorBase operator++(int); HashTableIteratorBase& operator++(); HashTableIteratorBase(BucketBase** first, BucketBase** last); protected: BucketBase** _first; BucketBase** _last; BucketBase* _bucket; friend HashTableBase; }; /** The HashTableBase class is the base class which HashTable derives from. This code is primarily an internal class used to implement the HashTable. But there may be occasions to use it directly. HashTableBase parcels out much of the large code so that that code is not instantiated by the HashTable template class many times. This scheme helps reduce code bloat caused by templates. The HashTable template class below acts as kind of a wrapper around this class. HashTableBase is implemented as an array of pointers to chains of hash buckets. The table initially allocates some number of chains (which can be controlled by the constructor) and then may increase the number of chains later (resulting in a reorganization of the hash table). ATTN: reorganization not supported yet. */ class PEGASUS_COMMON_LINKAGE HashTableBase { public: /** This constructor allocates an array of pointers to chains of buckets, which of course are all empty at this time. The numChains argument If the numChains argument is less than eight, then eight chains will be created. @param numChains - specifies the initial number of chains. */ HashTableBase(Uint32 numChains); /** Destructor. */ ~HashTableBase(); /** Returns the size of this hash table (the number of entries). */ Uint32 getSize() const { return _size; } /** Clears the contents of this hash table. After this is called, the getSize() method returns zero. */ void clear(); /** Inserts new key-value pair into hash table. Deletes the bucket on failure so caller need not. @param hashCode - hash code generated by caller's hash function. @param bucket - bucket to be inserted. @param key - pointer to key. @return true if insertion successful; false if duplicate key. */ Boolean insert(Uint32 hashCode, BucketBase* bucket, const void* key); /** Finds the bucket with the given key. This method uses the BucketBase::equal() method to compare keys. @param hashCode - hash code generated by caller's hash function. @param key - void pointer to key. @return pointer to bucket with that key or zero otherwise. */ const BucketBase* lookup(Uint32 hashCode, const void* key); /** Removes the bucket with the given key. This method uses the BucketBase::equal() method to compare keys. @param hashCode - hash code generated by caller's hash function. @param key - void pointer to key. @return true if entry found and removed and false otherwise. */ Boolean remove(Uint32 hashCode, const void* key); protected: Uint32 _size; Uint32 _numChains; BucketBase** _chains; }; /** The Bucket class is used to implement the HashTable class. */ template class Bucket : public BucketBase { public: Bucket(const K& key, const V& value) : _key(key), _value(value) { } virtual ~Bucket(); virtual Boolean equal(const void* key) const; K& getKey() { return _key; } V& getValue() { return _value; } private: K _key; V _value; }; template Boolean Bucket::equal(const void* key) const { return *((K*)key) == _key; } template Bucket::~Bucket() { } /** Iterator for HashTable class. */ template class HashTableIterator : public HashTableIteratorBase { public: HashTableIterator() : HashTableIteratorBase() { } HashTableIterator(BucketBase** first, BucketBase** last) : HashTableIteratorBase(first, last) { } const K& key() const { return ((Bucket*)_bucket)->getKey(); } const V& value() const { return ((Bucket*)_bucket)->getValue(); } }; /** HashTable provides a simple hash table implementation which associates key-value pairs. */ template class HashTable : public HashTableBase { public: typedef HashTableIterator Iterator; /** By default, we create this many chains initially */ enum { DEFAULT_NUM_CHAINS = 32 }; /** Constructor. @param numChains - number of chains to create. */ HashTable(Uint32 numChains = DEFAULT_NUM_CHAINS) : HashTableBase(numChains) { } /** Inserts new key-value pair into hash table. @param key - key component. @param value - value component. @return true on success; false if duplicate key. */ Boolean insert(const K& key, const V& value) { return HashTableBase::insert( Hash(key), new Bucket(key, value), &key); } /** Looks up the entry with the given key. @param key - key of entry to be located. @param value - output value. @return true if found; false otherwise. */ Boolean lookup(const K& key, V& value); /** Removes the entry with the given key. @param key - key of entry to be removed. @return true on success; false otherwise. */ Boolean remove(const K& key) { return HashTableBase::remove(Hash(key), &key); } /** Obtains an iterator for this object. */ Iterator start() const { return Iterator(_chains, _chains + _numChains); } }; template inline Boolean HashTable::lookup(const K& key, V& value) { Bucket* bucket = (Bucket*)HashTableBase::lookup(Hash(key), &key); if (bucket) { value = bucket->getValue(); return true; } return false; } PEGASUS_NAMESPACE_END #endif /* Pegasus_HashTable_h */