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 WM4THASHSET_H 00018 #define WM4THASHSET_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. THashSet will use modular arithmetic to make this 00032 // happen. 00033 00034 #include "Wm4System.h" 00035 00036 namespace Wm4 00037 { 00038 00039 template <class TKEY> 00040 class THashSet 00041 { 00042 public: 00043 // construction and destruction 00044 THashSet (int iTableSize); 00045 ~THashSet (); 00046 00047 // element access 00048 int GetQuantity () const; 00049 00050 // A pointer to the actual storage is returned so that the caller has 00051 // direct access to it. This allows a subset of TKEY members to be used 00052 // in key comparison. 00053 TKEY* Insert (const TKEY& rtKey); 00054 00055 // If the input key exists, a pointer to the actual storage is returned. 00056 // This allows a subset of TKEY members to be used in key comparison, 00057 // but gives the caller a chance to modify other TKEY members. 00058 TKEY* Get (const TKEY& rtKey) const; 00059 00060 bool Remove (const TKEY& rtKey); 00061 void RemoveAll (); 00062 00063 // linear traversal of map 00064 TKEY* GetFirst () const; 00065 TKEY* GetNext () const; 00066 00067 // user-specified key-to-index construction 00068 int (*UserHashFunction)(const TKEY&); 00069 00070 private: 00071 class HashItem 00072 { 00073 public: 00074 TKEY m_tKey; 00075 HashItem* m_pkNext; 00076 }; 00077 00078 // Default key-to-index construction (override by user-specified when 00079 // requested). 00080 int HashFunction (const TKEY& rtKey) const; 00081 00082 // hash table 00083 int m_iTableSize; 00084 int m_iQuantity; 00085 HashItem** m_apkTable; 00086 00087 // iterator for traversal 00088 mutable int m_iIndex; 00089 mutable HashItem* m_pkItem; 00090 }; 00091 00092 } 00093 00094 #include "Wm4THashSet.inl" 00095 00096 #endif