gwnavruntime/querysystem/workingmemcontainers/workingmembinaryheap.h Source File

workingmembinaryheap.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #pragma once
9 
13 
14 namespace Kaim
15 {
16 
17 typedef KyUInt16 IndexInBinHeap;
18 static const IndexInBinHeap IndexInBinHeap_UnSet = KyUInt16MAXVAL;
19 static const IndexInBinHeap IndexInBinHeap_Removed = IndexInBinHeap_UnSet - 1;
20 
21 template <typename T>
22 class WorkingMemBinaryHeapIndexTracker_None
23 {
24 public:
25  static void OnAdd(T& /*value*/, KyUInt32 /*indexInBinaryHeap*/) {}
26  static void OnRemove(T& /*value*/) {}
27  static void OnSwap(T& /*lhs*/, T& /*rhs*/) {}
28 };
29 
30 template <class Item, typename Comparator = DefaultLess<Item>, typename BinaryHeapIndexTracker = WorkingMemBinaryHeapIndexTracker_None<Item> >
31 class WorkingMemBinaryHeap
32 {
33 public:
34  WorkingMemBinaryHeap();
35  WorkingMemBinaryHeap(WorkingMemory* workingMemory);
36 
37  void Init(WorkingMemory* workingMemory);
38 
39  KY_INLINE bool IsInitialized() const;
40  KY_INLINE KyUInt32 GetCount() const;
41  KY_INLINE KyUInt32 GetCapacity() const;
42 
43  KY_INLINE bool IsEmpty() const;
44  KY_INLINE bool IsFull();
45 
46  KY_INLINE void Clear();
47 
48  /* Retrieves the first sorted element in the container.
49  \pre IsEmpty() == false */
50  KY_INLINE void GetFirst(Item& item) const;
51 
52  /* Retrieves the first sorted element in the container.
53  \pre IsEmpty() == false */
54  KY_INLINE const Item& GetFirst() const;
55 
56  /* Add a element in the container. */
57  void Insert(const Item& item);
58 
59  /* Removes and retrieves the first sorted element in the container.
60  \pre IsEmpty() == false
61  \post IsSorted() == true */
62  void ExtractFirst(Item& item);
63 
64  /* Updates the referenced element and sort the container.
65  \pre index < GetCount()
66  \post IsSorted() == true */
67  void UpdateAt(KyUInt32 index);
68 
69  Comparator& GetComparator() { return m_comparator; }
70 
71  void ReleaseWorkingMemoryBuffer() { m_heap.ReleaseWorkingMemoryBuffer(); }
72 
73 private:
74  KY_INLINE KyUInt32 GetParentIndex(KyUInt32 index) { return (index - 1) >> 1; }
75  KY_INLINE KyUInt32 GetLeftChildIndex(KyUInt32 index) { return (index << 1) + 1; }
76  KY_INLINE KyUInt32 GetRigthChildIndex(KyUInt32 index) { return (index << 1) + 2; }
77 
78  /* Moves an element towards the first element until it is sorted.*/
79  void PercolateUp(KyUInt32 HoleIndex);
80 
81  /* Moves an element towards the last element until it is sorted.*/
82  void PercolateDown(KyUInt32 HoleIndex, KyUInt32 currentCount);
83 
84 private:
85  WorkingMemArray<Item> m_heap;
86 public:
87  Comparator m_comparator;
88  BinaryHeapIndexTracker m_binayHeapTracker;
89 };
90 
91 }
92 
94 
95 
96 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
std::uint16_t KyUInt16
uint16_t
Definition: types.h:28
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KyUInt16MAXVAL
KyUInt16 max value
Definition: types.h:67