IVSparse  v1.0
A sparse matrix compression library.
CSC_Iterator_Methods.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
13 //* Constructors *//
14 
15 // CSC Matrix Constructor
16 template <typename T, typename indexT, bool columnMajor>
18  SparseMatrix<T, indexT, 1, columnMajor> &mat, uint32_t vec) {
19 
20  this->outer = vec;
21 
22  // check if the vector is empty
23  if (mat.getOuterPointers()[vec] == mat.getOuterPointers()[vec + 1]) {
24  vals = nullptr;
25  indices = nullptr;
26  endPtr = nullptr;
27  return;
28  }
29 
30  // set the pointers to the correct locations
31  vals = &mat.vals[mat.outerPtr[vec]];
32  indices = &mat.innerIdx[mat.outerPtr[vec]];
33  endPtr = &mat.innerIdx[mat.outerPtr[vec + 1]];
34 
35  // set the values of the iterator
36  val = vals;
37  index = indices[0];
38 }
39 
40 // CSC Vector Constructor
41 template <typename T, typename indexT, bool columnMajor>
44 
45  this->outer = 0;
46 
47  // set the pointers to the correct locations
48  vals = vec.values();
49  indices = vec.indexPtr();
50  endPtr = vec.indexPtr() + vec.nonZeros();
51 
52  // set the values of the iterator
53  val = vals;
54  index = indices[0];
55 }
56 
57 //* Overloaded Operators *//
58 
59 // Increment Operator
60 template <typename T, typename indexT, bool columnMajor>
62  vals++;
63  indices++;
64 
65  // check if the iterator is at the end of the vector
66  if (indices == endPtr) {
67  return;
68  }
69 
70  // set the values of the iterator
71  val = vals;
72  index = *indices;
73 }
74 
75 // Equality Operator
76 template <typename T, typename indexT, bool columnMajor>
77 bool SparseMatrix<T, indexT, 1, columnMajor>::InnerIterator::operator==(const InnerIterator &other) {
78  return (vals == other.vals && indices == other.index);
79 }
80 
81 // Inequality Operator
82 template <typename T, typename indexT, bool columnMajor>
83 bool SparseMatrix<T, indexT, 1, columnMajor>::InnerIterator::operator!=(const InnerIterator &other) {
84  return (vals != other.vals || indices != other.index);
85 }
86 
87 // Less Than Operator
88 template <typename T, typename indexT, bool columnMajor>
89 bool SparseMatrix<T, indexT, 1, columnMajor>::InnerIterator::operator<(const InnerIterator &other) {
90  return (vals < other.vals && indices < other.index);
91 }
92 
93 // Greater Than Operator
94 template <typename T, typename indexT, bool columnMajor>
95 bool SparseMatrix<T, indexT, 1, columnMajor>::InnerIterator::operator>(const InnerIterator &other) {
96  return (vals > other.vals && indices > other.index);
97 }
98 
99 // Dereference Operator
100 template <typename T, typename indexT, bool columnMajor>
101 T &SparseMatrix<T, indexT, 1, columnMajor>::InnerIterator::operator*() {
102  return val;
103 }
104 
105 //* Getters & Setters *//
106 
107 // Get the current index of the iterator
108 template <typename T, typename indexT, bool columnMajor>
110  return index;
111 }
112 
113 // Get the current outer dimension of the iterator
114 template <typename T, typename indexT, bool columnMajor>
116  return outer;
117 }
118 
119 // Get the current row of the iterator
120 template <typename T, typename indexT, bool columnMajor>
122  if (columnMajor) {
123  return index;
124  } else {
125  return outer;
126  }
127 }
128 
129 // Get the current column of the iterator
130 template <typename T, typename indexT, bool columnMajor>
132  if (columnMajor) {
133  return outer;
134  } else {
135  return index;
136  }
137 }
138 
139 // Get the current value of the iterator
140 template <typename T, typename indexT, bool columnMajor>
142  return *val;
143 }
144 
145 // coefficent access method
146 template <typename T, typename indexT, bool columnMajor>
148  *val = value;
149 }
150 
151 } // 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: IVCSC_Vector.hpp:25
uint32_t nonZeros()
Definition: IVCSC_Vector_Methods.hpp:165
Definition: CSC_SparseMatrix.hpp:24
indexT * getOuterPointers() const
Definition: CSC_Methods.hpp:57
Definition: IVCSC_SparseMatrix.hpp:29