Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_array.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
45#ifndef TPL_ARRAY_H
46#define TPL_ARRAY_H
47
48# include <iostream>
49# include <utility>
50# include <vector>
51# include <aleph.H>
52# include <htlist.H>
53# include <tpl_dynDlist.H>
54# include <ah-dry.H>
55# include <ah-args-ctor.H>
56# include <tpl_memArray.H>
57
58namespace Aleph
59{
60
132template<typename T>
133class Array : public LocateFunctions<Array<T>, T>,
134 public FunctionalMethods<Array<T>, T>,
135 public GenericKeys<Array<T>, T>,
136 public EqualToMethod<Array<T>>,
137 public StlAlephIterator<Array<T>>
138{
140
141public:
142
143 using Item_Type = T;
144 using Key_Type = T;
145
147 Array(size_t dim = 32) : array(dim) { /* empty */ }
148
161 Array(size_t n, const T& value) : array(n)
162 {
163 for (size_t i = 0; i < n; ++i)
164 array.put(value);
165 }
166
191 static Array create(size_t n)
192 {
193 Array ret(n);
194 ret.putn(n);
195 return ret;
196 }
197
199 Array(const Array &s) : array(s.array) { /* empty */ }
200
202 Array(Array &&s) noexcept: array(std::forward<MemArray < T>>
203 (s.array)) { /* empty */ }
204
206
208
211 {
212 if (this == &s)
213 return *this;
214
215 array = s.array;
216
217 return *this;
218 }
219
221 void swap(Array &s) noexcept
222 {
223 array.swap(s.array);
224 }
225
227 Array &operator=(Array &&s) noexcept
228 {
229 array.swap(s.array);
230 return *this;
231 }
232
239 T &append(const T &data)
240 {
241 return array.put(data);
242 }
243
250 T &append(T &&data)
251 {
252 return array.put(std::forward<T>(data));
253 }
254
255 Array &append(const Array &a)
256 {
257 array.append(a.array);
258 return *this;
259 }
260
261 Array append(const Array &a) const
262 {
263 Array ret = *this;
264 return ret.append(a);
265 }
266
274 T &insert(const T &data)
275 {
276 return array.push(data);
277 }
278
286 T &insert(T &&data)
287 {
288 return array.push(std::forward<T>(data));
289 }
290
298 void putn(const size_t n)
299 {
300 array.putn(n);
301 }
302
308 void reserve(size_t cap)
309 {
310 array.reserve(cap);
311 }
312
315 {
316 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
317 return *array.get_ptr();
318 }
319
320 const T &base() const
321 {
322 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
323 return *array.get_ptr();
324 }
325
327 void empty() noexcept { array.empty(); }
328
330 [[nodiscard]] constexpr bool is_empty() const noexcept { return array.size() == 0; }
331
333 [[nodiscard]] constexpr size_t size() const noexcept { return array.size(); }
334
336 [[nodiscard]] constexpr size_t capacity() const noexcept { return array.capacity(); }
337
340 [[nodiscard]] T &get_first() noexcept { return array.first(); }
341
344 [[nodiscard]] const T &get_first() const noexcept { return array.first(); }
345
348 [[nodiscard]] T &get_last() noexcept { return array.last(); }
349
352 [[nodiscard]] const T &get_last() const noexcept { return array.last(); }
353
354 [[nodiscard]] T remove_last() { return array.remove_last(); }
355
356 [[nodiscard]] T remove_first() { return array.remove_first(); }
357
360 [[nodiscard]] T &operator[](size_t i)
361 {
362 return array[i];
363 }
364
367 [[nodiscard]] const T &operator[](size_t i) const
368 {
369 return array[i];
370 }
371
373 [[nodiscard]] constexpr T &operator()(const size_t i) noexcept
374 {
375 return array(i);
376 }
377
379 [[nodiscard]] constexpr const T &operator()(const size_t i) const noexcept
380 {
381 return array(i);
382 }
383
386 {
387 array.reverse();
388 return *this;
389 }
390
393 {
394 const size_t &n = array.size();
395 Array ret(n);
396 for (size_t i = 0; i < n; ++i)
397 ret.append(array(n - i - 1));
398 return ret;
399 }
400
401 Array &rev() { return reverse(); }
402
403 Array rev() const { return reverse(); }
404
412 template<class Operation>
414 {
415 return array.traverse(operation);
416 }
417
419 template<class Operation>
421 {
422 return array.traverse(operation);
423 }
424
426 template<class Operation>
428 {
429 return array.traverse(std::forward<Operation>(operation));
430 }
431
433 template<class Operation>
435 {
436 return array.traverse(std::forward<Operation>(operation));
437 }
438
439 [[nodiscard]] bool is_valid() const noexcept { return array.is_valid(); }
440
445 struct Iterator : public MemArray<T>::Iterator
446 {
447 using Base = typename MemArray<T>::Iterator;
448 using Base::Base;
450
452 Iterator(const Array<T> &s) noexcept: Base(s.array) {}
453 };
454};
455
456template<typename T, typename ... Args>
461
462template <class Container>
463inline std::vector<typename Container::Item_Type>
465{
466 std::vector<typename Container::Item_Type> ret(c.size());
467 for (size_t i = 0; i < c.size(); ++i)
468 ret[i] = c(i);
469 return ret;
470}
471
472
473} // end namespace Aleph
474
475# endif /* TPL_ARRAY_H */
476
Variadic constructor macros for containers.
#define Args_Ctor(Name, Type)
Container traversal and functional operation mixins.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:368
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:113
Core header for the Aleph-w library.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
Array rev() const
Definition tpl_array.H:403
constexpr const T & operator()(const size_t i) const noexcept
Return a constant reference to the ith element. It does not perform bound_statics checks.
Definition tpl_array.H:379
Array & operator=(Array &&s) noexcept
Assign by moving s to this
Definition tpl_array.H:227
static Array create(size_t n)
Create an array with n logical elements.
Definition tpl_array.H:191
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:333
void empty() noexcept
Empty the stack.
Definition tpl_array.H:327
T & append(T &&data)
Append data
Definition tpl_array.H:250
constexpr bool is_empty() const noexcept
Return true if stack is empty.
Definition tpl_array.H:330
Array(size_t dim=32)
The type of key.
Definition tpl_array.H:147
bool traverse(Operation &&operation=Operation()) const
Definition tpl_array.H:427
bool traverse(Operation &operation) const
Definition tpl_array.H:420
Array(const Array &s)
Copy constructor.
Definition tpl_array.H:199
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:314
T Key_Type
The type of element.
Definition tpl_array.H:144
MemArray< T > array
Definition tpl_array.H:139
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:274
bool traverse(Operation &&operation=Operation())
Definition tpl_array.H:434
Array & rev()
Definition tpl_array.H:401
Array(Array &&s) noexcept
Move constructor.
Definition tpl_array.H:202
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:221
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:239
Array append(const Array &a) const
Definition tpl_array.H:261
Array & operator=(const Array &s)
Assign by copy s to this
Definition tpl_array.H:210
bool traverse(Operation &operation)
Traverse all the items of the stack from the youngest to the oldest and conditionally performs an ope...
Definition tpl_array.H:413
Array & append(const Array &a)
Definition tpl_array.H:255
const T & base() const
Definition tpl_array.H:320
const T & get_first() const noexcept
return a constant reference to the first element.
Definition tpl_array.H:344
T & insert(T &&data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:286
T & operator[](size_t i)
Return a reference to the ith element.
Definition tpl_array.H:360
T & get_first() noexcept
return a modifiable reference to the first element.
Definition tpl_array.H:340
Array(size_t n, const T &value)
Construct an array with n copies of value.
Definition tpl_array.H:161
const T & operator[](size_t i) const
Return a constant reference to the ith element.
Definition tpl_array.H:367
constexpr T & operator()(const size_t i) noexcept
Return a reference to the ith element. It does not perform bound_statics checks.
Definition tpl_array.H:373
Array & reverse()
Reverse the order of items in array.
Definition tpl_array.H:385
bool is_valid() const noexcept
Definition tpl_array.H:439
const T & get_last() const noexcept
return a constant reference to the last element.
Definition tpl_array.H:352
T & get_last() noexcept
return a modifiable reference to the last element.
Definition tpl_array.H:348
constexpr size_t capacity() const noexcept
Return the internal capacity.
Definition tpl_array.H:336
Array reverse() const
Return a copy of this with its items reversed.
Definition tpl_array.H:392
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:308
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:298
Simple, scalable and fast dynamic array.
Equality test for containers.
Definition ah-dry.H:1534
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:548
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
Common sequential searching methods on containers.
Definition ah-dry.H:164
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4052
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
Array< T > build_array(Args ... args)
Definition tpl_array.H:457
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
Definition tpl_array.H:464
DynList< T > maps(const C &c, Op op)
Classic map operation.
Iterator on the items of an array.
Definition tpl_array.H:446
Iterator(const Array< T > &s) noexcept
Initialize an iterator on array s
Definition tpl_array.H:452
typename MemArray< T >::Iterator Base
Definition tpl_array.H:447
Simple iterator on elements of array.
Generic list of items stored in a container.
Definition ah-dry.H:1501
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.