IVSparse  v1.0
A sparse matrix compression library.
CSC_Vector_Methods.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse
12 {
13 
14  //* Constructors and Destructor *//
15 
16  // Destructor
17  template <typename T, typename indexT, bool columnMajor>
19  {
20  if (vals != nullptr)
21  {
22  free(vals);
23  }
24  if (innerIdx != nullptr)
25  {
26  free(innerIdx);
27  }
28  }
29 
30  // IVSparse Matrix Constructor
31  template <typename T, typename indexT, bool columnMajor>
33  {
34 
35 #ifdef CSF_DEBUG
36  // make sure the vector is in bounds
37  assert((vec >= 0 && vec < mat.outerSize()) && "Vector index out of bounds");
38 
39  // make sure the matrix is not empty
40  assert((mat.outerSize() > 0 && mat.innerSize() > 0) && "Matrix is empty");
41 #endif
42 
43  length = mat.innerSize();
44 
45  // if the vector is empty, return
46  if (mat.byteSize() == 0)
47  {
48  vals = nullptr;
49  innerIdx = nullptr;
50  return;
51  }
52 
53  nnz = mat.outerPtr[vec + 1] - mat.outerPtr[vec];
54 
55  try
56  {
57  vals = (T *)malloc(nnz * sizeof(T));
58  innerIdx = (indexT *)malloc(nnz * sizeof(indexT));
59  }
60  catch (std::bad_alloc &e)
61  {
62  std::cerr << "Allocation failed: " << e.what() << '\n';
63  }
64 
65  memcpy(vals, mat.vals + mat.outerPtr[vec], nnz * sizeof(T));
66  memcpy(innerIdx, mat.innerIdx + mat.outerPtr[vec], nnz * sizeof(indexT));
67 
68  calculateCompSize();
69 
70 #ifdef CSF_DEBUG
71  userChecks();
72 #endif
73  }
74 
75  // IVSparse Vector Constructor
76  template <typename T, typename indexT, bool columnMajor>
78  {
79  length = vec.length;
80  nnz = vec.nnz;
81  size = vec.size;
82 
83  try
84  {
85  vals = (T *)malloc(nnz * sizeof(T));
86  innerIdx = (indexT *)malloc(nnz * sizeof(indexT));
87  }
88  catch (std::bad_alloc &e)
89  {
90  std::cerr << "Allocation failed: " << e.what() << '\n';
91  }
92 
93  memcpy(vals, vec.vals, nnz * sizeof(T));
94  memcpy(innerIdx, vec.innerIdx, nnz * sizeof(indexT));
95 
96 #ifdef CSF_DEBUG
97  userChecks();
98 #endif
99  }
100 
101  //* Private Class Methods *//
102 
103  // User Checks
104  template <typename T, typename indexT, bool columnMajor>
106  {
107  assert(std::is_floating_point<indexT>::value == false && "The index type must be a non-floating point type");
108  assert((std::is_arithmetic<T>::value && std::is_arithmetic<indexT>::value) && "The value and index types must be numeric types");
109  assert((std::is_same<indexT, bool>::value == false) && "The index type must not be bool");
110  }
111 
112  // Calculate the size of the vector
113  template <typename T, typename indexT, bool columnMajor>
115  {
116  size = 0;
117 
118  // calculate the size of the vector
119  size += sizeof(T) * nnz;
120  size += sizeof(indexT) * nnz;
121  }
122 
123  //* Getters *//
124 
125  // Get the inner dimension of the vector
126  template <typename T, typename indexT, bool columnMajor>
128 
129  // Get the outer dimension of the vector
130  template <typename T, typename indexT, bool columnMajor>
132 
133  // Get the number of nonzeros in the vector
134  template <typename T, typename indexT, bool columnMajor>
136 
137  // Get the size of the vector in bytes
138  template <typename T, typename indexT, bool columnMajor>
140 
141  // Get a value from the vector at index
142  template <typename T, typename indexT, bool columnMajor>
143  T SparseMatrix<T, indexT, 1, columnMajor>::Vector::coeff(uint32_t index) { return (*this)[index]; }
144 
145  // Get the length of the vector
146  template <typename T, typename indexT, bool columnMajor>
148 
149  // get values
150  template <typename T, typename indexT, bool columnMajor>
152 
153  // get inner indices
154  template <typename T, typename indexT, bool columnMajor>
156 
157  //* Utility Methods *//
158 
159  // print
160  template <typename T, typename indexT, bool columnMajor>
162  {
163  std::cout << "Vector: " << std::endl;
164  std::cout << std::endl;
165 
166  // print the denese vector up to 100 elements
167  for (uint32_t i = 0; i < std::min(length, (uint32_t)100); i++)
168  {
169  std::cout << (*this)[i] << " ";
170  }
171 
172  std::cout << std::endl;
173  }
174 
175  //* Operator Overloads *//
176 
177  // Assignment Operator
178  template <typename T, typename indexT, bool columnMajor>
180  {
181 
182  // check if the vector is the same
183  if (this == &vec)
184  {
185  return *this;
186  }
187 
188  if (vals != nullptr)
189  {
190  delete[] vals;
191  }
192  if (innerIdx != nullptr)
193  {
194  delete[] innerIdx;
195  }
196 
197  length = vec.length;
198  nnz = vec.nnz;
199  size = vec.size;
200 
201  // if the vector is empty, return
202  if (size == 0)
203  {
204  vals = nullptr;
205  innerIdx = nullptr;
206  return *this;
207  }
208 
209  try
210  {
211  vals = (T *)malloc(nnz * sizeof(T));
212  innerIdx = (indexT *)malloc(nnz * sizeof(indexT));
213  }
214  catch (std::bad_alloc &e)
215  {
216  std::cerr << "Allocation failed: " << e.what() << '\n';
217  }
218 
219  memcpy(vals, vec.vals, nnz * sizeof(T));
220  memcpy(innerIdx, vec.innerIdx, nnz * sizeof(indexT));
221 
222 #ifdef CSF_DEBUG
223  userChecks();
224 #endif
225 
226  return *this;
227  }
228 
229  // Equality Operator
230  template <typename T, typename indexT, bool columnMajor>
231  bool SparseMatrix<T, indexT, 1, columnMajor>::Vector::operator==(typename SparseMatrix<T, indexT, 1, columnMajor>::Vector &vec)
232  {
233  // check if the vector dimensions are the same
234  if (length != vec.length || nnz != vec.nnz)
235  {
236  return false;
237  }
238 
239  // check if the values and indices are the same
240  for (uint32_t i = 0; i < nnz; i++)
241  {
242  if (vals[i] != vec.vals[i] || innerIdx[i] != vec.innerIdx[i])
243  {
244  return false;
245  }
246  }
247 
248  // if all the values and indices are the same, return true
249  return true;
250  }
251 
252  // Inequality Operator
253  template <typename T, typename indexT, bool columnMajor>
254  bool SparseMatrix<T, indexT, 1, columnMajor>::Vector::operator!=(typename SparseMatrix<T, indexT, 1, columnMajor>::Vector &vec)
255  {
256  return !(*this == vec);
257  }
258 
259  // Bracket Operator
260  template <typename T, typename indexT, bool columnMajor>
261  T SparseMatrix<T, indexT, 1, columnMajor>::Vector::operator[](uint32_t index)
262  {
263 #ifdef CSF_DEBUG
264  assert((index >= 0 && index < length) && "Index out of bounds");
265 #endif
266 
267  for (uint32_t i = 0; i < nnz; i++)
268  {
269  if (innerIdx[i] == index)
270  {
271  return vals[i];
272  }
273  }
274 
275  return 0;
276  }
277 
278 } // namespace IVSparse
Definition: IVCSC_Vector.hpp:27
uint32_t getLength()
Definition: IVCSC_Vector_Methods.hpp:183
T coeff(uint32_t index)
Definition: IVCSC_Vector_Methods.hpp:179
void print()
Definition: IVCSC_Vector_Methods.hpp:189
uint32_t innerSize()
Definition: IVCSC_Vector_Methods.hpp:155
size_t byteSize()
Definition: IVCSC_Vector_Methods.hpp:175
Vector()
Definition: IVCSC_Vector.hpp:61
uint32_t nonZeros()
Definition: IVCSC_Vector_Methods.hpp:163
uint32_t outerSize()
Definition: IVCSC_Vector_Methods.hpp:159
~Vector()
Definition: IVCSC_Vector_Methods.hpp:18
Definition: CSC_SparseMatrix.hpp:24
uint32_t innerSize() const
Definition: IVSparse_Base_Methods.hpp:28
size_t byteSize() const
Definition: IVSparse_Base_Methods.hpp:37
uint32_t outerSize() const
Definition: IVSparse_Base_Methods.hpp:31
Definition: IVCSC_SparseMatrix.hpp:27