00001 // Wild Magic Source Code 00002 // David Eberly 00003 // http://www.geometrictools.com 00004 // Copyright (c) 1998-2007 00005 // 00006 // This library is free software; you can redistribute it and/or modify it 00007 // under the terms of the GNU Lesser General Public License as published by 00008 // the Free Software Foundation; either version 2.1 of the License, or (at 00009 // your option) any later version. The license is available for reading at 00010 // either of the locations: 00011 // http://www.gnu.org/copyleft/lgpl.html 00012 // http://www.geometrictools.com/License/WildMagicLicense.pdf 00013 // The license applies to versions 0 through 4 of Wild Magic. 00014 // 00015 // Version: 4.0.0 (2006/06/28) 00016 00017 #ifndef WM4THASHTABLE_H 00018 #define WM4THASHTABLE_H 00019 00020 #include "Wm4FoundationLIB.h" 00021 00022 // The class TKEY is either native data or is class data that has the 00023 // following member functions: 00024 // TKEY::TKEY () 00025 // TKEY& TKEY::operator= (const TKEY&) 00026 // bool TKEY::operator== (const TKEY&) const 00027 // bool TKEY::operator!= (const TKEY&) const 00028 // TKEY::operator unsigned int () const 00029 // The implicit conversion to unsigned int is used to select a hash table 00030 // index for the T object. The return value need not be within the range of 00031 // hash table indices. THashTable will use modular arithmetic to make this 00032 // happen. 00033 // 00034 // The class TVALUE is either native data or is class data that has the 00035 // following member functions: 00036 // TVALUE::TVALUE () 00037 // TVALUE& TVALUE::operator= (const TVALUE&) 00038 00039 #include "Wm4System.h" 00040 00041 namespace Wm4 00042 { 00043 00044 template <class TKEY, class TVALUE> 00045 class THashTable 00046 { 00047 public: 00048 // construction and destruction 00049 THashTable (int iTableSize); 00050 ~THashTable (); 00051 00052 // element access 00053 int GetQuantity () const; 00054 00055 // insert a key-value pair into the hash table 00056 bool Insert (const TKEY& rtKey, const TVALUE& rtValue); 00057 00058 // search for a key and returns it value (null, if key does not exist) 00059 TVALUE* Find (const TKEY& rtKey) const; 00060 00061 // remove key-value pairs from the hash table 00062 bool Remove (const TKEY& rtKey); 00063 void RemoveAll (); 00064 00065 // linear traversal of table 00066 TVALUE* GetFirst (TKEY* ptKey) const; 00067 TVALUE* GetNext (TKEY* ptKey) const; 00068 00069 // user-specified key-to-index construction 00070 int (*UserHashFunction)(const TKEY&); 00071 00072 private: 00073 class HashItem 00074 { 00075 public: 00076 TKEY m_tKey; 00077 TVALUE m_tValue; 00078 HashItem* m_pkNext; 00079 }; 00080 00081 // Default key-to-index construction (override by user-specified when 00082 // requested). 00083 int HashFunction (const TKEY& rtKey) const; 00084 00085 // hash table 00086 int m_iTableSize; 00087 int m_iQuantity; 00088 HashItem** m_apkTable; 00089 00090 // iterator for traversal 00091 mutable int m_iIndex; 00092 mutable HashItem* m_pkItem; 00093 }; 00094 00095 } 00096 00097 #include "Wm4THashTable.inl" 00098 00099 #endif