Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
ListSet.h
1 /*
2  * ListSet class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2017, IRIT UPS.
6  *
7  * ELM 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  * ELM 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 ELM; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_DATA_LISTSET_H_
22 #define ELM_DATA_LISTSET_H_
23 
24 #include "SortedList.h"
25 
26 namespace elm {
27 
28 template <class T, class C = Comparator<T>, class A = DefaultAlloc >
29 class ListSet: public SortedList<T, C, A> {
31 public:
32  typedef T t;
34  inline ListSet(void) { }
35  inline ListSet(const ListSet<T, C>& set): base_t(set) { }
36 
37  // Set concept
38  inline void insert(const T& v)
39  { if(!base_t::contains(v)) base_t::add(v); }
40 
41  bool subsetOf(const SortedList<T>& l) const {
42  auto i = l.base_t::begin(), j = base_t::begin();
43  for(; i() && j(); i++) {
44  int cmp = base_t::comparator().doCompare(*i, *j);
45  if(cmp > 0)
46  return false;
47  if(cmp == 0)
48  j++;
49  }
50  return !j();
51  }
52  inline bool operator<=(const SortedList<T>& l) const { return subsetOf(l); }
53  inline bool operator<(const SortedList<T>& l) const { return !equals(l) && subsetOf(l); }
54  inline bool operator>=(const SortedList<T>& l) const { return l.subsetOf(*this); }
55  inline bool operator>(const SortedList<T>& l) const { return !equals(l) && l.subsetOf(*this); }
56 
57  void join(const self_t& set) {
58  typename base_t::list_t::PrecIter i(base_t::list);
59  typename base_t::Iter j(set);
60  while(i() && j()) {
61  int c = base_t::comparator().doCompare(*i, *j);
62  if(c == 0) { i++; j++; }
63  else if(c < 0) i++;
64  else { base_t::list.addBefore(i, *j); j++; }
65  }
66  while(j()) { base_t::list.addBefore(i, *j); j++; }
67  }
68 
69  void meet(const self_t& set) {
70  typename base_t::list_t::PrecIter i(base_t::list);
71  typename base_t::Iter j(set);
72  while(i() && j()) {
73  int c = base_t::comparator().doCompare(*i, *j);
74  if(c == 0) { i++; j++; }
75  else if(c < 0) base_t::list.remove(i);
76  else j++;
77  }
78  while(i()) base_t::list.remove(i);
79  }
80 
81  void diff(const self_t& set) {
82  typename base_t::list_t::PrecIter i(base_t::list);
83  typename base_t::Iter j(set);
84  while(i() && j()) {
85  int c = base_t::comparator().doCompare(*i, *j);
86  if(c == 0) { base_t::list.remove(i); j++; }
87  else if(c < 0) i++;
88  else j++;
89  }
90  }
91 
92  inline self_t& operator+=(const self_t& set) { join(set); return *this; }
93  inline self_t& operator|=(const self_t& set) { join(set); return *this; }
94  inline self_t& operator*=(const self_t& set) { meet(set); return *this; }
95  inline self_t& operator&=(const self_t& set) { meet(set); return *this; }
96  inline self_t& operator-=(const self_t& set) { diff(set); return *this; }
97 
98  inline self_t& operator+(const self_t& set) { self_t res = *this; res.join(set); return res; }
99  inline self_t& operator|(const self_t& set) { self_t res = *this; res.join(set); return res; }
100  inline self_t& operator*(const self_t& set) { self_t res = *this; res.meet(set); return res; }
101  inline self_t& operator&(const self_t& set) { self_t res = *this; res.meet(set); return res; }
102  inline self_t& operator-(const self_t& set) { self_t res = *this; res.diff(set); return res; }
103 
104  // MutableCollection concept fix
105  inline void add(const T& v) { insert(v); }
106  inline self_t& operator+=(const T& val) { insert(val); return *this; }
107  inline self_t& operator-=(const T& val) { base_t::remove(val); return *this; }
108 
109 };
110 
111 template <class T, class M>
112 inline bool operator<=(const T& val, const ListSet<T, M>& set) { return set.contains(val); }
113 
114 } // elm
115 
116 #endif /* ELM_DATA_LISTSET_H_ */
elm::ListSet::ListSet
ListSet(void)
Definition: ListSet.h:34
elm::ListSet::operator<
bool operator<(const SortedList< T > &l) const
Definition: ListSet.h:53
elm::operator<=
bool operator<=(const T &v, const FragTable< T, E, A > &t)
Definition: FragTable.h:158
elm::ListSet::operator-=
self_t & operator-=(const self_t &set)
Definition: ListSet.h:96
elm::ListSet
Definition: ListSet.h:29
elm::ListSet::operator*=
self_t & operator*=(const self_t &set)
Definition: ListSet.h:94
elm::SortedList::add
void add(const T &value)
Definition: SortedList.h:85
elm::SortedList::contains
bool contains(const T &item) const
Definition: SortedList.h:53
elm::SortedList
Definition: SortedList.h:34
elm::ListSet::operator>
bool operator>(const SortedList< T > &l) const
Definition: ListSet.h:55
elm::ListSet::operator+=
self_t & operator+=(const T &val)
Definition: ListSet.h:106
elm::SortedList::list
list_t list
Definition: SortedList.h:126
elm::ListSet::self_t
ListSet< T, C, A > self_t
Definition: ListSet.h:33
elm::ListSet::add
void add(const T &v)
Definition: ListSet.h:105
elm::ListSet::diff
void diff(const self_t &set)
Definition: ListSet.h:81
elm::ListSet::operator<=
bool operator<=(const SortedList< T > &l) const
Definition: ListSet.h:52
elm::SortedList::remove
void remove(const T &item)
Definition: SortedList.h:96
elm::ListSet::operator|
self_t & operator|(const self_t &set)
Definition: ListSet.h:99
elm::ListSet::t
T t
Definition: ListSet.h:32
elm::ListSet::operator>=
bool operator>=(const SortedList< T > &l) const
Definition: ListSet.h:54
elm::ListSet::operator-
self_t & operator-(const self_t &set)
Definition: ListSet.h:102
elm
Definition: adapter.h:26
elm::SortedList< T, Comparator< T >, DefaultAlloc >::equals
bool equals(const SortedList< T > &l) const
Definition: SortedList.h:71
elm::ListSet::operator*
self_t & operator*(const self_t &set)
Definition: ListSet.h:100
elm::ListSet::operator&
self_t & operator&(const self_t &set)
Definition: ListSet.h:101
elm::ListSet::operator&=
self_t & operator&=(const self_t &set)
Definition: ListSet.h:95
elm::ListSet::operator|=
self_t & operator|=(const self_t &set)
Definition: ListSet.h:93
elm::ListSet::operator+=
self_t & operator+=(const self_t &set)
Definition: ListSet.h:92
elm::ListSet::join
void join(const self_t &set)
Definition: ListSet.h:57
elm::List::remove
void remove(const T &value)
Definition: List.h:136
elm::List::addBefore
void addBefore(PrecIter &pos, const T &value)
Definition: List.h:161
elm::ListSet::ListSet
ListSet(const ListSet< T, C > &set)
Definition: ListSet.h:35
elm::SortedList< T, Comparator< T >, DefaultAlloc >::set
void set(Iter i, const T &val)
Definition: SortedList.h:125
elm::ListSet::operator-=
self_t & operator-=(const T &val)
Definition: ListSet.h:107
elm::ListSet::meet
void meet(const self_t &set)
Definition: ListSet.h:69
elm::ListSet::insert
void insert(const T &v)
Definition: ListSet.h:38
elm::ListSet::operator+
self_t & operator+(const self_t &set)
Definition: ListSet.h:98
elm::ListSet::subsetOf
bool subsetOf(const SortedList< T > &l) const
Definition: ListSet.h:41
elm::SortedList::Iter
Definition: SortedList.h:60
elm::SortedList::begin
Iter begin(void) const
Definition: SortedList.h:68
elm::SortedList::comparator
C & comparator()
Definition: SortedList.h:43