Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynMat.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
50#ifndef TPL_DYNMAT_H
51#define TPL_DYNMAT_H
52
53#include <limits>
54#include <memory>
55#include <ah-errors.H>
56#include <ahDry.H>
57#include <tpl_dynArray.H>
58
59namespace Aleph {
60
118template <typename T>
120 : public LocateFunctions<DynMatrix<T>, T>,
121 public FunctionalMethods<DynMatrix<T>, T>
122{
123 size_t n = 0;
124 size_t m = 0;
125
127
129
130 [[nodiscard]] const T & read_array_entry(const size_t i) const noexcept
131 {
132 T * entry_ptr = array_ptr->test(i);
133 return entry_ptr == nullptr ? default_value : *entry_ptr;
134 }
135
136 T & write_array_entry(const size_t i, const T & data)
137 {
138 T & ref = array_ptr->touch(i) = data;
139 return ref;
140 }
141
142 T & write_array_entry(const size_t i, T && data)
143 {
144 T & ref = array_ptr->touch(i) = std::move(data);
145 return ref;
146 }
147
148 [[nodiscard]] constexpr size_t get_index(const size_t i,
149 const size_t j) const noexcept
150 {
151 return i * m + j;
152 }
153
154 void check_indices(const size_t i, const size_t j) const
155 {
157 << "DynMatrix: row index " << i << " out of range [0, " << n << ")";
159 << "DynMatrix: column index " << j << " out of range [0, " << m << ")";
160 }
161
162public:
163
166
168 using Item_Type = T;
169
171 using Key_Type = T;
172
184 {
186 if (array_ptr)
187 array_ptr->set_default_initial_value(value);
188 }
189
194 [[nodiscard]] const T & get_default_value() const noexcept
195 {
196 return default_value;
197 }
198
212 void swap(DynMatrix & mat) noexcept(std::is_nothrow_swappable_v<T>)
213 {
214 // the swap that the specification evaluates, and not std::swap()
215 // alone, whose moves may throw where a swap of T does not
216 using std::swap;
217 swap(default_value, mat.default_value);
218 std::swap(n, mat.n);
219 std::swap(m, mat.m);
220 std::swap(array_ptr, mat.array_ptr);
221 }
222
243 void set_dimension(const size_t num_rows, const size_t num_cols)
244 {
245 ah_overflow_error_if(num_cols != 0 and num_rows > std::numeric_limits<size_t>::max() / num_cols)
246 << "DynMatrix::set_dimension(" << num_rows << ", " << num_cols
247 << "): the number of entries overflows size_t";
248
249 size_t d, s, b;
250 DynArray<T>::compute_sizes(num_rows * num_cols, d, s, b);
251 auto fresh = std::make_unique<DynArray<T>>(d, s, b);
252 fresh->set_default_initial_value(default_value);
253
254 // Nothing below can throw
255 delete array_ptr;
256 array_ptr = fresh.release();
257 n = num_rows;
258 m = num_cols;
259 }
260
271 void allocate()
272 {
273 array_ptr->reserve(n * m);
274 }
275
288 DynMatrix(const size_t num_rows, const size_t num_cols, const T & zero = T())
289 : n(num_rows), m(num_cols), array_ptr(nullptr), default_value(zero)
290 {
291 ah_invalid_argument_if(num_rows == 0 || num_cols == 0)
292 << "DynMatrix dimensions must be > 0";
293 set_dimension(n, m);
294 }
295
307 DynMatrix() noexcept(std::is_nothrow_default_constructible_v<T>) = default;
308
312 {
313 delete array_ptr;
314 }
315
325 : n(mat.n), m(mat.m), array_ptr(nullptr), default_value(mat.default_value)
326 {
327 if (mat.array_ptr)
328 {
329 set_dimension(n, m);
330 *array_ptr = *mat.array_ptr;
331 }
332 }
333
348 noexcept(std::is_nothrow_default_constructible_v<T> and
349 std::is_nothrow_swappable_v<T>)
350 : DynMatrix()
351 {
352 swap(mat);
353 }
354
365 {
366 if (this == &mat)
367 return *this;
368
370
371 if (n != mat.n || m != mat.m)
372 set_dimension(mat.n, mat.m);
373
374 if (mat.array_ptr)
375 array_ptr->copy_array(*mat.array_ptr);
376
377 return *this;
378 }
379
393 noexcept(std::is_nothrow_default_constructible_v<T> and
394 std::is_nothrow_swappable_v<T>)
395 {
396 if (this == &mat)
397 return *this;
398
399 DynMatrix taken; // before anything changes: its default value may throw
400 taken.swap(mat); // mat is left as a matrix built by default
401 swap(taken); // and what this matrix had is freed with taken
402
403 return *this;
404 }
405
414 [[nodiscard]] bool operator == (const DynMatrix<T> & mat) const
415 {
416 if (n != mat.n || m != mat.m)
417 return false;
418
419 const size_t N = n * m;
420 for (size_t i = 0; i < N; ++i)
421 if (read_array_entry(i) != mat.read_array_entry(i))
422 return false;
423
424 return true;
425 }
426
432 [[nodiscard]] bool operator != (const DynMatrix<T> & mat) const
433 {
434 return !(*this == mat);
435 }
436
441 [[nodiscard]] constexpr size_t rows() const noexcept { return n; }
442
447 [[nodiscard]] constexpr size_t cols() const noexcept { return m; }
448
453 [[nodiscard]] constexpr size_t size() const noexcept { return n * m; }
454
459 [[nodiscard]] constexpr bool is_square() const noexcept { return n == m; }
460
465 [[nodiscard]] constexpr bool is_empty() const noexcept { return n == 0 || m == 0; }
466
480 [[nodiscard]] const T & read(const size_t i, const size_t j) const
481 {
482 check_indices(i, j);
483 return read_array_entry(get_index(i, j));
484 }
485
494 [[nodiscard]] const T & read_ne(const size_t i, const size_t j) const noexcept
495 {
496 return read_array_entry(get_index(i, j));
497 }
498
514 T & write(const size_t i, const size_t j, const T & data)
515 {
516 check_indices(i, j);
517 return write_array_entry(get_index(i, j), data);
518 }
519
521 T & write(const size_t i, const size_t j, T && data)
522 {
523 check_indices(i, j);
524 return write_array_entry(get_index(i, j), std::move(data));
525 }
526
542 [[nodiscard]] T & access(const size_t i, const size_t j)
543 {
544 return array_ptr->access(get_index(i, j));
545 }
546
553 [[nodiscard]] const T & access(const size_t i, const size_t j) const
554 {
555 return array_ptr->access(get_index(i, j));
556 }
557
566 [[nodiscard]] T & operator () (const size_t i, const size_t j)
567 {
568 return access(i, j);
569 }
570
577 [[nodiscard]] const T & operator () (const size_t i, const size_t j) const
578 {
579 return access(i, j);
580 }
581
590 void fill(const T & value)
591 {
592 allocate();
593 for (size_t idx = 0; idx < n * m; ++idx)
594 array_ptr->access(idx) = value;
595 }
596
605 [[nodiscard]] DynMatrix<T> transpose() const
606 {
607 DynMatrix<T> result(m, n, default_value);
608 for (size_t i = 0; i < n; ++i)
609 for (size_t j = 0; j < m; ++j)
610 {
611 const T & val = read_array_entry(get_index(i, j));
612 if (&val != &default_value) // Only copy non-default entries
613 result.write(j, i, val);
614 }
615 return result;
616 }
617
627 {
628 const DynMatrix * mat_ptr = nullptr;
629 size_t curr_idx = 0;
630
631 public:
632
635
640 Iterator(const DynMatrix<T> & mat) noexcept
641 : mat_ptr(&mat), curr_idx(0) {}
642
647 [[nodiscard]] bool has_curr() const noexcept
648 {
649 return curr_idx < mat_ptr->size();
650 }
651
656 [[nodiscard]] const T & get_curr_ne() const noexcept
657 {
659 }
660
666 [[nodiscard]] const T & get_curr() const
667 {
669 << "DynMatrix::Iterator: no current element";
670 return get_curr_ne();
671 }
672
675 void next_ne() noexcept { ++curr_idx; }
676
681 void next()
682 {
684 << "DynMatrix::Iterator: no more elements";
685 next_ne();
686 }
687
692 [[nodiscard]] size_t get_row() const noexcept
693 {
694 return curr_idx / mat_ptr->m;
695 }
696
701 [[nodiscard]] size_t get_col() const noexcept
702 {
703 return curr_idx % mat_ptr->m;
704 }
705
708 void reset() noexcept { curr_idx = 0; }
709 };
710
715 [[nodiscard]] Iterator get_it() const { return Iterator(*this); }
716
726 template <class Operation>
728 bool traverse(Operation & operation) const
729 {
730 for (size_t i = 0; i < n * m; ++i)
731 if (!operation(read_array_entry(i)))
732 return false;
733 return true;
734 }
735
737 template <class Operation>
739 bool traverse(Operation & operation)
740 {
741 return const_cast<const DynMatrix*>(this)->traverse(operation);
742 }
743
745 template <class Operation>
747 bool traverse(Operation && operation) const
748 {
749 return traverse<Operation>(operation);
750 }
751
753 template <class Operation>
755 bool traverse(Operation && operation)
756 {
757 return traverse<Operation>(operation);
758 }
759
775 template <class Operation>
776 bool traverse_allocated(Operation && operation)
777 {
778 if (array_ptr == nullptr)
779 return true;
780
781 const size_t total = n * m;
782 for (size_t idx = 0; idx < total; ++idx)
783 if (T *entry = array_ptr->test(idx))
784 {
785 if (!operation(*entry))
786 return false;
787 }
788
789 return true;
790 }
791
793 template <class Operation>
794 bool traverse_allocated(Operation && operation) const
795 {
796 if (array_ptr == nullptr)
797 return true;
798
799 const size_t total = n * m;
800 for (size_t idx = 0; idx < total; ++idx)
801 if (const T *entry = array_ptr->test(idx))
802 {
803 if (!operation(*entry))
804 return false;
805 }
806
807 return true;
808 }
809};
810
811} // end namespace Aleph
812
813#endif // TPL_DYNMAT_H
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_unless(C)
Throws std::overflow_error if condition does NOT hold.
Definition ah-errors.H:627
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
DRY (Don't Repeat Yourself) utilities and macros.
size_t size_t int32_t value
Definition ca-c-api.h:116
static void compute_sizes(const size_t n, size_t &d, size_t &s, size_t &b)
Given a dimension n, it proposes values for the directory, segment and block sizes.
Iterator for traversing matrix elements.
Definition tpl_dynMat.H:627
Iterator(const DynMatrix< T > &mat) noexcept
Construct an iterator for a matrix.
Definition tpl_dynMat.H:640
size_t get_row() const noexcept
Get the current row index.
Definition tpl_dynMat.H:692
const T & get_curr() const
Get the current element.
Definition tpl_dynMat.H:666
const T & get_curr_ne() const noexcept
Get the current element without bounds checking.
Definition tpl_dynMat.H:656
const DynMatrix * mat_ptr
Definition tpl_dynMat.H:628
void next_ne() noexcept
Advance to the next element without bounds checking.
Definition tpl_dynMat.H:675
bool has_curr() const noexcept
Check if iterator has a current element.
Definition tpl_dynMat.H:647
size_t get_col() const noexcept
Get the current column index.
Definition tpl_dynMat.H:701
void next()
Advance to the next element.
Definition tpl_dynMat.H:681
void reset() noexcept
Reset iterator to the beginning.
Definition tpl_dynMat.H:708
Dynamic matrix with sparse storage.
Definition tpl_dynMat.H:122
bool traverse(Operation &operation) const
Traverse all elements in row-major order.
Definition tpl_dynMat.H:728
size_t n
Number of rows.
Definition tpl_dynMat.H:123
T & write(const size_t i, const size_t j, T &&data)
See write(const size_t i, const size_t j, const T & data).
Definition tpl_dynMat.H:521
void set_dimension(const size_t num_rows, const size_t num_cols)
Change the matrix dimensions.
Definition tpl_dynMat.H:243
T & write(const size_t i, const size_t j, const T &data)
Write a value to position (i, j).
Definition tpl_dynMat.H:514
bool operator==(const DynMatrix< T > &mat) const
Equality comparison.
Definition tpl_dynMat.H:414
bool traverse(Operation &&operation) const
See traverse(Operation & operation).
Definition tpl_dynMat.H:747
T & write_array_entry(const size_t i, const T &data)
Definition tpl_dynMat.H:136
DynArray< T > * array_ptr
Definition tpl_dynMat.H:126
T & operator()(const size_t i, const size_t j)
Subscript operator for allocated entries.
Definition tpl_dynMat.H:566
bool traverse(Operation &&operation)
See traverse(Operation & operation).
Definition tpl_dynMat.H:755
bool operator!=(const DynMatrix< T > &mat) const
Inequality comparison.
Definition tpl_dynMat.H:432
const T & read(const size_t i, const size_t j) const
Read the entry at position (i, j).
Definition tpl_dynMat.H:480
DynMatrix() noexcept(std::is_nothrow_default_constructible_v< T >)=default
Default constructor - creates an invalid matrix.
constexpr bool is_empty() const noexcept
Check if the matrix is empty (uninitialized).
Definition tpl_dynMat.H:465
constexpr size_t cols() const noexcept
Get the number of columns.
Definition tpl_dynMat.H:447
bool traverse_allocated(Operation &&operation)
Traverse entries in allocated memory blocks.
Definition tpl_dynMat.H:776
Iterator get_it() const
Get an iterator positioned at the first element.
Definition tpl_dynMat.H:715
DynMatrix(const DynMatrix< T > &mat)
Copy constructor.
Definition tpl_dynMat.H:324
void check_indices(const size_t i, const size_t j) const
Definition tpl_dynMat.H:154
void allocate()
Pre-allocate memory for the entire matrix.
Definition tpl_dynMat.H:271
DynMatrix(DynMatrix< T > &&mat) noexcept(std::is_nothrow_default_constructible_v< T > and std::is_nothrow_swappable_v< T >)
Move constructor.
Definition tpl_dynMat.H:347
bool traverse_allocated(Operation &&operation) const
See traverse_allocated(Operation && operation).
Definition tpl_dynMat.H:794
const T & get_default_value() const noexcept
Get the default value for unwritten entries.
Definition tpl_dynMat.H:194
bool traverse(Operation &operation)
Definition tpl_dynMat.H:739
constexpr size_t rows() const noexcept
Get the number of rows.
Definition tpl_dynMat.H:441
void swap(DynMatrix &mat) noexcept(std::is_nothrow_swappable_v< T >)
Swap the contents of this matrix with another.
Definition tpl_dynMat.H:212
void set_default_initial_value(const T &value)
Set the default value for unwritten entries.
Definition tpl_dynMat.H:183
const T & read_ne(const size_t i, const size_t j) const noexcept
Read entry without bounds checking.
Definition tpl_dynMat.H:494
constexpr size_t size() const noexcept
Get the total number of entries.
Definition tpl_dynMat.H:453
T Key_Type
Alias for Item_Type, required by functional mixins.
Definition tpl_dynMat.H:171
constexpr bool is_square() const noexcept
Check if the matrix is square.
Definition tpl_dynMat.H:459
T & access(const size_t i, const size_t j)
Direct access to an allocated entry.
Definition tpl_dynMat.H:542
DynMatrix(const size_t num_rows, const size_t num_cols, const T &zero=T())
Construct a matrix with specified dimensions.
Definition tpl_dynMat.H:288
DynMatrix< T > transpose() const
Create a transposed copy of the matrix.
Definition tpl_dynMat.H:605
DynMatrix< T > & operator=(const DynMatrix< T > &mat)
Copy assignment operator.
Definition tpl_dynMat.H:364
T Item_Type
The type of elements stored in the matrix.
Definition tpl_dynMat.H:168
void fill(const T &value)
Fill all entries with a value.
Definition tpl_dynMat.H:590
const T & read_array_entry(const size_t i) const noexcept
Definition tpl_dynMat.H:130
size_t m
Number of columns.
Definition tpl_dynMat.H:124
T & write_array_entry(const size_t i, T &&data)
Definition tpl_dynMat.H:142
constexpr size_t get_index(const size_t i, const size_t j) const noexcept
Definition tpl_dynMat.H:148
const T & access(const size_t i, const size_t j) const
Direct const access to an allocated entry.
Definition tpl_dynMat.H:553
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
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
Common sequential searching methods on containers.
Definition ah-dry.H:555
f(args...) is a valid call whose result is usable as a condition.
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
STL namespace.
Lazy and scalable dynamic array implementation.