API Documentation
Loading...
Searching...
No Matches
BinaryHeap.hpp
Go to the documentation of this file.
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{
36 /**--------------------------------------------------------------------------------------------------
37 \brief A heap data structure that takes the form of a binary tree which is a common way of
38 implementing priority queues.
39 *-----------------------------------------------------------------------------------------------**/
40 template<class t_comp_type, class t_value_type, bool t_is_min>
42 {
43 public:
45 : m_values(size)
46 {}
47 void insert(t_comp_type comparison, t_value_type value)
48 {
49 const uint04 length = m_values.size();
50 m_values.add({ value, comparison });
51 bubbleUp(length);
52 }
54 {
55 return m_values;
56 }
57
59 {
60 return m_values;
61 }
62 [[nodiscard]] t_comp_type extremeComp() const
63 {
64 return m_values[0].second;
65 }
66 [[nodiscard]] t_value_type extremeValue() const
67 {
68 return m_values[0].first;
69 }
70 void clear()
71 {
72 m_values.clear();
73 }
74 [[nodiscard]] uint04 size() const
75 {
76 return m_values.size();
77 }
79 {
80 return m_values;
81 }
83 {
84 BinaryHeap heap = *this;
85 Buffer<std::pair<t_value_type, t_comp_type>> sorted_values(m_values.size());
86 sorted_values.setSize(m_values.size());
87 uint04 size = m_values.size();
88 for (uint04 i = size - 1; !IsInvalid(i); --i)
89 {
90 sorted_values[i] = heap.m_values[0];
91 heap.deleteExtreme();
92 }
93 return sorted_values;
94 }
95 [[nodiscard]] Buffer<t_value_type> sortedIndices() const
96 {
97 BinaryHeap heap = *this;
98 Buffer<t_value_type> sorted_values(m_values.size());
99 sorted_values.setSize(m_values.size());
100 uint04 size = m_values.size();
101 for (uint04 i = size - 1; !IsInvalid(i); --i)
102 {
103 sorted_values[i] = heap.m_values[0].first;
104 heap.deleteExtreme();
105 }
106 return sorted_values;
107 }
108 [[nodiscard]] t_value_type replaceExtreme(t_comp_type comparison, t_value_type value) const
109 {
110 t_value_type extreme = m_values[0];
111 m_values[0].first = value;
112 m_values[0].second = comparison;
113 return extreme;
114 }
115 [[nodiscard]] t_value_type popExtreme()
116 {
117 t_value_type extreme = m_values[0].first;
119 return extreme;
120 }
122 {
123 m_values[0] = m_values.last();
124 m_values.removeLast();
125 bubbleDown(0);
126 }
127 private:
128 void bubbleDown(uint04 index)
129 {
130 const uint04 length = m_values.size();
131 for (;;)
132 {
133 uint04 left_child_index = (index << 1) + 1U;
134 uint04 right_child_index = (index << 1) + 2U;
135
136 if (left_child_index >= length)
137 return; //index is a leaf
138
139 uint04 min_index = index;
140
141 if (Compare(m_values[index].second, m_values[left_child_index].second))
142 min_index = left_child_index;
143
144 if ((right_child_index < length) && Compare(m_values[min_index].second, m_values[right_child_index].second))
145 min_index = right_child_index;
146
147 if (min_index != index)
148 {
149 //need to swap
150 m_values.swapIndices(index, min_index);
151 index = min_index;
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:
182 Buffer<std::pair<t_value_type, t_comp_type>, uint04, ObjectAllocator<true>> m_values;
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.
Definition BinaryHeap.hpp:42
const Buffer< std::pair< t_value_type, t_comp_type > > & getAll() const
Definition BinaryHeap.hpp:78
t_value_type replaceExtreme(t_comp_type comparison, t_value_type value) const
Definition BinaryHeap.hpp:108
void insert(t_comp_type comparison, t_value_type value)
Definition BinaryHeap.hpp:47
Buffer< std::pair< t_value_type, t_comp_type > > & values()
Definition BinaryHeap.hpp:58
t_value_type extremeValue() const
Definition BinaryHeap.hpp:66
uint04 size() const
Definition BinaryHeap.hpp:74
t_value_type popExtreme()
Definition BinaryHeap.hpp:115
Buffer< std::pair< t_value_type, t_comp_type > > sortedValues() const
Definition BinaryHeap.hpp:82
void deleteExtreme()
Definition BinaryHeap.hpp:121
void clear()
Definition BinaryHeap.hpp:70
t_comp_type extremeComp() const
Definition BinaryHeap.hpp:62
const Buffer< std::pair< t_value_type, t_comp_type > > & values() const
Definition BinaryHeap.hpp:53
BinaryHeap(uint04 size)
Definition BinaryHeap.hpp:44
Buffer< t_value_type > sortedIndices() const
Definition BinaryHeap.hpp:95
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:59
void setSize(t_index_type new_size)
Definition Buffer.hpp:1330
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:177
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:94