Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Array.h
1 /*
2  * Array classes 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 ELM_ARRAY_TABLE_H_
22 #define ELM_ARRAY_TABLE_H_
23 
24 #include <elm/assert.h>
25 #include <elm/PreIterator.h>
26 #include <elm/array.h>
27 #include <elm/compare.h>
28 
29 namespace elm {
30 
31 template <class T>
32 class Array {
33 public:
34  typedef T t;
35  typedef Array<T> self_t;
36 
37  inline Array(void): cnt(0), buf(0) { }
38  inline Array(int count, T *buffer): cnt(count), buf(buffer) { }
39 
40  inline const T *buffer(void) const { return buf; }
41  inline T *buffer(void) { return buf; }
42  inline int size(void) const { return count(); }
43  inline void set(int count, T *buffer) { cnt = count; buf = buffer; }
44  inline void set(const Array<T>& t) { cnt = t.cnt; buf = t.buf; }
45  inline void copy(const Array& t) { cnt = min(cnt, t.cnt); array::copy(buf, t.buf, cnt); }
46  inline void fill(const T& val) { array::set(buf, cnt, val); }
47  inline const T *operator()(void) const { return buffer(); }
48  inline T *operator()(void) { return buffer(); }
49  inline Array<T>& operator=(const Array<T>& t) { set(t); return *this; }
50 
51  class Iter: public PreIterator<Iter, T> {
52  public:
53  inline Iter(void): p(0), t(0) { }
54  inline Iter(const Array<T>& table): p(table.buffer()), t(table.buffer() + table.count()) { }
55  inline Iter(const Array<T>& table, bool end): p(table.buffer() + (end ? table.count() : 0)), t(table.buffer() + table.count()) { }
56  inline bool ended(void) const { return p >= t; }
57  inline const T& item(void) const { ASSERT(p < t); return *p; }
58  inline void next(void) { p++; }
59  inline bool equals(const Iter& i) const { return p == i.p && t == i.t; }
60  private:
61  const T *p, *t;
62  };
63 
64  // Collection concept
65  static const Array<T> null;
66  inline Iter items(void) const { return Iter(*this); }
67  inline Iter operator*(void) const { return items(); }
68  inline Iter begin(void) const { return items(); }
69  inline Iter end(void) const { return Iter(*this, true); }
70 
71  inline int count(void) const { return cnt; }
72  inline bool contains(const T& item)
73  { for(auto x: *this) if(x == item) return true; return false; }
74  template <class C> inline bool containsAll(const C& c)
75  { for(auto x: c) if(!contains(x)) return false; return true; }
76  inline bool isEmpty(void) const { return cnt == 0; }
77  inline operator bool(void) const { return !isEmpty(); }
78  inline bool equals(const Array<T>& a) {
79  if(cnt != a.cnt) return false;
80  for(auto i = begin(), j = a.begin(); i; ++i, ++j)
81  if(*i != *j) return false;
82  return true;
83  }
84  inline bool operator==(const Array<T>& a) const { return equals(a); }
85  inline bool operator!=(const Array<T>& a) const { return !equals(a); }
86  inline bool operator<=(const Array<T>& a) const { return a.containsAll(*this); }
87  inline bool operator<(const Array<T>& a) const { return a.containsAll(*this) && !equals(a); }
88  inline bool operator>=(const Array<T>& a) const { return containsAll(a); }
89  inline bool operator>(const Array<T>& a) const { return containsAll(a) && !equals(a); }
90 
91  // Array concept
92  inline int length(void) const { return count(); }
93  inline const T& get(int idx) const { ASSERT(0 <= idx && idx < cnt); return buf[idx]; }
94  inline int indexOf(const T& v, int i = 0) const
95  { for(; i < count(); i++) if(v == get(i)) return i; return -1; }
96  inline int lastIndexOf(const T& v, int i = -1) const
97  { if(i < 0) i = count() - 1; for(; i >= 0; i--) if(v == get(i)) return i; return -1; }
98  inline const T& operator[](int idx) const { return get(idx); }
99 
100  // MutableArray concept
101  inline void set(int idx, const T& val) { ASSERT(0 <= idx && idx < cnt); buf[idx] = val; }
102  inline void set(const Iter& i, const T& val) { ASSERT(buf <= i.p && i.p < buf + cnt); *i.p = val; }
103  inline T& get(int idx) { ASSERT(0 <= idx && idx < cnt); return buf[idx]; }
104  inline T& operator[](int idx) { return get(idx); }
105 
106 
107 protected:
108  int cnt;
109  T *buf;
110 };
111 
112 template <class T>
114 
115 
116 template <class T>
117 class AllocArray: public Array<T> {
118 public:
119  inline AllocArray(void) { }
120  inline AllocArray(int count, T *buffer): Array<T>(count, buffer) { }
121  inline AllocArray(int count): Array<T>(count, new T[count]) { }
122  inline AllocArray(int count, const T& val): Array<T>(count, new T[count]) { fill(val); }
123  inline AllocArray(const Array<T>& t): Array<T>(t.count(), new T[t.count()]) { Array<T>::copy(t); }
124  inline AllocArray(const AllocArray<T>& t): Array<T>(t.cnt, new T[t.cnt]) { Array<T>::copy(t); }
125  inline ~AllocArray(void) { if(this->buf) delete [] this->buf; }
126 
127  inline void copy(const Array<T>& t)
128  { if(this->count() < t.count()) { if(this->buf) delete [] this->buf;
129  Array<T>::set(t.count(), new T[t.count()]); } Array<T>::copy(t); }
130  inline void tie(int cnt, T *buffer) { if(this->buf) delete [] this->buf; Array<T>::set(cnt, buffer); }
131  inline void tie(const Array<T>& t) { if(this->buf) delete [] this->buf; Array<T>::set(t); }
132 
133  inline AllocArray<T>& operator=(const Array<T>& t) { copy(t); return *this; }
134  inline AllocArray<T>& operator=(const AllocArray<T>& t) { copy(t); return *this; }
135 };
136 
137 template <class T>
138 inline Array<T> _array(int n, T t[]) { return Array<T>(n, t); }
139 
140 } // elm
141 
142 #endif /* ELM_ARRAY_TABLE_H_ */
elm::AllocArray::AllocArray
AllocArray(int count)
Definition: Array.h:121
elm::Array::Array
Array(int count, T *buffer)
Definition: Array.h:38
elm::Array::end
Iter end(void) const
Definition: Array.h:69
elm::Array::buffer
const T * buffer(void) const
Definition: Array.h:40
elm::array::copy
void copy(T *target, const T *source, int size)
Definition: array.h:70
elm::AllocArray::~AllocArray
~AllocArray(void)
Definition: Array.h:125
elm::Array::size
int size(void) const
Definition: Array.h:42
elm::Array::buf
T * buf
Definition: Array.h:109
elm::Array::Iter::Iter
Iter(void)
Definition: Array.h:53
elm::Array::operator()
const T * operator()(void) const
Definition: Array.h:47
elm::PreIterator< Iter, T >::t
T t
Definition: iter.h:30
elm::AllocArray::AllocArray
AllocArray(int count, T *buffer)
Definition: Array.h:120
elm::Array::self_t
Array< T > self_t
Definition: Array.h:35
elm::Array::operator=
Array< T > & operator=(const Array< T > &t)
Definition: Array.h:49
elm::Array::begin
Iter begin(void) const
Definition: Array.h:68
elm::AllocArray::AllocArray
AllocArray(int count, const T &val)
Definition: Array.h:122
elm::Array::operator*
Iter operator*(void) const
Definition: Array.h:67
elm::AllocArray::copy
void copy(const Array< T > &t)
Definition: Array.h:127
elm::Array::operator>
bool operator>(const Array< T > &a) const
Definition: Array.h:89
elm::Array::t
T t
Definition: Array.h:34
elm::Array::copy
void copy(const Array &t)
Definition: Array.h:45
elm::Array::set
void set(int idx, const T &val)
Definition: Array.h:101
elm::Array::contains
bool contains(const T &item)
Definition: Array.h:72
elm::Array::length
int length(void) const
Definition: Array.h:92
elm::Array::Iter::equals
bool equals(const Iter &i) const
Definition: Array.h:59
bool
elm::Array::Iter
Definition: Array.h:51
elm::Array::lastIndexOf
int lastIndexOf(const T &v, int i=-1) const
Definition: Array.h:96
elm::min
const T & min(const T &x, const T &y)
Definition: compare.h:104
elm::Array::isEmpty
bool isEmpty(void) const
Definition: Array.h:76
elm::Array::set
void set(int count, T *buffer)
Definition: Array.h:43
elm::Array::operator<
bool operator<(const Array< T > &a) const
Definition: Array.h:87
elm::AllocArray::AllocArray
AllocArray(const AllocArray< T > &t)
Definition: Array.h:124
elm::AllocArray::tie
void tie(const Array< T > &t)
Definition: Array.h:131
elm::AllocArray::operator=
AllocArray< T > & operator=(const AllocArray< T > &t)
Definition: Array.h:134
elm::Array::buffer
T * buffer(void)
Definition: Array.h:41
elm::Array::Iter::ended
bool ended(void) const
Definition: Array.h:56
elm::Array::set
void set(const Iter &i, const T &val)
Definition: Array.h:102
elm::Array::operator[]
T & operator[](int idx)
Definition: Array.h:104
elm
Definition: adapter.h:26
elm::array::set
void set(T *target, int size, const T &v)
Definition: array.h:76
elm::Array::containsAll
bool containsAll(const C &c)
Definition: Array.h:74
elm::Array::cnt
int cnt
Definition: Array.h:108
elm::AllocArray::operator=
AllocArray< T > & operator=(const Array< T > &t)
Definition: Array.h:133
elm::Array::get
T & get(int idx)
Definition: Array.h:103
elm::AllocArray::tie
void tie(int cnt, T *buffer)
Definition: Array.h:130
elm::Array::Iter::Iter
Iter(const Array< T > &table)
Definition: Array.h:54
elm::AllocArray::AllocArray
AllocArray(const Array< T > &t)
Definition: Array.h:123
elm::Array::indexOf
int indexOf(const T &v, int i=0) const
Definition: Array.h:94
elm::Array::operator!=
bool operator!=(const Array< T > &a) const
Definition: Array.h:85
elm::Array::Array
Array(void)
Definition: Array.h:37
elm::AllocArray
Definition: Array.h:117
elm::String
Definition: String.h:30
elm::AllocArray::AllocArray
AllocArray(void)
Definition: Array.h:119
elm::Array::operator<=
bool operator<=(const Array< T > &a) const
Definition: Array.h:86
elm::Array::fill
void fill(const T &val)
Definition: Array.h:46
elm::Array::count
int count(void) const
Definition: Array.h:71
elm::_array
Array< T > _array(int n, T t[])
Definition: Array.h:138
elm::Array
Definition: Array.h:32
elm::Array::equals
bool equals(const Array< T > &a)
Definition: Array.h:78
elm::Array::Iter::next
void next(void)
Definition: Array.h:58
elm::Array::set
void set(const Array< T > &t)
Definition: Array.h:44
elm::Array::operator>=
bool operator>=(const Array< T > &a) const
Definition: Array.h:88
elm::PreIterator
Definition: iter.h:28
elm::Array::get
const T & get(int idx) const
Definition: Array.h:93
elm::Array::Iter::item
const T & item(void) const
Definition: Array.h:57
elm::Array::Iter::Iter
Iter(const Array< T > &table, bool end)
Definition: Array.h:55
elm::Array::items
Iter items(void) const
Definition: Array.h:66
elm::Array::operator==
bool operator==(const Array< T > &a) const
Definition: Array.h:84
elm::Array::operator()
T * operator()(void)
Definition: Array.h:48
elm::Array::operator[]
const T & operator[](int idx) const
Definition: Array.h:98