Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SortedListMap.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_SORTEDLISTMAP_HXX_
35#define _UTILS_SORTEDLISTMAP_HXX_
36
37#include <vector>
38#include <algorithm>
39
40#include "utils/macros.h"
41
46template <class D, class CMP> class SortedListSet
47{
48public:
50 typedef D data_type;
51
52private:
54 typedef std::vector<data_type> container_type;
55
56public:
58 typedef typename container_type::iterator iterator;
60 typedef typename container_type::const_iterator const_iterator;
61
62 template <typename... Args>
63 SortedListSet(Args &&...args)
64 : cmp_(std::forward<Args>(args)...)
65 {
66 }
67
70 void reserve(size_t sz)
71 {
72 container_.reserve(sz);
73 }
74
77 {
78 lazy_init();
79 return container_.begin();
80 }
81
84 {
85 return container_.begin() + sortedCount_;
86 }
87
89 size_t size() {
90 return container_.size();
91 }
92
94 template<class key_type>
95 iterator lower_bound(key_type key)
96 {
97 lazy_init();
98 return std::lower_bound(
99 container_.begin(), container_.end(), key, cmp_);
100 }
101
103 template<class key_type>
104 iterator upper_bound(key_type key)
105 {
106 lazy_init();
107 return std::upper_bound(
108 container_.begin(), container_.end(), key, cmp_);
109 }
110
113 template<class key_type>
114 iterator find(key_type key)
115 {
116 lazy_init();
117 auto lb = lower_bound(key);
118 auto ub = upper_bound(key);
119 if (ub == lb) return end();
120 return lb;
121 }
122
124 template<class key_type>
125 std::pair<iterator, iterator> equal_range(key_type key)
126 {
127 lazy_init();
128 return std::equal_range(
129 container_.begin(), container_.end(), key, cmp_);
130 }
131
134 {
135 container_.push_back(d);
136 }
137
139 void erase(const iterator &it)
140 {
142 container_.erase(it);
143 --sortedCount_;
144 }
145
148 void erase(const iterator &it_b, const iterator& it_e)
149 {
150 container_.erase(it_b, it_e);
151 lazy_init(); // will set sortedCount_.
152 }
153
155 void clear()
156 {
157 container_.clear();
158 sortedCount_ = 0;
159 }
160
161private:
164 {
165 if (sortedCount_ != container_.size())
166 {
167 sort(container_.begin(), container_.end(), cmp_);
168 sortedCount_ = container_.size();
169 }
170 }
171
174
176 CMP cmp_;
177
179 size_t sortedCount_{0};
180};
181
182#endif // _UTILS_SORTEDLISTMAP_HXX_
An mostly std::set<> compatible class that stores the internal data in a sorted vector.
iterator find(key_type key)
Searches for a single entry.
iterator begin()
container_type container_
Holds the actual data elements.
CMP cmp_
Comparator instance.
void lazy_init()
Reestablishes sorted order in case anything was inserted or removed.
iterator lower_bound(key_type key)
void erase(const iterator &it)
Removes an entry from the vector, pointed by an iterator.
void erase(const iterator &it_b, const iterator &it_e)
Removes a sequence of entries from the vector, pointed by a pair of iterators.
container_type::const_iterator const_iterator
Const Iterator type.
iterator upper_bound(key_type key)
std::vector< data_type > container_type
Type that we store data in internally.
void reserve(size_t sz)
Ensures that a given size can be reached without memory allocation.
void insert(data_type &&d)
Adds new entry to the vector.
container_type::iterator iterator
Iterator type.
D data_type
Type of data stored in the set.
void clear()
Removes all entries.
size_t sortedCount_
The first this many elements in the container are already sorted.
std::pair< iterator, iterator > equal_range(key_type key)
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138