8 #ifndef Sawyer_Container_Set_H
9 #define Sawyer_Container_Set_H
11 #include <Sawyer/Interval.h>
12 #include <Sawyer/Sawyer.h>
15 #include <boost/version.hpp>
16 #if BOOST_VERSION == 107400
17 #include <boost/serialization/library_version_type.hpp>
20 #include <boost/foreach.hpp>
21 #include <boost/range/iterator_range.hpp>
22 #include <boost/serialization/serialization.hpp>
23 #include <boost/serialization/access.hpp>
24 #include <boost/serialization/nvp.hpp>
25 #include <boost/serialization/set.hpp>
51 template<
typename T,
class C = std::less<T>,
class A = std::allocator<T> >
53 typedef std::set<T, C, A> InternalSet;
65 friend class boost::serialization::access;
68 void serialize(S &s,
const unsigned ) {
69 s & BOOST_SERIALIZATION_NVP(set_);
80 : set_(comparator, allocator) {}
99 template<
class InputIterator>
100 Set(InputIterator begin, InputIterator end,
102 : set_(begin, end, comparator, allocator) {}
104 template<
class InputIterator>
105 explicit Set(
const boost::iterator_range<InputIterator> &range,
107 : set_(range.begin(), range.end()) {}
112 : set_(other.set_) {}
127 boost::iterator_range<ConstIterator>
values()
const {
128 return boost::iterator_range<ConstIterator>(set_.begin(), set_.end());
146 return 1 == set_.count(value);
153 BOOST_FOREACH (
const Value &otherValue, other.
values()) {
164 BOOST_FOREACH (
const Value &otherValue, other.
values()) {
183 return *set_.begin();
209 return set_.size() == other.set_.size() && std::equal(set_.begin(), set_.end(), other.set_.begin());
217 return (set_.size() != other.set_.size() ||
218 std::mismatch(set_.begin(), set_.end(), other.set_.begin()).first != set_.end());
229 return set_.insert(value).second;
237 bool isInserted =
false;
238 BOOST_FOREACH (
const Value &value,
values.values()) {
239 if (set_.insert(value).second)
249 return 1 == set_.erase(value);
257 bool isErased =
false;
258 BOOST_FOREACH (
const Value &value,
values.values()) {
259 if (1 == set_.erase(value))
276 std::vector<Value> toErase;
277 toErase.reserve(set_.size());
278 BOOST_FOREACH (
const Value &value, set_) {
280 toErase.push_back(value);
282 BOOST_FOREACH (
const Value &value, toErase)
301 std::vector<Value> toErase;
302 toErase.reserve(set_.size());
303 BOOST_FOREACH (
const Value &value, set_) {
305 toErase.push_back(value);
307 BOOST_FOREACH (
const Value &value, toErase)
bool erase(const Value &value)
Erase a value.
Value greatest() const
Largest member.
Interval< Value > hull() const
Range of members.
bool erase(const Set &values)
Erase multiple values.
void clear()
Erase all values.
C Comparator
How to compare values with each other.
Set & operator-=(const Set &other)
Differences two sets.
Set & operator|=(const Set &other)
Unions this set with another.
bool insert(const Set &values)
Insert multiple values.
A Allocator
How to allocate storge for new values.
boost::iterator_range< ConstIterator > values() const
Value iterator range.
bool existsAll(const Set &other) const
Whether all values exist.
bool exists(const Value &value) const
Whether a value exists.
Set operator&(const Set &other) const
Compute the intersection of this set with another.
Set(InputIterator begin, InputIterator end, const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Iterative constructor.
bool isEmpty() const
Whether the set is empty.
Set(const Set &other)
Copy constructor.
Set operator|(const Set &other) const
Compute the union of this set with another.
Value least() const
Smallest member.
bool insert(const Value &value)
Insert a value.
Name space for the entire library.
static Interval hull(T v1, T v2)
Construct an interval from two endpoints.
Range of values delimited by endpoints.
Set operator-(const Set &other) const
Compute the difference of this set with another.
Set & operator=(const Set &other)
Assignment operator.
Set(const Value &value)
Singleton constructor.
bool existsAny(const Set &other) const
Whether any value exists.
bool operator!=(const Set &other) const
Whether two sets do not contain the same members.
size_t size() const
Size of the set.
Set(const boost::iterator_range< InputIterator > &range, const Comparator &=Comparator(), const Allocator &=Allocator())
Iterative constructor.
InternalSet::const_iterator ConstIterator
Iterator for traversing values stored in the set.
Set & operator&=(const Set &other)
Intersects this set with another.
T Value
Type of values stored in this set.
bool operator==(const Set &other) const
Whether two sets contain the same members.
Set(const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Default constructor.