15 #include <initializer_list> 21 #include <hilti/rt/extension-points.h> 22 #include <hilti/rt/iterator.h> 23 #include <hilti/rt/types/set_fwd.h> 24 #include <hilti/rt/types/vector_fwd.h> 25 #include <hilti/rt/util.h> 35 std::weak_ptr<S*> _control;
36 typename S::V::iterator _iterator;
41 typename S::reference operator*()
const {
42 if (
auto&& l = _control.lock() ) {
44 if ( _iterator ==
static_cast<const std::set<T>&
>(**l).end() )
45 throw IndexError(
"iterator is invalid");
50 throw IndexError(
"iterator is invalid");
54 if ( ! _control.lock() )
55 throw IndexError(
"iterator is invalid");
68 if ( a._control.lock() != b._control.lock() )
69 throw InvalidArgument(
"cannot compare iterators into different sets");
71 return a._iterator == b._iterator;
74 friend bool operator!=(
const Iterator& a,
const Iterator& b) {
return ! (a == b); }
79 Iterator(
typename S::V::iterator iterator,
const typename S::C& control)
80 : _control(control), _iterator(std::move(iterator)) {}
107 class Set :
protected std::set<T> {
109 using V = std::set<T>;
110 using C = std::shared_ptr<Set<T>*>;
112 C _control = std::make_shared<Set<T>*>(
this);
114 using reference =
const T&;
115 using const_reference =
const T&;
121 using value_type = T;
123 using size_type = uint64_t;
126 Set(
const Set&) =
default;
127 Set(
Set&&) noexcept =
default;
128 Set(
const Vector<T>& l) : std::set<T>(l.begin(), l.end()) {}
129 Set(std::initializer_list<T> l) : std::set<T>(std::move(l)) {}
132 Set& operator=(
const Set&) =
default;
133 Set& operator=(
Set&&) noexcept =
default;
140 bool contains(
const T& t)
const {
return this->count(t); }
142 auto begin()
const {
return iterator(static_cast<const V&>(*this).begin(), empty() ? nullptr : _control); }
143 auto end()
const {
return iterator(static_cast<const V&>(*this).end(), empty() ? nullptr : _control); }
145 size_type size()
const {
return V::size(); }
154 size_type
erase(
const key_type& key) {
156 _control = std::make_shared<Set<T>*>();
158 return static_cast<V&
>(*this).erase(key);
167 _control = std::make_shared<Set<T>*>();
169 return static_cast<V&
>(*this).clear();
176 friend bool operator==(
const Set& a,
const Set& b) {
return static_cast<const V&
>(a) == static_cast<const V&>(b); }
177 friend bool operator!=(
const Set& a,
const Set& b) {
return ! (a == b); }
186 inline bool operator==(
const Empty& ,
const Empty& ) {
return true; }
189 inline bool operator==(
const Set<T>& v,
const Empty& ) {
194 inline bool operator==(
const Empty& ,
const Set<T>& v) {
198 inline bool operator!=(
const Empty& ,
const Empty& ) {
return false; }
201 inline bool operator!=(
const Set<T>& v,
const Empty& ) {
206 inline bool operator!=(
const Empty& ,
const Set<T>& v) {
211 namespace detail::adl {
221 return "<set iterator>";
226 inline std::ostream& operator<<(std::ostream& out, const Set<T>& x) {
231 inline std::ostream& operator<<(std::ostream& out,
const set::Empty& x) {
237 inline std::ostream& operator<<(std::ostream& out, const set::Iterator<T>& x) {
std::string to_string(T &&x)
Definition: extension-points.h:26
size_type erase(const key_type &key)
Definition: set.h:154
auto transform(const std::vector< X > &x, F f)
Definition: util.h:299
bool contains(const T &t) const
Definition: set.h:140
void clear()
Definition: set.h:165
std::string join(const T &l, const std::string &delim="")
Definition: util.h:281
std::string fmt(const char *fmt, const Args &... args)
Definition: fmt.h:13