ROSE 0.11.145.192
Tracker.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_Container_Tracker_H
9#define Sawyer_Container_Tracker_H
10
11#include <Sawyer/Set.h>
12#include <Sawyer/Synchronization.h>
13
14#include <boost/unordered/unordered_set.hpp>
15
16namespace Sawyer {
17namespace Container {
18
23template<class Key>
25 Set<Key> set;
26public:
27 void clear() {
28 set.clear();
29 }
30 bool exists(const Key &key) const {
31 return set.exists(key);
32 }
33 bool insert(const Key &key) {
34 return set.insert(key);
35 }
36};
37
42template<class Key>
44 std::vector<bool> bvec;
45public:
46 void clear() {
47 bvec.clear();
48 }
49 bool exists(const Key &key) const {
50 return key < bvec.size() && bvec[key];
51 }
52 bool insert(const Key &key) {
53 bool retval = !exists(key);
54 if (key >= bvec.size())
55 bvec.resize(key+1, false);
56 bvec[key] = true;
57 return retval;
58 }
59};
60
65template<class Key>
67 boost::unordered_set<Key> set;
68public:
69 void clear() {
70 set.clear();
71 }
72 bool exists(const Key &key) const {
73 return set.find(key) != set.end();
74 }
75 bool insert(const Key &key) {
76 return set.insert(key).second;
77 }
78};
79
81template<class Key>
91
166template<class T, class K = T, class Traits = TrackerTraits<K> >
167class Tracker {
168public:
170 typedef T Value;
171
175 typedef K Key;
176
177private:
178 mutable SAWYER_THREAD_TRAITS::Mutex mutex_; // protects the following data members.
179 typename Traits::Index index_;
180
181public:
185 void clear() {
186 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
187 index_.clear();
188 }
189
195 bool testAndSet(const Value &value) {
196 Key key(value);
197 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
198 return !index_.insert(key);
199 }
200
202 bool operator()(const Value &value) {
203 return testAndSet(value);
204 }
205
211 bool wasSeen(const Value &value) const {
212 Key key(value);
213 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
214 return index_.exists(key);
215 }
216
223 bool insert(const Value &value) {
224 return !testAndSet(value);
225 }
226
247 void removeIfSeen(std::vector<Value> &vector) {
248 size_t nSaved = 0;
249 for (size_t i = 0; i < vector.size(); ++i) {
250 if (insert(vector[i]))
251 vector[nSaved++] = vector[i];
252 }
253 vector.resize(nSaved);
254 }
255};
256
257} // namespace
258} // namespace
259
260#endif
Ordered set of values.
Definition Set.h:56
Set-based index referenced by TrackerTraits.
Definition Tracker.h:24
Hash-based index referenced by TrackerTraits.
Definition Tracker.h:66
Vector-based index referenced by TrackerTraits.
Definition Tracker.h:43
Tracks whether something has been seen before.
Definition Tracker.h:167
K Key
Key type for the values represented by the tracker.
Definition Tracker.h:175
void clear()
Make this tracker forget everything it has seen.
Definition Tracker.h:185
bool wasSeen(const Value &value) const
Test whether a value has been encountered previously.
Definition Tracker.h:211
bool testAndSet(const Value &value)
Test and then insert the value.
Definition Tracker.h:195
bool operator()(const Value &value)
Unary operator is the same as testAndSet.
Definition Tracker.h:202
void removeIfSeen(std::vector< Value > &vector)
Remove and track items from a vector.
Definition Tracker.h:247
bool insert(const Value &value)
Cause this tracker to see a value.
Definition Tracker.h:223
T Value
Type of values represented by the tracker.
Definition Tracker.h:170
Sawyer support library.
Traits for Tracker.
Definition Tracker.h:82
TrackerSetIndex< Key > Index
Type of index for storing member keys.
Definition Tracker.h:89