Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Range.h
1 /*
2  * Range class
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_RANGE_H_
22 #define ELM_DATA_RANGE_H_
23 
24 #include "../equiv.h"
25 
26 namespace elm {
27 
28 template <class I>
29 class Range {
30 public:
31  typedef typename I::t t;
32 
33  inline Range(const I& begin, const I& end)
34  : _begin(begin), _end(end) { }
35 
36  class Iter: public PreIterator<Iter, t> {
37  public:
38  typedef Range collec_t;
39  inline Iter(const Range& collection): c(collection), i(c._begin) { }
40  inline Iter(const Range& collection, const I& ii): c(collection), i(ii) { }
41  inline bool ended(void) const { return i.equals(c._end); }
42  inline typename I::return_t item(void) const { return i.item(); }
43  inline void next(void) { i.next(); }
44  inline bool equals(const Iter& ii) const { return i.equals(ii.i); }
45  private:
46  const Range& c;
47  I i;
48  };
49 
50  inline Iter begin() const { return Iter(*this, _begin); }
51  inline Iter end() const { return Iter(*this, _end); }
52 
53  inline bool isEmpty(void) const { return _begin.equals(_end); }
54  inline int count(void) const
55  { int c = 0; for(Iter i(*this); i(); i++) c++; return c; }
56  inline operator bool(void) const { return !isEmpty(); }
57  inline bool contains(const t& v)
58  { for(Iter i(*this); i(); i++) if(Equiv<t>::equals(*i, v)) return true; return false; }
59  template <class C> inline bool containsAll(const C& c)
60  { for(typename C::Iter i(c); i; i++) if(!contains(*i)) return false; return true; }
61 
62 private:
63  I _begin, _end;
64 };
65 
66 template <class I>
67 Range<I> range(const I& begin, const I& end) { return Range<I>(begin, end); }
68 
69 } // elm
70 
71 #endif /* ELM_DATA_RANGE_H_ */
elm::Range::t
I::t t
Definition: Range.h:31
elm::Range
Definition: Range.h:29
elm::Range::isEmpty
bool isEmpty(void) const
Definition: Range.h:53
elm::Range::count
int count(void) const
Definition: Range.h:54
elm::Range::Iter::next
void next(void)
Definition: Range.h:43
elm::Range::Iter::Iter
Iter(const Range &collection, const I &ii)
Definition: Range.h:40
elm::Range::begin
Iter begin() const
Definition: Range.h:50
bool
elm::range
Range< I > range(const I &begin, const I &end)
Definition: Range.h:67
elm::Range::Iter::collec_t
Range collec_t
Definition: Range.h:38
elm
Definition: adapter.h:26
elm::Range::containsAll
bool containsAll(const C &c)
Definition: Range.h:59
elm::Range::Iter::equals
bool equals(const Iter &ii) const
Definition: Range.h:44
elm::Range::Iter::item
I::return_t item(void) const
Definition: Range.h:42
elm::Range::contains
bool contains(const t &v)
Definition: Range.h:57
elm::Range::Range
Range(const I &begin, const I &end)
Definition: Range.h:33
elm::Range::Iter
Definition: Range.h:36
elm::Range::Iter::ended
bool ended(void) const
Definition: Range.h:41
elm::PreIterator
Definition: iter.h:28
elm::Equiv
Definition: equiv.h:32
elm::Range::Iter::Iter
Iter(const Range &collection)
Definition: Range.h:39
elm::Range::end
Iter end() const
Definition: Range.h:51