Aleph-w 7.10.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 <iterator>
52#include <initializer_list>
53#include <type_traits>
54#include <aleph.H>
55#include <htlist.H>
56#include <tpl_dynDlist.H>
57#include <ah-dry.H>
58#include <ah-args-ctor.H>
59#include <tpl_memArray.H>
60#include <ah-iterator.H>
61
62namespace Aleph {
63
135template <typename T>
136class Array : public LocateFunctions<Array<T>, T>,
137 public FunctionalMethods<Array<T>, T>,
138 public GenericKeys<Array<T>, T>,
139 public EqualSequenceMethod<Array<T>>,
140 public StlAlephIterator<Array<T>>
141{
143
145 struct Reserve_Tag {};
146
153 Array(Reserve_Tag, const size_t cap) : array(cap)
154 { /* empty */
155 }
156
157public:
158 using Item_Type = T;
159 using Key_Type = T;
160
165 Array() : array(32)
166 { /* empty */
167 }
168
185 Array(size_t) = delete;
186
194 [[nodiscard]] static Array create_reserved(const size_t cap)
195 {
196 return Array(Reserve_Tag{}, cap);
197 }
198
214 Array(size_t n, const T &value)
215 requires(std::is_copy_constructible_v<T> and std::is_copy_assignable_v<T>)
216 : array(n)
217 {
218 for (size_t i = 0; i < n; ++i)
219 array.put(value);
220 }
221
245 static Array create(const size_t n)
246 {
247 Array ret = create_reserved(n);
248 ret.putn(n);
249 return ret;
250 }
251
253 Array(const Array &s)
254 requires(std::is_copy_constructible_v<T> and std::is_copy_assignable_v<T>)
255 : array(s.array)
256 { /* empty */
257 }
258
260 Array(Array &&s) noexcept : array(std::forward<MemArray<T>>(s.array))
261 { /* empty */
262 }
263
269 template <template <typename> class List>
271 {
272 items.for_each([this](const T &item) { append(item); });
273 }
274
282 template <std::input_iterator It, std::sentinel_for<It> Sentinel>
283 Array(It first, Sentinel last) : Array()
284 {
285 for (; first != last; ++first)
286 append(*first);
287 }
288
294 Array(std::initializer_list<T> items) : Array()
295 {
296 for (const auto &item : items)
297 append(item);
298 }
299
302 requires(std::is_copy_constructible_v<T> and std::is_copy_assignable_v<T>)
303 {
304 if (this == &s)
305 return *this;
306
307 array = s.array;
308
309 return *this;
310 }
311
313 void swap(Array &s) noexcept
314 {
315 array.swap(s.array);
316 }
317
319 Array &operator = (Array &&s) noexcept
320 {
321 array.swap(s.array);
322 return *this;
323 }
324
331 T &append(const T &data)
332 requires std::is_copy_assignable_v<T>
333 {
334 return array.put(data);
335 }
336
343 T &append(T &&data)
344 {
345 return array.put(std::forward<T>(data));
346 }
347
348 Array &append(const Array &a)
349 {
350 array.append(a.array);
351 return *this;
352 }
353
354 Array append(const Array &a) const
355 {
356 Array ret = *this;
357 ret.append(a);
358 return ret; // returning ret.append(a), a reference, would copy ret again
359 }
360
368 T &insert(const T &data)
369 {
370 return array.push(data);
371 }
372
380 T &insert(T &&data)
381 {
382 return array.push(std::forward<T>(data));
383 }
384
392 void putn(const size_t n)
393 {
394 array.putn(n);
395 }
396
402 void reserve(size_t cap)
403 {
404 array.reserve(cap);
405 }
406
409 {
410 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
411 return *array.get_ptr();
412 }
413
414 const T &base() const
415 {
416 ah_underflow_error_if(is_empty()) << "Array::base(): empty array";
417 return *array.get_ptr();
418 }
419
426 void empty() noexcept
427 {
428 array.empty();
429 }
430
435 void clear() noexcept
436 {
437 empty();
438 }
439
444 [[nodiscard]] constexpr bool is_empty() const noexcept
445 {
446 return array.size() == 0;
447 }
448
450 [[nodiscard]] constexpr size_t size() const noexcept
451 {
452 return array.size();
453 }
454
456 [[nodiscard]] constexpr size_t capacity() const noexcept
457 {
458 return array.capacity();
459 }
460
465 [[nodiscard]] T &get_first()
466 {
467 return array.first();
468 }
469
474 [[nodiscard]] const T &get_first() const
475 {
476 return array.first();
477 }
478
483 [[nodiscard]] T &get_last()
484 {
485 return array.last();
486 }
487
492 [[nodiscard]] const T &get_last() const
493 {
494 return array.last();
495 }
496
497 [[nodiscard]] T remove_last()
498 {
499 return array.remove_last();
500 }
501
502 [[nodiscard]] T remove_first()
503 {
504 return array.remove_first();
505 }
506
509 [[nodiscard]] T &operator [] (size_t i)
510 {
511 return array[i];
512 }
513
516 [[nodiscard]] const T &operator [] (size_t i) const
517 {
518 return array[i];
519 }
520
522 [[nodiscard]] constexpr T &operator () (const size_t i) noexcept
523 {
524 return array(i);
525 }
526
528 [[nodiscard]] constexpr const T &operator () (const size_t i) const noexcept
529 {
530 return array(i);
531 }
532
541 {
542 array.reverse();
543 return *this;
544 }
545
556 {
557 const size_t &n = array.size();
558 Array ret = create_reserved(n);
559 for (size_t i = 0; i < n; ++i)
560 ret.append(array(n - i - 1));
561 return ret;
562 }
563
573 {
574 return reverse();
575 }
576
586 {
587 return reverse_in_place();
588 }
589
599 Array rev() const
600 {
601 return reverse();
602 }
603
615 template <class Op>
616 requires std::predicate<Op &, T &>
617 bool traverse(Op &op)
618 {
619 return array.traverse(op);
620 }
621
634 template <class Op>
635 requires std::predicate<Op &, const T &>
636 bool traverse(Op &op) const
637 {
638 for (size_t i = 0; i < size(); ++i)
639 if (not op((*this)[i]))
640 return false;
641 return true;
642 }
643
658 template <class Op>
659 requires std::predicate<Op &, T &>
660 bool traverse(Op &&op)
661 {
662 return traverse<Op>(op);
663 }
664
677 template <class Op>
678 requires std::predicate<Op &, const T &>
679 bool traverse(Op &&op) const
680 {
681 return traverse<Op>(op);
682 }
683
684 [[nodiscard]] bool is_valid() const noexcept
685 {
686 return array.is_valid();
687 }
688
691 requires std::copy_constructible<T>
692 {
693 return *this; // copy
694 }
695
700 struct Iterator : public MemArray<T>::Iterator
701 {
702 using Base = typename MemArray<T>::Iterator;
703 using Base::Base;
705
710
712 Iterator(Array<T> &s) noexcept : Base(s.array) {}
713
715 Iterator(const Array<T> &) = delete;
716 };
717
720};
721
722template <typename T, typename... Args>
723Array<T> build_array(Args &&... args)
724{
725 return build_container<Array<T>>(std::forward<Args>(args)...);
726}
727
728template <class Container>
729inline std::vector<typename Container::Item_Type> to_stdvector(const Container &c)
730{
731 std::vector<typename Container::Item_Type> ret(c.size());
732 for (size_t i = 0; i < c.size(); ++i)
733 ret[i] = c(i);
734 return ret;
735}
736
737} // end namespace Aleph
738
739#endif /* TPL_ARRAY_H */
Variadic constructor macros for containers.
Container traversal and functional operation mixins.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
STL-compatible iterator adapters for Aleph containers.
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:141
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
Definition tpl_array.H:194
Array rev() const
Alias for reverse() const.
Definition tpl_array.H:599
bool traverse(Op &&op) const
Traverse const elements with a temporary operation.
Definition tpl_array.H:679
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
void empty() noexcept
Empties the container.
Definition tpl_array.H:426
void clear() noexcept
Empties the container.
Definition tpl_array.H:435
T & append(T &&data)
Append data
Definition tpl_array.H:343
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
Array(const List< T > &items)
Copy elements from an Aleph container in traversal order.
Definition tpl_array.H:270
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:408
Array(size_t n, const T &value)
Construct an array with n copies of value.
Definition tpl_array.H:214
T Key_Type
The type of element.
Definition tpl_array.H:159
MemArray< T > array
Definition tpl_array.H:142
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:368
const T & get_last() const
Return a constant reference to the last element.
Definition tpl_array.H:492
Array(size_t)=delete
Deleted: a single integer argument is not accepted.
Array & rev()
Reverse this array in place.
Definition tpl_array.H:585
Array(Reserve_Tag, const size_t cap)
Construct an empty array with at least cap slots of capacity.
Definition tpl_array.H:153
Array(Array &&s) noexcept
Move constructor.
Definition tpl_array.H:260
Array(std::initializer_list< T > items)
Construct from the listed elements in order.
Definition tpl_array.H:294
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
Array & reverse_in_place()
Alias for reverse().
Definition tpl_array.H:572
bool traverse(Op &op) const
Traverse const elements from first to last.
Definition tpl_array.H:636
T & get_last()
Return a modifiable reference to the last element.
Definition tpl_array.H:483
const T & get_first() const
Return a constant reference to the first element.
Definition tpl_array.H:474
Array append(const Array &a) const
Definition tpl_array.H:354
bool traverse(Op &op)
Traverse mutable elements from first to last.
Definition tpl_array.H:617
bool traverse(Op &&op)
Traverse mutable elements with a temporary operation.
Definition tpl_array.H:660
Array & append(const Array &a)
Definition tpl_array.H:348
const T & base() const
Definition tpl_array.H:414
T & insert(T &&data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:380
T & operator[](size_t i)
Return a reference to the ith element.
Definition tpl_array.H:509
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
Array()
The type of key.
Definition tpl_array.H:165
constexpr T & operator()(const size_t i) noexcept
Return element i; requires i < size(), asserted in Debug builds.
Definition tpl_array.H:522
Array(const Array &s)
Copy constructor.
Definition tpl_array.H:253
Array & reverse()
Reverse the order of items in this array, in place.
Definition tpl_array.H:540
bool is_valid() const noexcept
Definition tpl_array.H:684
Array & operator=(const Array &s)
Assign by copy s to this
Definition tpl_array.H:301
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
constexpr size_t capacity() const noexcept
Return the internal capacity.
Definition tpl_array.H:456
Array reverse() const
Return a copy of this array with its items reversed.
Definition tpl_array.H:555
Array(It first, Sentinel last)
Construct from an input iterator range [first, last).
Definition tpl_array.H:283
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:392
T & get_first()
Return a modifiable reference to the first element.
Definition tpl_array.H:465
Array to_array() const
Copy to Aleph::Array (requires copyable elements).
Definition tpl_array.H:690
Read-only view of an Aleph iterator: the iterator of a constant container.
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Simple, scalable and fast dynamic array.
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:2945
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Common sequential searching methods on containers.
Definition ah-dry.H:555
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Array< T > build_array(Args &&... args)
Definition tpl_array.H:723
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
Definition tpl_array.H:729
STL namespace.
Iterator on the items of an array.
Definition tpl_array.H:701
Iterator(Array< T > &s) noexcept
Iterator on the modifiable array s.
Definition tpl_array.H:712
Iterator(const Array< T > &)=delete
A constant array is iterated through Const_Iterator.
typename MemArray< T >::Iterator Base
Definition tpl_array.H:702
Tag that selects the capacity constructor used by create_reserved().
Definition tpl_array.H:145
Simple iterator on elements of array.
Generic list of items stored in a container.
Definition ah-dry.H:2896
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Definition ah-dry.H:2904
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.