/home/coin/SVN-release/Cbc-1.1.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();
00447 
00448 #endif

Generated on Thu May 15 21:59:05 2008 by  doxygen 1.4.7