IVSparse  v1.0
A sparse matrix compression library.
IVCSC_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, uint8_t compressionLevel, bool columnMajor>
19 
20  // check if the vector is out of bounds
21  #ifdef IVSPARSE_DEBUG
22  assert((vec < matrix.outerDim && vec >= 0) && "Vector index out of bounds");
23  #endif
24 
25  // check if data is nullptr
26  if (matrix.vectorPointer(vec) == nullptr) {
27  // Trips bool operator
28  data = (void *)0xFFFFFFFFFFFFFFFF;
29  endPtr = (void *)0xFFFFFFFFFFFFFFFF;
30  return;
31  }
32 
33  // Sets the column
34  this->outer = vec;
35 
36  // Points value to the first value in the column
37  data = matrix.vectorPointer(vec);
38 
39  // Sets the end pointer
40  endPtr = (uint8_t *)data + matrix.getVectorSize(vec);
41 
42  val = (T *)data;
43  data = (uint8_t *)data + sizeof(T);
44 
45  // Sets row width to the width of the first run
46  indexWidth = *(uint8_t *)data;
47  data = (uint8_t *)data + sizeof(uint8_t);
48 
49  decodeIndex();
50  index = newIndex;
51 }
52 
53 // Vector Constructor
54 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
57 
58  // set the column to -1
59  this->outer = 0;
60 
61  // set the data pointer
62  data = vector.begin();
63 
64  // If the column is all zeros, set the data to the end pointer
65  if (data == nullptr) {
66  // Trips bool operator
67  data = endPtr;
68  return;
69  }
70 
71  val = (T *)data;
72  data = (uint8_t *)data + sizeof(T);
73 
74  // set the end pointer
75  endPtr = vector.end();
76 
77  // Sets row width to the width of the first run
78  indexWidth = *(uint8_t *)data;
79  data = (uint8_t *)data + sizeof(uint8_t);
80 
81  decodeIndex();
82  index = newIndex;
83 }
84 
85 //* Getters *//
86 
87 // If the iterator is at a new run
88 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
90  return firstIndex;
91 }
92 
93 // Get the current outer dimension of the iterator
94 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
96  return outer;
97 }
98 
99 // Get the current value of the iterator
100 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
102  return *val;
103 }
104 
105 // Get the current index of the iterator
106 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
108  return index;
109 }
110 
111 // Updates the value of the iterator to newValue
112 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
114  *val = newValue;
115 }
116 
117 // Current row of the iterator
118 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
120  if constexpr (!columnMajor) {
121  return outer;
122  } else {
123  return index;
124  }
125 }
126 
127 // Current column of the iterator
128 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
130  if constexpr (!columnMajor) {
131  return index;
132  } else {
133  return outer;
134  }
135 }
136 
137 //* Private Class Methods *//
138 
139 // Index decoder
140 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
142  switch (indexWidth) {
143  case 1:
144  newIndex = static_cast<indexT>(*static_cast<uint8_t *>(data));
145  break;
146  case 2:
147  newIndex = static_cast<indexT>(*static_cast<uint16_t *>(data));
148  break;
149  case 4:
150  newIndex = static_cast<indexT>(*static_cast<uint32_t *>(data));
151  break;
152  case 8:
153  newIndex = static_cast<indexT>(*static_cast<uint64_t *>(data));
154  break;
155  }
156 }
157 
158 //* Operator Overloads *//
159 
160 // Dereference Operator
161 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
162 T &SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator::operator*() {
163  return *val;
164 }
165 
166 // Equality Operator
167 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
168 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator::operator==(const InnerIterator &other) {
169  return data == other.getIndex();
170 }
171 
172 // Inequality Operator
173 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
174 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator::operator!=(const InnerIterator &other) {
175  return data != other.getIndex();
176 }
177 
178 // Less Than Operator
179 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
180 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator::operator<(const InnerIterator &other) {
181  return data < other.getIndex();
182 }
183 
184 // Greater Than Operator
185 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
186 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator::operator>(const InnerIterator &other) {
187  return data > other.getIndex();
188 }
189 
190 // Increment Operator
191 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
192 void SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator::operator++() {
193  data = (uint8_t *)data + indexWidth;
194  decodeIndex();
195 
196  // IVSparse 3
197  // If new_row is 0 and not the first row, then the row is a delimitor
198  if (newIndex == 0) {
199  if (data >= (uint8_t *)endPtr - indexWidth) [[unlikely]] {
200  return;
201  }
202 
203  data = (uint8_t *)data + indexWidth;
204 
205  // val is the first row of the run
206  val = (T *)data;
207  data = (uint8_t *)data + sizeof(T);
208 
209  // Sets row width to the width of the first run
210  indexWidth = *(uint8_t *)data;
211  data = (uint8_t *)data + sizeof(uint8_t);
212 
213  // Make row 0 as it is a new run
214  decodeIndex();
215  index = newIndex;
216  firstIndex = true;
217  return;
218  }
219  index += newIndex;
220 
221  firstIndex = false;
222 }
223 
224 } // 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
bool isNewRun()
Definition: IVCSC_Iterator_Methods.hpp:89
Definition: IVCSC_Vector.hpp:25
void * end()
Definition: IVCSC_Vector_Methods.hpp:177
void * begin()
Definition: IVCSC_Vector_Methods.hpp:171
Definition: IVCSC_SparseMatrix.hpp:29
void * vectorPointer(uint32_t vec)
Definition: IVCSC_Methods.hpp:36
size_t getVectorSize(uint32_t vec) const
Definition: IVCSC_Methods.hpp:57