12#ifndef SCRIPTS_INCLUDE__ENGINE_VECTOR_H
13#define SCRIPTS_INCLUDE__ENGINE_VECTOR_H
14#include "engine_threading.h"
15#include "engine_common.h"
18class FastCriticalSectionClass;
20template <
typename T>
class NoEqualsClass
23 bool operator== (
const T &src)
27 bool operator!= (
const T &src)
35 void operator () (
void)
const {};
37template <
class T>
class VectorClass {
43 bool VectorClassPad[2];
45 VectorClass(NoInitClass
const &)
48 explicit VectorClass(
int size=0, T
const * array=0) : Vector(0),VectorMax(size),IsValid(true),IsAllocated(false)
54 Vector =
new((
void*)array) T[size];
63 VectorClass(VectorClass<T>
const & vector) : Vector(0),VectorMax(0),IsValid(true),IsAllocated(false)
67 VectorClass<T> &operator= (VectorClass<T>
const &vector)
72 VectorMax = vector.Length();
75 Vector =
new T[VectorMax];
79 for (
int index = 0; index < VectorMax; index++)
81 Vector[index] = vector[index];
96 VectorClass(VectorClass<T>&& vector): Vector(vector.Vector), VectorMax(vector.VectorMax), IsValid(vector.IsValid), IsAllocated(vector.IsAllocated)
101 VectorClass<T>& operator=(VectorClass<T>&& vector)
106 Vector = vector.Vector;
107 VectorMax = vector.VectorMax;
108 IsValid = vector.IsValid;
109 IsAllocated = vector.IsAllocated;
116 virtual ~VectorClass()
121 T & operator[](
int index)
123 return(Vector[index]);
126 T
const & operator[](
int index)
const
128 return(Vector[index]);
131 virtual bool operator== (VectorClass<T>
const &vector)
const
133 if (VectorMax == vector.Length())
135 for (
int index = 0; index < VectorMax; index++)
137 if (Vector[index] != vector[index])
146 virtual bool Resize(
int newsize, T
const * array=0)
154 newptr =
new T[newsize];
158 newptr =
new((
void*)array) T[newsize];
167 int copycount = (newsize < VectorMax) ? newsize : VectorMax;
168 for (
int index = 0; index < copycount; index++)
170 newptr[index] = std::move(Vector[index]);
180 IsAllocated = (Vector && !array);
188 virtual void Clear(
void)
192 if (IsAllocated)
delete[] Vector;
198 int Length(
void)
const
202 virtual int ID(T
const *ptr)
208 return(((
unsigned long)ptr - (
unsigned long)&(*
this)[0]) /
sizeof(T));
210 virtual int ID(T
const &
object)
216 for (
int index = 0; index < VectorMax; index++)
218 if ((*
this)[index] ==
object)
231 const T* begin()
const
237template <
class T>
class DynamicVectorClass :
public VectorClass<T> {
242 explicit DynamicVectorClass(
unsigned size=0, T
const *array = 0) : VectorClass<T>(size, array)
248 DynamicVectorClass(
const DynamicVectorClass<T>& vector): VectorClass<T>(vector), GrowthStep(vector.GrowthStep), ActiveCount(vector.ActiveCount)
253 DynamicVectorClass<T> & operator =(DynamicVectorClass<T>
const &rvalue)
255 VectorClass<T>::operator =(rvalue);
256 ActiveCount = rvalue.ActiveCount;
257 GrowthStep = rvalue.GrowthStep;
263 DynamicVectorClass(DynamicVectorClass<T>&& vector): VectorClass<T>(std::move(vector)), GrowthStep(vector.GrowthStep), ActiveCount(vector.ActiveCount)
268 DynamicVectorClass<T>& operator=(DynamicVectorClass<T>&& vector)
272 VectorClass<T>::operator =(std::move(vector));
273 ActiveCount = vector.ActiveCount;
274 GrowthStep = vector.GrowthStep;
280 bool operator== (
const DynamicVectorClass &src)
284 bool operator!= (
const DynamicVectorClass &src)
288 bool Resize(
int newsize, T
const *array = 0)
290 if (VectorClass<T>::Resize(newsize, array))
292 if (Length() < ActiveCount)
294 ActiveCount = Length();
303 VectorClass<T>::Clear();
305 void Reset_Active(
void)
309 void Set_Active(
int count)
313 int Count(
void)
const
317 bool Add(T
const &
object)
319 if (ActiveCount >= Length())
321 if ((IsAllocated || !VectorMax) && GrowthStep > 0)
323 if (!Resize(Length() + GrowthStep))
333 (*this)[ActiveCount++] = object;
336 bool Add_Head(T
const &
object)
338 return Insert(0,
object);
340 bool Insert(
int index,T
const &
object)
346 if (index > ActiveCount)
350 if (ActiveCount >= Length())
352 if ((IsAllocated || !VectorMax) && GrowthStep > 0)
354 if (!Resize(Length() + GrowthStep))
364 for (
int i = ActiveCount; i > index; --i)
366 Vector[i] = Vector[i-1];
368 (*this)[index] = object;
377 if (ActiveCount >= Length())
379 if ((IsAllocated || !VectorMax) && GrowthStep > 0)
381 if (!Resize(Length() + GrowthStep))
391 (*this)[ActiveCount++] = std::move(
object);
395 bool Add_Head(T&&
object)
397 return Insert(0, std::move(
object));
400 bool Insert(
int index, T&&
object)
406 if (index > ActiveCount)
410 if (ActiveCount >= Length())
412 if ((IsAllocated || !VectorMax) && GrowthStep > 0)
414 if (!Resize(Length() + GrowthStep))
424 for (
int i = ActiveCount; i > index; --i)
426 Vector[i] = std::move(Vector[i-1]);
428 (*this)[index] = std::move(
object);
434 bool DeleteObj(T
const &
object)
443 bool Delete(
int index)
445 if (index < ActiveCount)
448 for (
int i = index; i < ActiveCount; i++)
450 (*this)[i] = std::move((*
this)[i+1]);
456 void Delete_All(
void)
462 int Set_Growth_Step(
int step)
464 return(GrowthStep = step);
466 int Growth_Step(
void)
470 virtual int ID(T
const *ptr)
472 return(VectorClass<T>::ID(ptr));
474 virtual int ID(T
const &
object)
476 for (
int index = 0; index < Count(); index++)
478 if ((*
this)[index] == object)
485 T *Uninitialized_Add(
void)
487 if (ActiveCount >= Length())
491 if (!Resize(Length() + GrowthStep))
501 return &((*this)[ActiveCount++]);
503 void Add_Multiple(
const DynamicVectorClass<T>& elements)
505 Add_Multiple(elements.begin(), elements.Count());
507 void Add_Multiple(
const T* elements,
int count)
509 int newcount = ActiveCount + count;
512 for (
int i = 0; i < count; i++)
514 Vector[ActiveCount + i] = elements[i];
516 ActiveCount = newcount;
518 void Add_Multiple(
int number_to_add)
520 int newcount = ActiveCount + number_to_add;
522 ActiveCount = newcount;
530 int len = VectorClass<T>::Length();
531 int newlen = len + (len >> 1);
532 if (newlen < 10) newlen = 10;
535 else if (GrowthStep > 0)
537 return Grow(VectorClass<T>::Length() + GrowthStep);
542 bool Grow(
int newlen)
544 if (newlen > Length() && (VectorClass<T>::IsAllocated || !VectorClass<T>::VectorMax))
546 return Resize(newlen);
553template <
typename T>
class PointerStack
556 inline PointerStack(T* initial_item)
558 m_pStack[0] = initial_item;
564 TT_ASSERT(m_iDepth >= 0);
565 return m_pStack[--m_iDepth];
568 inline void Push(T* data)
570 TT_ASSERT(m_iDepth < 128);
571 if (m_iDepth >= 128)
return;
572 m_pStack[m_iDepth++] = data;
585template <
class T>
class SimpleVecClass {
590 explicit SimpleVecClass(
int size = 0)
599 virtual ~SimpleVecClass()
609 SimpleVecClass(
const SimpleVecClass<T>& vector)
611 Vector = (T*)(
new char[vector.VectorMax *
sizeof(T)]);
612 VectorMax = vector.VectorMax;
613 memcpy(Vector,vector.Vector, VectorMax *
sizeof(T));
616 SimpleVecClass<T>& operator =(
const SimpleVecClass<T>& vector)
621 Vector = (T*)(
new char[vector.VectorMax *
sizeof(T)]);
622 VectorMax = vector.VectorMax;
623 memcpy(Vector,vector.Vector, VectorMax *
sizeof(T));
630 SimpleVecClass(SimpleVecClass<T>&& vector): Vector(vector.Vector), VectorMax(vector.VectorMax)
635 SimpleVecClass<T>& operator =(SimpleVecClass<T>&& vector)
640 Vector = vector.Vector;
641 VectorMax = vector.VectorMax;
648 virtual bool Resize(
int newsize)
650 if (VectorMax == newsize)
656 T *vec =
new T[newsize];
659 int count = VectorMax;
664 memcpy(vec,Vector,count*
sizeof(T));
682 virtual bool Uninitialised_Grow(
int newsize)
684 if (newsize <= VectorMax)
694 Vector =
new T[newsize];
700 void Uninitialized_Resize(
int newsize)
702 TT_ASSERT(newsize > 0);
704 Vector =
new T[newsize];
712 T &operator[](
int index)
714 return Vector[index];
716 T
const &operator[](
int index)
const
718 return Vector[index];
726 const T* Peek()
const
735 memset(Vector,0,VectorMax *
sizeof(T));
742template <
class T>
class SimpleDynVecClass :
743 public SimpleVecClass<T>
750 bool Grow(
int new_size_hint)
752 int new_size = max(VectorMax + VectorMax/4,VectorMax + 4);
753 new_size = max(new_size,new_size_hint);
754 return Resize(new_size);
758 if (ActiveCount < VectorMax/4)
760 return Resize(ActiveCount);
766 virtual ~SimpleDynVecClass()
770 explicit SimpleDynVecClass(
int size = 0) : SimpleVecClass<T>(size)
775 SimpleDynVecClass(
const SimpleDynVecClass<T>& vector): SimpleVecClass(vector), ActiveCount(vector.ActiveCount)
780 SimpleDynVecClass<T>& operator =(
const SimpleDynVecClass<T>& vector)
784 SimpleVecClass<T>::operator =(vector);
785 ActiveCount = vector.ActiveCount;
792 SimpleDynVecClass(SimpleDynVecClass<T>&& vector): SimpleVecClass(std::move(vector)), ActiveCount(vector.ActiveCount)
797 SimpleDynVecClass<T>& operator =(SimpleDynVecClass<T>&& vector)
801 SimpleVecClass::operator =(std::move(vector));
802 ActiveCount = vector.ActiveCount;
808 int Find_Index(T
const &
object)
810 for (
int index = 0;index < Count();index++)
812 if (Vector[index] ==
object)
824 T &operator[](
int index)
826 return Vector[index];
828 T
const &operator[](
int index)
const
830 return Vector[index];
832 bool Resize(
int newsize)
834 if (SimpleVecClass<T>::Resize(newsize))
836 if (VectorMax < ActiveCount)
838 ActiveCount = VectorMax;
844 bool Add(T
const& data,
int new_size_hint = 0)
846 if (ActiveCount >= VectorMax)
848 if (!Grow(new_size_hint))
853 Vector[ActiveCount++] = data;
856 T *Add_Multiple(
int number_to_add)
858 int index = ActiveCount;
859 ActiveCount += number_to_add;
860 if (ActiveCount > VectorMax)
864 return &Vector[index];
866 void Add_Multiple(
const SimpleDynVecClass<T>& elements)
868 Add_Multiple(elements.begin(), elements.Count());
870 void Add_Multiple(
const T* elements,
int count)
872 int newcount = ActiveCount + count;
873 if (newcount > VectorMax) Grow(newcount);
874 memcpy(Vector + ActiveCount, elements, count *
sizeof(T));
875 ActiveCount = newcount;
877 bool Add_Head(
const T&
object)
879 return Insert(0,
object);
881 bool Insert(
int index,
const T&
object)
883 TT_ASSERT(index >= 0 && index <= ActiveCount);
884 if (ActiveCount >= VectorMax)
891 if (index < ActiveCount)
893 memmove(&Vector[index+1], &Vector[index], (ActiveCount-index) *
sizeof(T));
895 Vector[index] = object;
899 bool Delete(
int index,
bool allow_shrink =
true)
901 if (index < ActiveCount-1)
903 memmove(&(Vector[index]),&(Vector[index+1]),(ActiveCount - index - 1) *
sizeof(T));
912 bool Delete(T
const &
object,
bool allow_shrink =
true)
914 int id = Find_Index(
object);
917 return Delete(
id,allow_shrink);
921 bool Delete_Range(
int start,
int count,
bool allow_shrink =
true)
923 if (start < ActiveCount - count)
925 memmove(&(Vector[start]),&(Vector[start + count]),(ActiveCount - start - count) *
sizeof(T));
927 ActiveCount -= count;
934 void Delete_All(
bool allow_shrink =
true)
943 void qsort(
int (*compareCallback)(
const T&,
const T&))
945 ::qsort(Vector, ActiveCount,
sizeof(T), (
int (*)(
const void*,
const void*))compareCallback);
948 bool isEmpty()
const {
return ActiveCount == 0; }
956 GenericNode(
void) : NextNode(0), PrevNode(0) {}
957 virtual ~GenericNode(
void) {Unlink();}
958 GenericNode(GenericNode & node) {node.Link(
this);}
959 GenericNode & operator = (GenericNode & node)
971 PrevNode->NextNode = NextNode;
972 NextNode->PrevNode = PrevNode;
977 GenericList * Main_List(
void)
const
979 GenericNode
const * node =
this;
980 while (node->PrevNode)
984 return((GenericList *)
this);
986 void Link(GenericNode * node)
988 TT_ASSERT(node != (GenericNode *)0);
990 node->NextNode = NextNode;
991 node->PrevNode =
this;
992 if (NextNode) NextNode->PrevNode = node;
995 GenericNode * Next(
void)
const {
return(NextNode);}
996 GenericNode * Next_Valid(
void)
const
998 return ((NextNode && NextNode->NextNode) ? NextNode : (GenericNode *)0);
1000 GenericNode * Prev(
void)
const {
return(PrevNode);}
1001 GenericNode * Prev_Valid(
void)
const
1003 return ((PrevNode && PrevNode->PrevNode) ? PrevNode : (GenericNode *)0);
1005 bool Is_Valid(
void)
const {
return(
this != (GenericNode *)0 && NextNode != (GenericNode *)0 && PrevNode != (GenericNode *)0);}
1007 GenericNode * NextNode;
1008 GenericNode * PrevNode;
1014 FirstNode.Link(&LastNode);
1016 virtual ~GenericList(
void)
1018 while (FirstNode.Next()->Is_Valid())
1020 FirstNode.Next()->Unlink();
1023 GenericNode * First(
void)
const {
return(FirstNode.Next());}
1024 GenericNode * First_Valid(
void)
const
1026 GenericNode *node = FirstNode.Next();
1027 return (node->Next() ? node : (GenericNode *)0);
1029 GenericNode * Last(
void)
const {
return(LastNode.Prev());}
1030 GenericNode * Last_Valid(
void)
const
1032 GenericNode *node = LastNode.Prev();
1033 return (node->Prev() ? node : (GenericNode *)0);
1035 bool Is_Empty(
void)
const {
return(!FirstNode.Next()->Is_Valid());}
1036 void Add_Head(GenericNode * node) {FirstNode.Link(node);}
1037 void Add_Tail(GenericNode * node) {LastNode.Prev()->Link(node);}
1038 int Get_Valid_Count(
void)
const
1040 GenericNode * node = First_Valid();
1045 node = node->Next_Valid();
1050 GenericNode FirstNode;
1051 GenericNode LastNode;
1053 GenericList(GenericList & list);
1054 GenericList & operator = (GenericList
const &);
1056template<
class T>
class List;
1058class Node :
public GenericNode
1061 List<T> * Main_List(
void)
const {
return((List<T> *)GenericNode::Main_List());}
1062 T Next(
void)
const {
return((T)GenericNode::Next());}
1063 T Next_Valid(
void)
const {
return((T)GenericNode::Next_Valid());}
1064 T Prev(
void)
const {
return((T)GenericNode::Prev());}
1065 T Prev_Valid(
void)
const {
return((T)GenericNode::Prev_Valid());}
1066 bool Is_Valid(
void)
const {
return(GenericNode::Is_Valid());}
1069class List :
public GenericList
1073 T First(
void)
const {
return((T)GenericList::First());}
1074 T First_Valid(
void)
const {
return((T)GenericList::First_Valid());}
1075 T Last(
void)
const {
return((T)GenericList::Last());}
1076 T Last_Valid(
void)
const {
return((T)GenericList::Last_Valid());}
1077 void Delete(
void) {
while (First()->Is_Valid())
delete First();}
1079 List(List<T>
const & rvalue);
1080 List<T> operator = (List<T>
const & rvalue);
1083class DataNode :
public GenericNode
1088 DataNode(T value) { Set(value); };
1089 void Set(T value) { Value = value; };
1090 T Get()
const {
return Value; };
1091 DataNode<T> * Next(
void)
const {
return (DataNode<T> *)GenericNode::Next(); }
1092 DataNode<T> * Next_Valid(
void)
const {
return (DataNode<T> *)GenericNode::Next_Valid(); }
1093 DataNode<T> * Prev(
void)
const {
return (DataNode<T> *)GenericNode::Prev(); }
1094 DataNode<T> * Prev_Valid(
void)
const {
return (DataNode<T> *)GenericNode::Prev_Valid(); }
1096template<
class C,
class D>
1097class ContextDataNode :
public DataNode<D>
1101 ContextDataNode() {};
1102 ContextDataNode(C context, D data) { Set_Context(context); Set(data); }
1103 void Set_Context(C context) { Context = context; };
1104 C Get_Context() {
return Context; };
1106template<
class C,
class D>
1107class SafeContextDataNode :
public ContextDataNode<C,D>
1110 SafeContextDataNode(C context, D data) : ContextDataNode<C,D>(context, data) { }
1112 SafeContextDataNode();
1114template<
class PRIMARY,
class SECONDARY>
1117 void Initialize() { Primary.Set(
this); Secondary.Set(
this); };
1118 PRIMARY PrimaryValue;
1119 SECONDARY SecondaryValue;
1121 typedef DoubleNode<PRIMARY, SECONDARY> Type;
1122 DataNode<Type *> Primary;
1123 DataNode<Type *> Secondary;
1124 DoubleNode() { Initialize(); };
1125 DoubleNode(PRIMARY primary, SECONDARY secondary) { Initialize(); Set_Primary(primary); Set_Secondary(secondary); };
1126 void Set_Primary(PRIMARY value) { PrimaryValue = value; };
1127 void Set_Secondary(SECONDARY value) { SecondaryValue = value; };
1128 PRIMARY Get_Primary() {
return PrimaryValue; };
1129 SECONDARY Get_Secondary() {
return SecondaryValue; };
1130 void Unlink() { Primary.Unlink(); Secondary.Unlink(); };
1133template <
typename T1,
class T2>
class IndexClass {
1134 struct NodeElement {
1137 NodeElement(T1
const &
id, T2
const &data) : ID(id), Data(data)
1140 NodeElement() : ID(0), Data(0)
1143 bool operator==(NodeElement
const &elem)
1145 return ID == elem.ID;
1147 bool operator<(NodeElement
const &elem)
1149 return ID < elem.ID;
1152 NodeElement* IndexTable;
1155 unsigned char IsSorted;
1156 const NodeElement* Archive;
1158 IndexClass() : IndexTable(0), IndexCount(0), IndexSize(0), IsSorted(false), Archive(0)
1160 Invalidate_Archive();
1166 bool Remove_Index(
const T1 &ID)
1169 for (
int i = 0;i < IndexCount;i++)
1171 if (IndexTable[i].ID == ID)
1183 for (
int i = pos;i < IndexCount;i++)
1185 IndexTable[i] = IndexTable[i+1];
1189 IndexTable[IndexCount].ID = 0;
1190 IndexTable[IndexCount].Data = 0;
1191 Invalidate_Archive();
1194 bool Is_Present(
const T1 &ID)
1198 if (Is_Archive_Same(ID))
1204 NodeElement *node = Search_For_Node(ID);
1218 bool Add_Index(
const T1 &ID,
const T2 &Data)
1220 if (IndexCount + 1 <= IndexSize)
1222 IndexTable[IndexCount].ID = ID;
1223 IndexTable[IndexCount++].Data = Data;
1227 int size = IndexSize;
1232 if (Increase_Table_Size(size))
1234 IndexTable[IndexCount].ID = ID;
1235 IndexTable[IndexCount++].Data = Data;
1248 const T2 &operator[](T1
const &ID)
1253 return Archive->Data;
1257 void Invalidate_Archive()
1265 delete[] IndexTable;
1271 Invalidate_Archive();
1273 bool Is_Archive_Same(
const T1 &ID)
1275 return Archive && Archive->ID == ID;
1277 NodeElement *Search_For_Node(
const T1 &ID)
1283 qsort(IndexTable,IndexCount,
sizeof(NodeElement),search_compfunc);
1284 Invalidate_Archive();
1287 NodeElement elem(ID,0);
1288 return Binary_Search<NodeElement>(IndexTable,IndexCount,elem);
1292 void Set_Archive(NodeElement
const *archive)
1296 bool Increase_Table_Size(
int amount)
1300 int newsize = IndexSize + amount;
1301 NodeElement *newindex =
new NodeElement[newsize];
1304 TT_ASSERT(IndexCount < newsize);
1305 for (
int i = 0;i < this->IndexCount;i++)
1307 newindex[i].ID = IndexTable[i].ID;
1308 newindex[i].Data = IndexTable[i].Data;
1311 delete[] IndexTable;
1312 IndexTable = newindex;
1313 IndexSize += amount;
1314 Invalidate_Archive();
1320 T1 Fetch_ID_By_Position(
int position)
1322 return IndexTable[position].ID;
1324 T2 Fetch_By_Position(
int position)
1326 return IndexTable[position].Data;
1328 static int search_compfunc(
void const *ptr2,
void const *ptr1)
1330 if (*(NodeElement *)ptr2 == *(NodeElement *)ptr1)
1336 if (*(NodeElement *)ptr1 < *(NodeElement *)ptr2)
1347template <
typename T> T *Binary_Search(T *list,
int count,T &var)
1353 T *list3 = &list2[pos / 2];
1354 if (var.ID >= list3->ID)
1356 if (list3->ID == var.ID)
1358 return &list2[pos / 2];
1361 pos = pos - pos / 2 - 1;
1371template<
typename T,
int BLOCK_SIZE>
class ObjectPoolClass
1377 void* BlockListHead;
1378 int FreeObjectCount;
1379 int TotalObjectCount;
1380 FastCriticalSectionClass ObjectPoolCS;
1388 FreeObjectCount = 0;
1389 TotalObjectCount = 0;
1395 if (FreeObjectCount != TotalObjectCount)
1398 sprintf(buffer,
"%d memory leaks found in " __FUNCTION__
"\n", TotalObjectCount - FreeObjectCount);
1399 OutputDebugStringA(buffer);
1403 void* block = BlockListHead;
1406 void* nextBlock = *(
void**)block;
1412 T* Allocate_Object_Memory()
1414 FastCriticalSectionClass::LockClass lock(ObjectPoolCS);
1417 void* newBlockListHead = (
void*)((
void*)
new char[
sizeof(
void*) +
sizeof(T[BLOCK_SIZE])]);
1418 *(
void**)newBlockListHead = BlockListHead;
1419 BlockListHead = newBlockListHead;
1420 FreeListHead = (T*)((intptr_t)BlockListHead +
sizeof(
void*));
1421 for (
int i = 0; i < BLOCK_SIZE; i++)
1422 (T*&)FreeListHead[i] = &FreeListHead[i+1];
1424 (T*&)FreeListHead[BLOCK_SIZE-1] = NULL;
1425 FreeObjectCount += BLOCK_SIZE;
1426 TotalObjectCount += BLOCK_SIZE;
1429 T*
object = FreeListHead;
1430 FreeListHead = *(T**)(FreeListHead);
1435 void Free_Object_Memory(T&
object)
1437 FastCriticalSectionClass::LockClass lock(ObjectPoolCS);
1438 writeDummyPattern(
object, 0xEDE7E10D);
1439 (T*&)
object = FreeListHead;
1440 FreeListHead = &object;
1444 void writeDummyPattern(T&
object, DWORD pattern)
1447 static_assert(
sizeof(T) % 4 == 0,
"Expected type size to be a multiple of 4.");
1448 DWORD* dword = (DWORD*)&
object;
1449 DWORD* endDword = (DWORD*)(&
object+1);
1450 for (; dword < endDword; ++dword)
1458#define objectPoolClass(T, nAlign) (Singleton<ObjectPoolClass<T, nAlign>>::getInstance())
1460#pragma push_macro("new")
1461#pragma push_macro("delete")
1466template<
typename T, u
int32 nAlign>
1472 static void*
operator new[](
size_t size,
const char* file = NULL, uint line = 0,
const char* function = NULL);
1473 static void operator delete[](
void* object);
1474 static void operator delete[](
void* object,
const char* file, uint line,
const char* function);
1478 static void*
operator new(
size_t size,
const char* file = NULL, uint line = 0,
const char* function = NULL)
1480 TT_ASSERT(size ==
sizeof(T));
1481 return (
void*)objectPoolClass(T, nAlign).Allocate_Object_Memory();
1484 static void operator delete(
void* object)
1487 objectPoolClass(T, nAlign).Free_Object_Memory(*(T*)
object);
1490 static void operator delete(
void* object,
const char* file, uint line,
const char* function)
1493 objectPoolClass(T, nAlign).Free_Object_Memory(*(T*)
object);
1498#pragma pop_macro("new")
1499#pragma pop_macro("delete")
1501class RefCountClass {
1505 RefCountClass() : NumRefs(1)
1508 RefCountClass(
const RefCountClass &) : NumRefs(1)
1511 virtual void Delete_This()
1515 TT_INLINE
void Release_Ref()
1517 TT_ASSERT(NumRefs > 0);
1521 TT_INLINE
void Add_Ref()
1525 TT_INLINE
void Release()
1534 virtual ~RefCountClass()
1537 TT_ASSERT(NumRefs == 0);
1541template <
class T>
class ShareBufferClass :
public RefCountClass {
1543 ShareBufferClass(
int count) : Count(count)
1545 Array =
new T[Count];
1547 ShareBufferClass(
const ShareBufferClass & that) : Count(that.Count)
1549 Array =
new T[Count];
1550 for (
int i = 0;i < Count;i++)
1552 Array[i] = that.Array[i];
1555 ~ShareBufferClass(
void)
1571 void Set_Element(
int index,
const T & thing)
1573 Array[index] = thing;
1575 const T &Get_Element(
int index)
const
1577 return Array[index];
1579 T &Get_Element(
int index)
1581 return Array[index];
1585 memset(Array,0,Count *
sizeof(T));
1590 ShareBufferClass & operator = (
const ShareBufferClass &);
1593template <
typename K,
typename T>
class PtrHashTable
1598 template <
typename K,
typename T>
struct Node
1603 Node(
const Node<K,T>& v){};
1621 Node<K,T>* Vector[4096];
1626 Node<K,T>* GetLast(Node<K,T>* node)
const
1631 while (node->Next != NULL)
1638 Node<K,T>* GetNodeWithNext(Node<K,T>* node, Node<K,T>* find)
const
1642 if (node->Next == find)
1652 Node<K,T>* GetFreeNode()
1654 Node<K,T>* node = Free;
1661 node =
new Node<K,T>;
1669 Node<K,T>* GetFreeNodeNoAlloc()
1671 Node<K,T>* node = Free;
1679 void AddFreeNode(Node<K,T>* node)
1686 static unsigned int GetIndex(K key)
1688 return (
reinterpret_cast<unsigned int>(
const_cast<K
>(key)) >> 4) & (4096 - 1);
1696 memset(Vector, 0x00,
sizeof(Node<K,T>*) * 4096);
1704 while (Node<K,T>* node = GetFreeNodeNoAlloc())
1707 for (
unsigned int i = 0; i < 4096; ++i)
1709 Node<K,T>* node = Vector[i];
1712 Node<K,T>* next = node->Next;
1722 for (
unsigned int i = 0; i < 4096; ++i)
1724 Node<K,T>* node = Vector[i];
1727 Node<K,T>* next = node->Next;
1739 unsigned int index = GetIndex(key);
1740 Node<K,T>* node = Vector[index];
1741 Node<K,T>** prevNextPtr = &Vector[index];
1745 if (node->Key == key)
1747 *prevNextPtr = node->Next;
1754 prevNextPtr = &node->Next;
1760 void Add(K key, T value)
1762 unsigned int index = GetIndex(key);
1764 Node<K,T>* free = GetFreeNode();
1766 free->Value = value;
1767 free->Next = Vector[index];
1768 Vector[index] = free;
1774 for (Node<K,T>* node = Vector[GetIndex(key)]; node; node = node->Next)
1775 if (node->Key == key)
1784 for (uint i = 0; i < _countof(Vector); i++)
1787 for (Node<K,T>* node = Vector[i]; node; node = node->Next)
1789 TT_ASSERT(node != node->Next);
1790 TT_ASSERT(limit-- != 0);
1799#define DEFINE_AUTO_POOL(T, BLOCKSIZE) ObjectPoolClass<T, BLOCKSIZE> AutoPoolClass<T,BLOCKSIZE>::Allocator;
1803#define REF_PTR_SET(dst,src) { if (src) (src)->Add_Ref(); if (dst) (dst)->Release_Ref(); (dst) = (src); }
1804#define REF_PTR_RELEASE(x) { if (x) x->Release_Ref(); x = NULL; }
1805#define REF_PTR_DTOR(x) { if (x) x->Release_Ref(); }
1807template <
typename T>
inline T* REF_PTR_NEW(T* src)
1814#include "multilist.h"