Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Vector.h
1 /*
2  * Vector 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 INCLUDE_ELM_DATA_VECTOR_H_
22 #define INCLUDE_ELM_DATA_VECTOR_H_
23 
24 #include "custom.h"
25 #include "Array.h"
26 #include "Manager.h"
27 
28 #include <elm/array.h>
29 #include <elm/compat.h>
30 
31 namespace elm {
32 
33 template <class T, class E = Equiv<T>, class A = DefaultAlloc >
34 class Vector: public E, public A {
35  inline T *newVec(int size)
36  { T *t = static_cast<T *>(A::allocate(size * sizeof(T)));
37  array::construct(t, size); return t; }
38  inline void deleteVec(T *t, int size) { array::destruct(t, size); A::free(t); }
39 
40 public:
41  typedef T t;
43 
44  inline Vector(int _cap = 8): tab(newVec(_cap)), cap(_cap), cnt(0) { }
45  inline Vector(const Vector<T>& vec): tab(0), cap(0), cnt(0) { copy(vec); }
46  inline ~Vector(void) { if(tab) deleteVec(tab, cap); }
47  inline const E& equivalence() const { return *this; }
48  inline E& equivalence() { return *this; }
49  inline const A& allocator() const { return *this; }
50  inline A& allocator() { return *this; }
51 
52  inline int capacity(void) const { return cap; }
53  inline Array<const T> asArray(void) const { return Array<const T>(count(), tab); }
54  inline Array<T> asArray(void) { return Array<T>(count(), tab); }
55  inline Array<T> detach(void)
56  { T *rt = tab; int rc = cnt; tab = 0; cnt = 0; return Array<T>(rc, rt); }
57  void grow(int new_cap)
58  { ASSERTP(new_cap >= cap, "new capacity must be bigger than old one");
59  T *new_tab = newVec(new_cap); array::copy(new_tab, tab, cnt); deleteVec(tab, cap); tab = new_tab; cap = new_cap; }
60  void setLength(int new_length)
61  { int new_cap; ASSERTP(new_length >= 0, "new length must be >= 0");
62  for(new_cap = 1; new_cap < new_length; new_cap *= 2);
63  if (new_cap > cap) grow(new_cap); cnt = new_length; }
64  inline T& addNew(void) { if(cnt >= cap) grow(cap * 2); return tab[cnt++]; }
65 
66  // Collection concept
67  class Iter: public InplacePreIterator<Iter, T> {
68  public:
69  friend class Vector;
70  inline Iter(const self_t& vec, int idx = 0): _vec(&vec), i(idx) { }
71  inline bool ended(void) const { return i >= _vec->length(); }
72  inline const T& item(void) const { return (*_vec)[i]; }
73  inline void next(void) { i++; }
74  inline int index(void) const { return i; }
75  inline bool equals(const Iter& it) const { return _vec == it._vec && i == it.i; }
76  private:
77  const self_t *_vec;
78  int i;
79  };
80 
81  inline int count(void) const { return cnt; }
82  bool contains(const T& v) const
83  { for(Iter i(*this); i(); i++) if(v == *i) return true; return false; }
84  template <class C> inline bool containsAll(const C& items)
85  { for(const auto& x: items) if(!contains(x)) return false; return true; }
86  inline bool isEmpty(void) const { return cnt == 0; }
87  inline operator bool(void) const { return cnt != 0; }
88  inline Iter begin(void) const { return Iter(*this); }
89  inline Iter end(void) const { return Iter(*this, count()); }
90 
91  template <class C> inline bool equals(const C& c) const {
92  int i = 0; for(const auto& x: c)
93  { if(i >= count() || !E::equals(tab[i], x)) return false; i++; } return i == count(); }
94  inline bool operator==(const Vector<T>& v) const { return equals(v); }
95  inline bool operator!=(const Vector<T>& v) const { return !equals(v); }
96 
97  static const Vector<T, E, A> null;
98 
99  // MutableCollection concept
100  inline void clear(void) { cnt = 0; }
101  void add(const T& v) { if(cnt >= cap) grow(cap * 2); tab[cnt++] = v; }
102  template <class C> inline void addAll(const C& c)
103  { for(typename C::Iter i(c); i(); i++) add(*i); }
104  inline void remove(const T& value) { int i = indexOf(value); if(i >= 0) removeAt(i); }
105  template <class C> inline void removeAll(const C& c)
106  { for(const auto x: c) remove(x); }
107  inline void remove(const Iter& i) { removeAt(i.i); }
108  inline Vector<T>& operator+=(const T x) { add(x); return *this; }
109  inline Vector<T>& operator-=(const T x) { remove(x); return *this; }
110  void copy(const Vector& vec)
111  { if(!tab || vec.cnt > cap) { if(tab) deleteVec(tab, cap); cap = vec.cap; tab = newVec(vec.cap); }
112  cnt = vec.cnt; array::copy(tab, vec.tab, cnt); }
113  inline Vector<T>& operator=(const Vector& vec) { copy(vec); return *this; };
114 
115  // Array concept
116  inline int length(void) const { return count(); }
117  inline const T& get(int i) const
118  { ASSERTP(0 <= i && i < cnt, "index out of bounds"); return tab[i]; }
119  inline int indexOf(const T& v, int p = 0) const
120  { ASSERTP(0 <= p && p <= cnt, "index out of bounds");
121  for(int i = p; i < cnt; i++) if(E::isEqual(v, tab[i])) return i; return -1; }
122  inline int lastIndexOf(const T& v, int p = -1) const
123  { ASSERTP(p <= cnt, "index out of bounds");
124  for(int i = (p < 0 ? cnt : p) - 1; i >= 0; i--) if(E::isEqual(v, tab[i])) return i; return -1; }
125  inline const T & operator[](int i) const { return get(i); }
126 
127  // MutableArray concept
128  inline void shrink(int l)
129  { ASSERTP(0 <= l && l <= cnt, "bad shrink value"); cnt = l; }
130  inline void set(int i, const T& v)
131  { ASSERTP(0 <= i && i < cnt, "index out of bounds"); tab[i] = v; }
132  inline void set (const Iter &i, const T &v) { set(i.i, v); }
133  inline T& get(int index)
134  { ASSERTP(index < cnt, "index out of bounds"); return tab[index]; }
135  inline T& get(const Iter& i) { return get(i.index()); }
136  inline T & operator[](int i) { return get(i); }
137  inline T & operator[](const Iter& i) { return get(i); }
138  void insert(int i, const T& v)
139  { ASSERTP(0 <= i && i <= cnt, "index out of bounds");
140  if(cnt >= cap) grow(cap * 2); array::move(tab + i + 1, tab + i, cnt - i);
141  tab[i] = v; cnt++; }
142  inline void insert(const Iter &i, const T &v) { insert(i.i, v); }
143  void removeAt(int i)
144  { ASSERTP(0 <= i && i <= cnt, "index out of bounds");
145  array::move(tab + i, tab + i + 1, cnt - i - 1); cnt--; }
146  inline void removeAt(const Iter& i) { removeAt(i.i); }
147 
148  // List concept
149  inline const T& first(void) const { ASSERT(cnt > 0); return tab[0]; }
150  inline const T& last(void) const { ASSERT(cnt > 0); return tab[cnt - 1]; }
151  inline Iter find(const T &v) const
152  { Iter i(*this); while(i() && !E::equals(*i, v)) i++; return i; }
153  inline Iter find (const T &v, const Iter &p) const
154  { Iter i(p); while(i() && !E::equals(*i, v)) i++; return i; }
155  inline const T& nth(int i) const { return get(i); }
156 
157  // MutableList concept
158  inline T& first() { ASSERT(cnt > 0); return tab[0]; }
159  inline T& last() { ASSERT(cnt > 0); return tab[cnt - 1]; }
160  inline void addFirst(const T &v) { insert(0, v); }
161  inline void addLast(const T &v) { add(v); }
162  inline void removeFirst(void) { removeAt(0); }
163  inline void removeLast(void) { removeAt(cnt - 1); }
164  inline void addAfter(const Iter &i, const T &v) { insert(i.i + 1, v); }
165  inline void addBefore(const Iter &i, const T &v) { insert(i.i, v); }
166  inline void removeBefore(const Iter& i) { removeAt(i.i - 1); }
167  inline void removeAfter(const Iter& i) { removeAt(i.i + 1); }
168 
169  // Stack concept
170  inline const T &top(void) const { return last(); }
171  inline T &top(void) { return tab[cnt - 1]; }
172  inline T pop(void) { ASSERTP(cnt > 0, "no more data to pop"); cnt--; return tab[cnt]; }
173  inline void push(const T &v) { add(v); }
174  inline void reset(void) { clear(); }
175 
176  // deprecated
177  inline Iter operator*(void) const { return items(); }
178  inline Iter items(void) const { return Iter(*this); }
179 
180 private:
181  T *tab;
182  int cap, cnt;
183 };
184 
185 template <class T, class E, class A>
187 
188 } // elm
189 
190 #endif /* INCLUDE_ELM_DATA_VECTOR_H_ */
elm::Vector::shrink
void shrink(int l)
Definition: Vector.h:128
elm::Vector::Vector
Vector(int _cap=8)
Definition: Vector.h:44
elm::array::copy
void copy(T *target, const T *source, int size)
Definition: array.h:70
elm::Vector::operator*
Iter operator*(void) const
Definition: Vector.h:177
elm::Vector::asArray
Array< const T > asArray(void) const
Definition: Vector.h:53
elm::Vector::lastIndexOf
int lastIndexOf(const T &v, int p=-1) const
Definition: Vector.h:122
elm::Vector::Iter::equals
bool equals(const Iter &it) const
Definition: Vector.h:75
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::Vector::insert
void insert(int i, const T &v)
Definition: Vector.h:138
elm::Vector::equivalence
const E & equivalence() const
Definition: Vector.h:47
elm::Vector::removeFirst
void removeFirst(void)
Definition: Vector.h:162
elm::Vector::count
int count(void) const
Definition: Vector.h:81
elm::Vector::detach
Array< T > detach(void)
Definition: Vector.h:55
elm::Vector::begin
Iter begin(void) const
Definition: Vector.h:88
elm::Vector::removeBefore
void removeBefore(const Iter &i)
Definition: Vector.h:166
elm::Vector::get
T & get(const Iter &i)
Definition: Vector.h:135
elm::Vector::allocator
const A & allocator() const
Definition: Vector.h:49
elm::Vector::Iter::item
const T & item(void) const
Definition: Vector.h:72
elm::equals
bool equals(const C1 &c1, const C2 &c2)
Definition: util.h:107
elm::Vector::self_t
Vector< T, E, A > self_t
Definition: Vector.h:42
elm::Vector::reset
void reset(void)
Definition: Vector.h:174
elm::Vector::t
T t
Definition: Vector.h:41
elm::Vector::first
const T & first(void) const
Definition: Vector.h:149
elm::Vector::items
Iter items(void) const
Definition: Vector.h:178
elm::Vector::first
T & first()
Definition: Vector.h:158
elm::Vector::push
void push(const T &v)
Definition: Vector.h:173
elm::Vector::removeAfter
void removeAfter(const Iter &i)
Definition: Vector.h:167
elm::Vector::copy
void copy(const Vector &vec)
Definition: Vector.h:110
elm::Vector::operator[]
T & operator[](int i)
Definition: Vector.h:136
value
elm::Vector::containsAll
bool containsAll(const C &items)
Definition: Vector.h:84
bool
elm::Vector::set
void set(const Iter &i, const T &v)
Definition: Vector.h:132
elm::Vector::addFirst
void addFirst(const T &v)
Definition: Vector.h:160
elm::Vector::Iter::Iter
Iter(const self_t &vec, int idx=0)
Definition: Vector.h:70
elm::Vector::addNew
T & addNew(void)
Definition: Vector.h:64
elm::Vector::last
const T & last(void) const
Definition: Vector.h:150
elm::Vector::asArray
Array< T > asArray(void)
Definition: Vector.h:54
elm::Vector::indexOf
int indexOf(const T &v, int p=0) const
Definition: Vector.h:119
elm::Vector::equals
bool equals(const C &c) const
Definition: Vector.h:91
elm::Vector::operator==
bool operator==(const Vector< T > &v) const
Definition: Vector.h:94
elm::Vector::nth
const T & nth(int i) const
Definition: Vector.h:155
elm::Vector::length
int length(void) const
Definition: Vector.h:116
elm::Vector::find
Iter find(const T &v) const
Definition: Vector.h:151
elm
Definition: adapter.h:26
elm::Vector::capacity
int capacity(void) const
Definition: Vector.h:52
elm::Vector::operator!=
bool operator!=(const Vector< T > &v) const
Definition: Vector.h:95
elm::Vector::add
void add(const T &v)
Definition: Vector.h:101
elm::Vector
Definition: Vector.h:34
elm::Vector::top
const T & top(void) const
Definition: Vector.h:170
elm::Vector::Iter::index
int index(void) const
Definition: Vector.h:74
elm::Vector::find
Iter find(const T &v, const Iter &p) const
Definition: Vector.h:153
elm::Vector::get
T & get(int index)
Definition: Vector.h:133
elm::Vector::Iter::next
void next(void)
Definition: Vector.h:73
elm::Vector::top
T & top(void)
Definition: Vector.h:171
elm::t::size
uint64 size
Definition: arch.h:35
elm::Vector::Iter::ended
bool ended(void) const
Definition: Vector.h:71
elm::Vector::addAfter
void addAfter(const Iter &i, const T &v)
Definition: Vector.h:164
elm::Vector::contains
bool contains(const T &v) const
Definition: Vector.h:82
elm::Vector::last
T & last()
Definition: Vector.h:159
elm::Vector::addAll
void addAll(const C &c)
Definition: Vector.h:102
elm::array::move
void move(T *target, const T *source, int size)
Definition: array.h:74
elm::Vector::remove
void remove(const T &value)
Definition: Vector.h:104
elm::array::construct
void construct(T *t, int size)
Definition: array.h:82
elm::Vector::removeLast
void removeLast(void)
Definition: Vector.h:163
elm::Vector::remove
void remove(const Iter &i)
Definition: Vector.h:107
elm::Vector::get
const T & get(int i) const
Definition: Vector.h:117
elm::Vector::operator[]
T & operator[](const Iter &i)
Definition: Vector.h:137
elm::Vector::end
Iter end(void) const
Definition: Vector.h:89
elm::Vector::removeAll
void removeAll(const C &c)
Definition: Vector.h:105
elm::Vector::equivalence
E & equivalence()
Definition: Vector.h:48
elm::Vector::operator+=
Vector< T > & operator+=(const T x)
Definition: Vector.h:108
elm::Vector::grow
void grow(int new_cap)
Definition: Vector.h:57
elm::Vector::insert
void insert(const Iter &i, const T &v)
Definition: Vector.h:142
elm::Vector::~Vector
~Vector(void)
Definition: Vector.h:46
elm::array::destruct
void destruct(T *t, int size)
Definition: array.h:84
elm::Array
Definition: Array.h:32
elm::Vector::set
void set(int i, const T &v)
Definition: Vector.h:130
elm::Vector::allocator
A & allocator()
Definition: Vector.h:50
elm::InplacePreIterator
Definition: iter.h:49
elm::Vector::removeAt
void removeAt(const Iter &i)
Definition: Vector.h:146
elm::Vector::operator-=
Vector< T > & operator-=(const T x)
Definition: Vector.h:109
elm::Vector::operator[]
const T & operator[](int i) const
Definition: Vector.h:125
elm::Vector::Vector
Vector(const Vector< T > &vec)
Definition: Vector.h:45
elm::Vector::setLength
void setLength(int new_length)
Definition: Vector.h:60
elm::Vector::operator=
Vector< T > & operator=(const Vector &vec)
Definition: Vector.h:113
Vector
elm::Vector::clear
void clear(void)
Definition: Vector.h:100
elm::Vector::addLast
void addLast(const T &v)
Definition: Vector.h:161
elm::Vector::addBefore
void addBefore(const Iter &i, const T &v)
Definition: Vector.h:165
elm::Vector::pop
T pop(void)
Definition: Vector.h:172
elm::Vector::Iter
Definition: Vector.h:67
elm::Vector::isEmpty
bool isEmpty(void) const
Definition: Vector.h:86
elm::Vector::removeAt
void removeAt(int i)
Definition: Vector.h:143