8 #ifndef Navigation_SmallPtrTrackedCollection_H
9 #define Navigation_SmallPtrTrackedCollection_H
21 class SmallPtrTrackedCollection
25 SmallPtrTrackedCollection() : m_count(0), m_capacity(1) { m_values.m_multipleValues =
KY_NULL; }
26 ~SmallPtrTrackedCollection() { KY_LOG_ERROR_IF(m_count != 0, (
"memory leak !")); }
28 KY_INLINE
bool IsEmpty()
const {
return GetCount() == 0; }
29 KY_INLINE
KyUInt32 GetCount()
const {
return m_count; }
31 KY_INLINE T** GetValues() {
return m_capacity == 1 ? &m_values.m_oneValue : m_values.m_multipleValues; }
33 void PushBack(MemoryHeap* heap, T* newValue)
40 newValue->SetIndexInCollection(0);
41 m_values.m_oneValue = newValue;
45 T* firstValue = m_values.m_oneValue;
48 m_values.m_multipleValues = (T**)KY_HEAP_ALLOC(heap,
KyUInt32(4 *
sizeof(T*)), MemStat_NavData);
53 m_values.m_multipleValues[0] = firstValue;
54 m_values.m_multipleValues[1] = newValue;
56 newValue->SetIndexInCollection(1);
60 if (m_count == m_capacity)
61 Reserve(heap, 2 * m_capacity);
63 m_values.m_multipleValues[m_count] = newValue;
64 newValue->SetIndexInCollection(m_count);
70 void Reserve(MemoryHeap* heap,
KyUInt32 newcapacity)
72 KY_DEBUG_ASSERTN(newcapacity > m_count, (
"no shrinking !"));
74 T** values = (T**)KY_HEAP_ALLOC(heap, newcapacity * (
KyUInt32)
sizeof(T*), MemStat_NavData);
76 memcpy(values, m_values.m_multipleValues, m_count *
sizeof(T*));
78 KY_FREE(m_values.m_multipleValues);
81 m_values.m_multipleValues = values;
88 for(
KyUInt32 i = 0; i < m_count; ++i)
89 m_values.m_multipleValues[i]->SetIndexInCollection(
KyUInt32MAXVAL);
91 KY_FREE(m_values.m_multipleValues);
104 void SwapWithLastAndPopBack(T* valueToRemove)
106 const KyUInt32 index = valueToRemove->GetIndexInCollection();
109 KY_DEBUG_ASSERTN(index < m_count, (
"error"));
112 m_values.m_multipleValues[index]->SetIndexInCollection(
KyUInt32MAXVAL);
114 if (index != lastIndex)
117 m_values.m_multipleValues[lastIndex]->SetIndexInCollection(index);
118 m_values.m_multipleValues[index] = m_values.m_multipleValues[lastIndex];
131 void GetOwnershipOfData(SmallPtrTrackedCollection& other)
135 m_values = other.m_values;
136 m_count = other.m_count;
137 m_capacity = other.m_capacity;
139 other.m_values.m_oneValue =
KY_NULL;
141 other.m_capacity = 0;
148 T** m_multipleValues;
158 #endif //Navigation_SmallPtrTrackedCollection_H
#define KY_NULL
Null value.
Definition: types.h:247
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
unsigned short KyUInt16
Type used internally to represent an unsigned 16-bit integer.
Definition: types.h:40
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
#define KyUInt32MAXVAL
The maximum value that can be stored in the KyUInt32 variable type.
Definition: types.h:226