Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
HashSet.h
1 /*
2  * HashSet class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2016, 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_DATA_HASHSET_H_
22 #define ELM_DATA_HASHSET_H_
23 
24 #include "List.h"
25 #include "HashTable.h"
26 #include <elm/adapter.h>
27 
28 namespace elm {
29 
30 template <class T, class H = HashKey<T>, class A = DefaultAlloc>
31 class HashSet {
32  typedef HashTable<T, H, A> tab_t;
33 public:
35 
36  inline HashSet(int size = 211): _tab(size) { }
37  inline HashSet(const HashSet<T>& s): _tab(s._tab) { }
38  inline const H& hash() const { return _tab.hash(); }
39  inline H& hash() { return _tab.hash(); }
40  inline const A& allocator() const { return _tab.allocator(); }
41  inline A& allocator() { return _tab.allocator(); }
42 
43  // Collection concept
44  inline int count(void) const { return _tab.count(); }
45  inline bool contains(const T& val) const { return _tab.hasKey(val); }
46  template <class C> inline bool containsAll(const C& coll)
47  { for(typename C::Iter i(coll); i(); i++) if(!contains(*i)) return false; return true; }
48  inline bool isEmpty(void) const { return _tab.isEmpty(); }
49  inline operator bool(void) const { return !isEmpty(); }
50 
51  class Iter: public InplacePreIterator<Iter, T> {
52  friend class HashSet;
53  public:
54  inline Iter(const HashSet& set): i(set._tab) { }
55  inline Iter(const HashSet& set, bool end): i(set._tab, end) { }
56  inline bool ended(void) const { return i.ended(); }
57  inline const T& item(void) const { return i.item(); }
58  inline void next(void) { i.next(); }
59  inline bool equals(const Iter& it) const { return i.equals(it.i); }
60  private:
61  typename tab_t::Iter i;
62  };
63  inline Iter begin(void) const { return Iter(*this); }
64  inline Iter end(void) const { return Iter(*this, true); }
65 
66  inline bool equals(const HashSet<T>& s) const
67  { return _tab.equals(s._tab); }
68  inline bool operator==(const HashSet<T>& s) const { return equals(s); }
69  inline bool operator!=(const HashSet<T>& s) const { return !equals(s); }
70 
71  // MutableCollection concept
72  inline void clear(void) { _tab.clear(); }
73  inline void add(const T& val) { insert(val); }
74  template <class C> void addAll(const C& coll)
75  { for(typename C::Iter i(coll); i(); i++) add(*i); }
76  inline void remove(const T& val) { _tab.remove(val); }
77  template <class C> void removeAll(const C& c)
78  { for(const auto x: c) remove(x); }
79  inline void remove(const Iter& i) { _tab.remove(i.i); }
80  inline void copy(const HashSet<T>& s) { _tab.copy(s._tab); }
81  inline self_t operator=(const HashSet<T>& s) { copy(s); return *this; }
82  inline self_t operator+=(const T& x) { add(x); return *this; }
83  inline self_t operator-=(const T& x) { remove(x); return *this; }
84 
85  // Set concept
86  inline void insert(const T& val) { _tab.put(val); }
87  inline bool subsetOf(const HashSet<T>& s) const
88  { for(const auto x: *this) if(!s.contains(x)) return false; return true; }
89  inline bool operator<=(const HashSet<T>& s) const { return subsetOf(s); }
90  inline bool operator>=(const HashSet<T>& s) const { return s.subsetOf(*this); }
91  inline bool operator<(const HashSet<T>& s) const { return _tab < s._tab; }
92  inline bool operator>(const HashSet<T>& s) const { return _tab > s._tab; }
93  inline void join(const HashSet<T>& c)
94  { for(const auto x: c) insert(x); }
95  inline void diff(const HashSet<T>& c)
96  { for(const auto x: c) remove(x); }
97  void meet(const HashSet<T>& c) {
98  List<T> l;
99  for(const auto x: *this)
100  if(!c.contains(x))
101  l.add(x);
102  for(const auto x: l)
103  remove(x);
104  }
105  inline self_t& operator+=(const HashSet<T>& s) { join(s); return *this; }
106  inline self_t& operator|=(const HashSet<T>& s) { join(s); return *this; }
107  inline self_t& operator-=(const HashSet<T>& s) { diff(s); return *this; }
108  inline self_t& operator&=(const HashSet<T>& s) { meet(s); return *this; }
109  inline self_t& operator*=(const HashSet<T>& s) { meet(s); return *this; }
110  inline self_t operator+(const HashSet<T>& s) const
111  { self_t r(*this); r.join(s); return r; }
112  inline self_t operator|(const HashSet<T>& s) const
113  { self_t r(*this); r.join(s); return r; }
114  inline self_t operator-(const HashSet<T>& s) const
115  { self_t r(*this); r.diff(s); return r; }
116  inline self_t operator&(const HashSet<T>& s) const
117  { self_t r(*this); r.meet(s); return r; }
118  inline self_t operator*(const HashSet<T>& s) const
119  { self_t r(*this); r.meet(s); return r; }
120 
121  static const self_t null;
122 
123 # ifdef ELM_STAT
124  int minEntry(void) const { return _tab.minEntry(); }
125  int maxEntry(void) const { return _tab.maxEntry(); }
126  int zeroEntry(void) const { return _tab.zeroEntry(); }
127  int size(void) const { return _tab.size(); }
128 # endif
129 
130  // deprecated
131  inline Iter items(void) const { return Iter(*this); }
132  inline Iter operator*(void) const { return items(); }
133 
134 private:
135  tab_t _tab;
136 };
137 
138 template <class T, class H, class A>
139 const HashSet<T, H, A> HashSet<T, H, A>::null(1);
140 
141 } // elm
142 
143 #endif /* ELM_DATA_HASHSET_H_ */
elm::HashSet::insert
void insert(const T &val)
Definition: HashSet.h:86
elm::HashSet::HashSet
HashSet(const HashSet< T > &s)
Definition: HashSet.h:37
elm::List::add
void add(const T &value)
Definition: List.h:131
elm::HashSet::operator+=
self_t & operator+=(const HashSet< T > &s)
Definition: HashSet.h:105
elm::HashSet::operator<=
bool operator<=(const HashSet< T > &s) const
Definition: HashSet.h:89
elm::HashSet::operator&
self_t operator&(const HashSet< T > &s) const
Definition: HashSet.h:116
elm::HashSet::operator+=
self_t operator+=(const T &x)
Definition: HashSet.h:82
elm::HashSet::operator*
Iter operator*(void) const
Definition: HashSet.h:132
elm::HashSet::null
static const self_t null
Definition: HashSet.h:121
elm::HashSet::hash
H & hash()
Definition: HashSet.h:39
elm::HashSet::removeAll
void removeAll(const C &c)
Definition: HashSet.h:77
elm::HashSet::operator==
bool operator==(const HashSet< T > &s) const
Definition: HashSet.h:68
elm::HashSet::operator+
self_t operator+(const HashSet< T > &s) const
Definition: HashSet.h:110
elm::HashSet::count
int count(void) const
Definition: HashSet.h:44
elm::HashSet::operator*=
self_t & operator*=(const HashSet< T > &s)
Definition: HashSet.h:109
elm::HashSet::isEmpty
bool isEmpty(void) const
Definition: HashSet.h:48
elm::HashSet::operator*
self_t operator*(const HashSet< T > &s) const
Definition: HashSet.h:118
elm::HashTable::hash
const H & hash() const
Definition: HashTable.h:98
elm::HashSet::Iter::item
const T & item(void) const
Definition: HashSet.h:57
elm::HashSet::Iter::ended
bool ended(void) const
Definition: HashSet.h:56
elm::HashTable::Iter
Definition: HashTable.h:135
elm::HashSet::Iter::equals
bool equals(const Iter &it) const
Definition: HashSet.h:59
elm::HashSet::contains
bool contains(const T &val) const
Definition: HashSet.h:45
elm::HashTable::allocator
const A & allocator() const
Definition: HashTable.h:100
bool
elm::HashSet::operator<
bool operator<(const HashSet< T > &s) const
Definition: HashSet.h:91
elm::HashTable::put
void put(const T &data)
Definition: HashTable.h:114
elm::HashSet::operator-=
self_t operator-=(const T &x)
Definition: HashSet.h:83
elm::HashSet::Iter::Iter
Iter(const HashSet &set, bool end)
Definition: HashSet.h:55
elm::HashSet::clear
void clear(void)
Definition: HashSet.h:72
elm::HashSet::add
void add(const T &val)
Definition: HashSet.h:73
elm::HashTable::Iter::item
const T & item(void) const
Definition: HashTable.h:139
elm::HashTable::equals
bool equals(const HashTable< T > &h) const
Definition: HashTable.h:144
elm::HashSet::end
Iter end(void) const
Definition: HashSet.h:64
elm
Definition: adapter.h:26
elm::HashSet::operator!=
bool operator!=(const HashSet< T > &s) const
Definition: HashSet.h:69
elm::HashTable::copy
void copy(const HashTable< T, H > &t)
Definition: HashTable.h:194
elm::HashSet::addAll
void addAll(const C &coll)
Definition: HashSet.h:74
elm::HashTable::hasKey
bool hasKey(const T &key) const
Definition: HashTable.h:107
elm::HashSet::meet
void meet(const HashSet< T > &c)
Definition: HashSet.h:97
elm::HashSet::hash
const H & hash() const
Definition: HashSet.h:38
elm::t::size
uint64 size
Definition: arch.h:35
elm::HashSet::self_t
HashSet< T, H, A > self_t
Definition: HashSet.h:34
elm::HashSet::diff
void diff(const HashSet< T > &c)
Definition: HashSet.h:95
elm::HashSet::subsetOf
bool subsetOf(const HashSet< T > &s) const
Definition: HashSet.h:87
elm::HashSet::join
void join(const HashSet< T > &c)
Definition: HashSet.h:93
elm::HashSet::operator-
self_t operator-(const HashSet< T > &s) const
Definition: HashSet.h:114
elm::HashSet::HashSet
HashSet(int size=211)
Definition: HashSet.h:36
elm::HashSet::operator-=
self_t & operator-=(const HashSet< T > &s)
Definition: HashSet.h:107
elm::HashTable::count
int count(void) const
Definition: HashTable.h:124
elm::HashSet
Definition: HashSet.h:31
elm::HashTable::clear
void clear(void)
Definition: HashTable.h:152
elm::HashTable::remove
void remove(const T &key)
Definition: HashTable.h:166
elm::HashSet::items
Iter items(void) const
Definition: HashSet.h:131
elm::HashTable
Definition: HashTable.h:32
elm::HashSet::equals
bool equals(const HashSet< T > &s) const
Definition: HashSet.h:66
elm::InplacePreIterator
Definition: iter.h:49
elm::HashSet::remove
void remove(const Iter &i)
Definition: HashSet.h:79
elm::HashSet::operator>=
bool operator>=(const HashSet< T > &s) const
Definition: HashSet.h:90
elm::HashSet::allocator
A & allocator()
Definition: HashSet.h:41
elm::HashSet::containsAll
bool containsAll(const C &coll)
Definition: HashSet.h:46
elm::HashSet::operator&=
self_t & operator&=(const HashSet< T > &s)
Definition: HashSet.h:108
elm::HashSet::Iter::Iter
Iter(const HashSet &set)
Definition: HashSet.h:54
elm::HashSet::operator=
self_t operator=(const HashSet< T > &s)
Definition: HashSet.h:81
elm::HashSet::copy
void copy(const HashSet< T > &s)
Definition: HashSet.h:80
elm::HashSet::remove
void remove(const T &val)
Definition: HashSet.h:76
elm::List
Definition: List.h:34
elm::HashSet::begin
Iter begin(void) const
Definition: HashSet.h:63
elm::HashSet::allocator
const A & allocator() const
Definition: HashSet.h:40
elm::HashSet::Iter
Definition: HashSet.h:51
elm::HashSet::Iter::next
void next(void)
Definition: HashSet.h:58
elm::HashSet::operator>
bool operator>(const HashSet< T > &s) const
Definition: HashSet.h:92
elm::HashSet::operator|
self_t operator|(const HashSet< T > &s) const
Definition: HashSet.h:112
elm::HashSet::operator|=
self_t & operator|=(const HashSet< T > &s)
Definition: HashSet.h:106
elm::HashTable::isEmpty
bool isEmpty(void) const
Definition: HashTable.h:121