17 #ifndef INC_KY_Kernel_ArrayPaged_H
18 #define INC_KY_Kernel_ArrayPaged_H
31 class ConstructorPagedPOD :
public ConstructorPOD<T>
34 static void ConstructArrayPaged(T**, UPInt, UPInt, UPInt, UPInt) {}
35 static void DestructArrayPaged (T**, UPInt, UPInt, UPInt, UPInt) {}
44 class ConstructorPagedMov :
public ConstructorMov<T>
47 static void ConstructArrayPaged(T** pages, UPInt start, UPInt end, UPInt pageShift, UPInt pageMask)
49 for (UPInt i = start; i < end; ++i)
51 ConstructorMov<T>::Construct(pages[i >> pageShift] + (i & pageMask));
55 static void DestructArrayPaged(T** pages, UPInt start, UPInt end, UPInt pageShift, UPInt pageMask)
57 for (UPInt i = end; i > start; --i)
59 ConstructorMov<T>::Destruct(pages[(i-1) >> pageShift] + ((i-1) & pageMask));
70 class ConstructorPagedMovCC :
public ConstructorMov<T>
73 const T& GetDefaultValue()
const;
74 void ConstructArrayPaged(T** pages, UPInt start, UPInt end, UPInt pageShift, UPInt pageMask)
76 for (UPInt i = start; i < end; ++i)
78 ConstructorMov<T>::ConstructAlt(pages[i >> pageShift] + (i & pageMask), GetDefaultValue());
82 static void DestructArrayPaged(T** pages, UPInt start, UPInt end, UPInt pageShift, UPInt pageMask)
84 for (UPInt i = end; i > start; --i)
86 ConstructorMov<T>::Destruct(pages[(i-1) >> pageShift] + ((i-1) & pageMask));
96 template<
class T,
int SID>
struct AllocatorPagedGH_POD : AllocatorBaseGH<SID>, ConstructorPagedPOD<T> {};
97 template<
class T,
int SID>
struct AllocatorPagedGH : AllocatorBaseGH<SID>, ConstructorPagedMov<T> {};
99 template<
class T,
int SID>
struct AllocatorPagedLH_POD : AllocatorBaseLH<SID>, ConstructorPagedPOD<T> {};
100 template<
class T,
int SID>
struct AllocatorPagedLH : AllocatorBaseLH<SID>, ConstructorPagedMov<T> {};
102 template<
typename T,
int SID>
struct AllocatorPagedCC : AllocatorBaseLH<SID>, ConstructorPagedMovCC<T> {};
124 template<
class T,
int PageSh,
int PtrPoolInc,
class Allocator>
125 class ArrayPagedBase :
public Allocator
131 PageSize = 1 << PageShift,
132 PageMask = PageSize - 1
135 typedef ArrayPagedBase<T, PageSh, PtrPoolInc, Allocator> SelfType;
137 typedef Allocator AllocatorType;
151 void ClearAndRelease()
155 T** blk = Pages + NumPages - 1;
157 const UPInt numDataPages = (Size > 0 ? Size >> PageShift : 0);
160 const UPInt freeCount = (NumPages < numDataPages ? PageSize : (NumPages == numDataPages ? Size & PageMask : 0));
161 Allocator::DestructArray(*blk, freeCount);
162 Allocator::Free(*blk);
165 Allocator::Free(Pages);
167 Size = NumPages = MaxPages = 0;
173 Allocator::DestructArrayPaged(Pages, 0, Size, PageShift, PageMask);
177 void PushBack(
const T& val)
179 Allocator::Construct(acquireDataPtr(), val);
183 bool PushBackSafe(
const T& val)
185 T* p = acquireDataPtrSafe();
186 if (!p)
return false;
187 Allocator::Construct(p, val);
193 void PushBackAlt(
const S& val)
195 Allocator::ConstructAlt(acquireDataPtr(), val);
203 Allocator::Destruct(&At(Size - 1));
207 void PopBack(UPInt count)
209 KY_ASSERT(Size >= count);
212 Allocator::Destruct(&At(Size - 1));
232 UPInt GetCapacity()
const
234 return NumPages << PageShift;
237 UPInt GetNumBytes()
const
239 return GetCapacity() *
sizeof(T) + MaxPages *
sizeof(T*);
242 void Reserve(UPInt newCapacity)
244 if(newCapacity > GetCapacity())
246 UPInt newNumPages = (newCapacity + PageMask) >> PageShift;
247 for(UPInt i = NumPages; i < newNumPages; ++i)
252 void Resize(UPInt newSize)
256 UPInt newNumPages = (newSize + PageMask) >> PageShift;
257 for(UPInt i = NumPages; i < newNumPages; ++i)
259 Allocator::ConstructArrayPaged(Pages, Size, newSize, PageShift, PageMask);
265 Allocator::DestructArrayPaged(Pages, newSize, Size, PageShift, PageMask);
270 void CutAt(UPInt newSize)
274 Allocator::DestructArrayPaged(Pages, newSize, Size, PageShift, PageMask);
279 void InsertAt(UPInt pos,
const T& val)
287 Allocator::Construct(acquireDataPtr());
291 for(i = Size-1; i > pos; --i)
299 void RemoveAt(UPInt pos)
304 for(++pos; pos < Size; pos++)
308 Allocator::Destruct(&At(Size - 1));
313 UPInt GetSize()
const
318 const T& operator [] (UPInt i)
const
320 return Pages[i >> PageShift][i & PageMask];
323 T& operator [] (UPInt i)
325 return Pages[i >> PageShift][i & PageMask];
328 const T& At(UPInt i)
const
330 return Pages[i >> PageShift][i & PageMask];
335 return Pages[i >> PageShift][i & PageMask];
338 T ValueAt(UPInt i)
const
340 return Pages[i >> PageShift][i & PageMask];
343 const T& Front()
const
353 const T& Back()
const
365 ArrayPagedBase(
const SelfType&);
366 const SelfType& operator = (
const SelfType&);
368 void allocatePage(UPInt nb)
374 Pages = (T**)Allocator::Realloc(
375 0, Pages, (MaxPages + PtrPoolInc) *
sizeof(T*),
380 Pages = (T**)Allocator::Alloc(
381 this, PtrPoolInc *
sizeof(T*),
384 MaxPages += PtrPoolInc;
386 Pages[nb] = (T*)Allocator::Alloc(
this, PageSize *
sizeof(T), __FILE__, __LINE__);
390 KY_INLINE T* acquireDataPtr()
392 UPInt np = Size >> PageShift;
397 return Pages[np] + (Size & PageMask);
400 bool allocatePageSafe(UPInt nb)
407 newPages = (T**)Allocator::Realloc(
408 0, Pages, (MaxPages + PtrPoolInc) *
sizeof(T*),
413 newPages = (T**)Allocator::Alloc(
414 this, PtrPoolInc *
sizeof(T*),
420 MaxPages += PtrPoolInc;
422 Pages[nb] = (T*)Allocator::Alloc(
this, PageSize *
sizeof(T), __FILE__, __LINE__);
429 KY_INLINE T* acquireDataPtrSafe()
431 UPInt np = Size >> PageShift;
434 if (!allocatePageSafe(np))
437 return Pages[np] + (Size & PageMask);
455 template<
class T,
int PageSh=6,
int PtrPoolInc=64,
int SID=Stat_Default_Mem>
456 class ArrayPagedPOD :
457 public ArrayPagedBase<T, PageSh, PtrPoolInc, AllocatorPagedGH_POD<T, SID> >
460 typedef ArrayPagedPOD<T, PageSh, PtrPoolInc, SID> SelfType;
462 typedef AllocatorPagedGH_POD<T, SID> AllocatorType;
472 template<
class T,
int PageSh=6,
int PtrPoolInc=64,
int SID=Stat_Default_Mem>
474 public ArrayPagedBase<T, PageSh, PtrPoolInc, AllocatorPagedGH<T, SID> >
477 typedef ArrayPaged<T, PageSh, PtrPoolInc, SID> SelfType;
479 typedef AllocatorPagedGH<T, SID> AllocatorType;
489 template<
class T,
int PageSh=6,
int PtrPoolInc=64,
int SID=Stat_Default_Mem>
490 class ArrayPagedLH_POD :
491 public ArrayPagedBase<T, PageSh, PtrPoolInc, AllocatorPagedLH_POD<T, SID> >
494 typedef ArrayPagedLH_POD<T, PageSh, PtrPoolInc, SID> SelfType;
496 typedef AllocatorPagedLH_POD<T, SID> AllocatorType;
506 template<
class T,
int PageSh=6,
int PtrPoolInc=64,
int SID=Stat_Default_Mem>
508 public ArrayPagedBase<T, PageSh, PtrPoolInc, AllocatorPagedLH<T, SID> >
511 typedef ArrayPagedLH<T, PageSh, PtrPoolInc, SID> SelfType;
513 typedef AllocatorPagedLH<T, SID> AllocatorType;
523 template<
typename T,
int PageSh=6,
int PtrPoolInc=64,
int SID=Stat_Default_Mem>
525 public ArrayPagedBase<T, PageSh, PtrPoolInc, AllocatorPagedCC<T, SID> >
528 typedef ArrayPagedCC<T, PageSh, PtrPoolInc, SID> SelfType;
530 typedef AllocatorPagedCC<T, SID> AllocatorType;
533 ArrayPagedCC(
const ValueType& v) : DefaultValue(v) {}
535 const T& GetDefaultValue()
const
541 ValueType DefaultValue;
Definition: gamekitcrowddispersion.h:20