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