15 #include <initializer_list> 21 #include <hilti/rt/extension-points.h> 22 #include <hilti/rt/iterator.h> 23 #include <hilti/rt/safe-int.h> 24 #include <hilti/rt/types/set_fwd.h> 25 #include <hilti/rt/types/vector_fwd.h> 26 #include <hilti/rt/util.h> 36 std::weak_ptr<S*> _control;
37 typename S::V::iterator _iterator;
42 typename S::reference operator*()
const {
43 if (
auto&& l = _control.lock() ) {
45 if ( _iterator ==
static_cast<const std::set<T>&
>(**l).end() )
46 throw IndexError(
"iterator is invalid");
51 throw IndexError(
"iterator is invalid");
55 if ( ! _control.lock() )
56 throw IndexError(
"iterator is invalid");
69 if ( a._control.lock() != b._control.lock() )
70 throw InvalidArgument(
"cannot compare iterators into different sets");
72 return a._iterator == b._iterator;
75 friend bool operator!=(
const Iterator& a,
const Iterator& b) {
return ! (a == b); }
80 Iterator(
typename S::V::iterator iterator,
const typename S::C& control)
81 : _control(control), _iterator(std::move(iterator)) {}
108 class Set :
protected std::set<T> {
110 using V = std::set<T>;
111 using C = std::shared_ptr<Set<T>*>;
113 C _control = std::make_shared<Set<T>*>(
this);
115 using reference =
const T&;
116 using const_reference =
const T&;
122 using value_type = T;
124 using size_type = integer::safe<uint64_t>;
127 Set(
const Set&) =
default;
128 Set(
Set&&) noexcept =
default;
129 Set(
const Vector<T>& l) : std::set<T>(l.begin(), l.end()) {}
130 Set(std::initializer_list<T> l) : std::set<T>(std::move(l)) {}
133 Set& operator=(
const Set&) =
default;
134 Set& operator=(
Set&&) noexcept =
default;
141 bool contains(
const T& t)
const {
return this->count(t); }
143 auto begin()
const {
return iterator(static_cast<const V&>(*this).begin(), empty() ? nullptr : _control); }
144 auto end()
const {
return iterator(static_cast<const V&>(*this).end(), empty() ? nullptr : _control); }
146 size_type size()
const {
return V::size(); }
155 size_type
erase(
const key_type& key) {
157 _control = std::make_shared<Set<T>*>();
159 return static_cast<V&
>(*this).erase(key);
168 _control = std::make_shared<Set<T>*>();
170 return static_cast<V&
>(*this).clear();
179 iterator
insert(iterator hint,
const T& value) {
180 auto it = V::insert(hint._iterator, value);
181 return iterator(it, _control);
188 friend bool operator==(
const Set& a,
const Set& b) {
return static_cast<const V&
>(a) == static_cast<const V&>(b); }
189 friend bool operator!=(
const Set& a,
const Set& b) {
return ! (a == b); }
198 inline bool operator==(
const Empty& ,
const Empty& ) {
return true; }
201 inline bool operator==(
const Set<T>& v,
const Empty& ) {
206 inline bool operator==(
const Empty& ,
const Set<T>& v) {
210 inline bool operator!=(
const Empty& ,
const Empty& ) {
return false; }
213 inline bool operator!=(
const Set<T>& v,
const Empty& ) {
218 inline bool operator!=(
const Empty& ,
const Set<T>& v) {
223 namespace detail::adl {
233 return "<set iterator>";
238 inline std::ostream& operator<<(std::ostream& out, const Set<T>& x) {
243 inline std::ostream& operator<<(std::ostream& out,
const set::Empty& x) {
250 inline std::ostream& operator<<(std::ostream& out, const Iterator<T>& x) {
iterator insert(iterator hint, const T &value)
Definition: set.h:179
std::string to_string(T &&x)
Definition: extension-points.h:26
size_type erase(const key_type &key)
Definition: set.h:155
auto transform(const C &x, F f)
Definition: util.h:332
bool contains(const T &t) const
Definition: set.h:141
void clear()
Definition: set.h:166
std::string join(const T &l, const std::string &delim="")
Definition: util.h:282
std::string fmt(const char *fmt, const Args &... args)
Definition: fmt.h:13