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>
16 #include <basics/outputOperator.hh>
17 #include <basics/exceptions.hh>
18 #include <basics/pointerOutput.hh>
19 #include <basics/output.hh>
20 #include "array.hh"
21 
22 #define SetInput_D 0
23 
24 namespace 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;
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
142  throw concepts::WrongInputException(__FILE__, __LINE__, __FUNCTION__,
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>
203  std::set<F> result;
204  union_(set.begin(), set.end(), inserter(result, result.begin()));
205  return result;
206  }
207 
208  template<class F>
210  std::set<F> result;
211  union_(set.begin(), set.end(), inserter(result, result.begin()));
212  return result;
213  }
214 
215  template<class F>
217  std::set<F> result;
218  intersection_(set.begin(), set.end(), inserter(result, result.begin()));
219  return result;
220  }
221 
222  template<class F>
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>
256  std::set<F> result;
257  difference_(set.begin(), set.end(), inserter(result, result.begin()));
258  return result;
259  }
260 
261  template<class F>
262  Set<uint> BaseSet<F>::operator==(const F val) const {
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
Set< G > operator()(G(H::*fun)() const) const
Returns element wise application of a member function, e.g.
Definition: set.hh:170
Set< F > operator&&(const Set< F > &set) const
Definition: set.hh:216
virtual void intersection_(const_iterator_ first, const_iterator_ last, insert_iterator_ i) const
Insert the set intersection of this set with that between iterator first and second into i.
Definition: set.hh:231
void pointerOutput(std::ostream &os, const F &val)
virtual std::ostream & info(std::ostream &os) const
Definition: set.hh:301
Set with operations, output operator, and method of the particular element types.
Definition: traces.hh:18
std::insert_iterator< std::set< F > > insert_iterator_
Definition: set.hh:115
virtual ~BaseSet()
Definition: set.hh:73
BaseSet(const std::string &str)
Constructor, set is defined by a string.
Definition: set.hh:153
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Set< F > makeSet(uint n, const F &first,...)
Creates an array of length from a comma separated list of values.
Definition: set.hh:320
std::istream & operator>>(std::istream &is, BaseSequence< F > &seq)
Definition: sequence.hh:133
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)
Constructor, set is defined by an array.
Definition: set.hh:162
Set(const F &val)
Definition: set.hh:42
BaseSet< F > & operator|=(const Set< F > &set)
Definition: set.hh:288
#define DEBUGL(doit, msg)
Set< F > operator-(const Set< F > &set) const
Definition: set.hh:248
Set< G > operator()(G(H::*fun)(I) const, J i) const
Returns element wise application of a member function, e.g.
Definition: set.hh:192
virtual void difference_(const_iterator_ first, const_iterator_ last, insert_iterator_ i) const
Insert the set difference of this set with that between iterator first and second into i.
Definition: set.hh:243
An array of objects.
Definition: bilinearForm.hh:23
Exception class for assertions.
Definition: exceptions.hh:258
BaseSet()
Standard Constructor.
Definition: set.hh:59
Set< uint > operator==(const F val) const
Returns the indices of elements with are equal to val.
Definition: set.hh:262
std::set< F >::const_iterator const_iterator_
Definition: set.hh:114
bool isempty() const
Returns true, if set is empty.
Definition: set.hh:110
Exception class to express an input criteria was not met.
Definition: exceptions.hh:152
Set with operations and output operator.
Definition: set.hh:28
Set< F > operator||(Set< F > &set) const
Definition: set.hh:209
Set(const G &set)
Definition: set.hh:44
virtual void union_(const_iterator_ first, const_iterator_ last, insert_iterator_ i) const
Insert the set union of this set with that between iterator first and second into i.
Definition: set.hh:237
#define SetInput_D
Definition: set.hh:22
virtual ~Set()
Definition: set.hh:45
Set< F > operator&&(Set< F > &set) const
Definition: set.hh:223
Set< F > operator-(Set< F > &set) const
Definition: set.hh:255
bool exist(F val) const
Returns true, if a value is in the set.
Definition: set.hh:296
std::string typeOf(const T &t)
Return the typeid name of a class object.
Definition: output.hh:43
Set< G * > operator()(G &(H::*fun)() const) const
Returns element wise application of a member function, e.g.
Definition: set.hh:181
Set< F > operator||(const Set< F > &set) const
Definition: set.hh:202
Set< F > operator()(const Set< uint > &set) const
Returns subset with indices set.
Definition: set.hh:271
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich