Aleph-w  1.9
General library for algorithms and data structures
tpl_array.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 
28 # ifndef TPL_ARRAY_H
29 # define TPL_ARRAY_H
30 
31 # include <iostream>
32 # include <aleph.H>
33 # include <htlist.H>
34 # include <tpl_dynDlist.H>
35 # include <ah-dry.H>
36 # include <ah-args-ctor.H>
37 # include <tpl_memArray.H>
38 
39 using namespace std;
40 
41 using namespace Aleph;
42 
43 namespace Aleph {
44 
53  template <typename T>
54 class Array : public LocateFunctions<Array<T>, T>,
55  public FunctionalMethods<Array<T>, T>,
56  public GenericKeys<Array<T>, T>,
57  public EqualToMethod<Array<T>>,
58  public StlAlephIterator<Array<T>>
59 {
60  MemArray<T> array;
61 
62 public:
63 
64  using Item_Type = T;
65 
67  Array(size_t dim = 32) : array(dim) { /* empty */ }
68 
70  Array(const Array & s) : array(s.array) { /* empty */ }
71 
73  Array(Array && s) : array(std::forward<MemArray<T>>(s.array)) { /* empty */ }
74 
75  Special_Ctors(Array, T);
76 
77  Args_Ctor(Array, T);
78 
80  Array & operator = (const Array & s)
81  {
82  if (this == &s)
83  return *this;
84 
85  array = s.array;
86 
87  return *this;
88  }
89 
91  void swap(Array & s) noexcept
92  {
93  array.swap(s.array);
94  }
95 
97  Array & operator = (Array && s) noexcept
98  {
99  array.swap(s.array);
100  return *this;
101  }
102 
109  T & append(const T & data)
110  {
111  return array.put(data);
112  }
113 
120  T & append(T && data)
121  {
122  return array.put(std::forward<T>(data));
123  }
124 
125  Array & append(const Array & a)
126  {
127  array.append(a.array);
128  return *this;
129  }
130 
131  Array append(const Array & a) const
132  {
133  Array ret = *this;
134  return ret.append(a);
135  }
136 
144  T & insert(const T & data)
145  {
146  return array.push(data);
147  }
148 
156  T & insert(T && data)
157  {
158  return array.push(std::forward<T>(data));
159  }
160 
170  void putn(const size_t n) { array.putn(n); }
171 
177  void reserve(size_t cap) { array.reserve(cap); }
178 
180  T & base() const noexcept { return *array.get_ptr(); }
181 
183  void empty() noexcept { array.empty(); }
184 
186  bool is_empty() const noexcept { return array.size() == 0; }
187 
189  size_t size() const noexcept { return array.size(); }
190 
192  size_t capacity() const noexcept { return array.capacity(); }
193 
196  T & get_first() const { return array.first(); }
197 
200  T & get_last() const { return array.last(); }
201 
202  T remove_last() { return array.remove_last(); }
203 
204  T remove_first() { return array.remove_first(); }
205 
208  T & operator [] (size_t i) const
209  {
210  return array[i];
211  }
212 
214  T & operator () (const size_t i) const noexcept
215  {
216  return array(i);
217  }
218 
221  {
222  array.reverse();
223  return *this;
224  }
225 
227  Array reverse() const
228  {
229  const size_t & n = array.size();
230  Array ret(n);
231  for (size_t i = 0; i < n; ++i)
232  ret.append(array(n - i - 1));
233  return ret;
234  }
235 
236  Array & rev() { return reverse(); }
237 
238  const Array rev() const { return reverse(); }
239 
247  template <class Operation>
248  bool traverse(Operation & operation) noexcept(noexcept(operation))
249  {
250  return array.traverse(operation);
251  }
252 
254  template <class Operation>
255  bool traverse(Operation & operation) const noexcept(noexcept(operation))
256  {
257  return array.traverse(operation);
258  }
259 
261  template <class Operation>
262  bool traverse(Operation && operation = Operation()) const
263  noexcept(noexcept(operation))
264  {
265  return array.traverse(operation);
266  }
267 
269  template <class Operation>
270  bool traverse(Operation && operation = Operation())
271  noexcept(noexcept(operation))
272  {
273  return array.traverse(operation);
274  }
275 
276  bool is_valid() const noexcept { return array.is_valid(); }
277 
282  struct Iterator : public MemArray<T>::Iterator
283  {
284  using Base = typename MemArray<T>::Iterator;
285  using Base::Base;
286  using Set_Type = Array;
287 
289  Iterator(const Array<T> & s) noexcept : Base(s.array) {}
290  };
291 };
292 
293  template <typename T, typename ... Args>
294 Array<T> build_array(Args ... args)
295 {
296  return build_container<Array<T>>(args...);
297 }
298 
299 
300 } // end namespace Aleph
301 
302 # endif /* TPL_ARRAY_H */
303 
Definition: tpl_memArray.H:73
void swap(Array &s) noexcept
Swap this with s
Definition: tpl_array.H:91
Definition: htlist.H:450
T & append(T &&data)
Definition: tpl_array.H:120
Definition: htlist.H:1290
Array(const Array &s)
Copy constructor.
Definition: tpl_array.H:70
Definition: htlist.H:133
bool traverse(Operation &&operation=Operation()) noexcept(noexcept(operation))
Definition: tpl_array.H:270
bool traverse(Operation &&operation=Operation()) const noexcept(noexcept(operation))
Definition: tpl_array.H:262
T & push(const T &item)
Definition: tpl_memArray.H:350
void putn(const size_t n)
Definition: tpl_array.H:170
size_t capacity() const noexcept
The type of element of array.
Definition: tpl_memArray.H:200
T & first() const
Return a modifiable reference to the first element.
Definition: tpl_memArray.H:542
void reserve(const size_t cap)
Definition: tpl_memArray.H:481
void swap(MemArray &a) noexcept
Swap in constant time this with a
Definition: tpl_memArray.H:238
Array reverse() const
Return a copy of this with its items reversed.
Definition: tpl_array.H:227
T & last() const
Return a modifiable reference to the last element.
Definition: tpl_memArray.H:534
T * get_ptr() const noexcept
Return the current base of array.
Definition: tpl_memArray.H:92
bool traverse(Operation &operation)
Definition: tpl_memArray.H:598
size_t size() const noexcept
Return the number of elements.
Definition: tpl_memArray.H:203
T & base() const noexcept
Return a modifiable reference to first element of array.
Definition: tpl_array.H:180
size_t size() const noexcept
Return the number of elements stored in the stack.
Definition: tpl_array.H:189
Array(Array &&s)
Move constructor.
Definition: tpl_array.H:73
Definition: ah-comb.H:35
T & insert(const T &data)
Definition: tpl_array.H:144
T & insert(T &&data)
Definition: tpl_array.H:156
T remove_last()
Definition: tpl_memArray.H:531
Definition: tpl_memArray.H:635
void putn(const size_t more)
Definition: tpl_memArray.H:431
Definition: tpl_array.H:282
Array & reverse()
Reverse the order of items in array.
Definition: tpl_array.H:220
void empty() noexcept
Empty the stack.
Definition: tpl_array.H:183
bool is_empty() const noexcept
Return true if stack is empty.
Definition: tpl_array.H:186
Iterator(const Array< T > &s) noexcept
Initialize an iterator on array s
Definition: tpl_array.H:289
T & append(const T &data)
Definition: tpl_array.H:109
bool traverse(Operation &operation) const noexcept(noexcept(operation))
Definition: tpl_array.H:255
Array(size_t dim=32)
The type of element.
Definition: tpl_array.H:67
Definition: tpl_array.H:54
T remove_first()
Remove first item. Gap is closed.
Definition: tpl_memArray.H:381
T & append(T &item)
Definition: tpl_memArray.H:395
void empty()
Empty the container. The array is not contracted.
Definition: tpl_memArray.H:292
bool traverse(Operation &operation) noexcept(noexcept(operation))
Definition: tpl_array.H:248
T & get_first() const
Definition: tpl_array.H:196
void reserve(size_t cap)
Definition: tpl_array.H:177
T & put(const T &item)
Definition: tpl_memArray.H:310
size_t capacity() const noexcept
Return the internal capacity.
Definition: tpl_array.H:192
MemArray & reverse()
Reverse the order of items in array.
Definition: tpl_memArray.H:556
T & get_last() const
Definition: tpl_array.H:200
Definition: htlist.H:1323

Leandro Rabindranath León