Class documentation of Concepts

Loading...
Searching...
No Matches
set.hh
Go to the documentation of this file.
1
7#ifndef set_hh
8#define set_hh
9
10#include <set>
11#include <algorithm>
12#include <iterator>
13#include <stdarg.h>
14#include <initializer_list>
15#include <basics/debug.hh>
17#include <basics/exceptions.hh>
19#include <basics/output.hh>
20#include "array.hh"
21
22#define SetInput_D 0
23
24namespace concepts {
25
26 // forward declaration
27 template<class F>
28 class BaseSet;
29
30 // ******************************************************************* Set **
31
38 template<class F>
39 class Set : public BaseSet<F> {
40 public:
41 Set() : BaseSet<F>() {}
42 Set(const F& val) : BaseSet<F>() { this->insert(val); }
43 template<class G>
44 Set(const G& set) : BaseSet<F>(set) {}
45 virtual ~Set() {}
46 };
47
48 // *************************************************************** BaseSet **
49
55 template<class F>
56 class BaseSet : public std::set<F>, public virtual OutputOperator {
57 public:
59 BaseSet() : std::set<F>() {}
65 BaseSet(const std::string& str);
71 template<class G>
72 BaseSet(const G& set) : std::set<F>(set) {}
73 virtual ~BaseSet() {}
74
77 template<class G, class H>
78 inline Set<G> operator()(G (H::*fun)() const) const;
82 template<class G, class H>
83 inline Set<G*> operator()(G& (H::*fun)() const) const;
87 template<class G, class H, class I, class J>
88 inline Set<G> operator()(G (H::*fun)(I) const, J i) const;
89
90 // Return set union
91 inline Set<F> operator||(const Set<F>& set) const;
92 inline Set<F> operator||(Set<F>& set) const;
93 // Return set intersection
94 inline Set<F> operator&&(const Set<F>& set) const;
95 inline Set<F> operator&&(Set<F>& set) const;
96 // Return set difference
97 inline Set<F> operator-(const Set<F>& set) const;
98 inline Set<F> operator-(Set<F>& set) const;
100 inline Set<uint> operator==(const F val) const;
102 inline Set<F> operator()(const Set<uint>& set) const;
103
104 // Return set union
105 inline BaseSet<F>& operator|=(const Set<F>& set);
106
108 inline bool exist(F val) const;
110 inline bool isempty() const { return this->begin() == this->end(); }
111 protected:
112 virtual std::ostream& info(std::ostream& os) const;
113
114 typedef typename std::set<F>::const_iterator const_iterator_;
115 typedef typename std::insert_iterator<std::set<F> > insert_iterator_;
119 virtual void union_(const_iterator_ first, const_iterator_ last,
120 insert_iterator_ i) const;
124 virtual void intersection_(const_iterator_ first, const_iterator_ last,
125 insert_iterator_ i) const;
129 virtual void difference_(const_iterator_ first, const_iterator_ last,
130 insert_iterator_ i) const;
131 };
132
133 template<class F>
134 std::istream& operator>>(std::istream& is, BaseSet<F>& set) {
135 F val;
136 while(!is.eof()) {
137 is >> val;
138 DEBUGL(SetInput_D, "val = " << val);
139 if (!is.fail())
140 set.insert(val);
141 else
143 std::string("The current Set<F> with F = ") + concepts::typeOf(val) +
144 std::string(" can not be created. The input symbol ` ") +
145 std::string(std::istreambuf_iterator<char>(is), {}).substr(0,1) +
146 std::string(" ` does not meet the conversion criteria"
147 " of the operator>>, failbit was setted to true."));
148 }
149 return is;
150 }
151
152 template<class F>
153 BaseSet<F>::BaseSet(const std::string& str) {
154 if (str.size() > 0) {
155 std::stringstream s;
156 s << str;
157 s >> *this;
158 }
159 }
160
161 template<class F>
163 const F* e = (const F*)a;
164 for(uint i = a.size(); i--;) this->insert(*e++);
165 }
166
167 // e.g. Key::key()
168 template<class F>
169 template<class G, class H>
170 Set<G> BaseSet<F>::operator()(G (H::*fun)() const) const {
171 std::set<G> set;
172 for(const_iterator_ i = this->begin(); i != this->end(); ++i) {
173 G val = ((*i)->*fun)();
174 set.insert(val);
175 }
176 return set;
177 }
178 // e.g. Connector1::key()
179 template<class F>
180 template<class G, class H>
181 Set<G*> BaseSet<F>::operator()(G& (H::*fun)() const) const {
182 std::set<G*> set;
183 for(const_iterator_ i = this->begin(); i != this->end();++i) {
184 G* val = &((*i)->*fun)();
185 set.insert(val);
186 }
187 return set;
188 }
189 // e.g. Connector1::vertex(uint i)
190 template<class F>
191 template<class G, class H, class I, class J>
192 Set<G> BaseSet<F>::operator()(G (H::*fun)(I) const, J i) const {
193 std::set<G> set;
194 for(const_iterator_ is = this->begin(); is != this->end();++is) {
195 G val = ((*is)->*fun)(i);
196 set.insert(val);
197 }
198 return set;
199 }
200
201 template<class F>
202 Set<F> BaseSet<F>::operator||(const Set<F>& set) const {
203 std::set<F> result;
204 union_(set.begin(), set.end(), inserter(result, result.begin()));
205 return result;
206 }
207
208 template<class F>
209 Set<F> BaseSet<F>::operator||(Set<F>& set) const {
210 std::set<F> result;
211 union_(set.begin(), set.end(), inserter(result, result.begin()));
212 return result;
213 }
214
215 template<class F>
216 Set<F> BaseSet<F>::operator&&(const Set<F>& set) const {
217 std::set<F> result;
218 intersection_(set.begin(), set.end(), inserter(result, result.begin()));
219 return result;
220 }
221
222 template<class F>
223 Set<F> BaseSet<F>::operator&&(Set<F>& set) const {
224 std::set<F> result;
225 intersection_(set.begin(), set.end(), inserter(result, result.begin()));
226 return result;
227 }
228
229 template<class F>
231 (const_iterator_ first, const_iterator_ last, insert_iterator_ i) const {
232 set_intersection(this->begin(), this->end(), first, last, i);
233 }
234
235 template<class F>
237 (const_iterator_ first, const_iterator_ last, insert_iterator_ i) const {
238 set_union(this->begin(), this->end(), first, last, i);
239 }
240
241 template<class F>
243 (const_iterator_ first, const_iterator_ last, insert_iterator_ i) const {
244 set_difference(this->begin(), this->end(), first, last, i);
245 }
246
247 template<class F>
248 Set<F> BaseSet<F>::operator-(const Set<F>& set) const {
249 std::set<F> result;
250 difference_(set.begin(), set.end(), inserter(result, result.begin()));
251 return result;
252 }
253
254 template<class F>
255 Set<F> BaseSet<F>::operator-(Set<F>& set) const {
256 std::set<F> result;
257 difference_(set.begin(), set.end(), inserter(result, result.begin()));
258 return result;
259 }
260
261 template<class F>
263 std::set<uint> result;
264 uint i = 0;
265 for(const_iterator_ is = this->begin(); is != this->end(); ++is, ++i)
266 if (*is == val) result.insert(i);
267 return result;
268 }
269
270 template<class F>
272 std::set<F> result;
273 uint i = 0;
274 typename std::set<uint>::const_iterator it = set.begin();
275 const_iterator_ is = this->begin();
276 while(it != set.end() && is != this->end()) {
277 if (i == *it) {
278 result.insert(*is);
279 ++it;
280 }
281 ++is; ++i;
282 }
283 conceptsAssert(it == set.end(), Assertion());
284 return result;
285 }
286
287 template<class F>
289 set_union(this->begin(), this->end(),
290 set.begin(), set.end(),
291 inserter(*this, this->begin()));
292 return *this;
293 }
294
295 template<class F>
296 bool BaseSet<F>::exist(F val) const {
297 return this->find(val) != this->end();
298 }
299
300 template<class F>
301 std::ostream& BaseSet<F>::info(std::ostream& os) const {
302 os << concepts::typeOf(*this) << "(";
303 for(const_iterator_ i = this->begin(); i != this->end();) {
304 pointerOutput(os, *i);
305 if (++i != this->end()) os << ", ";
306 }
307 return os << ")";
308 }
309
310 // *************************************************************** makeSet **
311
319 template<class F>
320 Set<F> makeSet(uint n, const F& first, ...) {
321 Set<F> data;
322 data.insert(first);
323
324 va_list param;
325 va_start(param, first);
326 for(uint i = 1; i < n; ++i)
327 data.insert(va_arg(param, F));
328 va_end(param);
329 return data;
330 }
331
338 template<class F>
339 Set<F> makeSet(std::initializer_list<F> list) {
340 Set<F> data;
341 for( F elem : list )
342 data.insert(elem);
343 return data;
344 }
345
346} // namespace concepts
347
348#endif // set_hh
uint size() const
Returns the requested size of the array.
Definition array.hh:259
virtual void union_(const_iterator_ first, const_iterator_ last, insert_iterator_ i) const
Definition set.hh:237
Set< G * > operator()(G &(H::*fun)() const) const
Definition set.hh:181
BaseSet(const std::string &str)
Definition set.hh:153
virtual void difference_(const_iterator_ first, const_iterator_ last, insert_iterator_ i) const
Definition set.hh:243
Set< G > operator()(G(H::*fun)() const) const
Definition set.hh:170
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition set.hh:301
virtual void intersection_(const_iterator_ first, const_iterator_ last, insert_iterator_ i) const
Definition set.hh:231
bool exist(F val) const
Returns true, if a value is in the set.
Definition set.hh:296
BaseSet(const G &set)
Constructor, which uses the constructor of the base class std::set.
Definition set.hh:72
BaseSet(const concepts::Array< F > &a)
Definition set.hh:162
bool isempty() const
Returns true, if set is empty.
Definition set.hh:110
Set< F > operator()(const Set< uint > &set) const
Returns subset with indices set.
Definition set.hh:271
Set< uint > operator==(const F val) const
Returns the indices of elements with are equal to val.
Definition set.hh:262
Set< G > operator()(G(H::*fun)(I) const, J i) const
Definition set.hh:192
BaseSet()
Standard Constructor.
Definition set.hh:59
#define conceptsAssert(cond, exc)
#define DEBUGL(doit, msg)
Definition debug.hh:40
std::string typeOf(const T &t)
Definition output.hh:43
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320