Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

sparvec.h
Go to the documentation of this file.
1 
30 #pragma once
31 
32 #include <ogdf/lib/abacus/global.h>
33 
34 namespace abacus {
35 
36 
38 
47 class SparVec : public AbacusRoot {
48 public:
49 
51 
61  SparVec(AbacusGlobal *glob,
62  int size,
63  double reallocFac = 10.0);
64 
66 
85  SparVec(AbacusGlobal *glob,
86  int size,
87  const Array<int> &s,
88  const Array<double> &c,
89  double reallocFac = 10.0);
90 
92 
109  SparVec(AbacusGlobal *glob,
110  int size,
111  int *s,
112  double *c,
113  double reallocFac = 10.0);
114 
116 
119  SparVec(const SparVec& rhs);
120 
122  ~SparVec();
123 
125 
133  SparVec& operator=(const SparVec& rhs);
134 
136 
144  friend std::ostream& operator<<(std::ostream& out, const SparVec& rhs);
145 
151  int support(int i) const {
152 #ifdef OGDF_DEBUG
153  rangeCheck(i);
154 #endif
155  return support_[i];
156  }
157 
163  double coeff(int i) const {
164 #ifdef OGDF_DEBUG
165  rangeCheck(i);
166 #endif
167  return coeff_[i];
168  }
169 
175  double origCoeff(int i) const;
176 
178 
184  void insert(int s, double c) {
185  if (nnz_ == size_) realloc();
186 
187  support_[nnz_] = s;
188  coeff_[nnz_] = c;
189  ++nnz_;
190  }
191 
193 
200  void leftShift(ArrayBuffer<int> &del);
201 
203 
210  void copy (const SparVec &vec);
211 
213  void clear() { nnz_ = 0; }
214 
216 
221  void rename(Array<int> &newName);
222 
224  int size() const { return size_; }
225 
227 
232  int nnz() const { return nnz_; }
233 
235  double norm();
236 
238  /***
239  * This function is called if an automatic reallocation takes place.
240  */
241  void realloc();
242 
244 
249  void realloc(int newSize);
250 
251 protected:
252 
254 
264  void rangeCheck(int i) const;
265 
268 
270  int size_;
271 
273  int nnz_;
274 
277  double reallocFac_;
278 
280  int *support_;
281 
283  double *coeff_;
284 };
285 
286 }
ogdf::ArrayBuffer
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition: Array.h:46
abacus::SparVec::nnz_
int nnz_
The number of stored elements ("nonzeros").
Definition: sparvec.h:273
abacus::SparVec::origCoeff
double origCoeff(int i) const
global.h
global.
abacus::SparVec::norm
double norm()
Returns the Euclidean norm of the sparse vector.
abacus::SparVec::insert
void insert(int s, double c)
Adds a new support/coefficient pair to the vector.
Definition: sparvec.h:184
abacus::SparVec::~SparVec
~SparVec()
The destructor.
abacus::SparVec::SparVec
SparVec(AbacusGlobal *glob, int size, double reallocFac=10.0)
Creates an empty sparse vector.
abacus::SparVec::glob_
AbacusGlobal * glob_
A pointer to the corresponding global object.
Definition: sparvec.h:267
abacus
Definition: abacusroot.h:48
abacus::SparVec::support_
int * support_
The array storing the nonzero variables.
Definition: sparvec.h:280
abacus::SparVec::nnz
int nnz() const
Returns the number of nonzero elements.
Definition: sparvec.h:232
abacus::SparVec::reallocFac_
double reallocFac_
If a new element is inserted but the sparse vector is full, then its size is increased by reallocFac_...
Definition: sparvec.h:277
abacus::SparVec::size_
int size_
The maximal number of nonzero coefficients which can be stored without reallocation.
Definition: sparvec.h:270
abacus::SparVec::rename
void rename(Array< int > &newName)
Replaces the index of the support by new names.
abacus::SparVec::copy
void copy(const SparVec &vec)
Copies vector vec.
abacus::SparVec::coeff
double coeff(int i) const
Definition: sparvec.h:163
abacus::SparVec::coeff_
double * coeff_
The array storing the corresponding nonzero coefficients.
Definition: sparvec.h:283
abacus::SparVec::rangeCheck
void rangeCheck(int i) const
Checks whether i is a valid index.
abacus::AbacusRoot
Base class of all other classes of ABACUS.
Definition: abacusroot.h:68
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:214
abacus::SparVec
Sparse vectors.
Definition: sparvec.h:47
abacus::SparVec::size
int size() const
Returns the maximal length of the sparse vector.
Definition: sparvec.h:224
abacus::SparVec::support
int support(int i) const
Definition: sparvec.h:151
abacus::SparVec::operator<<
friend std::ostream & operator<<(std::ostream &out, const SparVec &rhs)
The output operator.
abacus::SparVec::operator=
SparVec & operator=(const SparVec &rhs)
The assignment operator.
abacus::SparVec::leftShift
void leftShift(ArrayBuffer< int > &del)
Deletes the elements listed in a buffer from the sparse vector.
abacus::SparVec::realloc
void realloc()
Increases the size of the sparse vector by reallocFac_ percent of the original size.
abacus::AbacusGlobal
Global data and functions.
Definition: global.h:57
abacus::SparVec::clear
void clear()
Removes all nonzeros from the sparse vector.
Definition: sparvec.h:213