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

- File List
- File Members

# /home/coin/SVN-release/Vol-1.2.0/CoinUtils/src/CoinIndexedVector.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinIndexedVector.hpp 1235 2009-12-04 14:28:29Z forrest $ */
    00002 // Copyright (C) 2000, International Business Machines
    00003 // Corporation and others.  All Rights Reserved.
    00004 #ifndef CoinIndexedVector_H
    00005 #define CoinIndexedVector_H
    00006
    00007 #if defined(_MSC_VER)
    00008 // Turn off compiler warning about long names
    00009 #  pragma warning(disable:4786)
    00010 #endif
    00011
    00012 #include <map>
    00013 #ifndef CLP_NO_VECTOR
    00014 #include "CoinPackedVectorBase.hpp"
    00015 #endif
    00016 #include "CoinSort.hpp"
    00017 #include "CoinHelperFunctions.hpp"
    00018 #include <cassert>
    00019
    00020 #ifndef COIN_FLOAT
    00021 #define COIN_INDEXED_TINY_ELEMENT 1.0e-50
    00022 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-100
    00023 #else
    00024 #define COIN_INDEXED_TINY_ELEMENT 1.0e-35
    00025 #define COIN_INDEXED_REALLY_TINY_ELEMENT 1.0e-39
    00026 #endif
    00027
    00101 class CoinIndexedVector {
    00102    friend void CoinIndexedVectorUnitTest();
    00103
    00104 public:
    00107
    00108    inline int getNumElements() const { return nElements_; }
    00110    inline const int * getIndices() const { return indices_; }
    00112    // ** No longer supported virtual const double * getElements() const ;
    00114    inline int * getIndices() { return indices_; }
    00118    inline double * denseVector() const { return elements_; }
    00120   inline void setDenseVector(double * array)
    00121   { elements_ = array;}
    00123   inline void setIndexVector(int * array)
    00124   { indices_ = array;}
    00127    double & operator[](int i) const;
    00128
    00130
    00131    //-------------------------------------------------------------------
    00132    // Set indices and elements
    00133    //-------------------------------------------------------------------
    00136
    00137    inline void setNumElements(int value) { nElements_ = value;
    00138    if (!nElements_) packedMode_=false;}
    00140    void clear();
    00142    void empty();
    00144    CoinIndexedVector & operator=(const CoinIndexedVector &);
    00145 #ifndef CLP_NO_VECTOR
    00146
    00148    CoinIndexedVector & operator=(const CoinPackedVectorBase & rhs);
    00149 #endif
    00150
    00153    void copy(const CoinIndexedVector & rhs, double multiplier=1.0);
    00154
    00157   void borrowVector(int size, int numberIndices, int* inds, double* elems);
    00158
    00162    void returnVector();
    00163
    00168   void setVector(int numberIndices, const int * inds, const double * elems);
    00169
    00174    void setVector(int size, int numberIndices, const int * inds, const double * elems);
    00175
    00177   void setConstant(int size, const int * inds, double elems);
    00178
    00180   void setFull(int size, const double * elems);
    00181
    00185    void setElement(int index, double element);
    00186
    00188    void insert(int index, double element);
    00191    void add(int index, double element);
    00195    inline void quickAdd(int index, double element)
    00196                {
    00197                  if (elements_[index]) {
    00198                    element += elements_[index];
    00199                    if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
    00200                      elements_[index] = element;
    00201                    } else {
    00202                      elements_[index] = 1.0e-100;
    00203                    }
    00204                  } else if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
    00205                    indices_[nElements_++] = index;
    00206                    assert (nElements_<=capacity_);
    00207                    elements_[index] = element;
    00208                  }
    00209                }
    00212    inline void zero(int index)
    00213                {
    00214                  if (elements_[index])
    00215                    elements_[index] = 1.0e-100;
    00216                }
    00219    int clean(double tolerance);
    00221    int cleanAndPack(double tolerance);
    00223    int cleanAndPackSafe(double tolerance);
    00225   inline void setPacked()
    00226   { packedMode_ = true;}
    00228    void checkClear();
    00230    void checkClean();
    00232    int scan();
    00236    int scan(int start, int end);
    00239    int scan(double tolerance);
    00243    int scan(int start, int end, double tolerance);
    00245    int scanAndPack();
    00246    int scanAndPack(int start, int end);
    00247    int scanAndPack(double tolerance);
    00248    int scanAndPack(int start, int end, double tolerance);
    00250    void createPacked(int number, const int * indices,
    00251                     const double * elements);
    00253    void expand();
    00254 #ifndef CLP_NO_VECTOR
    00256    void append(const CoinPackedVectorBase & caboose);
    00257 #endif
    00259    void append(const CoinIndexedVector & caboose);
    00260
    00262    void swap(int i, int j);
    00263
    00265    void truncate(int newSize);
    00267    void print() const;
    00269
    00271
    00272    void operator+=(double value);
    00274    void operator-=(double value);
    00276    void operator*=(double value);
    00278    void operator/=(double value);
    00280
    00283 #ifndef CLP_NO_VECTOR
    00284
    00286    bool operator==(const CoinPackedVectorBase & rhs) const;
    00288    bool operator!=(const CoinPackedVectorBase & rhs) const;
    00289 #endif
    00290
    00292    bool operator==(const CoinIndexedVector & rhs) const;
    00294    bool operator!=(const CoinIndexedVector & rhs) const;
    00296
    00299
    00300    int getMaxIndex() const;
    00302    int getMinIndex() const;
    00304
    00305
    00309    void sort()
    00310    { std::sort(indices_,indices_+nElements_); }
    00311
    00312    void sortIncrIndex()
    00313    { std::sort(indices_,indices_+nElements_); }
    00314
    00315    void sortDecrIndex();
    00316
    00317    void sortIncrElement();
    00318
    00319    void sortDecrElement();
    00320
    00322
    00323   //#############################################################################
    00324
    00336
    00337 CoinIndexedVector operator+(
    00338                            const CoinIndexedVector& op2);
    00339
    00341 CoinIndexedVector operator-(
    00342                            const CoinIndexedVector& op2);
    00343
    00345 CoinIndexedVector operator*(
    00346                            const CoinIndexedVector& op2);
    00347
    00349 CoinIndexedVector operator/(
    00350                            const CoinIndexedVector& op2);
    00352 void operator+=(const CoinIndexedVector& op2);
    00353
    00355 void operator-=( const CoinIndexedVector& op2);
    00356
    00358 void operator*=(const CoinIndexedVector& op2);
    00359
    00361 void operator/=(const CoinIndexedVector& op2);
    00363
    00370    void reserve(int n);
    00374    int capacity() const { return capacity_; }
    00376    inline void setPackedMode(bool yesNo)
    00377    { packedMode_=yesNo;}
    00379    inline bool packedMode() const
    00380    { return packedMode_;}
    00382
    00386    CoinIndexedVector();
    00388   CoinIndexedVector(int size, const int * inds, const double * elems);
    00390   CoinIndexedVector(int size, const int * inds, double element);
    00393   CoinIndexedVector(int size, const double * elements);
    00395   CoinIndexedVector(int size);
    00397    CoinIndexedVector(const CoinIndexedVector &);
    00399    CoinIndexedVector(const CoinIndexedVector *);
    00400 #ifndef CLP_NO_VECTOR
    00401
    00402    CoinIndexedVector(const CoinPackedVectorBase & rhs);
    00403 #endif
    00404
    00405    ~CoinIndexedVector ();
    00407
    00408 private:
    00411
    00412    void gutsOfSetVector(int size,
    00413                         const int * inds, const double * elems);
    00414    void gutsOfSetVector(int size, int numberIndices,
    00415                         const int * inds, const double * elems);
    00416    void gutsOfSetPackedVector(int size, int numberIndices,
    00417                         const int * inds, const double * elems);
    00419    void gutsOfSetConstant(int size,
    00420                           const int * inds, double value);
    00422
    00423 private:
    00426
    00427    int * indices_;
    00429    double * elements_;
    00431    int nElements_;
    00433    int capacity_;
    00435    int offset_;
    00437    bool packedMode_;
    00439 };
    00440
    00441 //#############################################################################
    00447 void
    00448 CoinIndexedVectorUnitTest();
    00465 class CoinArrayWithLength {
    00466
    00467 public:
    00470
    00471   inline int getSize() const
    00472   { return size_; }
    00474   inline int rawSize() const
    00475   { return size_; }
    00477   inline bool switchedOn() const
    00478   { return size_!=-1; }
    00480   inline int getCapacity() const
    00481   { return (size_>-2) ? size_ : (-size_)-2; }
    00483   inline void setCapacity()
    00484   { if (size_<=-2) size_ = (-size_)-2; }
    00486   inline const char * array() const
    00487   { return (size_>-2) ? array_ : NULL; }
    00489
    00492
    00493   inline void setSize(int value)
    00494   { size_ = value; }
    00496   inline void switchOff()
    00497   { size_ = -1; }
    00499   void setPersistence(int flag,int currentLength);
    00501   void clear();
    00503   void swap(CoinArrayWithLength & other);
    00505   void extend(int newSize);
    00507
    00510
    00511   char * conditionalNew(long sizeWanted);
    00513   void conditionalDelete();
    00515
    00519   inline CoinArrayWithLength()
    00520   { array_=NULL; size_=-1;}
    00522   inline CoinArrayWithLength(int size)
    00523   { array_=new char [size]; size_=-1;}
    00528   inline CoinArrayWithLength(int size, int mode)
    00529   { array_ = new char [size]; if (mode) memset(array_,0,size);size_=size;}
    00531   CoinArrayWithLength(const CoinArrayWithLength & rhs);
    00533   CoinArrayWithLength(const CoinArrayWithLength * rhs);
    00535   CoinArrayWithLength& operator=(const CoinArrayWithLength & rhs);
    00537   void copy(const CoinArrayWithLength & rhs, int numberBytes=-1);
    00539   void allocate(const CoinArrayWithLength & rhs, int numberBytes);
    00541   inline ~CoinArrayWithLength ()
    00542   { delete [] array_; }
    00543   // was { free(array_); }
    00545
    00546 protected:
    00549
    00550   char * array_;
    00552   int size_;
    00554 };
    00556
    00557 class CoinDoubleArrayWithLength : public CoinArrayWithLength {
    00558
    00559 public:
    00562
    00563   inline int getSize() const
    00564   { return size_/CoinSizeofAsInt(double); }
    00566   inline double * array() const
    00567   { return reinterpret_cast<double *> ((size_>-2) ? array_ : NULL); }
    00569
    00572
    00573   inline void setSize(int value)
    00574   { size_ = value*CoinSizeofAsInt(double); }
    00576
    00579
    00580   inline double * conditionalNew(int sizeWanted)
    00581   { return reinterpret_cast<double *> ( CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> ((sizeWanted)*CoinSizeofAsInt(double)) : -1)); }
    00583
    00587   inline CoinDoubleArrayWithLength()
    00588   { array_=NULL; size_=-1;}
    00590   inline CoinDoubleArrayWithLength(int size)
    00591   { array_=new char [size*CoinSizeofAsInt(double)]; size_=-1;}
    00596   inline CoinDoubleArrayWithLength(int size, int mode)
    00597     : CoinArrayWithLength(size*CoinSizeofAsInt(double),mode) {}
    00599   inline CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength & rhs)
    00600     : CoinArrayWithLength(rhs) {}
    00602   inline CoinDoubleArrayWithLength(const CoinDoubleArrayWithLength * rhs)
    00603     : CoinArrayWithLength(rhs) {}
    00605   inline CoinDoubleArrayWithLength& operator=(const CoinDoubleArrayWithLength & rhs)
    00606   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00608 };
    00610
    00611 class CoinFactorizationDoubleArrayWithLength : public CoinArrayWithLength {
    00612
    00613 public:
    00616
    00617   inline int getSize() const
    00618   { return size_/CoinSizeofAsInt(CoinFactorizationDouble); }
    00620   inline CoinFactorizationDouble * array() const
    00621   { return reinterpret_cast<CoinFactorizationDouble *> ((size_>-2) ? array_ : NULL); }
    00623
    00626
    00627   inline void setSize(int value)
    00628   { size_ = value*CoinSizeofAsInt(CoinFactorizationDouble); }
    00630
    00633
    00634   inline CoinFactorizationDouble * conditionalNew(int sizeWanted)
    00635   { return reinterpret_cast<CoinFactorizationDouble *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(CoinFactorizationDouble)) : -1)); }
    00637
    00641   inline CoinFactorizationDoubleArrayWithLength()
    00642   { array_=NULL; size_=-1;}
    00644   inline CoinFactorizationDoubleArrayWithLength(int size)
    00645   { array_=new char [size*CoinSizeofAsInt(CoinFactorizationDouble)]; size_=-1;}
    00650   inline CoinFactorizationDoubleArrayWithLength(int size, int mode)
    00651     : CoinArrayWithLength(size*CoinSizeofAsInt(CoinFactorizationDouble),mode) {}
    00653   inline CoinFactorizationDoubleArrayWithLength(const CoinFactorizationDoubleArrayWithLength & rhs)
    00654     : CoinArrayWithLength(rhs) {}
    00656   inline CoinFactorizationDoubleArrayWithLength(const CoinFactorizationDoubleArrayWithLength * rhs)
    00657     : CoinArrayWithLength(rhs) {}
    00659   inline CoinFactorizationDoubleArrayWithLength& operator=(const CoinFactorizationDoubleArrayWithLength & rhs)
    00660   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00662 };
    00664
    00665 class CoinIntArrayWithLength : public CoinArrayWithLength {
    00666
    00667 public:
    00670
    00671   inline int getSize() const
    00672   { return size_/CoinSizeofAsInt(int); }
    00674   inline int * array() const
    00675   { return reinterpret_cast<int *> ((size_>-2) ? array_ : NULL); }
    00677
    00680
    00681   inline void setSize(int value)
    00682   { size_ = value*CoinSizeofAsInt(int); }
    00684
    00687
    00688   inline int * conditionalNew(int sizeWanted)
    00689   { return reinterpret_cast<int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(int)) : -1)); }
    00691
    00695   inline CoinIntArrayWithLength()
    00696   { array_=NULL; size_=-1;}
    00698   inline CoinIntArrayWithLength(int size)
    00699   { array_=new char [size*CoinSizeofAsInt(int)]; size_=-1;}
    00704   inline CoinIntArrayWithLength(int size, int mode)
    00705     : CoinArrayWithLength(size*CoinSizeofAsInt(int),mode) {}
    00707   inline CoinIntArrayWithLength(const CoinIntArrayWithLength & rhs)
    00708     : CoinArrayWithLength(rhs) {}
    00710   inline CoinIntArrayWithLength(const CoinIntArrayWithLength * rhs)
    00711     : CoinArrayWithLength(rhs) {}
    00713   inline CoinIntArrayWithLength& operator=(const CoinIntArrayWithLength & rhs)
    00714   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00716 };
    00718
    00719 class CoinBigIndexArrayWithLength : public CoinArrayWithLength {
    00720
    00721 public:
    00724
    00725   inline int getSize() const
    00726   { return size_/CoinSizeofAsInt(CoinBigIndex); }
    00728   inline CoinBigIndex * array() const
    00729   { return reinterpret_cast<CoinBigIndex *> ((size_>-2) ? array_ : NULL); }
    00731
    00734
    00735   inline void setSize(int value)
    00736   { size_ = value*CoinSizeofAsInt(CoinBigIndex); }
    00738
    00741
    00742   inline CoinBigIndex * conditionalNew(int sizeWanted)
    00743   { return reinterpret_cast<CoinBigIndex *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(CoinBigIndex)) : -1)); }
    00745
    00749   inline CoinBigIndexArrayWithLength()
    00750   { array_=NULL; size_=-1;}
    00752   inline CoinBigIndexArrayWithLength(int size)
    00753   { array_=new char [size*CoinSizeofAsInt(CoinBigIndex)]; size_=-1;}
    00758   inline CoinBigIndexArrayWithLength(int size, int mode)
    00759     : CoinArrayWithLength(size*CoinSizeofAsInt(CoinBigIndex),mode) {}
    00761   inline CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength & rhs)
    00762     : CoinArrayWithLength(rhs) {}
    00764   inline CoinBigIndexArrayWithLength(const CoinBigIndexArrayWithLength * rhs)
    00765     : CoinArrayWithLength(rhs) {}
    00767   inline CoinBigIndexArrayWithLength& operator=(const CoinBigIndexArrayWithLength & rhs)
    00768   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00770 };
    00772
    00773 class CoinUnsignedIntArrayWithLength : public CoinArrayWithLength {
    00774
    00775 public:
    00778
    00779   inline int getSize() const
    00780   { return size_/CoinSizeofAsInt(unsigned int); }
    00782   inline unsigned int * array() const
    00783   { return reinterpret_cast<unsigned int *> ((size_>-2) ? array_ : NULL); }
    00785
    00788
    00789   inline void setSize(int value)
    00790   { size_ = value*CoinSizeofAsInt(unsigned int); }
    00792
    00795
    00796   inline unsigned int * conditionalNew(int sizeWanted)
    00797   { return reinterpret_cast<unsigned int *> (CoinArrayWithLength::conditionalNew(sizeWanted>=0 ? static_cast<long> (( sizeWanted)*CoinSizeofAsInt(unsigned int)) : -1)); }
    00799
    00803   inline CoinUnsignedIntArrayWithLength()
    00804   { array_=NULL; size_=-1;}
    00806   inline CoinUnsignedIntArrayWithLength(int size)
    00807   { array_=new char [size*CoinSizeofAsInt(unsigned int)]; size_=-1;}
    00812   inline CoinUnsignedIntArrayWithLength(int size, int mode)
    00813     : CoinArrayWithLength(size*CoinSizeofAsInt(unsigned int),mode) {}
    00815   inline CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength & rhs)
    00816     : CoinArrayWithLength(rhs) {}
    00818   inline CoinUnsignedIntArrayWithLength(const CoinUnsignedIntArrayWithLength * rhs)
    00819     : CoinArrayWithLength(rhs) {}
    00821   inline CoinUnsignedIntArrayWithLength& operator=(const CoinUnsignedIntArrayWithLength & rhs)
    00822   { CoinArrayWithLength::operator=(rhs);  return *this;}
    00824 };
    00825 #endif