-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0456384
commit 9a322d7
Showing
8 changed files
with
433 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#ifndef PRIORITYSTORE_H | ||
#define PRIORITYSTORE_H | ||
|
||
#include <memory> | ||
#include <vector> | ||
#include <iterator> | ||
#include <algorithm> | ||
#include <unordered_map> | ||
#include <map> | ||
#include <cstdint> | ||
#include <iostream> | ||
|
||
typedef std::string ElementId; | ||
typedef uint64_t Priority; | ||
|
||
template <class T> | ||
class PriorityStore | ||
{ | ||
public: | ||
|
||
struct Record | ||
{ | ||
Record(T e, Priority p, ElementId i) | ||
: element(e), p(p), id(i) | ||
{} | ||
|
||
T element; | ||
Priority p; | ||
ElementId id; | ||
}; | ||
|
||
void add(T & element, ElementId id, Priority p = 0) | ||
{ | ||
if (idToIndex.find(id) != idToIndex.end()) | ||
{ | ||
throw std::runtime_error("id: "+id+", already use in PriorityStore"); | ||
} | ||
|
||
insert(Record(element, p, id)); | ||
} | ||
|
||
void remove(ElementId id) | ||
{ | ||
if (idToIndex.find(id) == idToIndex.end()){ return; } | ||
|
||
auto pos = cache.begin(); | ||
std::advance(pos, idToIndex[id]); | ||
if (pos != cache.end()) | ||
{ | ||
cache.erase(pos); | ||
} | ||
idToIndex.erase(id); | ||
} | ||
|
||
void clear() | ||
{ | ||
idToIndex.clear(); | ||
cache.clear(); | ||
} | ||
|
||
void updatePriority(ElementId id, Priority newPriority) | ||
{ | ||
if (idToIndex.find(id) == idToIndex.end()){ return; } | ||
|
||
Record element = cache[idToIndex[id]]; | ||
auto pos = cache.begin(); | ||
std::advance(pos, idToIndex[id]); | ||
if (pos != cache.end()) | ||
{ | ||
cache.erase(pos); | ||
} | ||
idToIndex.erase(id); | ||
|
||
element.p = newPriority; | ||
insert(element); | ||
} | ||
|
||
T & operator [](ElementId id) { return cache[idToIndex[id]].element; } | ||
|
||
typename std::vector<Record>::const_iterator begin() const { return cache.cbegin(); } | ||
typename std::vector<Record>::const_iterator end() const { return cache.cend(); } | ||
|
||
uint64_t size() const { return cache.size(); } | ||
|
||
void reserve(uint64_t n) | ||
{ | ||
idToIndex.reserve(n); | ||
cache.reserve(n); | ||
} | ||
|
||
protected: | ||
|
||
struct Ordering | ||
{ | ||
bool operator() | ||
( | ||
const Record & l, | ||
const Record & r | ||
) const | ||
{ return l.p < r.p; } | ||
}; | ||
|
||
std::unordered_map<ElementId, uint64_t> idToIndex; | ||
std::vector<Record> cache; | ||
|
||
void insert(Record element) | ||
{ | ||
if (cache.empty()) { cache.push_back(element); return; } | ||
if (element.p <= cache.front().p) { cache.insert(cache.begin(), element); return; } | ||
if (element.p >= cache.back().p) { cache.push_back(element); return; } | ||
|
||
cache.insert | ||
( | ||
std::upper_bound(cache.begin(), cache.end(), element, Ordering()), | ||
element | ||
); | ||
|
||
reindex(); | ||
} | ||
|
||
void reindex() | ||
{ | ||
for (uint64_t i = 0; i < cache.size(); i++) | ||
{ | ||
idToIndex[cache[i].id] = i; | ||
} | ||
} | ||
}; | ||
|
||
#endif /* PRIORITYSTORE_H */ |
Oops, something went wrong.