11#include <kernel/lafem/sparse_vector.hpp>
12#include <kernel/lafem/sparse_vector_blocked.hpp>
33 template<
typename DT_,
typename IT_,
int block_size_ = 1>
37 typedef DT_ ScalarDataType;
57 explicit SparseVectorFactory(
const SparseVector<DT_, IT_>& vec) :
63 explicit SparseVectorFactory(
const SparseVectorBlocked<DT_, IT_, block_size_>& vec) :
92 auto ib =
_entries.emplace(IT_(i), v_i);
94 ib.first->second = v_i;
99 XASSERTM(vec.
size() == this->size(),
"invalid vector size");
103 for(
Index i = 0; i < nze; ++i)
105 auto ib =
_entries.emplace(v_idx[i], BlockedValueType(v_val[i]));
107 ib.first->second = BlockedValueType(v_val[i]);
111 void add(
const SparseVectorBlocked<DT_, IT_, block_size_>& vec)
113 XASSERTM(vec.size() == this->size(),
"invalid vector size");
114 const Memory::TypedView<IT_> v_idx = vec.indices_view_r();
115 const Memory::TypedView<Tiny::Vector<DT_, block_size_>> v_val = vec.elements_view_r();
116 Index nze = vec.num_nzes();
117 for(
Index i = 0; i < nze; ++i)
119 auto ib =
_entries.emplace(v_idx[i], v_val[i]);
121 ib.first->second = v_val[i];
144 for(
Index i = 0; it != it_end; ++i, ++it)
146 v_idx[i] = it->first;
147 v_val[i] = it->second[0];
152 SparseVectorBlocked<DT_, IT_, block_size_> make_svb()
const
154 SparseVectorBlocked<DT_, IT_, block_size_> vec(this->
size(), this->
num_nzes());
155 Memory::TypedView<IT_> v_idx = vec.indices_view_w();
156 Memory::TypedView<Tiny::Vector<DT_, block_size_>> v_val = vec.elements_view_w();
159 for(
Index i = 0; it != it_end; ++i, ++it)
161 v_idx[i] = it->first;
162 v_val[i] = it->second;
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Factory for SparseVector and SparseVectorBlocked construction.
std::map< IT_, BlockedValueType > _entries
entry map
void add(Index i, BlockedValueType v_i)
Adds a new vector entry to the factory.
Index size() const
Returns the total vector size.
Index _size
Total size of vector.
void add(Index i, DT_ v_i)
Adds a new vector entry to the factory.
Index num_nzes() const
Returns the number of nonzero entries.
Sparse vector class template.
Index num_nzes() const
Returns the number of non-zero elements in the sparse vector.
Memory::TypedView< DT_ > elements_view_r(Memory::Location loc=Memory::Location::main) const
Creates a read-only memory view for the elements array for a given memory location.
Index size() const
Returns the native size of the sparse vector.
Memory::TypedView< IT_ > indices_view_r(Memory::Location loc=Memory::Location::main) const
Creates a read-only memory view for the indices array for a given memory location.
Tiny Vector class template.
std::uint64_t Index
Index data type.