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