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