/home/coin/SVN-release/Bcps-0.91.0/CoinUtils/src/CoinIndexedVector.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #ifndef CoinIndexedVector_H
00004 #define CoinIndexedVector_H
00005 
00006 #if defined(_MSC_VER)
00007 // Turn off compiler warning about long names
00008 #  pragma warning(disable:4786)
00009 #endif
00010 
00011 #include <map>
00012 #ifndef CLP_NO_VECTOR
00013 #include "CoinPackedVectorBase.hpp"
00014 #endif
00015 #include "CoinSort.hpp"
00016 #include <cassert>
00017 
00018 #ifndef COIN_FLOAT
00019 #define COIN_INDEXED_TINY_ELEMENT 1.0e-50
00020 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-100
00021 #else
00022 #define COIN_INDEXED_TINY_ELEMENT 1.0e-35
00023 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-39
00024 #endif
00025 
00099 class CoinIndexedVector {
00100    friend void CoinIndexedVectorUnitTest();
00101   
00102 public:
00105 
00106    inline int getNumElements() const { return nElements_; }
00108    inline const int * getIndices() const { return indices_; }
00110    // ** No longer supported virtual const double * getElements() const ;
00112    inline int * getIndices() { return indices_; }
00116    inline double * denseVector() const { return elements_; }
00118   inline void setDenseVector(double * array)
00119   { elements_ = array;}
00121   inline void setIndexVector(int * array)
00122   { indices_ = array;}
00125    double & operator[](int i) const; 
00126 
00128  
00129    //-------------------------------------------------------------------
00130    // Set indices and elements
00131    //------------------------------------------------------------------- 
00134 
00135    inline void setNumElements(int value) { nElements_ = value;
00136    if (!nElements_) packedMode_=false;}
00138    void clear();
00140    void empty();
00142    CoinIndexedVector & operator=(const CoinIndexedVector &);
00143 #ifndef CLP_NO_VECTOR
00144 
00146    CoinIndexedVector & operator=(const CoinPackedVectorBase & rhs);
00147 #endif
00148 
00151    void copy(const CoinIndexedVector & rhs, double multiplier=1.0);
00152 
00155   void borrowVector(int size, int numberIndices, int* inds, double* elems);
00156 
00160    void returnVector();
00161 
00166   void setVector(int numberIndices, const int * inds, const double * elems);
00167   
00172    void setVector(int size, int numberIndices, const int * inds, const double * elems);
00173   
00175   void setConstant(int size, const int * inds, double elems);
00176   
00178   void setFull(int size, const double * elems);
00179 
00183    void setElement(int index, double element);
00184 
00186    void insert(int index, double element);
00189    void add(int index, double element);
00193    inline void quickAdd(int index, double element)
00194                {
00195                  if (elements_[index]) {
00196                    element += elements_[index];
00197                    if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
00198                      elements_[index] = element;
00199                    } else {
00200                      elements_[index] = 1.0e-100;
00201                    }
00202                  } else if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
00203                    indices_[nElements_++] = index;
00204                    assert (nElements_<=capacity_);
00205                    elements_[index] = element;
00206                  }
00207                }
00210    inline void zero(int index)
00211                {
00212                  if (elements_[index]) 
00213                    elements_[index] = 1.0e-100;
00214                }
00217    int clean(double tolerance);
00219    int cleanAndPack(double tolerance);
00221    int cleanAndPackSafe(double tolerance);
00223   inline void setPacked()
00224   { packedMode_ = true;}
00226    void checkClear();
00228    void checkClean();
00230    int scan();
00234    int scan(int start, int end);
00237    int scan(double tolerance);
00241    int scan(int start, int end, double tolerance);
00243    int scanAndPack();
00244    int scanAndPack(int start, int end);
00245    int scanAndPack(double tolerance);
00246    int scanAndPack(int start, int end, double tolerance);
00248    void createPacked(int number, const int * indices, 
00249                     const double * elements);
00251    void expand();
00252 #ifndef CLP_NO_VECTOR
00254    void append(const CoinPackedVectorBase & caboose);
00255 #endif
00257    void append(const CoinIndexedVector & caboose);
00258 
00260    void swap(int i, int j); 
00261 
00263    void truncate(int newSize); 
00265    void print() const;
00267 
00269 
00270    void operator+=(double value);
00272    void operator-=(double value);
00274    void operator*=(double value);
00276    void operator/=(double value);
00278 
00281 #ifndef CLP_NO_VECTOR
00282 
00284    bool operator==(const CoinPackedVectorBase & rhs) const;
00286    bool operator!=(const CoinPackedVectorBase & rhs) const;
00287 #endif
00288 
00290    bool operator==(const CoinIndexedVector & rhs) const;
00292    bool operator!=(const CoinIndexedVector & rhs) const;
00294 
00297 
00298    int getMaxIndex() const;
00300    int getMinIndex() const;
00302 
00303 
00307    void sort()
00308    { std::sort(indices_,indices_+nElements_); }
00309 
00310    void sortIncrIndex()
00311    { std::sort(indices_,indices_+nElements_); }
00312 
00313    void sortDecrIndex();
00314   
00315    void sortIncrElement();
00316 
00317    void sortDecrElement();
00318 
00320 
00321   //#############################################################################
00322 
00334 
00335 CoinIndexedVector operator+(
00336                            const CoinIndexedVector& op2);
00337 
00339 CoinIndexedVector operator-(
00340                            const CoinIndexedVector& op2);
00341 
00343 CoinIndexedVector operator*(
00344                            const CoinIndexedVector& op2);
00345 
00347 CoinIndexedVector operator/(
00348                            const CoinIndexedVector& op2);
00350 void operator+=(const CoinIndexedVector& op2);
00351 
00353 void operator-=( const CoinIndexedVector& op2);
00354 
00356 void operator*=(const CoinIndexedVector& op2);
00357 
00359 void operator/=(const CoinIndexedVector& op2);
00361 
00368    void reserve(int n);
00372    int capacity() const { return capacity_; }
00374    inline void setPackedMode(bool yesNo)
00375    { packedMode_=yesNo;}
00377    inline bool packedMode() const
00378    { return packedMode_;}
00380 
00384    CoinIndexedVector();
00386   CoinIndexedVector(int size, const int * inds, const double * elems);
00388   CoinIndexedVector(int size, const int * inds, double element);
00391   CoinIndexedVector(int size, const double * elements);
00393   CoinIndexedVector(int size);
00395    CoinIndexedVector(const CoinIndexedVector &);
00397    CoinIndexedVector(const CoinIndexedVector *);
00398 #ifndef CLP_NO_VECTOR
00399 
00400    CoinIndexedVector(const CoinPackedVectorBase & rhs);
00401 #endif
00402 
00403    ~CoinIndexedVector ();
00405     
00406 private:
00409 
00410    void gutsOfSetVector(int size,
00411                         const int * inds, const double * elems);
00412    void gutsOfSetVector(int size, int numberIndices,
00413                         const int * inds, const double * elems);
00414    void gutsOfSetPackedVector(int size, int numberIndices,
00415                         const int * inds, const double * elems);
00417    void gutsOfSetConstant(int size,
00418                           const int * inds, double value);
00420 
00421 private:
00424 
00425    int * indices_;
00427    double * elements_;
00429    int nElements_;
00431    int capacity_;
00433    int offset_;
00435    bool packedMode_;
00437 };
00438 
00439 //#############################################################################
00445 void
00446 CoinIndexedVectorUnitTest();
00463 class CoinArrayWithLength {
00464   
00465 public:
00468 
00469   inline int getSize() const 
00470   { return size_; }
00472   inline int rawSize() const 
00473   { return size_; }
00475   inline bool switchedOn() const 
00476   { return size_!=-1; }
00478   inline int getCapacity() const 
00479   { return (size_>-2) ? size_ : (-size_)-2; }
00481   inline void setCapacity() 
00482   { if (size_<=-2) size_ = (-size_)-2; }
00484   inline const char * array() const 
00485   { return (size_>-2) ? array_ : NULL; }
00487   
00490 
00491   inline void setSize(int value) 
00492   { size_ = value; }
00494   inline void switchOff() 
00495   { size_ = -1; }
00497   void setPersistence(int flag,int currentLength);
00499   void clear();
00501   void swap(CoinArrayWithLength & other);
00503   void extend(int newSize);
00505   
00508 
00509   char * conditionalNew(long sizeWanted); 
00511   void conditionalDelete();
00513   
00517   inline CoinArrayWithLength()
00518   { array_=NULL; size_=-1;}
00520   inline CoinArrayWithLength(int size)
00521   { array_=new char [size]; size_=-1;}
00526   inline CoinArrayWithLength(int size, int mode)
00527   { array_ = new char [size]; if (mode) memset(array_,0,size);size_=size;}
00529   CoinArrayWithLength(const CoinArrayWithLength & rhs);
00531   CoinArrayWithLength(const CoinArrayWithLength * rhs);
00533   CoinArrayWithLength& operator=(const CoinArrayWithLength & rhs);
00535   void copy(const CoinArrayWithLength & rhs, int numberBytes=-1);
00537   void allocate(const CoinArrayWithLength & rhs, int numberBytes);
00539   inline ~CoinArrayWithLength ()
00540   { delete [] array_; }
00541   // was { free(array_); }
00543   
00544 protected:
00547 
00548   char * array_;
00550   int size_;
00552 };
00554 
00555 class CoinDoubleArrayWithLength : public CoinArrayWithLength {
00556   
00557 public:
00560 
00561   inline int getSize() const 
00562   { return size_/sizeof(double); }
00564   inline double * array() const 
00565   { return reinterpret_cast<double *> ((size_>-2) ? array_ : NULL); }
00567   
00570 
00571   inline void setSize(int value) 
00572   { size_ = value*sizeof(double); }
00574   
00577 
00578   inline double * conditionalNew(int sizeWanted)
00579   { return reinterpret_cast<double *> ( CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> ((sizeWanted)*sizeof(double)) : -1)); }
00581   
00585   inline CoinDoubleArrayWithLength()
00586   { array_=NULL; size_=-1;}
00588   inline CoinDoubleArrayWithLength(int size)
00589   { array_=new char [size*sizeof(double)]; size_=-1;}
00594   inline CoinDoubleArrayWithLength(int size, int mode)
00595     : CoinArrayWithLength(size*sizeof(double),mode) {}
00597   inline CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength & rhs)
00598     : CoinArrayWithLength(rhs) {}
00600   inline CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength * rhs)
00601     : CoinArrayWithLength(rhs) {}
00603   inline CoinDoubleArrayWithLength& operator=(const CoinDoubleArrayWithLength & rhs)
00604   { CoinArrayWithLength::operator=(rhs);  return *this;}
00606 };
00608 
00609 class CoinFactorizationDoubleArrayWithLength : public CoinArrayWithLength {
00610   
00611 public:
00614 
00615   inline int getSize() const 
00616   { return size_/sizeof(CoinFactorizationDouble); }
00618   inline CoinFactorizationDouble * array() const 
00619   { return reinterpret_cast<CoinFactorizationDouble *> ((size_>-2) ? array_ : NULL); }
00621   
00624 
00625   inline void setSize(int value) 
00626   { size_ = value*sizeof(CoinFactorizationDouble); }
00628   
00631 
00632   inline CoinFactorizationDouble * conditionalNew(int sizeWanted)
00633   { return reinterpret_cast<CoinFactorizationDouble *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*sizeof(CoinFactorizationDouble)) : -1)); }
00635   
00639   inline CoinFactorizationDoubleArrayWithLength()
00640   { array_=NULL; size_=-1;}
00642   inline CoinFactorizationDoubleArrayWithLength(int size)
00643   { array_=new char [size*sizeof(CoinFactorizationDouble)]; size_=-1;}
00648   inline CoinFactorizationDoubleArrayWithLength(int size, int mode)
00649     : CoinArrayWithLength(size*sizeof(CoinFactorizationDouble),mode) {}
00651   inline CoinFactorizationDoubleArrayWithLength(const CoinFactorizationDoubleArrayWithLength & rhs)
00652     : CoinArrayWithLength(rhs) {}
00654   inline CoinFactorizationDoubleArrayWithLength(const CoinFactorizationDoubleArrayWithLength * rhs)
00655     : CoinArrayWithLength(rhs) {}
00657   inline CoinFactorizationDoubleArrayWithLength& operator=(const CoinFactorizationDoubleArrayWithLength & rhs)
00658   { CoinArrayWithLength::operator=(rhs);  return *this;}
00660 };
00662 
00663 class CoinIntArrayWithLength : public CoinArrayWithLength {
00664   
00665 public:
00668 
00669   inline int getSize() const 
00670   { return size_/sizeof(int); }
00672   inline int * array() const 
00673   { return reinterpret_cast<int *> ((size_>-2) ? array_ : NULL); }
00675   
00678 
00679   inline void setSize(int value) 
00680   { size_ = value*sizeof(int); }
00682   
00685 
00686   inline int * conditionalNew(int sizeWanted)
00687   { return reinterpret_cast<int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*sizeof(int)) : -1)); }
00689   
00693   inline CoinIntArrayWithLength()
00694   { array_=NULL; size_=-1;}
00696   inline CoinIntArrayWithLength(int size)
00697   { array_=new char [size*sizeof(int)]; size_=-1;}
00702   inline CoinIntArrayWithLength(int size, int mode)
00703     : CoinArrayWithLength(size*sizeof(int),mode) {}
00705   inline CoinIntArrayWithLength(const CoinIntArrayWithLength & rhs)
00706     : CoinArrayWithLength(rhs) {}
00708   inline CoinIntArrayWithLength(const CoinIntArrayWithLength * rhs)
00709     : CoinArrayWithLength(rhs) {}
00711   inline CoinIntArrayWithLength& operator=(const CoinIntArrayWithLength & rhs)
00712   { CoinArrayWithLength::operator=(rhs);  return *this;}
00714 };
00716 
00717 class CoinBigIndexArrayWithLength : public CoinArrayWithLength {
00718   
00719 public:
00722 
00723   inline int getSize() const 
00724   { return size_/sizeof(CoinBigIndex); }
00726   inline CoinBigIndex * array() const 
00727   { return reinterpret_cast<CoinBigIndex *> ((size_>-2) ? array_ : NULL); }
00729   
00732 
00733   inline void setSize(int value) 
00734   { size_ = value*sizeof(CoinBigIndex); }
00736   
00739 
00740   inline CoinBigIndex * conditionalNew(int sizeWanted)
00741   { return reinterpret_cast<CoinBigIndex *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*sizeof(CoinBigIndex)) : -1)); }
00743   
00747   inline CoinBigIndexArrayWithLength()
00748   { array_=NULL; size_=-1;}
00750   inline CoinBigIndexArrayWithLength(int size)
00751   { array_=new char [size*sizeof(CoinBigIndex)]; size_=-1;}
00756   inline CoinBigIndexArrayWithLength(int size, int mode)
00757     : CoinArrayWithLength(size*sizeof(CoinBigIndex),mode) {}
00759   inline CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength & rhs)
00760     : CoinArrayWithLength(rhs) {}
00762   inline CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength * rhs)
00763     : CoinArrayWithLength(rhs) {}
00765   inline CoinBigIndexArrayWithLength& operator=(const CoinBigIndexArrayWithLength & rhs)
00766   { CoinArrayWithLength::operator=(rhs);  return *this;}
00768 };
00770 
00771 class CoinUnsignedIntArrayWithLength : public CoinArrayWithLength {
00772   
00773 public:
00776 
00777   inline int getSize() const 
00778   { return size_/sizeof(unsigned int); }
00780   inline unsigned int * array() const 
00781   { return reinterpret_cast<unsigned int *> ((size_>-2) ? array_ : NULL); }
00783   
00786 
00787   inline void setSize(int value) 
00788   { size_ = value*sizeof(unsigned int); }
00790   
00793 
00794   inline unsigned int * conditionalNew(int sizeWanted)
00795   { return reinterpret_cast<unsigned int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*sizeof(unsigned int)) : -1)); }
00797   
00801   inline CoinUnsignedIntArrayWithLength()
00802   { array_=NULL; size_=-1;}
00804   inline CoinUnsignedIntArrayWithLength(int size)
00805   { array_=new char [size*sizeof(unsigned int)]; size_=-1;}
00810   inline CoinUnsignedIntArrayWithLength(int size, int mode)
00811     : CoinArrayWithLength(size*sizeof(unsigned int),mode) {}
00813   inline CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength & rhs)
00814     : CoinArrayWithLength(rhs) {}
00816   inline CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength * rhs)
00817     : CoinArrayWithLength(rhs) {}
00819   inline CoinUnsignedIntArrayWithLength& operator=(const CoinUnsignedIntArrayWithLength & rhs)
00820   { CoinArrayWithLength::operator=(rhs);  return *this;}
00822 };
00823 #endif

Generated on Mon Jun 8 03:02:38 2009 by  doxygen 1.4.7