The Sparta Modeling Framework
Loading...
Searching...
No Matches
PriorityQueue.hpp
Go to the documentation of this file.
1// <PriorityQueue.hpp> -*- C++ -*-
2
3
9#pragma once
10
11#include <list>
12#include <algorithm>
13#include <functional>
14
17
18namespace sparta
19{
20
53 template <class DataT,
54 class SortingAlgorithmT = std::less<DataT>,
55 size_t bounded_cnt=0>
57 {
58 private:
59 using PQueueType =
60 typename std::conditional<bounded_cnt == 0,
61 std::list<DataT>,
63
64 public:
65
66 // For collection
67 using size_type = size_t;
68 using iterator = typename PQueueType::iterator;
69 using const_iterator = typename PQueueType::const_iterator;
70
76 priority_items_(bounded_cnt)
77 {}
78
84 PriorityQueue(const SortingAlgorithmT & sort_alg) :
85 priority_items_(bounded_cnt),
86 sort_alg_(sort_alg)
87 {}
88
94 void insert(const DataT & data)
95 {
96 const auto eit = priority_items_.end();
97 for(auto it = priority_items_.begin(); it != eit; ++it)
98 {
99 if(sort_alg_(data, *it)) {
100 priority_items_.insert(it, data);
101 return;
102 }
103 }
104 priority_items_.emplace_back(data);
105 }
106
108 size_t size() const {
109 return priority_items_.size();
110 }
111
113 bool empty() const {
114 return priority_items_.empty();
115 }
116
118 const DataT & top() const {
119 sparta_assert(false == empty(), "Grabbing top from an empty queue");
120 return priority_items_.front();
121 }
122
124 const DataT & back() const {
125 sparta_assert(false == empty(), "Grabbing back from an empty queue");
126 return priority_items_.back();
127 }
128
130 void pop() {
131 sparta_assert(false == empty(), "Popping on an empty priority queue");
132 priority_items_.pop_front();
133 }
134
136 void clear() {
137 priority_items_.clear();
138 }
139
141 void remove(const DataT & data) {
142 priority_items_.remove(data);
143 }
144
146 void erase(const const_iterator & it) {
147 priority_items_.erase(it);
148 }
149
161 void forceFront(const DataT & data) {
162 priority_items_.emplace_front(data);
163 }
164
168 iterator begin() { return priority_items_.begin(); }
169
171 const_iterator begin() const { return priority_items_.begin(); }
172
174 iterator end() { return priority_items_.end(); }
175
177 const_iterator end() const { return priority_items_.end(); }
180 private:
181
183 PQueueType priority_items_;
184
186 SortingAlgorithmT sort_alg_;
187 };
188}
File that defines the FastList class – an alternative to std::list when the user knows the size of th...
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
A data structure that allows pushing/emplacing into it with a given sorter.
PriorityQueue(const SortingAlgorithmT &sort_alg)
Create a priority queue with a specific instance of the sorting algorithm.
const DataT & back() const
Get the last element (lowest priority) in the queue.
size_t size() const
Get the number of items in the queue.
void remove(const DataT &data)
Remove the item from the queue.
const DataT & top() const
Get the first element in the queue.
bool empty() const
Is the queue empty?
void forceFront(const DataT &data)
Force a data entry to the front of the queue.
void pop()
Pop the front of the queue (highest priority)
void clear()
Clear the entire queue.
void erase(const const_iterator &it)
Erase the item from the queue (const_iterator/iterator)
PriorityQueue()
Create a priority queue with a default instance of the sorting algorithm.
void insert(const DataT &data)
Inserts the data item into the list using the sorting alg. Stops at the first insertion.
An alternative to std::list, about 70% faster.
Definition FastList.hpp:47
const_iterator end() const
Const Iterator to end of the queue – end priority.
iterator begin()
Iterator to beginning of the queue – highest priority.
iterator end()
Iterator to end of the queue – end priority.
const_iterator begin() const
Const Iterator to beginning of the queue – highest priority.
Macros for handling exponential backoff.