Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Set.h
1 /*
2  * avl::Set class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2013, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_AVL_SET_H_
22 #define ELM_AVL_SET_H_
23 
24 #include <elm/avl/GenTree.h>
25 
26 namespace elm { namespace avl {
27 
28 template <class T, class C = elm::Comparator<T> >
29 class Set: public GenTree<T, IdAdapter<T>, C> {
30 public:
31  typedef T t;
32  typedef Set<T, C> self_t;
34 
35  // MutableCollection concept
36  inline self_t& operator+=(const T& x) { insert(x); return *this; }
37  inline self_t& operator-=(const T& x) { base_t::remove(x); return *this; }
38  inline self_t& operator=(const self_t& s) { base_t::copy(s); return *this; }
39 
40  // Set concept
41  inline void insert(const T& x) { base_t::add(x); }
42 
43  bool subsetOf(const Set<T, C>& s) const {
44  auto i = base_t::begin(); auto j = s.begin();
45  while(i() && j()) {
46  int c = C::doCompare(*i, *j);
47  if(c == 0) i++;
48  else if(c < 0) return false;
49  j++;
50  }
51  return !i();
52  }
53 
54  inline void join(const Set<T, C>& s) { for(const auto x: s) base_t::add(x); }
55  inline void diff(const Set<T, C>& s) { for(const auto x: s) base_t::remove(x); }
56  void meet(const Set<T, C>& s) {
57  self_t is;
58  for(const auto x: *this) if(s.contains(x)) is.add(x);
59  diff(is);
60  }
61  inline self_t& operator+=(const Set<T, C>& s) { join(s); return *this; }
62  inline self_t& operator|=(const Set<T, C>& s) { join(s); return *this; }
63  inline self_t& operator-=(const Set<T, C>& s) { diff(s); return *this; }
64  inline self_t& operator&=(const Set<T, C>& s) { meet(s); return *this; }
65  inline self_t& operator*=(const Set<T, C>& s) { meet(s); return *this; }
66 
67  inline self_t operator+(const Set<T, C>& s) const { self_t r(*this); r.join(s); return s; }
68  inline self_t operator|(const Set<T, C>& s) const { self_t r(*this); r.join(s); return s; }
69  inline self_t operator-(const Set<T, C>& s) const { self_t r(*this); r.diff(s); return s; }
70  inline self_t operator*(const Set<T, C>& s) const { self_t r(*this); r.meet(s); return s; }
71  inline self_t operator&(const Set<T, C>& s) const { self_t r(*this); r.meet(s); return s; }
72 
73 };
74 
75 } } // elm::avl
76 
77 #endif /* ELM_AVL_SET_H_ */
elm::avl::Set::subsetOf
bool subsetOf(const Set< T, C > &s) const
Definition: Set.h:43
elm::avl::Set::t
T t
Definition: Set.h:31
elm::avl::Set::insert
void insert(const T &x)
Definition: Set.h:41
elm::avl::Set::operator+=
self_t & operator+=(const T &x)
Definition: Set.h:36
elm::avl::Set::operator=
self_t & operator=(const self_t &s)
Definition: Set.h:38
elm::avl::GenTree::remove
void remove(const T &x)
Definition: GenTree.h:232
elm::avl::GenTree< T, IdAdapter< T >, elm::Comparator< T > >::contains
bool contains(const typename IdAdapter< T > ::key_t &item) const
Definition: GenTree.h:158
elm::avl::Set::operator&
self_t operator&(const Set< T, C > &s) const
Definition: Set.h:71
elm::avl::Set::diff
void diff(const Set< T, C > &s)
Definition: Set.h:55
elm::avl::Set::operator&=
self_t & operator&=(const Set< T, C > &s)
Definition: Set.h:64
elm::avl::Set::meet
void meet(const Set< T, C > &s)
Definition: Set.h:56
elm::avl::GenTree::begin
Iter begin(void) const
Definition: GenTree.h:192
elm::avl::Set::operator|
self_t operator|(const Set< T, C > &s) const
Definition: Set.h:68
elm::avl::Set::base_t
GenTree< T, IdAdapter< T >, C > base_t
Definition: Set.h:33
elm::avl::Set::operator|=
self_t & operator|=(const Set< T, C > &s)
Definition: Set.h:62
elm::avl::Set::operator-
self_t operator-(const Set< T, C > &s) const
Definition: Set.h:69
elm::avl::Set::operator*=
self_t & operator*=(const Set< T, C > &s)
Definition: Set.h:65
elm
Definition: adapter.h:26
elm::avl::Set::operator+
self_t operator+(const Set< T, C > &s) const
Definition: Set.h:67
elm::avl::Set::operator*
self_t operator*(const Set< T, C > &s) const
Definition: Set.h:70
elm::avl::GenTree::add
void add(const T &item)
Definition: GenTree.h:222
elm::avl::Set
Definition: Set.h:29
elm::avl::Set::join
void join(const Set< T, C > &s)
Definition: Set.h:54
elm::avl::GenTree
Definition: GenTree.h:97
elm::avl::Set::operator-=
self_t & operator-=(const Set< T, C > &s)
Definition: Set.h:63
elm::avl::Set::self_t
Set< T, C > self_t
Definition: Set.h:32
elm::avl::Set::operator+=
self_t & operator+=(const Set< T, C > &s)
Definition: Set.h:61
elm::is
static bool is(unsigned char c, t::uint64 set[])
Definition: string_Char.cpp:36
elm::avl::GenTree::copy
void copy(const GenTree< T, K, C > &tree)
Definition: GenTree.h:242
elm::avl::Set::operator-=
self_t & operator-=(const T &x)
Definition: Set.h:37