ROSE 0.11.145.192
Set.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_Set_H
9#define Sawyer_Container_Set_H
10
11#include <Sawyer/Interval.h>
12#include <Sawyer/Sawyer.h>
13
14#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
15 // Work around a bug in boost::serialization for 1.74.0
16 #include <boost/version.hpp>
17 #if BOOST_VERSION == 107400
18 #include <boost/serialization/library_version_type.hpp>
19 #endif
20 #include <boost/serialization/serialization.hpp> // needed by <boost/serialization/set.hpp> in boost 1.58 - 1.60
21 #include <boost/serialization/set.hpp>
22#endif
23
24#ifdef SAWYER_HAVE_CEREAL
25#include <cereal/types/set.hpp>
26#endif
27
28#include <boost/foreach.hpp>
29#include <boost/range/iterator_range.hpp>
30#include <vector>
31
32namespace Sawyer {
33namespace Container {
34
55template<typename T, class C = std::less<T>, class A = std::allocator<T> >
56class Set {
57 typedef std::set<T, C, A> InternalSet;
58 InternalSet set_;
59public:
60 typedef T Value;
61 typedef C Comparator;
62 typedef A Allocator;
63 typedef typename InternalSet::const_iterator ConstIterator;
66 // Serialization
68#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
69private:
70 friend class boost::serialization::access;
71
72 template<class S>
73 void serialize(S &s, const unsigned /*version*/) {
74 s & BOOST_SERIALIZATION_NVP(set_);
75 }
76#endif
77
78#ifdef SAWYER_HAVE_CEREAL
79private:
80 friend class cereal::access;
81
82 template<class Archive>
83 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
84 archive(CEREAL_NVP(set_));
85 }
86#endif
87
89 // Construction
91public:
95 explicit Set(const Comparator &comparator = Comparator(), const Allocator &allocator = Allocator())
96 : set_(comparator, allocator) {}
97
101 Set(const Value &value) /*implicit*/ {
102 set_.insert(value);
103 }
104
115 template<class InputIterator>
116 Set(InputIterator begin, InputIterator end,
117 const Comparator &comparator = Comparator(), const Allocator &allocator = Allocator())
118 : set_(begin, end, comparator, allocator) {}
119
120 template<class InputIterator>
121 explicit Set(const boost::iterator_range<InputIterator> &range,
122 const Comparator &/*comparator*/ = Comparator(), const Allocator &/*allocator*/ = Allocator())
123 : set_(range.begin(), range.end()) {}
127 Set(const Set &other)
128 : set_(other.set_) {}
129
131 Set& operator=(const Set &other) {
132 set_ = other.set_;
133 return *this;
134 }
135
137 // Iterators
139public:
143 boost::iterator_range<ConstIterator> values() const {
144 return boost::iterator_range<ConstIterator>(set_.begin(), set_.end());
145 }
146
148 // Predicates and queries
150public:
154 bool isEmpty() const {
155 return set_.empty();
156 }
157
161 bool exists(const Value &value) const {
162 return 1 == set_.count(value);
163 }
164
168 bool existsAny(const Set &other) const {
169 BOOST_FOREACH (const Value &otherValue, other.values()) {
170 if (exists(otherValue))
171 return true;
172 }
173 return false;
174 }
175
179 bool existsAll(const Set &other) const {
180 BOOST_FOREACH (const Value &otherValue, other.values()) {
181 if (!exists(otherValue))
182 return false;
183 }
184 return true;
185 }
186
190 size_t size() const {
191 return set_.size();
192 }
193
197 Value least() const {
198 ASSERT_forbid(isEmpty());
199 return *set_.begin();
200 }
201
205 Value greatest() const {
206 ASSERT_forbid(isEmpty());
207 ConstIterator i = set_.end();
208 --i;
209 return *i;
210 }
211
216 if (isEmpty())
217 return Interval<Value>();
219 }
220
224 bool operator==(const Set &other) const {
225 return set_.size() == other.set_.size() && std::equal(set_.begin(), set_.end(), other.set_.begin());
226 }
227
232 bool operator!=(const Set &other) const {
233 return (set_.size() != other.set_.size() ||
234 std::mismatch(set_.begin(), set_.end(), other.set_.begin()).first != set_.end());
235 }
236
240 explicit operator bool() const {
241 return !isEmpty();
242 }
243
247 bool operator!() const {
248 return isEmpty();
249 }
250
252 // Mutators
254public:
258 bool insert(const Value &value) {
259 return set_.insert(value).second;
260 }
261
266 bool insert(const Set &values) {
267 bool isInserted = false;
268 BOOST_FOREACH (const Value &value, values.values()) {
269 if (set_.insert(value).second)
270 isInserted = true;
271 }
272 return isInserted;
273 }
274
278 bool erase(const Value &value) {
279 return 1 == set_.erase(value);
280 }
281
286 bool erase(const Set &values) {
287 bool isErased = false;
288 BOOST_FOREACH (const Value &value, values.values()) {
289 if (1 == set_.erase(value))
290 isErased = true;
291 }
292 return isErased;
293 }
294
298 void clear() {
299 set_.clear();
300 }
301
305 Set& operator&=(const Set &other) {
306 std::vector<Value> toErase;
307 toErase.reserve(set_.size());
308 BOOST_FOREACH (const Value &value, set_) {
309 if (!other.exists(value))
310 toErase.push_back(value);
311 }
312 BOOST_FOREACH (const Value &value, toErase)
313 set_.erase(value);
314 return *this;
315 }
316
322 Set& operator|=(const Set &other) {
323 BOOST_FOREACH (const Value &v, other.values())
324 set_.insert(v);
325 return *this;
326 }
327
328 Set& operator+=(const Set &other) {
329 BOOST_FOREACH (const Value &v, other.values())
330 set_.insert(v);
331 return *this;
332 }
339 Set& operator-=(const Set &other) {
340 std::vector<Value> toErase;
341 toErase.reserve(set_.size());
342 BOOST_FOREACH (const Value &value, set_) {
343 if (other.exists(value))
344 toErase.push_back(value);
345 }
346 BOOST_FOREACH (const Value &value, toErase)
347 set_.erase(value);
348 return *this;
349 }
350
351
353 // Set-theoretic operations
355public:
359 Set operator&(const Set &other) const {
360 Set retval = *this;
361 retval &= other;
362 return retval;
363 }
364
370 Set operator|(const Set &other) const {
371 Set retval = *this;
372 retval |= other;
373 return retval;
374 }
375
376 Set operator+(const Set &other) const {
377 return *this | other;
378 }
384 Set operator-(const Set &other) const {
385 Set retval = *this;
386 retval -= other;
387 return retval;
388 }
389};
390
391} // namespace
392} // namespace
393
394#endif
Range of values delimited by endpoints.
Definition Interval.h:31
static Interval hull(T v1, T v2)
Construct an interval from two endpoints.
Definition Interval.h:162
Ordered set of values.
Definition Set.h:56
Set & operator|=(const Set &other)
Unions this set with another.
Definition Set.h:322
bool insert(const Value &value)
Insert a value.
Definition Set.h:258
Set operator&(const Set &other) const
Compute the intersection of this set with another.
Definition Set.h:359
Set & operator=(const Set &other)
Assignment operator.
Definition Set.h:131
Set(const Value &value)
Singleton constructor.
Definition Set.h:101
Set & operator-=(const Set &other)
Differences two sets.
Definition Set.h:339
bool erase(const Set &values)
Erase multiple values.
Definition Set.h:286
Set operator-(const Set &other) const
Compute the difference of this set with another.
Definition Set.h:384
Set & operator&=(const Set &other)
Intersects this set with another.
Definition Set.h:305
size_t size() const
Size of the set.
Definition Set.h:190
Set(InputIterator begin, InputIterator end, const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Iterative constructor.
Definition Set.h:116
C Comparator
How to compare values with each other.
Definition Set.h:61
InternalSet::const_iterator ConstIterator
Iterator for traversing values stored in the set.
Definition Set.h:63
Set operator|(const Set &other) const
Compute the union of this set with another.
Definition Set.h:370
bool exists(const Value &value) const
Whether a value exists.
Definition Set.h:161
bool erase(const Value &value)
Erase a value.
Definition Set.h:278
Interval< Value > hull() const
Range of members.
Definition Set.h:215
bool existsAny(const Set &other) const
Whether any value exists.
Definition Set.h:168
Set(const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Default constructor.
Definition Set.h:95
bool insert(const Set &values)
Insert multiple values.
Definition Set.h:266
Set(const boost::iterator_range< InputIterator > &range, const Comparator &=Comparator(), const Allocator &=Allocator())
Iterative constructor.
Definition Set.h:121
bool operator!=(const Set &other) const
Whether two sets do not contain the same members.
Definition Set.h:232
bool existsAll(const Set &other) const
Whether all values exist.
Definition Set.h:179
bool isEmpty() const
Whether the set is empty.
Definition Set.h:154
Value least() const
Smallest member.
Definition Set.h:197
boost::iterator_range< ConstIterator > values() const
Value iterator range.
Definition Set.h:143
Set operator+(const Set &other) const
Compute the union of this set with another.
Definition Set.h:376
T Value
Type of values stored in this set.
Definition Set.h:60
Value greatest() const
Largest member.
Definition Set.h:205
Set & operator+=(const Set &other)
Unions this set with another.
Definition Set.h:328
bool operator==(const Set &other) const
Whether two sets contain the same members.
Definition Set.h:224
A Allocator
How to allocate storge for new values.
Definition Set.h:62
Set(const Set &other)
Copy constructor.
Definition Set.h:127
bool operator!() const
Whether the set is empty.
Definition Set.h:247
void clear()
Erase all values.
Definition Set.h:298
Sawyer support library.