8 #ifndef Navigation_BinaryHeap_H
9 #define Navigation_BinaryHeap_H
24 class BinaryHeapIndexTracker_None
28 static void OnRemove(T& ) {}
29 static void OnSwap(T& , T& ) {}
34 class BinaryHeapIndexTracker_IndexInBinaryHeap
38 static KY_INLINE
void OnAdd(T& value,
KyUInt32 indexInBinaryHeap) { value.m_indexInBinaryHeap = indexInBinaryHeap; }
40 static KY_INLINE
void OnRemove(T& value) { value.m_indexInBinaryHeap = BinaryHeapInvalidReference; }
42 static KY_INLINE
void OnSwap(T& lhs, T& rhs) { Alg::Swap(lhs.m_indexInBinaryHeap, rhs.m_indexInBinaryHeap); }
49 class BinaryHeapIndexTracker_IndexInBinaryHeap<T*>
53 static KY_INLINE
void OnAdd(T*& value,
KyUInt32 indexInBinaryHeap) { value->m_indexInBinaryHeap = indexInBinaryHeap; }
55 static KY_INLINE
void OnRemove(T*& value) { value->m_indexInBinaryHeap = BinaryHeapInvalidReference; }
57 static KY_INLINE
void OnSwap(T*& lhs, T*& rhs) { Alg::Swap(lhs->m_indexInBinaryHeap, rhs->m_indexInBinaryHeap); }
65 template <
class T,
typename ArrayType = KyArray<T>,
typename Comparator = DefaultLess<T>,
typename BinaryHeapIndexTracker = BinaryHeapIndexTracker_None<T> >
71 typedef typename ArrayType::Iterator Iterator;
72 typedef typename ArrayType::ConstIterator ConstIterator;
77 KY_INLINE
void GetFirst(T& item) const;
81 KY_INLINE const T& GetFirst()
const {
return *Begin(); }
85 void Insert(
const T& item);
95 void ExtractFirst(T& item);
97 KY_INLINE
void Clear() { m_heap.Clear(); }
98 KY_INLINE
void Reserve(
KyUInt32 size) {
return m_heap.Reserve(size); }
101 KY_INLINE
KyUInt32 GetCapacity()
const {
return (
KyUInt32)m_heap.GetCapacity();}
102 KY_INLINE
bool IsEmpty()
const {
return GetCount() == 0; }
104 bool IsSorted()
const;
110 KY_INLINE
const T& operator[](
KyUInt32 index)
const {
return m_heap[index]; }
111 KY_INLINE T& operator[](
KyUInt32 index) {
return m_heap[index]; }
126 Iterator Erase(Iterator it);
129 KY_INLINE ConstIterator Begin()
const {
return m_heap.Begin(); }
130 KY_INLINE Iterator Begin() {
return m_heap.Begin(); }
133 KY_INLINE ConstIterator End()
const {
return m_heap.End(); }
134 KY_INLINE Iterator End() {
return m_heap.End(); }
136 Comparator& GetComparator() {
return m_comparator; }
137 ArrayType& GetHeap() {
return m_heap; }
139 KY_INLINE ConstIterator GetLast()
const {
return ConstIterator(&m_heap, (GetCount() - 1)); }
140 KY_INLINE Iterator GetLast() {
return Iterator(&m_heap, (GetCount() - 1)); }
142 KY_INLINE ConstIterator GetParent(ConstIterator it)
const {
return Begin() + ((GetIndex(it) - 1) >> 1);}
143 KY_INLINE Iterator GetParent(Iterator it) {
return Begin() + ((GetIndex(it) - 1) >> 1);}
146 KY_INLINE ConstIterator GetLeft(ConstIterator it)
const {
return Begin() + 1 + (GetIndex(it) << 1);}
147 KY_INLINE Iterator GetLeft(Iterator it) {
return Begin() + 1 + (GetIndex(it) << 1);}
150 KY_INLINE ConstIterator GetRight(ConstIterator it)
const {
return Begin() + 2 + (GetIndex(it) << 1);}
151 KY_INLINE Iterator GetRight(Iterator it) {
return Begin() + 2 + (GetIndex(it) << 1);}
153 KY_INLINE
KyUInt32 GetIndex(ConstIterator it)
const {
return KyUInt32(it.GetIndex()); }
154 KY_INLINE
KyUInt32 GetIndex(Iterator it)
const {
return KyUInt32(it.GetIndex()); }
157 void PercolateUp(Iterator hole);
160 void PercolateDown(Iterator hole);
164 Comparator m_comparator;
171 #endif // Navigation_BinaryHeap_H
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
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