Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CoinIndexedVector.hpp

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 
00013 #include "CoinPackedVectorBase.hpp"
00014 #include "CoinSort.hpp"
00015 
00016 #define COIN_INDEXED_TINY_ELEMENT 1.0e-50
00017 
00091 class CoinIndexedVector {
00092    friend void CoinIndexedVectorUnitTest();
00093   
00094 public:
00097 
00098    inline int getNumElements() const { return nElements_; }
00100    inline const int * getIndices() const { return indices_; }
00102    // ** No longer supported virtual const double * getElements() const ;
00104    inline int * getIndices() { return indices_; }
00108    inline double * denseVector() const { return elements_; }
00110   inline void setDenseVector(double * array)
00111   { elements_ = array;};
00114    double & operator[](int i) const; 
00115 
00117  
00118    //-------------------------------------------------------------------
00119    // Set indices and elements
00120    //------------------------------------------------------------------- 
00123 
00124    inline void setNumElements(int value) { nElements_ = value;
00125    if (!nElements_) packedMode_=false;};
00127    void clear();
00129    void empty();
00131    CoinIndexedVector & operator=(const CoinIndexedVector &);
00134    CoinIndexedVector & operator=(const CoinPackedVectorBase & rhs);
00135 
00138   void borrowVector(int size, int numberIndices, int* inds, double* elems);
00139 
00143    void returnVector();
00144 
00149   void setVector(int numberIndices, const int * inds, const double * elems);
00150   
00155    void setVector(int size, int numberIndices, const int * inds, const double * elems);
00156   
00158   void setConstant(int size, const int * inds, double elems);
00159   
00161   void setFull(int size, const double * elems);
00162 
00166    void setElement(int index, double element);
00167 
00169    void insert(int index, double element);
00172    void add(int index, double element);
00176    inline void quickAdd(int index, double element)
00177                {
00178                  if (elements_[index]) {
00179                    element += elements_[index];
00180                    if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
00181                      elements_[index] = element;
00182                    } else {
00183                      elements_[index] = 1.0e-100;
00184                    }
00185                  } else if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) {
00186                    indices_[nElements_++] = index;
00187                    elements_[index] = element;
00188                  }
00189                };
00192    inline void zero(int index)
00193                {
00194                  if (elements_[index]) 
00195                    elements_[index] = 1.0e-100;
00196                };
00199    int clean(double tolerance);
00201    int cleanAndPack(double tolerance);
00203    int cleanAndPackSafe(double tolerance);
00205   inline void setPacked()
00206   { packedMode_ = true;};
00208    void checkClear();
00210    void checkClean();
00212    int scan();
00216    int scan(int start, int end);
00219    int scan(double tolerance);
00223    int scan(int start, int end, double tolerance);
00225    int scanAndPack();
00226    int scanAndPack(int start, int end);
00227    int scanAndPack(double tolerance);
00228    int scanAndPack(int start, int end, double tolerance);
00230    void createPacked(int number, const int * indices, 
00231                     const double * elements);
00233    void expand();
00235    void append(const CoinPackedVectorBase & caboose);
00237    void append(const CoinIndexedVector & caboose);
00238 
00240    void swap(int i, int j); 
00241 
00243    void truncate(int newSize); 
00245 
00247 
00248    void operator+=(double value);
00250    void operator-=(double value);
00252    void operator*=(double value);
00254    void operator/=(double value);
00256 
00261    bool operator==(const CoinPackedVectorBase & rhs) const;
00263    bool operator!=(const CoinPackedVectorBase & rhs) const;
00266    bool operator==(const CoinIndexedVector & rhs) const;
00268    bool operator!=(const CoinIndexedVector & rhs) const;
00270 
00273 
00274    int getMaxIndex() const;
00276    int getMinIndex() const;
00278 
00279 
00283    void sort()
00284    { std::sort(indices_,indices_+nElements_); }
00285 
00286    void sortIncrIndex()
00287    { std::sort(indices_,indices_+nElements_); }
00288 
00289    void sortDecrIndex();
00290   
00291    void sortIncrElement();
00292 
00293    void sortDecrElement();
00294 
00296 
00297   //#############################################################################
00298 
00310 
00311 CoinIndexedVector operator+(
00312                            const CoinIndexedVector& op2);
00313 
00315 CoinIndexedVector operator-(
00316                            const CoinIndexedVector& op2);
00317 
00319 CoinIndexedVector operator*(
00320                            const CoinIndexedVector& op2);
00321 
00323 CoinIndexedVector operator/(
00324                            const CoinIndexedVector& op2);
00326 
00333    void reserve(int n);
00337    int capacity() const { return capacity_; }
00339    inline void setPackedMode(bool yesNo)
00340    { packedMode_=yesNo;};
00342    inline bool packedMode() const
00343    { return packedMode_;};
00345 
00349    CoinIndexedVector();
00351   CoinIndexedVector(int size, const int * inds, const double * elems);
00353   CoinIndexedVector(int size, const int * inds, double element);
00356   CoinIndexedVector(int size, const double * elements);
00358    CoinIndexedVector(const CoinIndexedVector &);
00360    CoinIndexedVector(const CoinIndexedVector *);
00362    CoinIndexedVector(const CoinPackedVectorBase & rhs);
00364    ~CoinIndexedVector ();
00366     
00367 private:
00370 
00371    void gutsOfSetVector(int size,
00372                         const int * inds, const double * elems);
00373    void gutsOfSetVector(int size, int numberIndices,
00374                         const int * inds, const double * elems);
00376    void gutsOfSetConstant(int size,
00377                           const int * inds, double value);
00379 
00380 private:
00383 
00384    int * indices_;
00386    double * elements_;
00388    int nElements_;
00390    int capacity_;
00392    int offset_;
00394    bool packedMode_;
00396 };
00397 
00398 //#############################################################################
00404 void
00405 CoinIndexedVectorUnitTest();
00406 
00407 #endif

Generated on Wed Dec 3 14:34:18 2003 for Coin by doxygen 1.3.5