- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/Cgl-0.53.1/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 bool switchedOn() const
    00473   { return size_!=-1; }
    00475   inline int getCapacity() const
    00476   { return (size_>-2) ? size_ : (-size_)-2; }
    00478   inline void setCapacity()
    00479   { if (size_<=-2) size_ = (-size_)-2; }
    00481   inline const char * array() const
    00482   { return (size_>-2) ? array_ : NULL; }
    00484
    00487
    00488   inline void setSize(int value)
    00489   { size_ = value; }
    00491   inline void switchOff()
    00492   { size_ = -1; }
    00494   void setPersistence(int flag,int currentLength);
    00496   void clear();
    00498   void swap(CoinArrayWithLength & other);
    00500   void extend(int newSize);
    00502
    00505
    00506   char * conditionalNew(long sizeWanted);
    00508   void conditionalDelete();
    00510
    00514   inline CoinArrayWithLength()
    00515   { array_=NULL; size_=-1;}
    00517   inline CoinArrayWithLength(int size)
    00518   { array_=new char [size]; size_=-1;}
    00523   inline CoinArrayWithLength(int size, int mode)
    00524   { array_ = new char [size]; if (mode) memset(array_,0,size);size_=size;}
    00526   CoinArrayWithLength(const CoinArrayWithLength & rhs);
    00528   CoinArrayWithLength(const CoinArrayWithLength * rhs);
    00530   CoinArrayWithLength& operator=(const CoinArrayWithLength & rhs);
    00532   void copy(const CoinArrayWithLength & rhs, int numberBytes=-1);
    00534   void allocate(const CoinArrayWithLength & rhs, int numberBytes);
    00536   inline ~CoinArrayWithLength ()
    00537   { delete [] array_; }
    00538   // was { free(array_); }
    00540
    00541 protected:
    00544
    00545   char * array_;
    00547   int size_;
    00549 };
    00551
    00552 class CoinDoubleArrayWithLength : public CoinArrayWithLength {
    00553
    00554 public:
    00557
    00558   inline int getSize() const
    00559   { return size_/sizeof(double); }
    00561   inline double * array() const
    00562   { return (double *) ((size_>-2) ? array_ : NULL); }
    00564
    00567
    00568   inline void setSize(int value)
    00569   { size_ = value*sizeof(double); }
    00571
    00574
    00575   inline double * conditionalNew(int sizeWanted)
    00576   { return (double *) CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? (long) (((long) sizeWanted)*sizeof(double)) : -1); }
    00578
    00582   inline CoinDoubleArrayWithLength()
    00583   { array_=NULL; size_=-1;}
    00585   inline CoinDoubleArrayWithLength(int size)
    00586   { array_=new char [size*sizeof(double)]; size_=-1;}
    00591   inline CoinDoubleArrayWithLength(int size, int mode)
    00592     : CoinArrayWithLength(size*sizeof(double),mode) {}
    00594   inline CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength & rhs)
    00595     : CoinArrayWithLength(rhs) {}
    00597   inline CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength * rhs)
    00598     : CoinArrayWithLength(rhs) {}
    00600   inline CoinDoubleArrayWithLength& operator=(const CoinDoubleArrayWithLength & rhs)
    00601   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00603 };
    00605
    00606 class CoinIntArrayWithLength : public CoinArrayWithLength {
    00607
    00608 public:
    00611
    00612   inline int getSize() const
    00613   { return size_/sizeof(int); }
    00615   inline int * array() const
    00616   { return (int *) ((size_>-2) ? array_ : NULL); }
    00618
    00621
    00622   inline void setSize(int value)
    00623   { size_ = value*sizeof(int); }
    00625
    00628
    00629   inline int * conditionalNew(int sizeWanted)
    00630   { return (int *) CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? (long) (((long) sizeWanted)*sizeof(int)) : -1); }
    00632
    00636   inline CoinIntArrayWithLength()
    00637   { array_=NULL; size_=-1;}
    00639   inline CoinIntArrayWithLength(int size)
    00640   { array_=new char [size*sizeof(int)]; size_=-1;}
    00645   inline CoinIntArrayWithLength(int size, int mode)
    00646     : CoinArrayWithLength(size*sizeof(int),mode) {}
    00648   inline CoinIntArrayWithLength(const CoinIntArrayWithLength & rhs)
    00649     : CoinArrayWithLength(rhs) {}
    00651   inline CoinIntArrayWithLength(const CoinIntArrayWithLength * rhs)
    00652     : CoinArrayWithLength(rhs) {}
    00654   inline CoinIntArrayWithLength& operator=(const CoinIntArrayWithLength & rhs)
    00655   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00657 };
    00659
    00660 class CoinBigIndexArrayWithLength : public CoinArrayWithLength {
    00661
    00662 public:
    00665
    00666   inline int getSize() const
    00667   { return size_/sizeof(CoinBigIndex); }
    00669   inline CoinBigIndex * array() const
    00670   { return (CoinBigIndex *) ((size_>-2) ? array_ : NULL); }
    00672
    00675
    00676   inline void setSize(int value)
    00677   { size_ = value*sizeof(CoinBigIndex); }
    00679
    00682
    00683   inline CoinBigIndex * conditionalNew(int sizeWanted)
    00684   { return (CoinBigIndex *) CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? (long) (((long) sizeWanted)*sizeof(CoinBigIndex)) : -1); }
    00686
    00690   inline CoinBigIndexArrayWithLength()
    00691   { array_=NULL; size_=-1;}
    00693   inline CoinBigIndexArrayWithLength(int size)
    00694   { array_=new char [size*sizeof(CoinBigIndex)]; size_=-1;}
    00699   inline CoinBigIndexArrayWithLength(int size, int mode)
    00700     : CoinArrayWithLength(size*sizeof(CoinBigIndex),mode) {}
    00702   inline CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength & rhs)
    00703     : CoinArrayWithLength(rhs) {}
    00705   inline CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength * rhs)
    00706     : CoinArrayWithLength(rhs) {}
    00708   inline CoinBigIndexArrayWithLength& operator=(const CoinBigIndexArrayWithLength & rhs)
    00709   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00711 };
    00713
    00714 class CoinUnsignedIntArrayWithLength : public CoinArrayWithLength {
    00715
    00716 public:
    00719
    00720   inline int getSize() const
    00721   { return size_/sizeof(unsigned int); }
    00723   inline unsigned int * array() const
    00724   { return (unsigned int *) ((size_>-2) ? array_ : NULL); }
    00726
    00729
    00730   inline void setSize(int value)
    00731   { size_ = value*sizeof(unsigned int); }
    00733
    00736
    00737   inline unsigned int * conditionalNew(int sizeWanted)
    00738   { return (unsigned int *) CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? (long) (((long) sizeWanted)*sizeof(unsigned int)) : -1); }
    00740
    00744   inline CoinUnsignedIntArrayWithLength()
    00745   { array_=NULL; size_=-1;}
    00747   inline CoinUnsignedIntArrayWithLength(int size)
    00748   { array_=new char [size*sizeof(unsigned int)]; size_=-1;}
    00753   inline CoinUnsignedIntArrayWithLength(int size, int mode)
    00754     : CoinArrayWithLength(size*sizeof(unsigned int),mode) {}
    00756   inline CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength & rhs)
    00757     : CoinArrayWithLength(rhs) {}
    00759   inline CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength * rhs)
    00760     : CoinArrayWithLength(rhs) {}
    00762   inline CoinUnsignedIntArrayWithLength& operator=(const CoinUnsignedIntArrayWithLength & rhs)
    00763   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00765 };
    00766 #endif