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
44#ifndef TPL_ARRAY_H
45#define TPL_ARRAY_H
46
47#include <iostream>
48#include <utility>
49#include <vector>
50#include <concepts>
51#include <type_traits>
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
132template <typename T>
133class Array : public LocateFunctions<Array<T>, T>,
134 public FunctionalMethods<Array<T>, T>,
135 public GenericKeys<Array<T>, T>,
136 public EqualSequenceMethod<Array<T>>,
137 public StlAlephIterator<Array<T>>
138{
140
141public:
142 using Item_Type = T;
143 using Key_Type = T;
144
146 Array(size_t dim = 32) : array(dim)
147 { /* empty */
148 }
149
164 Array(size_t n, const T &value)
165 requires(std::is_copy_constructible_v<T> and std::is_copy_assignable_v<T>)
166 : array(n)
167 {
168 for (size_t i = 0; i < n; ++i)
169 array.put(value);
170 }
171
196 static Array create(size_t n)
197 {
198 Array ret(n);
199 ret.putn(n);
200 return ret;
201 }
202
204 Array(const Array &s)
205 requires(std::is_copy_constructible_v<T> and std::is_copy_assignable_v<T>)
206 : array(s.array)
207 { /* empty */
208 }
209
211 Array(Array &&s) noexcept : array(std::forward<MemArray<T>>(s.array))
212 { /* empty */
213 }
214
216
218
221 requires(std::is_copy_constructible_v<T> and std::is_copy_assignable_v<T>)
222 {
223 if (this == &s)
224 return *this;
225
226 array = s.array;
227
228 return *this;
229 }
230
232 void swap(Array &s) noexcept
233 {
234 array.swap(s.array);
235 }
236
238 Array &operator = (Array &&s) noexcept
239 {
240 array.swap(s.array);
241 return *this;
242 }
243
250 T &append(const T &data)
251 requires std::is_copy_assignable_v<T>
252 {
253 return array.put(data);
254 }
255
262 T &append(T &&data)
263 {
264 return array.put(std::forward<T>(data));
265 }
266
267 Array &append(const Array &a)
268 {
269 array.append(a.array);
270 return *this;
271 }
272
273 Array append(const Array &a) const
274 {
275 Array ret = *this;
276 return ret.append(a);
277 }
278
286 T &insert(const T &data)
287 {
288 return array.push(data);
289 }
290
298 T &insert(T &&data)
299 {
300 return array.push(std::forward<T>(data));
301 }
302
310 void putn(const size_t n)
311 {
312 array.putn(n);
313 }
314
320 void reserve(size_t cap)
321 {
322 array.reserve(cap);
323 }
324
327 {
328 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
329 return *array.get_ptr();
330 }
331
332 const T &base() const
333 {
334 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
335 return *array.get_ptr();
336 }
337
342 {
343 array.empty();
344 }
345
351 {
352 empty();
353 }
354
359 [[nodiscard]] constexpr bool is_empty() const noexcept
360 {
361 return array.size() == 0;
362 }
363
365 [[nodiscard]] constexpr size_t size() const noexcept
366 {
367 return array.size();
368 }
369
371 [[nodiscard]] constexpr size_t capacity() const noexcept
372 {
373 return array.capacity();
374 }
375
379 {
380 return array.first();
381 }
382
386 {
387 return array.first();
388 }
389
393 {
394 return array.last();
395 }
396
400 {
401 return array.last();
402 }
403
405 {
406 return array.remove_last();
407 }
408
410 {
411 return array.remove_first();
412 }
413
416 [[nodiscard]] T &operator [] (size_t i)
417 {
418 return array[i];
419 }
420
423 [[nodiscard]] const T &operator [] (size_t i) const
424 {
425 return array[i];
426 }
427
429 [[nodiscard]] constexpr T &operator () (const size_t i) noexcept
430 {
431 return array(i);
432 }
433
435 [[nodiscard]] constexpr const T &operator () (const size_t i) const noexcept
436 {
437 return array(i);
438 }
439
448 {
449 array.reverse();
450 return *this;
451 }
452
463 {
464 const size_t &n = array.size();
465 Array ret(n);
466 for (size_t i = 0; i < n; ++i)
467 ret.append(array(n - i - 1));
468 return ret;
469 }
470
480 {
481 return reverse();
482 }
483
493 {
494 return reverse_in_place();
495 }
496
506 Array rev() const
507 {
508 return reverse();
509 }
510
522 template <class Op>
523 requires std::predicate<Op &, T &>
524 bool traverse(Op &op)
525 {
526 return array.traverse(op);
527 }
528
541 template <class Op>
542 requires std::predicate<Op &, const T &>
543 bool traverse(Op &op) const
544 {
545 for (size_t i = 0; i < size(); ++i)
546 if (not op((*this)[i]))
547 return false;
548 return true;
549 }
550
565 template <class Op>
566 requires std::predicate<Op &, T &>
567 bool traverse(Op &&op)
568 {
569 return traverse<Op>(op);
570 }
571
584 template <class Op>
585 requires std::predicate<Op &, const T &>
586 bool traverse(Op &&op) const
587 {
588 return traverse<Op>(op);
589 }
590
592 {
593 return array.is_valid();
594 }
595
599 {
600 return *this; // copy
601 }
602
607 struct Iterator : public MemArray<T>::Iterator
608 {
609 using Base = typename MemArray<T>::Iterator;
610 using Base::Base;
612
617
619 Iterator(const Array<T> &s) noexcept : Base(s.array) {}
620 };
621};
622
623template <typename T, typename... Args>
628
629template <class Container>
630inline std::vector<typename Container::Item_Type> to_stdvector(const Container &c)
631{
632 std::vector<typename Container::Item_Type> ret(c.size());
633 for (size_t i = 0; i < c.size(); ++i)
634 ret[i] = c(i);
635 return ret;
636}
637
638} // end namespace Aleph
639
640#endif /* TPL_ARRAY_H */
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:373
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:113
Core header for the Aleph-w library.
size_t size_t int32_t value
Definition ca-c-api.h:116
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
Array rev() const
Alias for reverse() const.
Definition tpl_array.H:506
bool traverse(Op &&op) const
Traverse const elements with a temporary operation.
Definition tpl_array.H:586
static Array create(size_t n)
Create an array with n logical elements.
Definition tpl_array.H:196
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:365
void empty() noexcept
Empties the container.
Definition tpl_array.H:341
void clear() noexcept
Empties the container.
Definition tpl_array.H:350
T & append(T &&data)
Append data
Definition tpl_array.H:262
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:359
Array(size_t dim=32)
The type of key.
Definition tpl_array.H:146
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:326
Array(size_t n, const T &value)
Construct an array with n copies of value.
Definition tpl_array.H:164
T Key_Type
The type of element.
Definition tpl_array.H:143
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:286
Array & rev()
Reverse this array in place.
Definition tpl_array.H:492
Array(Array &&s) noexcept
Move constructor.
Definition tpl_array.H:211
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:232
Array & reverse_in_place()
Alias for reverse().
Definition tpl_array.H:479
bool traverse(Op &op) const
Traverse const elements from first to last.
Definition tpl_array.H:543
Array append(const Array &a) const
Definition tpl_array.H:273
bool traverse(Op &op)
Traverse mutable elements from first to last.
Definition tpl_array.H:524
bool traverse(Op &&op)
Traverse mutable elements with a temporary operation.
Definition tpl_array.H:567
Array & append(const Array &a)
Definition tpl_array.H:267
const T & base() const
Definition tpl_array.H:332
const T & get_first() const noexcept
return a constant reference to the first element.
Definition tpl_array.H:385
T & insert(T &&data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:298
T & operator[](size_t i)
Return a reference to the ith element.
Definition tpl_array.H:416
T & get_first() noexcept
return a modifiable reference to the first element.
Definition tpl_array.H:378
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:429
Array(const Array &s)
Copy constructor.
Definition tpl_array.H:204
Array & reverse()
Reverse the order of items in this array, in place.
Definition tpl_array.H:447
bool is_valid() const noexcept
Definition tpl_array.H:591
Array & operator=(const Array &s)
Assign by copy s to this
Definition tpl_array.H:220
const T & get_last() const noexcept
return a constant reference to the last element.
Definition tpl_array.H:399
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:250
T & get_last() noexcept
return a modifiable reference to the last element.
Definition tpl_array.H:392
constexpr size_t capacity() const noexcept
Return the internal capacity.
Definition tpl_array.H:371
Array reverse() const
Return a copy of this array with its items reversed.
Definition tpl_array.H:462
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:320
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:310
Array to_array() const
Copy to Aleph::Array (requires copyable elements).
Definition tpl_array.H:597
Simple, scalable and fast dynamic array.
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:1891
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:658
and
Conditional mapping of the elements of the container.
Definition ah-dry.H:1137
Common sequential searching methods on containers.
Definition ah-dry.H:200
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:4063
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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:105
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
Definition tpl_array.H:630
Array< T > build_array(Args... args)
Definition tpl_array.H:624
STL namespace.
Iterator on the items of an array.
Definition tpl_array.H:608
Iterator(const Array< T > &s) noexcept
Initialize an iterator on array s
Definition tpl_array.H:619
typename MemArray< T >::Iterator Base
Definition tpl_array.H:609
Simple iterator on elements of array.
Generic list of items stored in a container.
Definition ah-dry.H:1846
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.