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 <concepts>
52# include <aleph.H>
53# include <htlist.H>
54# include <tpl_dynDlist.H>
55# include <ah-dry.H>
56# include <ah-args-ctor.H>
57# include <tpl_memArray.H>
58
59namespace Aleph
60{
61
133template<typename T>
134class Array : public LocateFunctions<Array<T>, T>,
135 public FunctionalMethods<Array<T>, T>,
136 public GenericKeys<Array<T>, T>,
137 public EqualSequenceMethod<Array<T>>,
138 public StlAlephIterator<Array<T>>
139{
141
142public:
143
144 using Item_Type = T;
145 using Key_Type = T;
146
148 Array(size_t dim = 32) : array(dim) { /* empty */ }
149
162 Array(size_t n, const T& value)
163 requires (std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>)
164 : array(n)
165 {
166 for (size_t i = 0; i < n; ++i)
167 array.put(value);
168 }
169
194 static Array create(size_t n)
195 {
196 Array ret(n);
197 ret.putn(n);
198 return ret;
199 }
200
202 Array(const Array &s)
203 requires (std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>)
204 : array(s.array) { /* empty */ }
205
207 Array(Array &&s) noexcept: array(std::forward<MemArray < T>>
208 (s.array)) { /* empty */ }
209
211
213
216 requires (std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>)
217 {
218 if (this == &s)
219 return *this;
220
221 array = s.array;
222
223 return *this;
224 }
225
227 void swap(Array &s) noexcept
228 {
229 array.swap(s.array);
230 }
231
233 Array &operator=(Array &&s) noexcept
234 {
235 array.swap(s.array);
236 return *this;
237 }
238
245 T &append(const T &data)
246 requires std::is_copy_assignable_v<T>
247 {
248 return array.put(data);
249 }
250
257 T &append(T &&data)
258 {
259 return array.put(std::forward<T>(data));
260 }
261
262 Array &append(const Array &a)
263 {
264 array.append(a.array);
265 return *this;
266 }
267
268 Array append(const Array &a) const
269 {
270 Array ret = *this;
271 return ret.append(a);
272 }
273
281 T &insert(const T &data)
282 {
283 return array.push(data);
284 }
285
293 T &insert(T &&data)
294 {
295 return array.push(std::forward<T>(data));
296 }
297
305 void putn(const size_t n)
306 {
307 array.putn(n);
308 }
309
315 void reserve(size_t cap)
316 {
317 array.reserve(cap);
318 }
319
322 {
323 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
324 return *array.get_ptr();
325 }
326
327 const T &base() const
328 {
329 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
330 return *array.get_ptr();
331 }
332
336 void empty() noexcept { array.empty(); }
337
342 void clear() noexcept { empty(); }
343
348 [[nodiscard]] constexpr bool is_empty() const noexcept { return array.size() == 0; }
349
351 [[nodiscard]] constexpr size_t size() const noexcept { return array.size(); }
352
354 [[nodiscard]] constexpr size_t capacity() const noexcept { return array.capacity(); }
355
358 [[nodiscard]] T &get_first() noexcept { return array.first(); }
359
362 [[nodiscard]] const T &get_first() const noexcept { return array.first(); }
363
366 [[nodiscard]] T &get_last() noexcept { return array.last(); }
367
370 [[nodiscard]] const T &get_last() const noexcept { return array.last(); }
371
372 [[nodiscard]] T remove_last() { return array.remove_last(); }
373
374 [[nodiscard]] T remove_first() { return array.remove_first(); }
375
378 [[nodiscard]] T &operator[](size_t i)
379 {
380 return array[i];
381 }
382
385 [[nodiscard]] const T &operator[](size_t i) const
386 {
387 return array[i];
388 }
389
391 [[nodiscard]] constexpr T &operator()(const size_t i) noexcept
392 {
393 return array(i);
394 }
395
397 [[nodiscard]] constexpr const T &operator()(const size_t i) const noexcept
398 {
399 return array(i);
400 }
401
404 {
405 array.reverse();
406 return *this;
407 }
408
411 {
412 const size_t &n = array.size();
413 Array ret(n);
414 for (size_t i = 0; i < n; ++i)
415 ret.append(array(n - i - 1));
416 return ret;
417 }
418
419 Array &rev() { return reverse(); }
420
421 Array rev() const { return reverse(); }
422
430 template<class Operation>
432 {
433 return array.traverse(operation);
434 }
435
437 template<class Operation>
439 {
440 return array.traverse(operation);
441 }
442
444 template<class Operation>
446 {
447 return array.traverse(std::forward<Operation>(operation));
448 }
449
451 template<class Operation>
453 {
454 return array.traverse(std::forward<Operation>(operation));
455 }
456
457 [[nodiscard]] bool is_valid() const noexcept { return array.is_valid(); }
458
461 {
462 return *this; // copy
463 }
464
469 struct Iterator : public MemArray<T>::Iterator
470 {
471 using Base = typename MemArray<T>::Iterator;
472 using Base::Base;
474
476 Iterator(const Array<T> &s) noexcept: Base(s.array) {}
477 };
478};
479
480template<typename T, typename ... Args>
485
486template <class Container>
487inline std::vector<typename Container::Item_Type>
489{
490 std::vector<typename Container::Item_Type> ret(c.size());
491 for (size_t i = 0; i < c.size(); ++i)
492 ret[i] = c(i);
493 return ret;
494}
495
496
497} // end namespace Aleph
498
499# endif /* TPL_ARRAY_H */
500
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:139
Array rev() const
Definition tpl_array.H:421
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:397
Array & operator=(Array &&s) noexcept
Assign by moving s to this
Definition tpl_array.H:233
static Array create(size_t n)
Create an array with n logical elements.
Definition tpl_array.H:194
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
void empty() noexcept
Empties the container.
Definition tpl_array.H:336
void clear() noexcept
Empties the container.
Definition tpl_array.H:342
T & append(T &&data)
Append data
Definition tpl_array.H:257
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:348
Array(size_t dim=32)
The type of key.
Definition tpl_array.H:148
bool traverse(Operation &&operation=Operation()) const
Definition tpl_array.H:445
bool traverse(Operation &operation) const
Definition tpl_array.H:438
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:321
Array & operator=(const Array &s)
Assign by copy s to this
Definition tpl_array.H:215
T Key_Type
The type of element.
Definition tpl_array.H:145
MemArray< T > array
Definition tpl_array.H:140
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:281
bool traverse(Operation &&operation=Operation())
Definition tpl_array.H:452
Array & rev()
Definition tpl_array.H:419
Array(Array &&s) noexcept
Move constructor.
Definition tpl_array.H:207
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:227
Array append(const Array &a) const
Definition tpl_array.H:268
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:431
Array & append(const Array &a)
Definition tpl_array.H:262
const T & base() const
Definition tpl_array.H:327
const T & get_first() const noexcept
return a constant reference to the first element.
Definition tpl_array.H:362
T & insert(T &&data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:293
T & operator[](size_t i)
Return a reference to the ith element.
Definition tpl_array.H:378
Array(size_t n, const T &value)
Construct an array with n copies of value.
Definition tpl_array.H:162
T & get_first() noexcept
return a modifiable reference to the first element.
Definition tpl_array.H:358
const T & operator[](size_t i) const
Return a constant reference to the ith element.
Definition tpl_array.H:385
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:391
Array & reverse()
Reverse the order of items in array.
Definition tpl_array.H:403
bool is_valid() const noexcept
Definition tpl_array.H:457
const T & get_last() const noexcept
return a constant reference to the last element.
Definition tpl_array.H:370
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
T & get_last() noexcept
return a modifiable reference to the last element.
Definition tpl_array.H:366
constexpr size_t capacity() const noexcept
Return the internal capacity.
Definition tpl_array.H:354
Array reverse() const
Return a copy of this with its items reversed.
Definition tpl_array.H:410
Array(const Array &s)
Copy constructor.
Definition tpl_array.H:202
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:315
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:305
Array to_array() const
Copy to Aleph::Array (requires copyable elements).
Definition tpl_array.H:460
Simple, scalable and fast dynamic array.
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:1756
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:642
Common sequential searching methods on containers.
Definition ah-dry.H:196
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
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
Array< T > build_array(Args ... args)
Definition tpl_array.H:481
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
Definition tpl_array.H:488
STL namespace.
Iterator on the items of an array.
Definition tpl_array.H:470
Iterator(const Array< T > &s) noexcept
Initialize an iterator on array s
Definition tpl_array.H:476
typename MemArray< T >::Iterator Base
Definition tpl_array.H:471
Simple iterator on elements of array.
Generic list of items stored in a container.
Definition ah-dry.H:1714
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.