FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
sparse_vector_factory.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
11#include <kernel/lafem/sparse_vector.hpp>
12#include <kernel/lafem/sparse_vector_blocked.hpp>
13
14// includes, system
15#include <map>
16#include <iterator>
17
18namespace FEAT
19{
20 namespace LAFEM
21 {
33 template<typename DT_, typename IT_, int block_size_ = 1>
35 {
36 public:
37 typedef DT_ ScalarDataType;
39
40 private:
44 std::map<IT_, BlockedValueType> _entries;
45
46 public:
48 _size(0)
49 {
50 }
51
52 explicit SparseVectorFactory(Index size_) :
53 _size(size_)
54 {
55 }
56
57 explicit SparseVectorFactory(const SparseVector<DT_, IT_>& vec) :
58 _size(vec.size())
59 {
60 this->add(vec);
61 }
62
63 explicit SparseVectorFactory(const SparseVectorBlocked<DT_, IT_, block_size_>& vec) :
64 _size(vec.size())
65 {
66 this->add(vec);
67 }
68
75 void add(Index i, DT_ v_i)
76 {
77 XASSERTM(i < _size, "invalid sparse vector index");
78 auto ib = _entries.emplace(IT_(i), BlockedValueType(v_i));
79 if(!ib.second)
80 ib.first->second = BlockedValueType(v_i);
81 }
82
90 {
91 XASSERTM(i < _size, "invalid sparse vector index");
92 auto ib = _entries.emplace(IT_(i), v_i);
93 if(!ib.second)
94 ib.first->second = v_i;
95 }
96
97 void add(const SparseVector<DT_, IT_>& vec)
98 {
99 XASSERTM(vec.size() == this->size(), "invalid vector size");
100 const Memory::TypedView<IT_> v_idx = vec.indices_view_r();
101 const Memory::TypedView<DT_> v_val = vec.elements_view_r();
102 Index nze = vec.num_nzes();
103 for(Index i = 0; i < nze; ++i)
104 {
105 auto ib = _entries.emplace(v_idx[i], BlockedValueType(v_val[i]));
106 if(!ib.second)
107 ib.first->second = BlockedValueType(v_val[i]);
108 }
109 }
110
111 void add(const SparseVectorBlocked<DT_, IT_, block_size_>& vec)
112 {
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)
118 {
119 auto ib = _entries.emplace(v_idx[i], v_val[i]);
120 if(!ib.second)
121 ib.first->second = v_val[i];
122 }
123 }
124
126 Index size() const
127 {
128 return _size;
129 }
130
133 {
134 return Index(_entries.size());
135 }
136
137 SparseVector<DT_, IT_> make_sv() const
138 {
139 SparseVector<DT_, IT_> vec(this->size(), this->num_nzes());
140 Memory::TypedView<IT_> v_idx = vec.indices_view_w();
141 Memory::TypedView<DT_> v_val = vec.elements_view_w();
142 auto it = _entries.begin();
143 auto it_end = _entries.end();
144 for(Index i = 0; it != it_end; ++i, ++it)
145 {
146 v_idx[i] = it->first;
147 v_val[i] = it->second[0];
148 }
149 return vec;
150 }
151
152 SparseVectorBlocked<DT_, IT_, block_size_> make_svb() const
153 {
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();
157 auto it = _entries.begin();
158 auto it_end = _entries.end();
159 for(Index i = 0; it != it_end; ++i, ++it)
160 {
161 v_idx[i] = it->first;
162 v_val[i] = it->second;
163 }
164 return vec;
165 }
166 }; // class SparseVectorFactory
167 } // namespace LAFEM
168} // namespace FEAT
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
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.
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.
Typed memory view class.
Tiny Vector class template.
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.