|
Elm
2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
|
21 #ifndef INCLUDE_ELM_DATA_VECTOR_H_
22 #define INCLUDE_ELM_DATA_VECTOR_H_
28 #include <elm/array.h>
29 #include <elm/compat.h>
33 template <
class T,
class E = Equiv<T>,
class A = DefaultAlloc >
35 inline T *newVec(
int size)
36 { T *
t =
static_cast<T *
>(A::allocate(
size *
sizeof(T)));
44 inline Vector(
int _cap = 8): tab(newVec(_cap)), cap(_cap), cnt(0) { }
46 inline ~Vector(
void) {
if(tab) deleteVec(tab, cap); }
52 inline int capacity(
void)
const {
return cap; }
56 { T *rt = tab;
int rc = cnt; tab = 0; cnt = 0;
return Array<T>(rc, rt); }
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; }
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++]; }
70 inline Iter(
const self_t& vec,
int idx = 0): _vec(&vec), i(idx) { }
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; }
81 inline int count(
void)
const {
return cnt; }
83 {
for(Iter i(*
this); i(); i++)
if(v == *i)
return true;
return false; }
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()); }
91 template <
class C>
inline bool equals(
const C& c)
const {
92 int i = 0;
for(
const auto& x: c)
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); }
106 {
for(
const auto x: c)
remove(x); }
111 {
if(!tab || vec.cnt > cap) {
if(tab) deleteVec(tab, cap); cap = vec.cap; tab = newVec(vec.cap); }
117 inline const T&
get(
int i)
const
118 { ASSERTP(0 <= i && i < cnt,
"index out of bounds");
return tab[i]; }
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; }
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; }
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); }
134 { ASSERTP(index < cnt,
"index out of bounds");
return tab[index]; }
135 inline T&
get(
const Iter& i) {
return get(i.index()); }
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);
144 { ASSERTP(0 <= i && i <= cnt,
"index out of bounds");
145 array::move(tab + i, tab + i + 1, cnt - i - 1); cnt--; }
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); }
158 inline T&
first() { ASSERT(cnt > 0);
return tab[0]; }
159 inline T&
last() { ASSERT(cnt > 0);
return tab[cnt - 1]; }
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]; }
178 inline Iter
items(
void)
const {
return Iter(*
this); }
185 template <
class T,
class E,
class A>
void shrink(int l)
Definition: Vector.h:128
Vector(int _cap=8)
Definition: Vector.h:44
void copy(T *target, const T *source, int size)
Definition: array.h:70
Iter operator*(void) const
Definition: Vector.h:177
Array< const T > asArray(void) const
Definition: Vector.h:53
int lastIndexOf(const T &v, int p=-1) const
Definition: Vector.h:122
bool equals(const Iter &it) const
Definition: Vector.h:75
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
void insert(int i, const T &v)
Definition: Vector.h:138
const E & equivalence() const
Definition: Vector.h:47
void removeFirst(void)
Definition: Vector.h:162
int count(void) const
Definition: Vector.h:81
Array< T > detach(void)
Definition: Vector.h:55
Iter begin(void) const
Definition: Vector.h:88
void removeBefore(const Iter &i)
Definition: Vector.h:166
T & get(const Iter &i)
Definition: Vector.h:135
const A & allocator() const
Definition: Vector.h:49
const T & item(void) const
Definition: Vector.h:72
bool equals(const C1 &c1, const C2 &c2)
Definition: util.h:107
Vector< T, E, A > self_t
Definition: Vector.h:42
void reset(void)
Definition: Vector.h:174
T t
Definition: Vector.h:41
const T & first(void) const
Definition: Vector.h:149
Iter items(void) const
Definition: Vector.h:178
T & first()
Definition: Vector.h:158
void push(const T &v)
Definition: Vector.h:173
void removeAfter(const Iter &i)
Definition: Vector.h:167
void copy(const Vector &vec)
Definition: Vector.h:110
T & operator[](int i)
Definition: Vector.h:136
bool containsAll(const C &items)
Definition: Vector.h:84
void set(const Iter &i, const T &v)
Definition: Vector.h:132
void addFirst(const T &v)
Definition: Vector.h:160
Iter(const self_t &vec, int idx=0)
Definition: Vector.h:70
T & addNew(void)
Definition: Vector.h:64
const T & last(void) const
Definition: Vector.h:150
Array< T > asArray(void)
Definition: Vector.h:54
int indexOf(const T &v, int p=0) const
Definition: Vector.h:119
bool equals(const C &c) const
Definition: Vector.h:91
bool operator==(const Vector< T > &v) const
Definition: Vector.h:94
const T & nth(int i) const
Definition: Vector.h:155
int length(void) const
Definition: Vector.h:116
Iter find(const T &v) const
Definition: Vector.h:151
int capacity(void) const
Definition: Vector.h:52
bool operator!=(const Vector< T > &v) const
Definition: Vector.h:95
void add(const T &v)
Definition: Vector.h:101
const T & top(void) const
Definition: Vector.h:170
int index(void) const
Definition: Vector.h:74
Iter find(const T &v, const Iter &p) const
Definition: Vector.h:153
T & get(int index)
Definition: Vector.h:133
void next(void)
Definition: Vector.h:73
T & top(void)
Definition: Vector.h:171
uint64 size
Definition: arch.h:35
bool ended(void) const
Definition: Vector.h:71
void addAfter(const Iter &i, const T &v)
Definition: Vector.h:164
bool contains(const T &v) const
Definition: Vector.h:82
T & last()
Definition: Vector.h:159
void addAll(const C &c)
Definition: Vector.h:102
void move(T *target, const T *source, int size)
Definition: array.h:74
void remove(const T &value)
Definition: Vector.h:104
void construct(T *t, int size)
Definition: array.h:82
void removeLast(void)
Definition: Vector.h:163
void remove(const Iter &i)
Definition: Vector.h:107
const T & get(int i) const
Definition: Vector.h:117
T & operator[](const Iter &i)
Definition: Vector.h:137
Iter end(void) const
Definition: Vector.h:89
void removeAll(const C &c)
Definition: Vector.h:105
E & equivalence()
Definition: Vector.h:48
Vector< T > & operator+=(const T x)
Definition: Vector.h:108
void grow(int new_cap)
Definition: Vector.h:57
void insert(const Iter &i, const T &v)
Definition: Vector.h:142
~Vector(void)
Definition: Vector.h:46
void destruct(T *t, int size)
Definition: array.h:84
void set(int i, const T &v)
Definition: Vector.h:130
A & allocator()
Definition: Vector.h:50
void removeAt(const Iter &i)
Definition: Vector.h:146
Vector< T > & operator-=(const T x)
Definition: Vector.h:109
const T & operator[](int i) const
Definition: Vector.h:125
Vector(const Vector< T > &vec)
Definition: Vector.h:45
void setLength(int new_length)
Definition: Vector.h:60
Vector< T > & operator=(const Vector &vec)
Definition: Vector.h:113
void clear(void)
Definition: Vector.h:100
void addLast(const T &v)
Definition: Vector.h:161
void addBefore(const Iter &i, const T &v)
Definition: Vector.h:165
T pop(void)
Definition: Vector.h:172
bool isEmpty(void) const
Definition: Vector.h:86
void removeAt(int i)
Definition: Vector.h:143