31 #ifndef SOLIDUTILS_INCLUDE_FIXEDPRIORITYQUEUE_HPP 32 #define SOLIDUTILS_INCLUDE_FIXEDPRIORITYQUEUE_HPP 48 template<
typename K,
typename V>
52 static constexpr
size_t const NULL_INDEX =
static_cast<size_t>(-1);
82 size_t const pos = m_q->m_index[m_index];
83 return m_q->m_data[pos].value;
95 }
while (m_index < m_q->m_index.size() && \
96 m_q->m_index[m_index] == NULL_INDEX);
111 return m_index == other.m_index;
124 return !(*
this == other);
153 while (index < m_q->m_index.size() && \
154 m_q->m_index[index] == NULL_INDEX) {
168 return Iterator(m_q->m_index.size(), m_q);
183 m_index(max, NULL_INDEX),
196 V
const value) noexcept
198 ASSERT_LESS(static_cast<size_t>(value), m_index.size());
199 ASSERT_NOTEQUAL(m_index[value], NULL_INDEX);
202 size_t const index = m_index[value];
215 V
const value) noexcept
217 ASSERT_LESS(static_cast<size_t>(value), m_index.size());
218 ASSERT_EQUAL(m_index[value], NULL_INDEX);
220 size_t const index = m_size++;
221 m_index[value] = index;
222 m_data[index].key = key;
223 m_data[index].value = value;
237 V
const value) noexcept
239 ASSERT_LESS(static_cast<size_t>(value), m_index.size());
240 ASSERT_NOTEQUAL(m_index[value], NULL_INDEX);
242 size_t const index = m_index[value];
243 m_data[index].key = key;
246 if (index > 0 && key > m_data[parentIndex(index)].key) {
262 V
const value) noexcept
264 ASSERT_LESS(static_cast<size_t>(value), m_index.size());
265 ASSERT_NOTEQUAL(m_index[value], NULL_INDEX);
267 size_t const index = m_index[value];
268 K
const key = m_data[index].key + delta;
281 V
const value)
const noexcept
283 ASSERT_LESS(static_cast<size_t>(value), m_index.size());
285 return m_index[value] != NULL_INDEX;
297 V
const value)
const noexcept
299 ASSERT_LESS(static_cast<size_t>(value), m_index.size());
300 ASSERT_NOTEQUAL(m_index[value], NULL_INDEX);
302 return m_data[m_index[value]].key;
313 ASSERT_GREATER(m_size, 0);
315 V
const value = m_data[0].value;
327 V
const &
peek() const noexcept
329 return m_data[0].value;
338 K
const &
max() const noexcept
340 return m_data[0].key;
360 for (
size_t i = 0; i < m_size; ++i) {
361 m_index[m_data[i].value] = NULL_INDEX;
380 struct kv_pair_struct
399 size_t const index)
const noexcept
412 size_t leftChildIndex(
413 size_t const index)
const noexcept
415 return (index * 2)+1;
426 size_t rightChildIndex(
427 size_t const index)
const noexcept
429 return leftChildIndex(index) + 1;
441 size_t const indexB) noexcept
443 V
const valueA = m_data[indexA].value;
444 V
const valueB = m_data[indexB].value;
446 std::swap(m_index[valueA], m_index[valueB]);
447 std::swap(m_data[indexA], m_data[indexB]);
457 size_t const index) noexcept
459 ASSERT_LESS(index, m_size);
462 V
const deletedValue = m_data[index].value;
466 m_data[index] = m_data[m_size];
468 V
const value = m_data[index].value;
469 m_index[value] = index;
474 m_index[deletedValue] = NULL_INDEX;
484 size_t index) noexcept
487 size_t const parent = parentIndex(index);
488 if (m_data[parent].key >= m_data[index].key) {
504 size_t index) noexcept
506 K
const key = m_data[index].key;
508 size_t const leftIndex = leftChildIndex(index);
509 size_t const rightIndex = rightChildIndex(index);
510 if (rightIndex < m_size && key < m_data[rightIndex].key) {
511 if (m_data[rightIndex].key >= m_data[leftIndex].key) {
512 swap(index, rightIndex);
515 swap(index, leftIndex);
518 }
else if (leftIndex < m_size && key < m_data[leftIndex].key) {
519 swap(index, leftIndex);
Iterator(size_t const index, FixedPriorityQueue< K, V > const *const q)
Create a new forward iterator.
Definition: FixedPriorityQueue.hpp:66
bool contains(V const value) const noexcept
Check if a value in present in the priority queue.
Definition: FixedPriorityQueue.hpp:280
bool operator==(Iterator const &other) const
Check if this iterator is the same as another.
Definition: FixedPriorityQueue.hpp:108
size_t size() const noexcept
Get the number of elements in the queue.
Definition: FixedPriorityQueue.hpp:349
Definition: FixedPriorityQueue.hpp:57
Iterator begin() const noexcept
Get the forward iterator to the start of the set.
Definition: FixedPriorityQueue.hpp:149
void clear() noexcept
Clear entries from the priority queue.
Definition: FixedPriorityQueue.hpp:358
ValueSet(FixedPriorityQueue< K, V > const *const q)
Create a new value set.
Definition: FixedPriorityQueue.hpp:137
V pop() noexcept
Pop the top value from the queue.
Definition: FixedPriorityQueue.hpp:311
void updateByDelta(K const delta, V const value) noexcept
Update the key associated with a given value by modifying the key.
Definition: FixedPriorityQueue.hpp:260
Definition: FixedPriorityQueue.hpp:54
K const & max() const noexcept
Get get the top of the priority queue's value.
Definition: FixedPriorityQueue.hpp:338
ValueSet remaining() const noexcept
Get the set of remaining items in the priority. The order of the values is arbitrary.
Definition: FixedPriorityQueue.hpp:373
bool operator!=(Iterator const &other) const
Check if this iterator is different from another iterator.
Definition: FixedPriorityQueue.hpp:121
void add(K const key, V const value) noexcept
Add an value to the queue.
Definition: FixedPriorityQueue.hpp:213
V const & peek() const noexcept
Get get the top of the priority queue's value.
Definition: FixedPriorityQueue.hpp:327
Iterator & operator++()
Move the iterator forward.
Definition: FixedPriorityQueue.hpp:91
The FixedPriorityQueue class provides a priority queue implementation with the standard O(log n) inse...
Definition: FixedPriorityQueue.hpp:49
A mutable array structure for storing self-allocated memory.
V operator*() const noexcept
Get the value of the iterator.
Definition: FixedPriorityQueue.hpp:80
FixedPriorityQueue(V const max)
Create a new priority queue that can hold element 0 through max.
Definition: FixedPriorityQueue.hpp:180
Iterator end() const noexcept
Get the forward iterator to the end of the set.
Definition: FixedPriorityQueue.hpp:166
void update(K const key, V const value) noexcept
Update the key associated with a given value.
Definition: FixedPriorityQueue.hpp:235