CoinIndexedVector.hpp
Go to the documentation of this file.
1 /* $Id: CoinIndexedVector.hpp 1239 2009-12-10 16:16:11Z ladanyi $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CoinIndexedVector_H
5 #define CoinIndexedVector_H
6 
7 #if defined(_MSC_VER)
8 // Turn off compiler warning about long names
9 # pragma warning(disable:4786)
10 #endif
11 
12 #include <map>
13 #ifndef CLP_NO_VECTOR
14 #include "CoinPackedVectorBase.hpp"
15 #endif
16 #include "CoinSort.hpp"
17 #include "CoinHelperFunctions.hpp"
18 #include <cassert>
19 
20 #ifndef COIN_FLOAT
21 #define COIN_INDEXED_TINY_ELEMENT 1.0e-50
22 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-100
23 #else
24 #define COIN_INDEXED_TINY_ELEMENT 1.0e-35
25 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-39
26 #endif
27 
102  friend void CoinIndexedVectorUnitTest();
103 
104 public:
107  inline int getNumElements() const { return nElements_; }
110  inline const int * getIndices() const { return indices_; }
112  // ** No longer supported virtual const double * getElements() const ;
114  inline int * getIndices() { return indices_; }
118  inline double * denseVector() const { return elements_; }
120  inline void setDenseVector(double * array)
121  { elements_ = array;}
123  inline void setIndexVector(int * array)
124  { indices_ = array;}
127  double & operator[](int i) const;
128 
130 
131  //-------------------------------------------------------------------
132  // Set indices and elements
133  //-------------------------------------------------------------------
136  inline void setNumElements(int value) { nElements_ = value;
138  if (!nElements_) packedMode_=false;}
140  void clear();
142  void empty();
145 #ifndef CLP_NO_VECTOR
146 
149 #endif
150 
153  void copy(const CoinIndexedVector & rhs, double multiplier=1.0);
154 
157  void borrowVector(int size, int numberIndices, int* inds, double* elems);
158 
162  void returnVector();
163 
168  void setVector(int numberIndices, const int * inds, const double * elems);
169 
174  void setVector(int size, int numberIndices, const int * inds, const double * elems);
175 
177  void setConstant(int size, const int * inds, double elems);
178 
180  void setFull(int size, const double * elems);
181 
185  void setElement(int index, double element);
186 
188  void insert(int index, double element);
191  void add(int index, double element);
195  inline void quickAdd(int index, double element)
196  {
197  if (elements_[index]) {
198  element += elements_[index];
199  if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
200  elements_[index] = element;
201  } else {
202  elements_[index] = 1.0e-100;
203  }
204  } else if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
205  indices_[nElements_++] = index;
206  assert (nElements_<=capacity_);
207  elements_[index] = element;
208  }
209  }
212  inline void zero(int index)
213  {
214  if (elements_[index])
215  elements_[index] = 1.0e-100;
216  }
219  int clean(double tolerance);
221  int cleanAndPack(double tolerance);
223  int cleanAndPackSafe(double tolerance);
225  inline void setPacked()
226  { packedMode_ = true;}
228  void checkClear();
230  void checkClean();
232  int scan();
236  int scan(int start, int end);
239  int scan(double tolerance);
243  int scan(int start, int end, double tolerance);
245  int scanAndPack();
246  int scanAndPack(int start, int end);
247  int scanAndPack(double tolerance);
248  int scanAndPack(int start, int end, double tolerance);
250  void createPacked(int number, const int * indices,
251  const double * elements);
253  void expand();
254 #ifndef CLP_NO_VECTOR
255  void append(const CoinPackedVectorBase & caboose);
257 #endif
258  void append(const CoinIndexedVector & caboose);
260 
262  void swap(int i, int j);
263 
265  void truncate(int newSize);
267  void print() const;
269 
271  void operator+=(double value);
274  void operator-=(double value);
276  void operator*=(double value);
278  void operator/=(double value);
280 
283 #ifndef CLP_NO_VECTOR
284 
286  bool operator==(const CoinPackedVectorBase & rhs) const;
288  bool operator!=(const CoinPackedVectorBase & rhs) const;
289 #endif
290 
292  bool operator==(const CoinIndexedVector & rhs) const;
294  bool operator!=(const CoinIndexedVector & rhs) const;
296 
299  int getMaxIndex() const;
302  int getMinIndex() const;
304 
305 
309  void sort()
310  { std::sort(indices_,indices_+nElements_); }
311 
313  { std::sort(indices_,indices_+nElements_); }
314 
315  void sortDecrIndex();
316 
317  void sortIncrElement();
318 
319  void sortDecrElement();
320 
322 
323  //#############################################################################
324 
338  const CoinIndexedVector& op2);
339 
342  const CoinIndexedVector& op2);
343 
346  const CoinIndexedVector& op2);
347 
350  const CoinIndexedVector& op2);
352 void operator+=(const CoinIndexedVector& op2);
353 
355 void operator-=( const CoinIndexedVector& op2);
356 
358 void operator*=(const CoinIndexedVector& op2);
359 
361 void operator/=(const CoinIndexedVector& op2);
363 
370  void reserve(int n);
374  int capacity() const { return capacity_; }
376  inline void setPackedMode(bool yesNo)
377  { packedMode_=yesNo;}
379  inline bool packedMode() const
380  { return packedMode_;}
382 
388  CoinIndexedVector(int size, const int * inds, const double * elems);
390  CoinIndexedVector(int size, const int * inds, double element);
393  CoinIndexedVector(int size, const double * elements);
395  CoinIndexedVector(int size);
400 #ifndef CLP_NO_VECTOR
401 
403 #endif
404 
407 
408 private:
411  void gutsOfSetVector(int size,
413  const int * inds, const double * elems);
414  void gutsOfSetVector(int size, int numberIndices,
415  const int * inds, const double * elems);
416  void gutsOfSetPackedVector(int size, int numberIndices,
417  const int * inds, const double * elems);
419  void gutsOfSetConstant(int size,
420  const int * inds, double value);
422 
423 private:
426  int * indices_;
429  double * elements_;
435  int offset_;
439 };
440 
441 //#############################################################################
447 void
466 
467 public:
470  inline int getSize() const
472  { return size_; }
474  inline int rawSize() const
475  { return size_; }
477  inline bool switchedOn() const
478  { return size_!=-1; }
480  inline int getCapacity() const
481  { return (size_>-2) ? size_ : (-size_)-2; }
483  inline void setCapacity()
484  { if (size_<=-2) size_ = (-size_)-2; }
486  inline const char * array() const
487  { return (size_>-2) ? array_ : NULL; }
489 
492  inline void setSize(int value)
494  { size_ = value; }
496  inline void switchOff()
497  { size_ = -1; }
499  void setPersistence(int flag,int currentLength);
501  void clear();
503  void swap(CoinArrayWithLength & other);
505  void extend(int newSize);
507 
510  char * conditionalNew(long sizeWanted);
513  void conditionalDelete();
515 
520  { array_=NULL; size_=-1;}
522  inline CoinArrayWithLength(int size)
523  { array_=new char [size]; size_=-1;}
528  inline CoinArrayWithLength(int size, int mode)
529  { array_ = new char [size]; if (mode) memset(array_,0,size);size_=size;}
537  void copy(const CoinArrayWithLength & rhs, int numberBytes=-1);
539  void allocate(const CoinArrayWithLength & rhs, int numberBytes);
542  { delete [] array_; }
543  // was { free(array_); }
545 
546 protected:
549  char * array_;
552  int size_;
554 };
556 
558 
559 public:
562  inline int getSize() const
564  { return size_/CoinSizeofAsInt(double); }
566  inline double * array() const
567  { return reinterpret_cast<double *> ((size_>-2) ? array_ : NULL); }
569 
572  inline void setSize(int value)
574  { size_ = value*CoinSizeofAsInt(double); }
576 
579  inline double * conditionalNew(int sizeWanted)
581  { return reinterpret_cast<double *> ( CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> ((sizeWanted)*CoinSizeofAsInt(double)) : -1)); }
583 
588  { array_=NULL; size_=-1;}
590  inline CoinDoubleArrayWithLength(int size)
591  { array_=new char [size*CoinSizeofAsInt(double)]; size_=-1;}
596  inline CoinDoubleArrayWithLength(int size, int mode)
597  : CoinArrayWithLength(size*CoinSizeofAsInt(double),mode) {}
600  : CoinArrayWithLength(rhs) {}
603  : CoinArrayWithLength(rhs) {}
606  { CoinArrayWithLength::operator=(rhs); return *this;}
608 };
610 
612 
613 public:
616  inline int getSize() const
620  inline CoinFactorizationDouble * array() const
621  { return reinterpret_cast<CoinFactorizationDouble *> ((size_>-2) ? array_ : NULL); }
623 
626  inline void setSize(int value)
630 
633  inline CoinFactorizationDouble * conditionalNew(int sizeWanted)
635  { return reinterpret_cast<CoinFactorizationDouble *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(CoinFactorizationDouble)) : -1)); }
637 
642  { array_=NULL; size_=-1;}
645  { array_=new char [size*CoinSizeofAsInt(CoinFactorizationDouble)]; size_=-1;}
650  inline CoinFactorizationDoubleArrayWithLength(int size, int mode)
654  : CoinArrayWithLength(rhs) {}
657  : CoinArrayWithLength(rhs) {}
660  { CoinArrayWithLength::operator=(rhs); return *this;}
662 };
664 
666 
667 public:
670  inline int getSize() const
672  { return size_/CoinSizeofAsInt(int); }
674  inline int * array() const
675  { return reinterpret_cast<int *> ((size_>-2) ? array_ : NULL); }
677 
680  inline void setSize(int value)
682  { size_ = value*CoinSizeofAsInt(int); }
684 
687  inline int * conditionalNew(int sizeWanted)
689  { return reinterpret_cast<int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(int)) : -1)); }
691 
696  { array_=NULL; size_=-1;}
698  inline CoinIntArrayWithLength(int size)
699  { array_=new char [size*CoinSizeofAsInt(int)]; size_=-1;}
704  inline CoinIntArrayWithLength(int size, int mode)
705  : CoinArrayWithLength(size*CoinSizeofAsInt(int),mode) {}
708  : CoinArrayWithLength(rhs) {}
711  : CoinArrayWithLength(rhs) {}
714  { CoinArrayWithLength::operator=(rhs); return *this;}
716 };
718 
720 
721 public:
724  inline int getSize() const
726  { return size_/CoinSizeofAsInt(CoinBigIndex); }
728  inline CoinBigIndex * array() const
729  { return reinterpret_cast<CoinBigIndex *> ((size_>-2) ? array_ : NULL); }
731 
734  inline void setSize(int value)
736  { size_ = value*CoinSizeofAsInt(CoinBigIndex); }
738 
741  inline CoinBigIndex * conditionalNew(int sizeWanted)
743  { return reinterpret_cast<CoinBigIndex *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(CoinBigIndex)) : -1)); }
745 
750  { array_=NULL; size_=-1;}
753  { array_=new char [size*CoinSizeofAsInt(CoinBigIndex)]; size_=-1;}
758  inline CoinBigIndexArrayWithLength(int size, int mode)
762  : CoinArrayWithLength(rhs) {}
765  : CoinArrayWithLength(rhs) {}
768  { CoinArrayWithLength::operator=(rhs); return *this;}
770 };
772 
774 
775 public:
778  inline int getSize() const
780  { return size_/CoinSizeofAsInt(unsigned int); }
782  inline unsigned int * array() const
783  { return reinterpret_cast<unsigned int *> ((size_>-2) ? array_ : NULL); }
785 
788  inline void setSize(int value)
790  { size_ = value*CoinSizeofAsInt(unsigned int); }
792 
795  inline unsigned int * conditionalNew(int sizeWanted)
797  { return reinterpret_cast<unsigned int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(unsigned int)) : -1)); }
799 
804  { array_=NULL; size_=-1;}
807  { array_=new char [size*CoinSizeofAsInt(unsigned int)]; size_=-1;}
812  inline CoinUnsignedIntArrayWithLength(int size, int mode)
813  : CoinArrayWithLength(size*CoinSizeofAsInt(unsigned int),mode) {}
816  : CoinArrayWithLength(rhs) {}
819  : CoinArrayWithLength(rhs) {}
822  { CoinArrayWithLength::operator=(rhs); return *this;}
824 };
825 #endif
int getMaxIndex() const
Get value of maximum index.
int getCapacity() const
Get the capacity.
int CoinBigIndex
~CoinArrayWithLength()
Destructor.
void extend(int newSize)
Extend a persistent array keeping data (size in bytes)
void setVector(int numberIndices, const int *inds, const double *elems)
Set vector numberIndices, indices, and elements.
void CoinIndexedVectorUnitTest()
A function that tests the methods in the CoinIndexedVector class.
void sortIncrIndex()
Sort the indexed storage vector (increasing indices).
CoinIndexedVector operator*(const CoinIndexedVector &op2)
Return the element-wise product of two indexed vectors.
void setSize(int value)
Set the size.
void setCapacity()
Set the capacity to >=0 if <=-2.
CoinArrayWithLength & operator=(const CoinArrayWithLength &rhs)
Assignment operator.
CoinFactorizationDouble * conditionalNew(int sizeWanted)
Conditionally gets new array.
void conditionalDelete()
Conditionally deletes.
CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength *rhs)
Copy constructor.2.
CoinFactorizationDoubleArrayWithLength()
Default constructor - NULL.
void operator/=(double value)
divide every entry by value (** 0 vanishes)
void setFull(int size, const double *elems)
Indices are not specified and are taken to be 0,1,...,size-1.
void swap(CoinArrayWithLength &other)
Swaps memory between two members.
CoinIndexedVector()
Default constructor.
void reserve(int n)
Reserve space.
CoinIntArrayWithLength & operator=(const CoinIntArrayWithLength &rhs)
Assignment operator.
void sort()
Sort the indexed storage vector (increasing indices).
void operator-=(double value)
subtract value from every entry
void sortDecrElement()
Sort the indexed storage vector (increasing indices).
int capacity_
Amount of memory allocated for indices_, and elements_.
int getSize() const
Get the size.
void checkClean()
For debug check vector is clean i.e. elements match indices.
int getNumElements() const
Get the size.
CoinBigIndexArrayWithLength(int size)
Alternate Constructor - length in bytes - size_ -1.
void zero(int index)
Makes nonzero tiny.
CoinDoubleArrayWithLength(int size)
Alternate Constructor - length in bytes - size_ -1.
CoinIndexedVector & operator=(const CoinIndexedVector &)
Assignment operator.
CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength *rhs)
Copy constructor.2.
double CoinFactorizationDouble
Definition: CoinFinite.hpp:86
int cleanAndPackSafe(double tolerance)
Same but packs down and is safe (i.e. if order is odd)
CoinFactorizationDoubleArrayWithLength & operator=(const CoinFactorizationDoubleArrayWithLength &rhs)
Assignment operator.
void quickAdd(int index, double element)
Insert or if exists add an element into the vector Any resulting zero elements will be made tiny...
void expand()
This is mainly for testing - goes from packed to indexed.
int * array() const
Get Array.
bool packedMode() const
Gets packed mode.
CoinFactorizationDoubleArrayWithLength(const CoinFactorizationDoubleArrayWithLength *rhs)
Copy constructor.2.
void setSize(int value)
Set the size.
CoinFactorizationDoubleArrayWithLength(const CoinFactorizationDoubleArrayWithLength &rhs)
Copy constructor.
CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength &rhs)
Copy constructor.
void sortIncrElement()
Sort the indexed storage vector (increasing indices).
bool operator==(const CoinPackedVectorBase &rhs) const
Equal.
CoinArrayWithLength()
Default constructor - NULL.
void sortDecrIndex()
Sort the indexed storage vector (increasing indices).
double * array() const
Get Array.
CoinIndexedVector operator-(const CoinIndexedVector &op2)
Return the difference of two indexed vectors.
void checkClear()
For debug check vector is clear i.e. no elements.
void switchOff()
Set the size to -1.
double * elements_
Vector elements.
void returnVector()
Return ownership of the arguments to this vector.
~CoinIndexedVector()
Destructor.
CoinIndexedVector operator+(const CoinIndexedVector &op2)
Return the sum of two indexed vectors.
CoinDoubleArrayWithLength()
Default constructor - NULL.
void setPackedMode(bool yesNo)
Sets packed mode.
CoinBigIndexArrayWithLength()
Default constructor - NULL.
Abstract base class for various sparse vectors.
double * denseVector() const
Get the vector as a dense vector.
double & operator[](int i) const
Access the i'th element of the full storage vector.
void insert(int index, double element)
Insert an element into the vector.
CoinBigIndexArrayWithLength & operator=(const CoinBigIndexArrayWithLength &rhs)
Assignment operator.
CoinDoubleArrayWithLength & operator=(const CoinDoubleArrayWithLength &rhs)
Assignment operator.
void borrowVector(int size, int numberIndices, int *inds, double *elems)
Borrow ownership of the arguments to this vector.
void copy(const CoinArrayWithLength &rhs, int numberBytes=-1)
Assignment with length (if -1 use internal length)
void print() const
Print out.
void setPersistence(int flag, int currentLength)
Does what is needed to set persistence.
CoinUnsignedIntArrayWithLength & operator=(const CoinUnsignedIntArrayWithLength &rhs)
Assignment operator.
void setSize(int value)
Set the size.
Pointer with length in bytes.
void operator+=(double value)
add value to every entry
void operator*=(double value)
multiply every entry by value
char * conditionalNew(long sizeWanted)
Conditionally gets new array.
CoinDoubleArrayWithLength(int size, int mode)
Alternate Constructor - length in bytes mode - 0 size_ set to size 1 size_ set to size and zeroed...
CoinFactorizationDouble * array() const
Get Array.
CoinBigIndex * array() const
Get Array.
int getSize() const
Get the size.
CoinIndexedVector operator/(const CoinIndexedVector &op2)
Return the element-wise ratio of two indexed vectors (0.0/0.0 => 0.0) (0 vanishes) ...
int * conditionalNew(int sizeWanted)
Conditionally gets new array.
unsigned int * array() const
Get Array.
int capacity() const
capacity returns the size which could be accomodated without having to reallocate storage...
void clear()
Zero out array.
CoinUnsignedIntArrayWithLength()
Default constructor - NULL.
Indexed Vector.
int getSize() const
Get the size.
CoinIntArrayWithLength()
Default constructor - NULL.
void gutsOfSetPackedVector(int size, int numberIndices, const int *inds, const double *elems)
Copy internal data.
friend void CoinIndexedVectorUnitTest()
A function that tests the methods in the CoinIndexedVector class.
int scanAndPack()
These are same but pack down.
void swap(int i, int j)
Swap values in positions i and j of indices and elements.
int * getIndices()
Get element values.
int nElements_
Size of indices and packed elements vectors.
int getSize() const
Get the size.
const char * array() const
Get Array.
bool switchedOn() const
See if persistence already on.
void setPacked()
Mark as packed.
void setSize(int value)
Set the size.
int clean(double tolerance)
set all small values to zero and return number remaining
CoinIntArrayWithLength(const CoinIntArrayWithLength *rhs)
Copy constructor.2.
CoinIntArrayWithLength(const CoinIntArrayWithLength &rhs)
Copy constructor.
void gutsOfSetConstant(int size, const int *inds, double value)
Copy internal data.
void setSize(int value)
Set the size.
void createPacked(int number, const int *indices, const double *elements)
Create packed array.
bool operator!=(const CoinPackedVectorBase &rhs) const
Not equal.
CoinUnsignedIntArrayWithLength(int size, int mode)
Alternate Constructor - length in bytes mode - 0 size_ set to size 1 size_ set to size and zeroed...
void append(const CoinPackedVectorBase &caboose)
Append a CoinPackedVector to the end.
int getSize() const
Get the size.
void setNumElements(int value)
Set the size.
unsigned int * conditionalNew(int sizeWanted)
Conditionally gets new array.
CoinBigIndexArrayWithLength(int size, int mode)
Alternate Constructor - length in bytes mode - 0 size_ set to size 1 size_ set to size and zeroed...
CoinArrayWithLength(int size)
Alternate Constructor - length in bytes - size_ -1.
void setIndexVector(int *array)
For very temporary use when user needs to borrow an index vector.
void setElement(int index, double element)
Set an existing element in the indexed vector The first argument is the "index" into the elements() a...
CoinFactorizationDoubleArrayWithLength(int size)
Alternate Constructor - length in bytes - size_ -1.
void empty()
Reset the vector (as if were just created an empty vector)
void add(int index, double element)
Insert or if exists add an element into the vector Any resulting zero elements will be made tiny...
CoinBigIndex * conditionalNew(int sizeWanted)
Conditionally gets new array.
CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength &rhs)
Copy constructor.
CoinBigIndex * version.
CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength *rhs)
Copy constructor.2.
void setConstant(int size, const int *inds, double elems)
Elements set to have the same scalar value.
CoinUnsignedIntArrayWithLength(int size)
Alternate Constructor - length in bytes - size_ -1.
CoinIntArrayWithLength(int size, int mode)
Alternate Constructor - length in bytes mode - 0 size_ set to size 1 size_ set to size and zeroed...
int rawSize() const
Get the size.
void allocate(const CoinArrayWithLength &rhs, int numberBytes)
Assignment with length - does not copy.
CoinFactorizationDoubleArrayWithLength(int size, int mode)
Alternate Constructor - length in bytes mode - 0 size_ set to size 1 size_ set to size and zeroed...
void setDenseVector(double *array)
For very temporary use when user needs to borrow a dense vector.
CoinIntArrayWithLength(int size)
Alternate Constructor - length in bytes - size_ -1.
double * conditionalNew(int sizeWanted)
Conditionally gets new array.
CoinFactorizationDouble * version.
void gutsOfSetVector(int size, const int *inds, const double *elems)
Copy internal data.
bool packedMode_
If true then is operating in packed mode.
int size_
Size of array in bytes.
void truncate(int newSize)
Throw away all entries in rows >= newSize.
int offset_
Offset to get where new allocated array.
int getMinIndex() const
Get value of minimum index.
void setSize(int value)
Set the size.
#define COIN_INDEXED_TINY_ELEMENT
void copy(const CoinIndexedVector &rhs, double multiplier=1.0)
Copy the contents of one vector into another.
#define CoinSizeofAsInt(type)
This helper returns "sizeof" as an int.
CoinArrayWithLength(int size, int mode)
Alternate Constructor - length in bytes mode - 0 size_ set to size 1 size_ set to size and zeroed...
const int * getIndices() const
Get indices of elements.
CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength &rhs)
Copy constructor.
int cleanAndPack(double tolerance)
Same but packs down.
void clear()
Reset the vector (as if were just created an empty vector). This leaves arrays!
int * indices_
Vector indices.
int scan()
Scan dense region and set up indices (returns number found)