NDEVR
API Documentation
BinaryHeap.hpp
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: BinaryHeap
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Buffer.h>
34namespace NDEVR
35{
41 template<class t_comp_type, class t_value_type, bool t_is_min>
42 class BinaryHeap
43 {
44 public:
45 explicit BinaryHeap(uint04 size)
46 : m_values(size)
47 {}
48 void insert(t_comp_type comparison, t_value_type value)
49 {
50 const uint04 length = m_values.size();
51 m_values.add({ value, comparison });
52 bubbleUp(length);
53 }
54 [[nodiscard]] const Buffer<std::pair<t_value_type, t_comp_type>>& values() const
55 {
56 return m_values;
57 }
58
59 [[nodiscard]] Buffer<std::pair<t_value_type, t_comp_type>>& values()
60 {
61 return m_values;
62 }
63 [[nodiscard]] t_comp_type extremeComp() const
64 {
65 return m_values[0].second;
66 }
67 [[nodiscard]] t_value_type extremeValue() const
68 {
69 return m_values[0].first;
70 }
71 void clear()
72 {
73 m_values.clear();
74 }
75 [[nodiscard]] uint04 size() const
76 {
77 return m_values.size();
78 }
80 {
81 return m_values;
82 }
83 [[nodiscard]] Buffer<std::pair<t_value_type, t_comp_type>> sortedValues() const
84 {
85 BinaryHeap heap = *this;
86 Buffer<std::pair<t_value_type, t_comp_type>> sorted_values(m_values.size());
87 sorted_values.setSize(m_values.size());
88 uint04 size = m_values.size();
89 for (uint04 i = size - 1; IsValid(i); --i)
90 {
91 sorted_values[i] = heap.m_values[0];
92 heap.deleteExtreme();
93 }
94 return sorted_values;
95 }
96 [[nodiscard]] Buffer<t_value_type> sortedIndices() const
97 {
98 BinaryHeap heap = *this;
99 Buffer<t_value_type> sorted_values(m_values.size());
100 sorted_values.setSize(m_values.size());
101 uint04 size = m_values.size();
102 for (uint04 i = size - 1; IsValid(i); --i)
103 {
104 sorted_values[i] = heap.m_values[0].first;
105 heap.deleteExtreme();
106 }
107 return sorted_values;
108 }
109 [[nodiscard]] t_value_type replaceExtreme(t_comp_type comparison, t_value_type value)
110 {
111 t_value_type extreme = m_values[0];
112 m_values[0].first = value;
113 m_values[0].second = comparison;
114 return extreme;
115 }
116 [[nodiscard]] t_value_type popExtreme()
117 {
118 t_value_type extreme = m_values[0].first;
119 deleteExtreme();
120 return extreme;
121 }
122 void deleteExtreme()
123 {
124 m_values[0] = m_values.last();
125 m_values.removeLast();
126 bubbleDown(0);
127 }
128 private:
129 void bubbleDown(uint04 index)
130 {
131 const uint04 length = m_values.size();
132 for (;;)
133 {
134 uint04 left = (index << 1) + 1;
135 uint04 right = left + 1;
136 uint04 best = index;
137
138 if (left >= length)
139 return; //index is a leaf
140
141 if (Compare(m_values[best].second, m_values[left].second))
142 best = left;
143
144 if (right < length && Compare(m_values[best].second, m_values[right].second))
145 best = right;
146
147 if (best != index)
148 {
149 //need to swap
150 m_values.swapIndices(index, best);
151 index = best;
152 }
153 else
154 return;
155 }
156 }
157 void bubbleUp(uint04 index)
158 {
159 for (;;)
160 {
161 if (index == 0)
162 return;
163 uint04 parent_index = ((index - 1) >> 1);
164 if (Compare(m_values[parent_index].second, m_values[index].second))
165 {
166 m_values.swapIndices(index, parent_index);
167 index = parent_index;
168 }
169 else
170 return;
171 }
172 }
173 static bool Compare(t_comp_type a, t_comp_type b)
174 {
175 if constexpr (t_is_min)
176 return a > b;
177 else
178 return a < b;
179 }
180
181 private:
183 };
184
185 template<class t_comp_type, class t_value_type>
187
188 template<class t_comp_type, class t_value_type>
190}
191
A heap data structure that takes the form of a binary tree which is a common way of implementing prio...
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
The primary namespace for the NDEVR SDK.
static constexpr bool IsValid(const Angle< t_type > &value)
Checks whether the given Angle holds a valid value.
Definition Angle.h:398
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...