24#ifndef VULPES_DATASTRUCTURE_CRS_MAP_HPP_
25#define VULPES_DATASTRUCTURE_CRS_MAP_HPP_
44template <
class T,
bool Static, T Default = T{}>
50template <
class T, T Default>
53 static constexpr T
value = Default;
71 IndexType RowSize = IndexType(-1)>
81 template <
bool Mutable>
86 typename std::conditional_t<Mutable, DataType&, const DataType&>;
89 typename std::conditional_t<Mutable, DataType*, const DataType*>;
147 constexpr IndexType
size()
const {
148 if constexpr (RowSize == IndexType(-1)) {
172 explicit CrsMap(
const std::vector<std::vector<DataType>>&
data) {
175 for (
size_t i = 0; i <
data.size(); ++i) {
177 for (
const auto& e :
data[i]) {
190 const std::vector<DataType>&
data,
const std::vector<IndexType>&
indices)
193 template <
class Archive>
223 std::vector<DataType>&&
data, std::vector<IndexType>&&
indices)
236 if constexpr (RowSize == IndexType(-1)) {
237 assert(row_idx < (IndexType)
m_indices.size() - 1);
257 if constexpr (RowSize == IndexType(-1)) {
258 assert(row_idx < (IndexType)
m_indices.size() - 1);
298 const DataType&
operator()(IndexType row_idx, IndexType col_idx)
const {
299 if constexpr (RowSize == IndexType(-1)) {
302 return m_data[row_idx * RowSize + col_idx];
319 if constexpr (RowSize == IndexType(-1)) {
322 return m_data[row_idx * RowSize + col_idx];
332 if constexpr (RowSize == IndexType(-1)) {
335 return m_data.size() / RowSize;
346 auto slice(
const std::vector<IndexType>& subindices)
const {
347 std::decay_t<
decltype(*this)> slice_map({}, {});
348 if constexpr (RowSize == IndexType(-1)) {
349 slice_map.m_indices.push_back(0);
350 for (std::size_t i = 0; i < subindices.size(); ++i) {
351 const auto idx = subindices[i];
352 assert(idx + 1 <
static_cast<IndexType
>(
m_indices.size()));
354 slice_map.m_indices.push_back(row_size + slice_map.m_indices.back());
355 for (IndexType j = 0; j < row_size; ++j) {
360 slice_map.m_data.resize(RowSize * subindices.size());
361 for (std::size_t i = 0; i < subindices.size(); ++i) {
362 const auto idx = subindices[i];
363 for (IndexType j = 0; j < RowSize; ++j) {
364 slice_map.m_data[i * RowSize + j] =
m_data[idx * RowSize + j];
380 const std::vector<IndexType>& perm,
381 const std::vector<IndexType>& perm_inv)
const {
382 assert(perm.size() == perm_inv.size());
383 assert(perm.size() ==
static_cast<std::size_t
>(
size()));
384 std::decay_t<
decltype(*this)> slice_map({}, {});
385 if constexpr (RowSize == IndexType(-1)) {
386 slice_map.m_indices.push_back(0);
387 for (std::size_t i = 0; i < perm_inv.size(); ++i) {
388 const auto idx = perm_inv[i];
389 assert(
static_cast<std::size_t
>(idx + 1) <
m_indices.size());
391 slice_map.m_indices.push_back(row_size + slice_map.m_indices.back());
392 for (IndexType j = 0; j < row_size; ++j) {
397 static_assert(RowSize * 0 &&
"NotImplemented");
408 void remove(
const DataType& value_to_remove) {
413 size_t row_start = 0;
414 for (
size_t row = 0; row <
m_indices.size() - 1; ++row) {
416 for (
size_t i = row_start; i < row_end; ++i) {
417 if (
m_data[i] != value_to_remove) {
429 for (
const auto& e : row) {
483template <
typename DataType,
typename IndexType, IndexType RowSize>
486 os <<
"CrsMap with " << map.
size() <<
" rows:\n";
487 for (IndexType row_idx = 0; row_idx < map.
size(); ++row_idx) {
488 os <<
"Row " << row_idx <<
": ";
490 for (
const auto& elem : map[row_idx]) {
509template <
typename T, std::
size_t N>
526 if (std::holds_alternative<StaticStorage>(
m_storage)) {
527 return std::get<StaticStorage>(
m_storage).data();
529 return std::get<DynamicStorage>(
m_storage).data();
542 if (std::holds_alternative<StaticStorage>(
m_storage)) {
559 if (std::holds_alternative<StaticStorage>(
m_storage)) {
560 return std::get<StaticStorage>(
m_storage).data();
562 return std::get<DynamicStorage>(
m_storage).data();
576 if (std::holds_alternative<StaticStorage>(
m_storage)) {
593 init_list.begin(), init_list.end(),
594 std::get<StaticStorage>(
m_storage).begin());
605 std::make_reverse_iterator(
begin), std::make_reverse_iterator(
end),
609 std::make_reverse_iterator(
begin), std::make_reverse_iterator(
end));
631 if (std::holds_alternative<StaticStorage>(
m_storage)) {
633 std::vector<T> dynamic_storage;
634 dynamic_storage.reserve(
m_size + 1);
635 for (std::size_t i = 0; i <
m_size; ++i) {
636 dynamic_storage.push_back(std::get<StaticStorage>(
m_storage)[i]);
640 std::get<DynamicStorage>(
m_storage).push_back(value);
655 template <
typename... Args>
661 if (std::holds_alternative<StaticStorage>(
m_storage)) {
663 std::vector<T> dynamic_storage;
664 dynamic_storage.reserve(
m_size + 1);
665 for (std::size_t i = 0; i <
m_size; ++i) {
666 dynamic_storage.push_back(std::get<StaticStorage>(
m_storage)[i]);
670 std::get<DynamicStorage>(
m_storage).emplace_back(
671 std::forward<Args>(args)...);
683 template <
class InputIt>
684 void assign(InputIt first, InputIt last) {
685 m_size = std::distance(first, last);
687 m_storage.template emplace<StaticStorage>();
688 std::copy(first, last, std::get<StaticStorage>(
m_storage).
begin());
690 m_storage.template emplace<DynamicStorage>(first, last);
702 if (std::holds_alternative<StaticStorage>(
m_storage)) {
703 return std::get<StaticStorage>(
m_storage)[index];
705 return std::get<DynamicStorage>(
m_storage)[index];
716 if (std::holds_alternative<StaticStorage>(
m_storage)) {
717 return std::get<StaticStorage>(
m_storage)[index];
719 return std::get<DynamicStorage>(
m_storage)[index];
757template <
typename DataType, std::
size_t N = 16>
807 constexpr void resize(std::size_t new_size) {
m_data.resize(new_size); }
838 m_data.push_back(std::move(value));
841 template <
class... Args>
843 m_data.emplace_back(std::forward<Args>(args)...);
858 if (row_idx >=
static_cast<std::size_t
>(
m_data.size())) {
859 m_data.resize(row_idx + 1);
875 template <
typename IndexType>
877 std::vector<DataType> data;
878 std::vector<IndexType> indices = {0};
879 for (
size_t i = 0; i <
m_data.size(); ++i) {
880 indices.push_back(indices.back() +
m_data[i].size());
881 for (
const auto& e :
m_data[i]) {
885 return CrsMap(std::move(data), std::move(indices));
903 std::vector<SmallVector<DataType, N>>
m_data;
906template <
typename DataType>
934template <
typename DataType>
937 if (static_size <= 4) {
939 }
else if (static_size <= 8) {
941 }
else if (static_size <= 16) {
943 }
else if (static_size <= 32) {
945 }
else if (static_size <= 64) {
947 }
else if (static_size <= 128) {
949 }
else if (static_size <= 256) {
951 }
else if (static_size <= 512) {
953 }
else if (static_size <= 1024) {
955 }
else if (static_size <= 2048) {
957 }
else if (static_size <= 4096) {
959 }
else if (static_size <= 8192) {
990template <
typename DataType, std::
size_t N>
993 os <<
"CrsMapUncompressed with " << map.
size() <<
" rows:\n";
994 for (std::size_t row_idx = 0; row_idx < map.
size(); ++row_idx) {
995 os <<
"Row " << row_idx <<
": ";
997 for (
const auto& elem : map[row_idx]) {
A class that behaves like a vector of vectors, providing automatic row range checking.
Definition crs_map.hpp:758
constexpr const auto & back() const
Returns a constant reference to the last row of the map.
Definition crs_map.hpp:795
std::vector< SmallVector< IndexType, 16 > > m_data
Definition crs_map.hpp:903
constexpr void emplace_back(Args &&... args)
Definition crs_map.hpp:842
CrsMapUncompressed(std::initializer_list< SmallVector< DataType, N > > init_list)
Constructs a CrsMapUncompressed object with an initializer list of SmallVector objects.
Definition crs_map.hpp:768
constexpr void push_back(SmallVector< DataType, N > &&value)
Appends an rvalue SmallVector to the map.
Definition crs_map.hpp:837
CrsMap< DataType, IndexType > compress() const
Compresses the uncompressed CrsMap into a compressed format.
Definition crs_map.hpp:876
constexpr const SmallVector< DataType, N > & operator[](std::size_t idx) const
Accesses a SmallVector at the given index.
Definition crs_map.hpp:815
constexpr auto & back()
Returns a reference to the last row of the map.
Definition crs_map.hpp:800
constexpr void resize(std::size_t new_size)
Resizes the map to contain the specified number of SmallVector objects.
Definition crs_map.hpp:807
constexpr auto begin()
Returns an iterator to the beginning of the map.
Definition crs_map.hpp:775
CrsMapUncompressed()=default
constexpr SmallVector< DataType, N > & operator[](std::size_t idx)
Accesses a SmallVector at the given index.
Definition crs_map.hpp:823
constexpr auto begin() const
Returns a constant iterator to the beginning of the map.
Definition crs_map.hpp:785
constexpr auto end() const
Returns a constant iterator to the end of the map.
Definition crs_map.hpp:790
std::size_t size() const
Returns the number of rows in the matrix.
Definition crs_map.hpp:893
constexpr void push_back(const SmallVector< DataType, N > &value)
Appends a copy of a SmallVector to the map.
Definition crs_map.hpp:830
constexpr auto end()
Returns an iterator to the end of the map.
Definition crs_map.hpp:780
SmallVector< DataType, N > & getRow(std::size_t row_idx)
Access a row of the matrix as a mutable reference.
Definition crs_map.hpp:857
const_iterator begin() const
Get the beginning iterator of the row (const version).
Definition crs_map.hpp:109
typename std::conditional_t< Mutable, DataType &, const DataType & > reference_t
Alias for the reference type, depending on mutability.
Definition crs_map.hpp:85
const_iterator end() const
Get the ending iterator of the row (const version).
Definition crs_map.hpp:114
iterator begin()
Get the beginning iterator of the row.
Definition crs_map.hpp:99
typename std::conditional_t< Mutable, DataType *, const DataType * > pointer_t
Alias for the pointer type, depending on mutability.
Definition crs_map.hpp:88
pointer_t const m_data
Definition crs_map.hpp:163
constexpr pointer_t const data() const
Definition crs_map.hpp:159
reference_t operator[](IndexType index)
Access an element in the row by index.
Definition crs_map.hpp:141
pointer_t iterator
Iterator type for mutable/non-mutable rows.
Definition crs_map.hpp:92
const reference_t operator[](IndexType index) const
Access an element in the row by index.
Definition crs_map.hpp:131
iterator end()
Get the ending iterator of the row.
Definition crs_map.hpp:104
internal::StaticSelector< IndexType, RowSize !=IndexType(-1), RowSize > m_size
Definition crs_map.hpp:166
const pointer_t const_iterator
Definition crs_map.hpp:93
RowView(pointer_t data, IndexType size)
Constructor to create a view over a row.
Definition crs_map.hpp:122
constexpr IndexType size() const
Get the size of the row.
Definition crs_map.hpp:147
Definition crs_map.hpp:72
auto & indices()
Accessor to the indices buffer.
Definition crs_map.hpp:450
CrsMap(const std::vector< DataType > &data, const std::vector< IndexType > &indices)
Construct a CrsMap with the provided data and index arrays.
Definition crs_map.hpp:189
CrsMap()=default
Default constructor.
CrsMap(const std::vector< std::vector< DataType > > &data)
Definition crs_map.hpp:172
void serialize(Archive &ar, const unsigned int)
Definition crs_map.hpp:194
IndexType size() const
Get the number of rows in the CRS map.
Definition crs_map.hpp:331
const auto & indices() const
Definition crs_map.hpp:442
MutableRowView getRow(IndexType row_idx)
Get a mutable view over a row.
Definition crs_map.hpp:235
friend bool operator==(const CrsMap &lhs, const CrsMap &rhs)
Definition crs_map.hpp:199
void remove(const DataType &value_to_remove)
Remove in-place all entries with a certain value.
Definition crs_map.hpp:408
DataType & operator()(IndexType row_idx, IndexType col_idx)
Access an element in the CRS map by row and column index (mutable version).
Definition crs_map.hpp:318
const DataType & operator()(IndexType row_idx, IndexType col_idx) const
Access an element in the CRS map by row and column index (constant version).
Definition crs_map.hpp:298
CrsMap(std::vector< DataType > &&data, std::vector< IndexType > &&indices)
Constructor that initializes a CrsMap by moving the provided data and indices.
Definition crs_map.hpp:222
std::vector< MonoTypeLifted > m_data
Definition crs_map.hpp:454
auto & data()
Accessor to the data buffer.
Definition crs_map.hpp:446
ConstRowView getRow(IndexType row_idx) const
Get a constant view over a row.
Definition crs_map.hpp:256
RowView< false > ConstRowView
Definition crs_map.hpp:169
auto slice(const std::vector< IndexType > &subindices) const
Get a CrsMap with the rows of the passed indexes.
Definition crs_map.hpp:346
ConstRowView operator[](IndexType row_idx) const
Access a row using the subscript operator (constant version).
Definition crs_map.hpp:277
MutableRowView operator[](IndexType row_idx)
Access a row using the subscript operator (mutable version).
Definition crs_map.hpp:284
auto permute(const std::vector< IndexType > &perm, const std::vector< IndexType > &perm_inv) const
Get a CrsMap obtained by applying a permutation to this instance.
Definition crs_map.hpp:379
void pushBack(const std::vector< DataType > &row)
Definition crs_map.hpp:427
RowView< true > MutableRowView
Definition crs_map.hpp:170
std::vector< index_t > m_indices
Definition crs_map.hpp:456
const auto & data() const
Definition crs_map.hpp:438
Definition crs_map.hpp:510
SmallVector()
Default constructor.
Definition crs_map.hpp:583
const T * const_iterator
Iterator for constant access.
Definition crs_map.hpp:513
void assign(InputIt first, InputIt last)
Assign the values in range [first, last) to the container.
Definition crs_map.hpp:684
std::variant< StaticStorage, DynamicStorage > m_storage
Variant storing either static or dynamic memory.
Definition crs_map.hpp:734
T * iterator
Iterator for mutable access.
Definition crs_map.hpp:512
constexpr std::size_t size() const
Get the number of elements in the vector.
Definition crs_map.hpp:727
const_iterator end() const
Returns a const iterator pointing to the end of the storage.
Definition crs_map.hpp:575
iterator begin()
Returns an iterator pointing to the beginning of the storage.
Definition crs_map.hpp:525
iterator end()
Returns an iterator pointing to the end of the storage.
Definition crs_map.hpp:541
void emplace_back(Args &&... args)
Constructs an element in-place at the end of the vector.
Definition crs_map.hpp:656
const_iterator begin() const
Returns a const iterator pointing to the beginning of the storage.
Definition crs_map.hpp:558
const T & operator[](std::size_t index) const
Access an element by index (constant).
Definition crs_map.hpp:714
std::vector< T > DynamicStorage
Dynamic storage for larger sizes.
Definition crs_map.hpp:731
std::size_t m_size
Number of elements in the vector.
Definition crs_map.hpp:735
SmallVector(const T *begin, const T *end)
Definition crs_map.hpp:600
SmallVector(std::initializer_list< T > init_list)
Constructor to initialize the vector with an initializer list.
Definition crs_map.hpp:590
std::array< T, N > StaticStorage
Static array for small sizes.
Definition crs_map.hpp:730
T & operator[](std::size_t index)
Access an element by index (mutable).
Definition crs_map.hpp:700
void push_back(const T &value)
Add an element to the end of the vector.
Definition crs_map.hpp:626
Definition crs_map.hpp:38
Definition crs_map.hpp:36
std::ostream & operator<<(std::ostream &os, const CrsMap< DataType, IndexType, RowSize > &map)
Overloaded stream insertion operator for CrsMap objects.
Definition crs_map.hpp:484
CrsMapUncompressedVariant< DataType > getUncompressedCrsMapWithStaticStorageGreaterThan(std::size_t static_size)
Function that builds an UncompressedCrsMap with at least a certain static storage,...
Definition crs_map.hpp:936
std::variant< CrsMapUncompressed< DataType, 4 >, CrsMapUncompressed< DataType, 8 >, CrsMapUncompressed< DataType, 16 >, CrsMapUncompressed< DataType, 32 >, CrsMapUncompressed< DataType, 64 >, CrsMapUncompressed< DataType, 128 >, CrsMapUncompressed< DataType, 256 >, CrsMapUncompressed< DataType, 512 >, CrsMapUncompressed< DataType, 1024 >, CrsMapUncompressed< DataType, 2048 >, CrsMapUncompressed< DataType, 4096 >, CrsMapUncompressed< DataType, 8192 > > CrsMapUncompressedVariant
Definition crs_map.hpp:907
Definition cell_manager.hpp:38
StaticSelector(T)
Definition crs_map.hpp:52
static constexpr T value
Definition crs_map.hpp:53
Definition crs_map.hpp:45
const T value
Definition crs_map.hpp:47
StaticSelector(T value_)
Definition crs_map.hpp:46