IVSparse  v1.0
A sparse matrix compression library.
VCSC_Iterator_Methods.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
13 //* Constructors *//
14 
15 // Matrix Constructor
16 template <typename T, typename indexT, bool columnMajor>
18 
19  #ifdef IVSPARSE_DEBUG
20  assert(vec < matrix.outerDim && vec >= 0 && "The vector index is out of bounds!");
21  #endif
22 
23  // check if the vector is empty
24  if (matrix.getNumUniqueVals(vec) == 0) {
25  this->vals = nullptr;
26  this->counts = nullptr;
27  this->indices = nullptr;
28  this->val = nullptr;
29  this->index = 0;
30  this->count = 0;
31 
32  countIndex = 0;
33  indexSize = 0;
34 
35  return;
36  }
37 
38  this->outer = vec;
39 
40  // set the pointers to the correct locations
41  this->vals = matrix.getValues(vec);
42  this->counts = matrix.getCounts(vec);
43  this->indices = matrix.getIndices(vec);
44 
45  this->valsSize = matrix.getNumUniqueVals(vec);
46  this->indexSize = matrix.getNumIndices(vec);
47 
48  // set the values of the iterator
49  this->val = vals;
50  this->index = indices[0];
51  this->count = counts[0];
52 }
53 
54 // Vector Constructor
55 template <typename T, typename indexT, bool columnMajor>
56 SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator::InnerIterator(SparseMatrix<T, indexT, 2, columnMajor>::Vector &vector) {
57  // check if the vector is empty
58  if (vector.nonZeros() == 0) {
59  this->vals = nullptr;
60  this->counts = nullptr;
61  this->indices = nullptr;
62  this->val = nullptr;
63  this->index = 0;
64  this->count = 0;
65 
66  countIndex = 0;
67  indexSize = 0;
68 
69  return;
70  }
71 
72  this->outer = 0;
73 
74  // set the pointers to the correct locations
75  this->vals = vector.getValues();
76  this->counts = vector.getCounts();
77  this->indices = vector.getIndices();
78 
79  this->valsSize = vector.uniqueVals();
80  this->indexSize = vector.nonZeros();
81 
82  // set the values of the iterator
83  this->val = vals;
84  this->index = indices[0];
85  this->count = counts[0];
86 }
87 
88 //* Getters *//
89 
90 // Get the outer dimension
91 template <typename T, typename indexT, bool columnMajor>
93  return outer;
94 }
95 
96 // Get the value
97 template <typename T, typename indexT, bool columnMajor>
99  return *val;
100 }
101 
102 // Get the index
103 template <typename T, typename indexT, bool columnMajor>
105  return index;
106 }
107 
108 // Get a pointer to the value
109 template <typename T, typename indexT, bool columnMajor>
111  T newValue) {
112  *val = newValue;
113 }
114 
115 // Get the current row
116 template <typename T, typename indexT, bool columnMajor>
118  if constexpr (!columnMajor) {
119  return outer;
120  } else {
121  return index;
122  }
123 }
124 
125 // Get the current column
126 template <typename T, typename indexT, bool columnMajor>
128  if constexpr (!columnMajor) {
129  return index;
130  } else {
131  return outer;
132  }
133 }
134 
135 //* Operator Overloads *//
136 
137 // Dereference Operator
138 template <typename T, typename indexT, bool columnMajor>
139 T &SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator::operator*() {
140  return *val;
141 }
142 
143 // Equality Operator
144 template <typename T, typename indexT, bool columnMajor>
145 bool SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator::operator==(const InnerIterator &other) {
146  return values == other.values;
147 }
148 
149 // Inequality Operator
150 template <typename T, typename indexT, bool columnMajor>
151 bool SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator::operator!=(const InnerIterator &other) {
152  return values != other.values;
153 }
154 
155 // Less Than Operator
156 template <typename T, typename indexT, bool columnMajor>
157 bool SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator::operator<(const InnerIterator &other) {
158  return values < other.values;
159 }
160 
161 // Greater Than Operator
162 template <typename T, typename indexT, bool columnMajor>
163 bool SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator::operator>(const InnerIterator &other) {
164  return values > other.values;
165 }
166 
167 // Increment Operator
168 template <typename T, typename indexT, bool columnMajor>
169 inline void SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator::operator++() {
170 
171  // decriment count
172  count--;
173 
174  // if count is 0 then we need to move to the next value
175  if (count == 0) {
176  // Check if we are at the end of the values array
177  countIndex++;
178 
179  if (countIndex >= indexSize) {
180  return;
181  }
182 
183  // Move to the next value
184  val++;
185 
186  // Get the new count
187  counts++;
188  count = *counts;
189 
190  // Get the new index
191  indices++;
192  index = *indices;
193  } else {
194  // Move to the next index
195  countIndex++;
196  indices++;
197  index = *indices;
198  }
199 
200 } // end operator++
201 
202 } // end namespace IVSparse
void coeff(T newValue)
Definition: IVCSC_Iterator_Methods.hpp:113
T value()
Definition: IVCSC_Iterator_Methods.hpp:101
indexT row()
Definition: IVCSC_Iterator_Methods.hpp:119
indexT col()
Definition: IVCSC_Iterator_Methods.hpp:129
indexT outerDim()
Definition: IVCSC_Iterator_Methods.hpp:95
indexT getIndex()
Definition: IVCSC_Iterator_Methods.hpp:107
InnerIterator()
Definition: IVCSC_Iterator.hpp:57
Definition: VCSC_SparseMatrix.hpp:21
indexT getNumUniqueVals(uint32_t vec) const
Definition: VCSC_Methods.hpp:48
indexT * getCounts(uint32_t vec) const
Definition: VCSC_Methods.hpp:35
indexT getNumIndices(uint32_t vec) const
Definition: VCSC_Methods.hpp:58
indexT * getIndices(uint32_t vec) const
Definition: VCSC_Methods.hpp:41
T * getValues(uint32_t vec) const
Definition: VCSC_Methods.hpp:29