00001 #ifndef __GXHASHTABLE
00002 #define __GXHASHTABLE
00003
00004
00005 #include "GXStandardDefines.h"
00006 #include "GXPrimitiveLL.h"
00007 #include "GXAutoArray.h"
00008
00009
00010
00011
00012
00013 template<typename type>
00014 class CGXHashTable
00015 {
00016 public:
00017 CGXHashTable()
00018 {
00019 m_uIteratorIndex=0;
00020 }
00021
00022 ~CGXHashTable()
00023 {
00024 }
00025
00026
00027 bool Init(UINT uHashSize)
00028 {
00029 assert(0!=uHashSize);
00030
00031 if(!m_Lists.Init(uHashSize))
00032 {
00033 return false;
00034 }
00035
00036 return true;
00037 }
00038
00039
00040 bool AddElement(type* pIn)
00041 {
00042 assert(pIn!=NULL);
00043 assert(m_Lists.GetCount()!=0);
00044
00045 UINT uHashIndex=(pIn->HashCode()) % (m_Lists.GetCount());
00046
00047
00048 CGXPrimitiveLL<type>* pTargetList=&(m_Lists.GetPtr()[uHashIndex]);
00049
00050 for (pTargetList->IteratorFirst(); pTargetList->IteratorHasMore(); )
00051 {
00052 type Next;
00053 if(!pTargetList->IteratorNext(&Next))
00054 {
00055 return false;
00056 }
00057
00058 if(*pIn==Next)
00059 {
00060 return true;
00061 }
00062 }
00063
00064
00065 if(!m_Lists.GetPtr()[uHashIndex].AddElement(pIn))
00066 {
00067 return false;
00068 }
00069
00070 return true;
00071 }
00072
00073 bool GetElement(type* pOut)
00074 {
00075 UINT uHashIndex=(pOut->HashCode()) % (m_Lists.GetCount());
00076
00077 CGXPrimitiveLL<type>* pTargetList=&(m_Lists.GetPtr()[uHashIndex]);
00078
00079
00080 for (pTargetList->IteratorFirst(); pTargetList->IteratorHasMore(); )
00081 {
00082 type Next;
00083 if(!pTargetList->IteratorNext(&Next))
00084 {
00085 return false;
00086 }
00087
00088 if(*pOut==Next)
00089 {
00090 if(!pOut->Assign(Next))
00091 {
00092 return false;
00093 }
00094
00095 return true;
00096 }
00097 }
00098 return false;
00099 }
00100
00101
00102
00103
00104 void IteratorFirst()
00105 {
00106 m_uIteratorIndex=0;
00107 if(m_Lists.GetCount()>0)
00108 {
00109 m_Lists.GetPtr()[m_uIteratorIndex].IteratorFirst();
00110 }
00111 }
00112
00113 bool IteratorHasMore()
00114 {
00115 if(m_uIteratorIndex>=m_Lists.GetCount())
00116 {
00117 return false;
00118 }
00119
00120 if(!m_Lists.GetPtr()[m_uIteratorIndex].IteratorHasMore())
00121 {
00122 m_uIteratorIndex++;
00123 return IteratorHasMore();
00124 }
00125
00126 return true;
00127 }
00128
00129 bool IteratorNext(type* pOut)
00130 {
00131 assert(pOut!=NULL);
00132
00133 if(!IteratorHasMore())
00134 {
00135 return false;
00136 }
00137
00138 return m_Lists.GetPtr()[m_uIteratorIndex].IteratorNext(pOut);
00139 }
00140
00141
00142
00143
00144
00145 protected:
00146 CGXAutoArray< CGXPrimitiveLL<type> > m_Lists;
00147 UINT m_uIteratorIndex;
00148 };
00149
00150
00151
00152 #endif