HashTable.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_HASHTABLE_H_
00014 #define _SUPPORT_HASHTABLE_H_
00015 
00021 #include <support/SupportDefs.h>
00022 #include <support/KeyedVector.h>
00023 
00024 #if _SUPPORTS_NAMESPACE
00025 namespace palmos {
00026 namespace support {
00027 #endif
00028 
00033 /**************************************************************************************/
00034 
00035 class SHasher
00036 {
00037     public:
00038     
00039                     SHasher(int32_t bits);
00040         uint32_t    BaseHash(const void *data, int32_t len) const;
00041 
00042     private:
00043 
00044         int32_t     m_bits;
00045         uint32_t    m_crcxor[256];
00046 };
00047 
00048 template <class KEY, class VALUE>
00049 class SHashTable : public SHasher
00050 {
00051     public:
00052     
00053         SHashTable(int32_t bits);
00054         ~SHashTable();
00055 
00056         uint32_t Hash(const KEY &key) const;
00057 
00058         const VALUE& Lookup(const KEY &key) const
00059         {
00060             return m_table[Hash(key)].ValueFor(key);
00061         }
00062 
00063         bool Lookup(const KEY &key, VALUE &value) const
00064         {
00065             bool found;
00066             value = m_table[Hash(key)].ValueFor(key, &found);
00067             return found;
00068         }
00069 
00070         void Insert(const KEY &key, const VALUE &value)
00071         {
00072             m_table[Hash(key)].AddItem(key,value);
00073         }   
00074 
00075         void Remove(const KEY &key)
00076         {
00077             m_table[Hash(key)].RemoveItemFor(key);
00078         }   
00079 
00080     private:
00081 
00082         SKeyedVector<KEY,VALUE> *   m_table;
00083 };
00084 
00087 template<class KEY, class VALUE>
00088 SHashTable<KEY, VALUE>::SHashTable(int32_t bits) : SHasher(bits)
00089 {
00090     m_table = new SKeyedVector<KEY,VALUE> [1<<bits];
00091 }
00092 
00093 template<class KEY, class VALUE>
00094 SHashTable<KEY, VALUE>::~SHashTable()
00095 {
00096 }
00097 
00098 /**************************************************************************************/
00099 
00100 #if _SUPPORTS_NAMESPACE
00101 } } // namespace palmos::support
00102 #endif
00103 
00104 #endif  /* _SUPPORT_HASHTABLE_H_ */