/home/coin/SVN-release/CoinAll-1.1.0/CoinUtils/src/CoinPackedVectorBase.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #ifndef CoinPackedVectorBase_H
00004 #define CoinPackedVectorBase_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 <set>
00012 #include <map>
00013 #include "CoinError.hpp"
00014 #include "CoinFloatEqual.hpp"
00015 
00016 class CoinPackedVector;
00017 
00025 class CoinPackedVectorBase  {
00026   
00027 public:
00030 
00031    virtual int getNumElements() const = 0;
00033    virtual const int * getIndices() const = 0;
00035    virtual const double * getElements() const = 0;
00037 
00053    void setTestForDuplicateIndex(bool test) const;
00062    void setTestForDuplicateIndexWhenTrue(bool test) const;
00065    bool testForDuplicateIndex() const { return testForDuplicateIndex_; }
00067    inline void setTestsOff() const
00068    { testForDuplicateIndex_=false; testedDuplicateIndex_=false;}
00070 
00078    double * denseVector(int denseSize) const;
00086    double operator[](int i) const; 
00088 
00091 
00092    int getMaxIndex() const;
00094    int getMinIndex() const;
00095 
00097    void duplicateIndex(const char* methodName = NULL,
00098                        const char * className = NULL) const;
00099 
00102    bool isExistingIndex(int i) const;
00103    
00106    int findIndex(int i) const;
00107  
00109   
00114    bool operator==(const CoinPackedVectorBase & rhs) const;
00116    bool operator!=(const CoinPackedVectorBase & rhs) const;
00117 
00118 #if 0
00119    // LL: This should be implemented eventually. It is useful to have.
00123    int lexCompare(const CoinPackedVectorBase& rhs);
00124 #endif
00125   
00132    int compare(const CoinPackedVectorBase& rhs) const;
00133 
00141    template <class FloatEqual> bool
00142    isEquivalent(const CoinPackedVectorBase& rhs, const FloatEqual& eq) const
00143    {
00144       if (getNumElements() != rhs.getNumElements())
00145          return false;
00146 
00147       duplicateIndex("equivalent", "CoinPackedVector");
00148       rhs.duplicateIndex("equivalent", "CoinPackedVector");
00149 
00150       std::map<int,double> mv;
00151       const int * inds = getIndices();
00152       const double * elems = getElements();
00153       int i;
00154       for ( i = getNumElements() - 1; i >= 0; --i) {
00155          mv.insert(std::make_pair(inds[i], elems[i]));
00156       }
00157 
00158       std::map<int,double> mvRhs;
00159       inds = rhs.getIndices();
00160       elems = rhs.getElements();
00161       for ( i = getNumElements() - 1; i >= 0; --i) {
00162          mvRhs.insert(std::make_pair(inds[i], elems[i]));
00163       }
00164 
00165       std::map<int,double>::const_iterator mvI = mv.begin();
00166       std::map<int,double>::const_iterator mvIlast = mv.end();
00167       std::map<int,double>::const_iterator mvIrhs = mvRhs.begin();
00168       while (mvI != mvIlast) {
00169          if (mvI->first != mvIrhs->first || ! eq(mvI->second, mvIrhs->second))
00170             return false;
00171          ++mvI;
00172          ++mvIrhs;
00173       }
00174       return true;
00175    }
00176    bool isEquivalent(const CoinPackedVectorBase& rhs) const
00177    {
00178       return isEquivalent(rhs,  CoinRelFltEq());
00179    }
00181 
00182 
00185 
00186    double dotProduct(const double* dense) const;
00187 
00189    double oneNorm() const;
00190 
00192    double normSquare() const;
00193 
00195    inline double twoNorm() const { return sqrt(normSquare()); }
00196 
00198    double infNorm() const;
00199 
00201    double sum() const;
00203 
00204 protected:
00205 
00211    CoinPackedVectorBase();
00212 
00213 public:
00215    virtual ~CoinPackedVectorBase();
00217 
00218 private:
00227    CoinPackedVectorBase(const CoinPackedVectorBase&);
00230    CoinPackedVectorBase& operator=(const CoinPackedVectorBase&);
00232    
00233 protected:
00234     
00237 
00238    void findMaxMinIndices() const;
00239 
00241    std::set<int> * indexSet(const char* methodName = NULL,
00242                             const char * className = NULL) const;
00243 
00245    void clearIndexSet() const;
00246    void clearBase() const;
00247    void copyMaxMinIndex(const CoinPackedVectorBase & x) const {
00248       maxIndex_ = x.maxIndex_;
00249       minIndex_ = x.minIndex_;
00250    }
00252     
00253 private:
00256 
00257    mutable int maxIndex_;
00259    mutable int minIndex_;
00263    mutable std::set<int> * indexSetPtr_;
00266    mutable bool testForDuplicateIndex_;
00269    mutable bool testedDuplicateIndex_;
00271 };
00272 
00273 #endif

Generated on Sun Nov 14 14:06:32 2010 for Coin-All by  doxygen 1.4.7