CoinUtils  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoinIndexedVector.hpp
Go to the documentation of this file.
1 /* $Id: CoinIndexedVector.hpp 1554 2012-10-31 16:52:28Z forrest $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 // This code is licensed under the terms of the Eclipse Public License (EPL).
5 
6 #ifndef CoinIndexedVector_H
7 #define CoinIndexedVector_H
8 
9 #if defined(_MSC_VER)
10 // Turn off compiler warning about long names
11 # pragma warning(disable:4786)
12 #endif
13 
14 #include <map>
15 #include "CoinFinite.hpp"
16 #ifndef CLP_NO_VECTOR
17 #include "CoinPackedVectorBase.hpp"
18 #endif
19 #include "CoinSort.hpp"
20 #include "CoinHelperFunctions.hpp"
21 #include <cassert>
22 
23 #ifndef COIN_FLOAT
24 #define COIN_INDEXED_TINY_ELEMENT 1.0e-50
25 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-100
26 #else
27 #define COIN_INDEXED_TINY_ELEMENT 1.0e-35
28 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-39
29 #endif
30 
105  friend void CoinIndexedVectorUnitTest();
106 
107 public:
110 
111  inline int getNumElements() const { return nElements_; }
113  inline const int * getIndices() const { return indices_; }
115  // ** No longer supported virtual const double * getElements() const ;
117  inline int * getIndices() { return indices_; }
121  inline double * denseVector() const { return elements_; }
123  inline void setDenseVector(double * array)
124  { elements_ = array;}
126  inline void setIndexVector(int * array)
127  { indices_ = array;}
130  double & operator[](int i) const;
131 
133 
134  //-------------------------------------------------------------------
135  // Set indices and elements
136  //-------------------------------------------------------------------
139 
140  inline void setNumElements(int value) { nElements_ = value;
141  if (!nElements_) packedMode_=false;}
143  void clear();
145  void empty();
148 #ifndef CLP_NO_VECTOR
149 
152 #endif
153 
156  void copy(const CoinIndexedVector & rhs, double multiplier=1.0);
157 
160  void borrowVector(int size, int numberIndices, int* inds, double* elems);
161 
165  void returnVector();
166 
171  void setVector(int numberIndices, const int * inds, const double * elems);
172 
177  void setVector(int size, int numberIndices, const int * inds, const double * elems);
178 
180  void setConstant(int size, const int * inds, double elems);
181 
183  void setFull(int size, const double * elems);
184 
188  void setElement(int index, double element);
189 
191  void insert(int index, double element);
193  inline void quickInsert(int index, double element)
194  {
195  assert (!elements_[index]);
196  indices_[nElements_++] = index;
197  assert (nElements_<=capacity_);
198  elements_[index] = element;
199  }
202  void add(int index, double element);
206  inline void quickAdd(int index, double element)
207  {
208  if (elements_[index]) {
209  element += elements_[index];
210  if ((element > 0 ? element : -element) >= COIN_INDEXED_TINY_ELEMENT) {
211  elements_[index] = element;
212  } else {
213  elements_[index] = 1.0e-100;
214  }
215  } else if ((element > 0 ? element : -element) >= COIN_INDEXED_TINY_ELEMENT) {
216  indices_[nElements_++] = index;
217  assert (nElements_<=capacity_);
218  elements_[index] = element;
219  }
220  }
225  inline void quickAddNonZero(int index, double element)
226  {
227  assert (element);
228  if (elements_[index]) {
229  element += elements_[index];
230  if ((element > 0 ? element : -element) >= COIN_INDEXED_TINY_ELEMENT) {
231  elements_[index] = element;
232  } else {
233  elements_[index] = COIN_DBL_MIN;
234  }
235  } else {
236  indices_[nElements_++] = index;
237  assert (nElements_<=capacity_);
238  elements_[index] = element;
239  }
240  }
243  inline void zero(int index)
244  {
245  if (elements_[index])
246  elements_[index] = COIN_DBL_MIN;
247  }
250  int clean(double tolerance);
252  int cleanAndPack(double tolerance);
254  int cleanAndPackSafe(double tolerance);
256  inline void setPacked()
257  { packedMode_ = true;}
258 #ifndef NDEBUG
259 
260  void checkClear();
262  void checkClean();
263 #else
264  inline void checkClear() {};
265  inline void checkClean() {};
266 #endif
267 
268  int scan();
272  int scan(int start, int end);
275  int scan(double tolerance);
279  int scan(int start, int end, double tolerance);
281  int scanAndPack();
282  int scanAndPack(int start, int end);
283  int scanAndPack(double tolerance);
284  int scanAndPack(int start, int end, double tolerance);
286  void createPacked(int number, const int * indices,
287  const double * elements);
289  void createUnpacked(int number, const int * indices,
290  const double * elements);
292  void createOneUnpackedElement(int index, double element);
294  void expand();
295 #ifndef CLP_NO_VECTOR
296 
297  void append(const CoinPackedVectorBase & caboose);
298 #endif
299 
300  void append(const CoinIndexedVector & caboose);
302  void append(CoinIndexedVector & other,int adjustIndex,bool zapElements=false);
303 
305  void swap(int i, int j);
306 
308  void truncate(int newSize);
310  void print() const;
312 
314 
315  void operator+=(double value);
317  void operator-=(double value);
319  void operator*=(double value);
321  void operator/=(double value);
323 
326 #ifndef CLP_NO_VECTOR
327 
329  bool operator==(const CoinPackedVectorBase & rhs) const;
331  bool operator!=(const CoinPackedVectorBase & rhs) const;
332 #endif
333 
335  bool operator==(const CoinIndexedVector & rhs) const;
337  bool operator!=(const CoinIndexedVector & rhs) const;
339  int isApproximatelyEqual(const CoinIndexedVector & rhs, double tolerance=1.0e-8) const;
341 
344 
345  int getMaxIndex() const;
347  int getMinIndex() const;
349 
350 
354  void sort()
355  { std::sort(indices_,indices_+nElements_); }
356 
358  { std::sort(indices_,indices_+nElements_); }
359 
360  void sortDecrIndex();
361 
362  void sortIncrElement();
363 
364  void sortDecrElement();
365  void sortPacked();
366 
368 
369  //#############################################################################
370 
382 
384  const CoinIndexedVector& op2);
385 
388  const CoinIndexedVector& op2);
389 
392  const CoinIndexedVector& op2);
393 
396  const CoinIndexedVector& op2);
398 void operator+=(const CoinIndexedVector& op2);
399 
401 void operator-=( const CoinIndexedVector& op2);
402 
404 void operator*=(const CoinIndexedVector& op2);
405 
407 void operator/=(const CoinIndexedVector& op2);
409 
416  void reserve(int n);
420  int capacity() const { return capacity_; }
422  inline void setPackedMode(bool yesNo)
423  { packedMode_=yesNo;}
425  inline bool packedMode() const
426  { return packedMode_;}
428 
434  CoinIndexedVector(int size, const int * inds, const double * elems);
436  CoinIndexedVector(int size, const int * inds, double element);
439  CoinIndexedVector(int size, const double * elements);
441  CoinIndexedVector(int size);
446 #ifndef CLP_NO_VECTOR
447 
449 #endif
450 
453 
454 private:
457 
458  void gutsOfSetVector(int size,
459  const int * inds, const double * elems);
460  void gutsOfSetVector(int size, int numberIndices,
461  const int * inds, const double * elems);
462  void gutsOfSetPackedVector(int size, int numberIndices,
463  const int * inds, const double * elems);
465  void gutsOfSetConstant(int size,
466  const int * inds, double value);
468 
469 protected:
472 
473  int * indices_;
475  double * elements_;
481  int offset_;
485 };
486 
487 //#############################################################################
493 void
512 
513 public:
516 
517  inline int getSize() const
518  { return size_; }
520  inline int rawSize() const
521  { return size_; }
523  inline bool switchedOn() const
524  { return size_!=-1; }
526  inline int capacity() const
527  { return (size_>-2) ? size_ : (-size_)-2; }
529  inline void setCapacity()
530  { if (size_<=-2) size_ = (-size_)-2; }
532  inline const char * array() const
533  { return (size_>-2) ? array_ : NULL; }
535 
538 
539  inline void setSize(int value)
540  { size_ = value; }
542  inline void switchOff()
543  { size_ = -1; }
545  inline void switchOn(int alignment=3)
546  { size_ = -2; alignment_=alignment;}
548  void setPersistence(int flag,int currentLength);
550  void clear();
552  void swap(CoinArrayWithLength & other);
554  void extend(int newSize);
556 
559 
560  char * conditionalNew(long sizeWanted);
562  void conditionalDelete();
564 
569  : array_(NULL),size_(-1),offset_(0),alignment_(0)
570  { }
572  inline CoinArrayWithLength(int size)
573  : size_(-1),offset_(0),alignment_(0)
574  { array_=new char [size];}
581  CoinArrayWithLength(int size, int mode);
589  void copy(const CoinArrayWithLength & rhs, int numberBytes=-1);
591  void allocate(const CoinArrayWithLength & rhs, int numberBytes);
595  void getArray(int size);
597  void reallyFreeArray();
599  void getCapacity(int numberBytes,int numberIfNeeded=-1);
601 
602 protected:
605 
606  char * array_;
610  int offset_;
614 };
616 
618 
619 public:
622 
623  inline int getSize() const
624  { return size_/CoinSizeofAsInt(double); }
626  inline double * array() const
627  { return reinterpret_cast<double *> ((size_>-2) ? array_ : NULL); }
629 
632 
633  inline void setSize(int value)
634  { size_ = value*CoinSizeofAsInt(double); }
636 
639 
640  inline double * conditionalNew(int sizeWanted)
641  { return reinterpret_cast<double *> ( CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> ((sizeWanted)*CoinSizeofAsInt(double)) : -1)); }
643 
648  { array_=NULL; size_=-1;}
650  inline CoinDoubleArrayWithLength(int size)
651  { array_=new char [size*CoinSizeofAsInt(double)]; size_=-1;}
656  inline CoinDoubleArrayWithLength(int size, int mode)
657  : CoinArrayWithLength(size*CoinSizeofAsInt(double),mode) {}
660  : CoinArrayWithLength(rhs) {}
663  : CoinArrayWithLength(rhs) {}
666  { CoinArrayWithLength::operator=(rhs); return *this;}
668 };
670 
672 
673 public:
676 
677  inline int getSize() const
680  inline CoinFactorizationDouble * array() const
681  { return reinterpret_cast<CoinFactorizationDouble *> ((size_>-2) ? array_ : NULL); }
683 
686 
687  inline void setSize(int value)
690 
693 
694  inline CoinFactorizationDouble * conditionalNew(int sizeWanted)
695  { return reinterpret_cast<CoinFactorizationDouble *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(CoinFactorizationDouble)) : -1)); }
697 
702  { array_=NULL; size_=-1;}
705  { array_=new char [size*CoinSizeofAsInt(CoinFactorizationDouble)]; size_=-1;}
710  inline CoinFactorizationDoubleArrayWithLength(int size, int mode)
714  : CoinArrayWithLength(rhs) {}
717  : CoinArrayWithLength(rhs) {}
720  { CoinArrayWithLength::operator=(rhs); return *this;}
722 };
724 
726 
727 public:
730 
731  inline int getSize() const
732  { return size_/CoinSizeofAsInt(long double); }
734  inline long double * array() const
735  { return reinterpret_cast<long double *> ((size_>-2) ? array_ : NULL); }
737 
740 
741  inline void setSize(int value)
742  { size_ = value*CoinSizeofAsInt(long double); }
744 
747 
748  inline long double * conditionalNew(int sizeWanted)
749  { return reinterpret_cast<long double *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(long double)) : -1)); }
751 
756  { array_=NULL; size_=-1;}
759  { array_=new char [size*CoinSizeofAsInt(long double)]; size_=-1;}
765  : CoinArrayWithLength(size*CoinSizeofAsInt(long double),mode) {}
768  : CoinArrayWithLength(rhs) {}
771  : CoinArrayWithLength(rhs) {}
774  { CoinArrayWithLength::operator=(rhs); return *this;}
776 };
778 
780 
781 public:
784 
785  inline int getSize() const
786  { return size_/CoinSizeofAsInt(int); }
788  inline int * array() const
789  { return reinterpret_cast<int *> ((size_>-2) ? array_ : NULL); }
791 
794 
795  inline void setSize(int value)
796  { size_ = value*CoinSizeofAsInt(int); }
798 
801 
802  inline int * conditionalNew(int sizeWanted)
803  { return reinterpret_cast<int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(int)) : -1)); }
805 
810  { array_=NULL; size_=-1;}
812  inline CoinIntArrayWithLength(int size)
813  { array_=new char [size*CoinSizeofAsInt(int)]; size_=-1;}
818  inline CoinIntArrayWithLength(int size, int mode)
819  : CoinArrayWithLength(size*CoinSizeofAsInt(int),mode) {}
822  : CoinArrayWithLength(rhs) {}
825  : CoinArrayWithLength(rhs) {}
828  { CoinArrayWithLength::operator=(rhs); return *this;}
830 };
832 
834 
835 public:
838 
839  inline int getSize() const
840  { return size_/CoinSizeofAsInt(CoinBigIndex); }
842  inline CoinBigIndex * array() const
843  { return reinterpret_cast<CoinBigIndex *> ((size_>-2) ? array_ : NULL); }
845 
848 
849  inline void setSize(int value)
850  { size_ = value*CoinSizeofAsInt(CoinBigIndex); }
852 
855 
856  inline CoinBigIndex * conditionalNew(int sizeWanted)
857  { return reinterpret_cast<CoinBigIndex *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(CoinBigIndex)) : -1)); }
859 
864  { array_=NULL; size_=-1;}
867  { array_=new char [size*CoinSizeofAsInt(CoinBigIndex)]; size_=-1;}
872  inline CoinBigIndexArrayWithLength(int size, int mode)
876  : CoinArrayWithLength(rhs) {}
879  : CoinArrayWithLength(rhs) {}
882  { CoinArrayWithLength::operator=(rhs); return *this;}
884 };
886 
888 
889 public:
892 
893  inline int getSize() const
894  { return size_/CoinSizeofAsInt(unsigned int); }
896  inline unsigned int * array() const
897  { return reinterpret_cast<unsigned int *> ((size_>-2) ? array_ : NULL); }
899 
902 
903  inline void setSize(int value)
904  { size_ = value*CoinSizeofAsInt(unsigned int); }
906 
909 
910  inline unsigned int * conditionalNew(int sizeWanted)
911  { return reinterpret_cast<unsigned int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(unsigned int)) : -1)); }
913 
918  { array_=NULL; size_=-1;}
921  { array_=new char [size*CoinSizeofAsInt(unsigned int)]; size_=-1;}
926  inline CoinUnsignedIntArrayWithLength(int size, int mode)
927  : CoinArrayWithLength(size*CoinSizeofAsInt(unsigned int),mode) {}
930  : CoinArrayWithLength(rhs) {}
933  : CoinArrayWithLength(rhs) {}
936  { CoinArrayWithLength::operator=(rhs); return *this;}
938 };
940 
942 
943 public:
946 
947  inline int getSize() const
948  { return size_/CoinSizeofAsInt(void *); }
950  inline void ** array() const
951  { return reinterpret_cast<void **> ((size_>-2) ? array_ : NULL); }
953 
956 
957  inline void setSize(int value)
958  { size_ = value*CoinSizeofAsInt(void *); }
960 
963 
964  inline void ** conditionalNew(int sizeWanted)
965  { return reinterpret_cast<void **> ( CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> ((sizeWanted)*CoinSizeofAsInt(void *)) : -1)); }
967 
972  { array_=NULL; size_=-1;}
975  { array_=new char [size*CoinSizeofAsInt(void *)]; size_=-1;}
980  inline CoinVoidStarArrayWithLength(int size, int mode)
981  : CoinArrayWithLength(size*CoinSizeofAsInt(void *),mode) {}
984  : CoinArrayWithLength(rhs) {}
987  : CoinArrayWithLength(rhs) {}
990  { CoinArrayWithLength::operator=(rhs); return *this;}
992 };
994 
996 
997 public:
1000 
1001  inline int getSize() const
1002  { return size_/lengthInBytes_; }
1004  inline void ** array() const
1005  { return reinterpret_cast<void **> ((size_>-2) ? array_ : NULL); }
1007 
1010 
1011  inline void setSize(int value)
1012  { size_ = value*lengthInBytes_; }
1014 
1017 
1018  inline char * conditionalNew(int length, int sizeWanted)
1019  { lengthInBytes_=length;return reinterpret_cast<char *> ( CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long>
1020  ((sizeWanted)*lengthInBytes_) : -1)); }
1022 
1026  inline CoinArbitraryArrayWithLength(int length=1)
1027  { array_=NULL; size_=-1;lengthInBytes_=length;}
1029  inline CoinArbitraryArrayWithLength(int length, int size)
1030  { array_=new char [size*length]; size_=-1; lengthInBytes_=length;}
1035  inline CoinArbitraryArrayWithLength(int length, int size, int mode)
1036  : CoinArrayWithLength(size*length,mode) {lengthInBytes_=length;}
1039  : CoinArrayWithLength(rhs) {}
1042  : CoinArrayWithLength(rhs) {}
1045  { CoinArrayWithLength::operator=(rhs); return *this;}
1047 
1048 protected:
1051 
1054 };
1056 
1057 public:
1058 #ifndef COIN_PARTITIONS
1059 #define COIN_PARTITIONS 8
1060 #endif
1061 
1063 
1064  inline int getNumElements(int partition) const { assert (partition<COIN_PARTITIONS);
1065  return numberElementsPartition_[partition]; }
1067  inline int getNumPartitions() const
1068  { return numberPartitions_; }
1070  inline int getNumElements() const { return nElements_; }
1072  inline int startPartition(int partition) const { assert (partition<=COIN_PARTITIONS);
1073  return startPartition_[partition]; }
1075  inline const int * startPartitions() const
1076  { return startPartition_; }
1078 
1079  //-------------------------------------------------------------------
1080  // Set indices and elements
1081  //-------------------------------------------------------------------
1084 
1085  inline void setNumElementsPartition(int partition, int value) { assert (partition<COIN_PARTITIONS);
1086  if (numberPartitions_) numberElementsPartition_[partition]=value; }
1088  inline void setTempNumElementsPartition(int partition, int value) { assert (partition<COIN_PARTITIONS);
1089  numberElementsPartition_[partition]=value; }
1091  void computeNumberElements();
1093  void compact();
1096  void reserve(int n);
1098  void setPartitions(int number,const int * starts);
1100  void clearAndReset();
1102  void clearAndKeep();
1104  void clearPartition(int partition);
1105 #ifndef NDEBUG
1106 
1107  void checkClear();
1109  void checkClean();
1110 #else
1111  inline void checkClear() {};
1112  inline void checkClean() {};
1113 #endif
1114 
1115  int scan(int partition, double tolerance=0.0);
1119 
1120  void print() const;
1122 
1126  void sort();
1128 
1134  CoinPartitionedVector(int size, const int * inds, const double * elems);
1136  CoinPartitionedVector(int size, const int * inds, double element);
1139  CoinPartitionedVector(int size, const double * elements);
1141  CoinPartitionedVector(int size);
1151 protected:
1154 
1161 };
1162 #endif