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