25BEGIN_MOOTOOLS_NAMESPACE
27 typedef unsigned int HashPos;
29 #define HashEnd (unsigned int)(-1)
30 #define MinimalHashSize 5
31 #define DefaultHashSize 23
33 #ifdef MOOTOOLS_PRIVATE_DEBUG
34 #define MOOTOOLS_HASH_ADVANCED_DEBUG
36 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
64 #undef DefaultHashSize
65 #define DefaultHashSize GetDefaultHashSize()
71 DLL_TOOLSFUNCTION
bool IsPrime(
unsigned int N);
74 DLL_TOOLSFUNCTION
unsigned int NextPrime(
unsigned int N);
94 inline unsigned int HashBuffer(
const wchar_t *
values,
longuint count)
111 template<
class TYPE>
inline longuint Hash64DefaultType(
TYPE key)
113 key = (~key) + (key << 21);
114 key = key ^ (key >> 24);
115 key = (key + (key << 3)) + (key << 8);
116 key = key ^ (key >> 14);
117 key = (key + (key << 2)) + (key << 4);
118 key = key ^ (key >> 28);
119 key = key + (key << 31);
123 template<
class TYPE>
inline unsigned int HashDefaultType(
TYPE key)
125 key = (~key) + (key << 18);
126 key = key ^ (key >> 31);
128 key = key ^ (key >> 11);
129 key = key + (key << 6);
130 key = key ^ (key >> 22);
131 return (
unsigned int)(key);
134 template<
class TYPE>
inline unsigned int HashMixValue(
TYPE value)
136 value += ~(value << 15);
137 value ^= (value >> 10);
138 value += (value << 3);
139 value ^= (value >> 6);
140 value += ~(value << 11);
141 value ^= (value >> 16);
148 static inline bool HashCompare(
const TYPE& a,
const TYPE& b);
149 static inline unsigned int HashValue(
const TYPE& a);
155 static inline bool HashCompare(
const TYPE *a,
const TYPE *b)
160 static inline unsigned int HashValue(
const TYPE *ptr)
162 return HashDefaultType((
longuint)(ptr));
169 static inline bool HashCompare(
unsigned int a,
unsigned int b)
174 static inline unsigned int HashValue(
unsigned int value)
185#if defined(__LINUX__) && (__WORDSIZE != 32)
189 static inline bool HashCompare(
unsigned long long a,
unsigned long long b)
196 static inline unsigned int HashValue(
unsigned long long key)
198 return HashDefaultType(key);
205 static inline bool HashCompare(
long long a,
long long b)
212 static inline unsigned int HashValue(
long long key)
214 return HashDefaultType(key);
229 static inline unsigned int HashValue(
longuint key)
231 return HashDefaultType(key);
243 static inline unsigned int HashValue(
longint key)
245 return HashDefaultType(key);
253 static inline bool HashCompare(
unsigned long a,
unsigned long b)
258 static inline unsigned int HashValue(
unsigned long value)
268 static inline bool HashCompare(
float a,
float b)
273 static inline unsigned int HashValue(
float value)
281 return *((
unsigned int *)&value);
288 static inline bool HashCompare(
double a,
double b)
293 static inline unsigned int HashValue(
double value)
301 return *((
unsigned int *)&value);
308 static inline unsigned int HashValue(
const CXStringW &Key)
322 static inline unsigned int HashValue(
const CXStringA &Key)
336 template<
class Etype,
typename HashFunctions = CHashMethods<Etype> >
class CHashTable
341 Active, Empty, Deleted
353 unsigned int GetHashTableSize()
const;
359 int Insert(
const Etype & X);
360 void InsertAndReplace(
const Etype & X);
362 int FindAndUpdate(
Etype & X);
363 int IsFound(
const Etype & X)
const;
367 unsigned int GetCount()
const;
368 HashPos GetFirst()
const;
371 Etype& GetNext(HashPos& pos)
const;
372 Etype *GetNextPtr(HashPos& pos)
const;
375 HashPos FindActiveElement(HashPos pos)
const;
387 unsigned int DefaultSize;
388 unsigned int ArraySize;
389 unsigned int CurrentSize;
390 unsigned int ActiveSize;
394 void AllocateArray();
396 unsigned int FindPos(
const Etype & X)
const;
402#ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
415 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
417 if (ArraySize > DefaultSize &&
stats.checkAllocAboveDefaultSize)
419 stats.checkAllocAboveDefaultSize =
false;
424 Array = xNewArray(HashEntry, ArraySize);
428 template<
class Etype,
typename HashFunctions>
433 ActiveSize = CurrentSize = 0;
440 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
446 template<
class Etype,
typename HashFunctions>
451 ActiveSize = CurrentSize = 0;
457#ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
465 template<
class Etype,
typename HashFunctions>
468 xDeleteArray(Array, ArraySize);
470 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
475 template<
class Etype,
typename HashFunctions>
496 XASSERT(ActiveSize == 0 && CurrentSize == 0);
498 xDeleteArray(Array, ArraySize);
503 ActiveSize = CurrentSize = 0;
506 template<
class Etype,
typename HashFunctions>
514 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
518 xDeleteArray(Array, ArraySize);
520 ArraySize = DefaultSize = DefaultHashSize;
521 ActiveSize = CurrentSize = 0;
528 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
532 if (ActiveSize == 0 && CurrentSize == 0)
535 ActiveSize = CurrentSize = 0;
536 for (
unsigned int i = 0;
i < ArraySize;
i++)
537 Array[
i].Info = Empty;
558 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
573 template<
class Etype,
typename HashFunctions>
577 unsigned int CurrentPos = HashFunctions::HashValue(X) % ArraySize;
584 if (HashFunctions::HashCompare(Array[
CurrentPos].Element, X))
599 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
600 if (
i >
stats.insertCollisionCount)
601 stats.insertCollisionCount =
i;
602 if (
i >
stats.rehashCollissionCount)
603 stats.rehashCollissionCount =
i;
628 XASSERT(ActiveSize >= 0);
641 HashPos pos = hash.GetFirst();
643 while (pos != HashEnd)
645 const Etype& value = hash.GetNext(pos);
652 if (GetCount() != hash.GetCount())
655 HashPos pos = hash.GetFirst();
657 while (pos != HashEnd)
659 value = hash.GetNextPtr(pos);
660 if (!IsFound(*value))
669 HashPos pos = hash.GetFirst();
672 while (pos != HashEnd)
674 value = hash.GetNextPtr(pos);
721 newSize = 2*(ActiveSize+1) < MinimalHashSize ? MinimalHashSize : 2*(ActiveSize+1);
725 MOOTOOLS_PRIVATE_ASSERT(CurrentSize < ArraySize/2);
731 if (CurrentSize < ArraySize/2)
734 float ratio = (ActiveSize / (
float)CurrentSize);
747 newSize = ArraySize < 251 ? 4*ArraySize : 2*ArraySize;
759 ActiveSize = CurrentSize = 0;
762 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
763 stats.rehashCollissionCount = 0;
775 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
778 if (
stats.rehashCollissionCount >
stats.rehashCollision)
779 stats.rehashCollision =
stats.rehashCollissionCount;
790 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
791 stats.insertCollisionCount = 0;
809 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
810 if (
stats.insertCollisionCount >
stats.insertCollision)
811 stats.insertCollision =
stats.insertCollisionCount;
814 if (CurrentSize < ArraySize/2)
817 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
826 template<
class Etype,
typename HashFunctions>
832 HashPos pos = GetFirst();
833 while (pos != HashEnd)
836 XASSERT(
i == GetCount());
840 template<
class Etype,
typename HashFunctions>
846 unsigned int i, size =
dstArray.GetSize();
847 for (
i=0;
i<size;
i++)
858 if (ArraySize !=
Rhs.ArraySize)
860 xDeleteArray(Array, ArraySize);
861 ArraySize =
Rhs.ArraySize;
865 for (
unsigned int i = 0;
i < ArraySize;
i++)
867 Array[
i].Info =
Rhs.Array[
i].Info;
868 if (Array[
i].Info == Active)
869 Array[
i].Element =
Rhs.Array[
i].Element;
872 DefaultSize =
Rhs.DefaultSize;
873 CurrentSize =
Rhs.CurrentSize;
874 ActiveSize =
Rhs.ActiveSize;
882 return (ActiveSize == 0);
886 template<
class Etype,
typename HashFunctions>
892 #ifdef MOOTOOLS_HASH_ADVANCED_DEBUG
895 if (CurrentSize == 0)
898 unsigned int size1 = 0;
899 unsigned int size2 = 0;
902 for (
unsigned int i = 0;
i < ArraySize;
i++)
904 if (Array[
i].Info == Active)
906 if (Array[
i].Info != Empty)
910 XASSERT(ActiveSize ==
size1);
911 XASSERT(CurrentSize ==
size2);
920 for (
unsigned int i = pos;
i < ArraySize;
i++)
922 if (Array[
i].Info == Active)
935 return FindActiveElement(0);
940 XASSERT(pos < ArraySize);
941 XASSERT(Array[pos].Info == Active);
946 pos = FindActiveElement(pos + 1);
962 XASSERT(pos < ArraySize);
963 XASSERT(Array[pos].Info == Active);
968 pos = FindActiveElement(pos + 1);
975 XASSERT(pos < ArraySize);
976 XASSERT(Array[pos].Info == Active);
981 pos = FindActiveElement(pos + 1);
983 return &Array[
prevpos].Element;
986 template<
class Etype,
typename HashFunctions>
989 if (
Rhs1.GetCount() !=
Rhs2.GetCount())
993 HashPos pos =
Rhs1.GetFirst();
994 while (pos != HashEnd)
996 key =
Rhs1.GetNextPtr(pos);
997 if (!
Rhs2.IsFound(*key))
1004 END_MOOTOOLS_NAMESPACE
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
CHashTable is a template class that implements an hash table.
Definition Hash.h:337
int GetLength() const
returns the length of the buffer in characters (can be greater than strlen, if set by GetBufferSetLen...
Definition XString.h:749