SenK
SenK is a C++ library for high-performance linear solvers.
senk_class.hpp
Go to the documentation of this file.
1
7#ifndef SENK_CLASS_HPP
8#define SENK_CLASS_HPP
9
10#include <tuple>
11#include "senk_utils.hpp"
12
13#define SP_LEN 8
14
15namespace senk {
16
17namespace sparse {
23template <typename T1, typename T2>
24class SpVec {
25private:
27 T1 *val;
29 T2 *lev;
31 int *idx;
33 int len;
35 int mlen;
36public:
42 val = utils::SafeMalloc<T1>(SP_LEN);
43 lev = utils::SafeMalloc<T2>(SP_LEN);
44 idx = utils::SafeMalloc<int>(SP_LEN);
45 len = 0;
46 mlen = SP_LEN;
47 }
56 }
63 inline void Append(T1 t_val, T2 t_lev, int t_idx) {
64 if(len == mlen) {
65 mlen *= 2;
69 }
70 val[len] = t_val;
71 lev[len] = t_lev;
72 idx[len] = t_idx;
73 len++;
74 }
82 inline void Append(T1 *t_val, T2 *t_lev, int *t_idx, int t_len) {
83 if(len+t_len > mlen) {
84 while(len+t_len > mlen) mlen *= 2;
88 }
89 for(int i=0; i<t_len; i++) {
90 val[len+i] = t_val[i];
91 lev[len+i] = t_lev[i];
92 idx[len+i] = t_idx[i];
93 }
94 len += t_len;
95 }
99 inline int GetLen() { return len; }
103 inline T1 GetVal(int i) { return val[i]; }
107 inline T2 GetLev(int i) { return lev[i]; }
111 inline int GetIdx(int i) { return idx[i]; }
115 inline std::tuple<T1, T2, int> Get(int i) {
116 //return std::make_tuple(val[i], idx[i]);
117 return {val[i], lev[i], idx[i]};
118 }
122 inline void Clear() { len = 0; }
123};
124
125/*
126template <typename T>
127class SpVec {
128private:
129 T *val;
130 int *idx;
131 int len;
132 int mlen;
133public:
134 SpVec() {
135 val = utils::SafeMalloc<T>(SP_LEN);
136 idx = utils::SafeMalloc<int>(SP_LEN);
137 len = 0;
138 mlen = SP_LEN;
139 }
140 ~SpVec() {
141 utils::SafeFree(&val);
142 utils::SafeFree(&idx);
143 }
144 inline int GetLen() { return len; }
145 inline void Append(T t_val, int t_idx) {
146 if(len == mlen) {
147 mlen *= 2;
148 val = utils::SafeRealloc(val, mlen);
149 idx = utils::SafeRealloc(idx, mlen);
150 }
151 val[len] = t_val;
152 idx[len] = t_idx;
153 len++;
154 }
155 inline void Append(T *t_val, int *t_idx, int t_len) {
156 if(len+t_len > mlen) {
157 while(len+t_len > mlen) mlen *= 2;
158 val = utils::SafeRealloc(val, mlen);
159 idx = utils::SafeRealloc(idx, mlen);
160 }
161 for(int i=0; i<t_len; i++) {
162 val[len+i] = t_val[i];
163 idx[len+i] = t_idx[i];
164 }
165 len += t_len;
166 }
167 inline std::tuple<T, int> Get(int i) {
168 //return std::make_tuple(val[i], idx[i]);
169 return {val[i], idx[i]};
170 }
171 inline void Clear() { len = 0; }
172};
173*/
174
175} // namespace sparse
176
177} // namespace senk
178
179#endif
Sparse vector class.
Definition: senk_class.hpp:24
void Clear()
Set the value of len to 0.
Definition: senk_class.hpp:122
int * idx
An array that stores the indices of the nonzero elements.
Definition: senk_class.hpp:31
SpVec()
Constructor.
Definition: senk_class.hpp:41
std::tuple< T1, T2, int > Get(int i)
Return the tuple of i-th values of val, lev, and idx.
Definition: senk_class.hpp:115
int GetLen()
Return len.
Definition: senk_class.hpp:99
int len
The number of the nonzero elements.
Definition: senk_class.hpp:33
T1 * val
An array that stores the values of the nonzero elements.
Definition: senk_class.hpp:27
~SpVec()
Destructor.
Definition: senk_class.hpp:52
int GetIdx(int i)
Return i-th value of idx.
Definition: senk_class.hpp:111
int mlen
The size of allocated memories.
Definition: senk_class.hpp:35
void Append(T1 *t_val, T2 *t_lev, int *t_idx, int t_len)
Append argument values to the arrays.
Definition: senk_class.hpp:82
T2 GetLev(int i)
Return i-th value of lev.
Definition: senk_class.hpp:107
T1 GetVal(int i)
Return i-th value of val.
Definition: senk_class.hpp:103
T2 * lev
An array that stores the supplemental values of the nonzero elements.
Definition: senk_class.hpp:29
void Append(T1 t_val, T2 t_lev, int t_idx)
Append argument values to the arrays.
Definition: senk_class.hpp:63
void SafeFree(T **ptr)
Free allocated memory.
Definition: senk_utils.hpp:60
T * SafeRealloc(T *old, int size)
Reallocate memory.
Definition: senk_utils.hpp:48
The top-level namespace of SenK.
Utility functions are defined.