jGL
Loading...
Searching...
No Matches
priorityStore.h
Go to the documentation of this file.
1#ifndef PRIORITYSTORE_H
2#define PRIORITYSTORE_H
3
4#include <memory>
5#include <vector>
6#include <iterator>
7#include <algorithm>
8#include <unordered_map>
9#include <cstdint>
10
15typedef std::string ElementId;
16
21typedef uint64_t Priority;
22
30template <class T>
32{
33public:
34
39 struct Info
40 {
42 : id(i), priority(p)
43 {}
44
46 : id(""), priority(0)
47 {}
48
51 };
52
58 PriorityStore(uint64_t sizeHint = 8)
59 {
60 idToElement.reserve(sizeHint);
61 cache.reserve(sizeHint);
62 }
63
64 virtual void clear()
65 {
66 idToElement.clear();
67 cache.clear();
68 }
69
78 virtual void add(T s, ElementId id, Priority priority = 0)
79 {
80 if (idToElement.find(id) != idToElement.end())
81 {
82 throw std::runtime_error("id: "+id+", already use in PriorityStore");
83 }
84
85 idToElement[id] = std::pair(s, priority);
86 auto pos = std::upper_bound
87 (
88 cache.begin(),
89 cache.end(),
90 priority,
91 []
92 (
93 Priority p,
94 std::pair<Info, T> r
95 )
96 {
97 return p < r.first.priority;
98 }
99 );
100 cache.insert(pos, std::pair(Info(id, priority), s));
101 }
102
103 virtual void remove(ElementId id)
104 {
105 if (idToElement.find(id) == idToElement.end()) { return; }
106
107 idToElement.erase(id);
108
109 auto pos = std::find_if
110 (
111 cache.begin(),
112 cache.end(),
113 [id](std::pair<Info, T> v)
114 {
115 return v.first.id == id;
116 }
117 );
118 if (pos != cache.end())
119 {
120 cache.erase(pos);
121 }
122 }
123
124 void updatePriority(ElementId id, Priority newPriority)
125 {
126 if (idToElement.find(id) == idToElement.end()){ return; }
127
128 auto element = idToElement[id];
129 remove(id);
130 add(element.first, id, newPriority);
131 }
132
141 std::vector<std::pair<Info, T>> vectorise
142 (
143 std::multimap<Priority, ElementId> & oids
144 )
145 {
146 std::vector<std::pair<Info, T>> s;
147 s.reserve(oids.size());
148 for (const auto & id : oids)
149 {
150 s.push_back(std::pair(Info(id.second, id.first), idToElement[id.second].first));
151 }
152 return s;
153 }
154
155 T & operator [](ElementId id) { return idToElement[id].first; }
156
157 typename std::vector<std::pair<Info, T>>::const_iterator begin() const { return cache.cbegin(); }
158 typename std::vector<std::pair<Info, T>>::const_iterator end() const { return cache.cend(); }
159
160 uint64_t size() const { return cache.size(); }
161
162 bool hasId(const ElementId id) const { return idToElement.find(id) != idToElement.end(); }
163
164protected:
165
166 // fast lookup of ids
167 std::unordered_map<ElementId, std::pair<T, Priority>> idToElement;
168 // contiguous for efficient drawing/iteration
169 std::vector<std::pair<Info, T>> cache;
170};
171
172#endif /* PRIORITYSTORE_H */
Store elements in a priority ordering, with identities.
Definition priorityStore.h:32
std::vector< std::pair< Info, T > > vectorise(std::multimap< Priority, ElementId > &oids)
Return a vector from overriding priorities.
Definition priorityStore.h:142
bool hasId(const ElementId id) const
Definition priorityStore.h:162
virtual void add(T s, ElementId id, Priority priority=0)
Insert an element.
Definition priorityStore.h:78
std::vector< std::pair< Info, T > >::const_iterator end() const
Definition priorityStore.h:158
void updatePriority(ElementId id, Priority newPriority)
Definition priorityStore.h:124
uint64_t size() const
Definition priorityStore.h:160
virtual void remove(ElementId id)
Definition priorityStore.h:103
T & operator[](ElementId id)
Definition priorityStore.h:155
PriorityStore(uint64_t sizeHint=8)
Construct a new Priority Store with a reserved size.
Definition priorityStore.h:58
virtual void clear()
Definition priorityStore.h:64
std::vector< std::pair< Info, T > > cache
Definition priorityStore.h:169
std::vector< std::pair< Info, T > >::const_iterator begin() const
Definition priorityStore.h:157
std::unordered_map< ElementId, std::pair< T, Priority > > idToElement
Definition priorityStore.h:167
uint64_t Priority
A priority for an element.
Definition priorityStore.h:21
std::string ElementId
User name for an Element.
Definition priorityStore.h:15
Combine an id and a priority.
Definition priorityStore.h:40
Info()
Definition priorityStore.h:45
Priority priority
Definition priorityStore.h:50
ElementId id
Definition priorityStore.h:49
Info(ElementId i, Priority p)
Definition priorityStore.h:41