Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Slice.h
1 /*
2  * Slice class
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2020, 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_SLICE_H_
22 #define ELM_DATA_SLICE_H_
23 
24 #include <elm/PreIterator.h>
25 
26 namespace elm {
27 
28 template <class C>
29 class Slice {
30 public:
31  typedef Slice<C> self_t;
32  typedef typename C::t t;
33 
34  inline Slice(): a(nullptr), f(0), c(0) { }
35  inline Slice(C& array, int first, int count): a(&array), f(first), c(count) { }
36 
37  C& array() const { return *a; }
38  inline int firstIndex() const { return f; }
39  inline int lastIndex() const { return f + c - 1; }
40  inline int count() const { return c; }
41 
42  inline const t& get(int i) const { return (*a)[f + i]; }
43  inline const t& operator[](int i) const { return get(i); }
44  inline t& get(int i) { return (*a)[f + i]; }
45  inline t& operator[](int i) { return get(i); }
46 
47  class BaseIter: public PreIter<BaseIter, t> {
48  public:
49  inline BaseIter(const self_t& slice, int idx = 0): s(slice), i(idx) { }
50  inline bool ended() const { return i >= s.c; }
51  inline const t& item() const { return s[i]; }
52  inline void next() { i++; }
53  inline bool equals(const BaseIter& ii) const { return &s == &ii.s && i == ii.i; }
54  protected:
55  const self_t& s;
56  int i;
57  };
58 
59  // Array concept additions
60  inline int length() const { return count(); }
61  inline int indexOf(const t& x, int i = 0) const
62  { for(; i < c; i++) if(x == get(i)) return i; return -1; }
63  inline int lastIndexOf(const t& x, int i = -1) const
64  { if(i == -1) i = c - 1; for(; i >= 0; i--) if(x == get(i)) break; return i; }
65 
66  // Collection concept
67  class Iter: public BaseIter, public ConstPreIter<Iter, t> {
68  public:
69  using BaseIter::BaseIter;
70  inline const t& item() const { return BaseIter::s[BaseIter::i]; }
71  };
72  inline Iter begin() const { return Iter(*this); }
73  inline Iter end() const { return Iter(*this, c); }
74 
75  inline bool contains(const t& x) const
76  { return indexOf(x) != -1; }
77  inline bool containsAll(const self_t& s) const
78  { for(auto x: s) if(!contains(x)) return false; return true; }
79  inline bool isEmpty() const { return c == 0; }
80  inline operator bool() const { return !isEmpty(); }
81  inline bool equals(const self_t& s) const {
82  if(c != s.c) return false;
83  for(int i = 0; i < c; i++) if(get(i) != s.get(i)) return false;
84  return true;
85  }
86  inline bool operator==(const self_t& s) const { return equals(s); }
87  inline bool operator!=(const self_t& s) const { return !equals(s); }
88 
89  // MutableCollecton concept
90  class MutIter: public BaseIter, public MutPreIter<MutIter, t> {
91  public:
92  inline MutIter(self_t& slice, int idx = 0): BaseIter(slice, idx) { }
93  inline t& item() { return const_cast<self_t&>(BaseIter::s)[BaseIter::i]; }
94  };
95  inline MutIter begin() { return MutIter(*this); }
96  inline MutIter end() { return MutIter(*this, c); }
97 
98 private:
99  C *a;
100  int f, c;
101 };
102 
103 template <class C>
104 Slice<C> slice(C& a, int fst, int cnt) { return Slice<C>(a, fst, cnt); }
105 
106 } // elm
107 
108 #endif /* ELM_DATA_SLICE_H_ */
elm::ConstPreIter
Definition: iter.h:83
elm::Slice::BaseIter::next
void next()
Definition: Slice.h:52
elm::Slice::self_t
Slice< C > self_t
Definition: Slice.h:31
elm::Slice::t
C::t t
Definition: Slice.h:32
elm::Slice::Slice
Slice(C &array, int first, int count)
Definition: Slice.h:35
elm::MutPreIter
Definition: iter.h:90
elm::Slice
Definition: Slice.h:29
elm::Slice::isEmpty
bool isEmpty() const
Definition: Slice.h:79
elm::Slice::operator==
bool operator==(const self_t &s) const
Definition: Slice.h:86
elm::Slice::begin
Iter begin() const
Definition: Slice.h:72
elm::Slice::BaseIter::ended
bool ended() const
Definition: Slice.h:50
elm::Slice::count
int count() const
Definition: Slice.h:40
elm::Slice::MutIter
Definition: Slice.h:90
bool
elm::Slice::BaseIter::s
const self_t & s
Definition: Slice.h:55
elm::Slice::lastIndex
int lastIndex() const
Definition: Slice.h:39
elm::Slice::end
MutIter end()
Definition: Slice.h:96
elm::Slice::end
Iter end() const
Definition: Slice.h:73
elm::Slice::lastIndexOf
int lastIndexOf(const t &x, int i=-1) const
Definition: Slice.h:63
elm
Definition: adapter.h:26
elm::Slice::operator[]
t & operator[](int i)
Definition: Slice.h:45
elm::Slice::Iter::item
const t & item() const
Definition: Slice.h:70
elm::Slice::indexOf
int indexOf(const t &x, int i=0) const
Definition: Slice.h:61
elm::Slice::Iter
Definition: Slice.h:67
elm::slice
Slice< C > slice(C &a, int fst, int cnt)
Definition: Slice.h:104
array
elm::Slice::BaseIter::i
int i
Definition: Slice.h:56
elm::Slice::get
t & get(int i)
Definition: Slice.h:44
elm::Slice::length
int length() const
Definition: Slice.h:60
elm::Slice::begin
MutIter begin()
Definition: Slice.h:95
elm::Slice::MutIter::item
t & item()
Definition: Slice.h:93
elm::Slice::BaseIter
Definition: Slice.h:47
elm::Slice::Slice
Slice()
Definition: Slice.h:34
elm::Slice::get
const t & get(int i) const
Definition: Slice.h:42
elm::Slice::contains
bool contains(const t &x) const
Definition: Slice.h:75
elm::Slice::BaseIter::item
const t & item() const
Definition: Slice.h:51
elm::Slice::equals
bool equals(const self_t &s) const
Definition: Slice.h:81
elm::Slice::MutIter::MutIter
MutIter(self_t &slice, int idx=0)
Definition: Slice.h:92
elm::Slice::array
C & array() const
Definition: Slice.h:37
elm::Slice::operator!=
bool operator!=(const self_t &s) const
Definition: Slice.h:87
elm::Slice::BaseIter::equals
bool equals(const BaseIter &ii) const
Definition: Slice.h:53
elm::Slice::BaseIter::BaseIter
BaseIter(const self_t &slice, int idx=0)
Definition: Slice.h:49
elm::Slice::operator[]
const t & operator[](int i) const
Definition: Slice.h:43
elm::Slice::firstIndex
int firstIndex() const
Definition: Slice.h:38
elm::PreIter
Definition: iter.h:69
elm::Slice::containsAll
bool containsAll(const self_t &s) const
Definition: Slice.h:77