8#ifndef Sawyer_Container_Set_H
9#define Sawyer_Container_Set_H
11#include <Sawyer/Interval.h>
12#include <Sawyer/Sawyer.h>
14#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
16 #include <boost/version.hpp>
17 #if BOOST_VERSION == 107400
18 #include <boost/serialization/library_version_type.hpp>
20 #include <boost/serialization/serialization.hpp>
21 #include <boost/serialization/set.hpp>
24#ifdef SAWYER_HAVE_CEREAL
25#include <cereal/types/set.hpp>
28#include <boost/foreach.hpp>
29#include <boost/range/iterator_range.hpp>
55template<
typename T,
class C = std::less<T>,
class A = std::allocator<T> >
57 typedef std::set<T, C, A> InternalSet;
68#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
70 friend class boost::serialization::access;
73 void serialize(S &s,
const unsigned ) {
74 s & BOOST_SERIALIZATION_NVP(set_);
78#ifdef SAWYER_HAVE_CEREAL
80 friend class cereal::access;
82 template<
class Archive>
83 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
84 archive(CEREAL_NVP(set_));
96 : set_(comparator, allocator) {}
115 template<
class InputIterator>
116 Set(InputIterator begin, InputIterator end,
118 : set_(begin, end, comparator, allocator) {}
120 template<
class InputIterator>
121 explicit Set(
const boost::iterator_range<InputIterator> &range,
123 : set_(range.begin(), range.end()) {}
128 : set_(other.set_) {}
143 boost::iterator_range<ConstIterator>
values()
const {
144 return boost::iterator_range<ConstIterator>(set_.begin(), set_.end());
162 return 1 == set_.count(value);
169 BOOST_FOREACH (
const Value &otherValue, other.
values()) {
180 BOOST_FOREACH (
const Value &otherValue, other.
values()) {
199 return *set_.begin();
225 return set_.size() == other.set_.size() && std::equal(set_.begin(), set_.end(), other.set_.begin());
233 return (set_.size() != other.set_.size() ||
234 std::mismatch(set_.begin(), set_.end(), other.set_.begin()).first != set_.end());
240 explicit operator bool()
const {
259 return set_.insert(value).second;
267 bool isInserted =
false;
268 BOOST_FOREACH (
const Value &value,
values.values()) {
269 if (set_.insert(value).second)
279 return 1 == set_.erase(value);
287 bool isErased =
false;
288 BOOST_FOREACH (
const Value &value,
values.values()) {
289 if (1 == set_.erase(value))
306 std::vector<Value> toErase;
307 toErase.reserve(set_.size());
308 BOOST_FOREACH (
const Value &value, set_) {
310 toErase.push_back(value);
312 BOOST_FOREACH (
const Value &value, toErase)
340 std::vector<Value> toErase;
341 toErase.reserve(set_.size());
342 BOOST_FOREACH (
const Value &value, set_) {
344 toErase.push_back(value);
346 BOOST_FOREACH (
const Value &value, toErase)
377 return *
this | other;
Range of values delimited by endpoints.
static Interval hull(T v1, T v2)
Construct an interval from two endpoints.
Set & operator|=(const Set &other)
Unions this set with another.
bool insert(const Value &value)
Insert a value.
Set operator&(const Set &other) const
Compute the intersection of this set with another.
Set & operator=(const Set &other)
Assignment operator.
Set(const Value &value)
Singleton constructor.
Set & operator-=(const Set &other)
Differences two sets.
bool erase(const Set &values)
Erase multiple values.
Set operator-(const Set &other) const
Compute the difference of this set with another.
Set & operator&=(const Set &other)
Intersects this set with another.
size_t size() const
Size of the set.
Set(InputIterator begin, InputIterator end, const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Iterative constructor.
C Comparator
How to compare values with each other.
InternalSet::const_iterator ConstIterator
Iterator for traversing values stored in the set.
Set operator|(const Set &other) const
Compute the union of this set with another.
bool exists(const Value &value) const
Whether a value exists.
bool erase(const Value &value)
Erase a value.
Interval< Value > hull() const
Range of members.
bool existsAny(const Set &other) const
Whether any value exists.
Set(const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Default constructor.
bool insert(const Set &values)
Insert multiple values.
Set(const boost::iterator_range< InputIterator > &range, const Comparator &=Comparator(), const Allocator &=Allocator())
Iterative constructor.
bool operator!=(const Set &other) const
Whether two sets do not contain the same members.
bool existsAll(const Set &other) const
Whether all values exist.
bool isEmpty() const
Whether the set is empty.
Value least() const
Smallest member.
boost::iterator_range< ConstIterator > values() const
Value iterator range.
Set operator+(const Set &other) const
Compute the union of this set with another.
T Value
Type of values stored in this set.
Value greatest() const
Largest member.
Set & operator+=(const Set &other)
Unions this set with another.
bool operator==(const Set &other) const
Whether two sets contain the same members.
A Allocator
How to allocate storge for new values.
Set(const Set &other)
Copy constructor.
bool operator!() const
Whether the set is empty.
void clear()
Erase all values.