CoinUtils trunk
|
00001 /* $Id$ */ 00002 // Copyright (C) 2000, International Business Machines 00003 // Corporation and others. All Rights Reserved. 00004 // This code is licensed under the terms of the Eclipse Public License (EPL). 00005 00006 #ifndef CoinPackedVectorBase_H 00007 #define CoinPackedVectorBase_H 00008 00009 #include <set> 00010 #include <map> 00011 #include "CoinPragma.hpp" 00012 #include "CoinError.hpp" 00013 00014 class CoinPackedVector; 00015 00023 class CoinPackedVectorBase { 00024 00025 public: 00028 00029 virtual int getNumElements() const = 0; 00031 virtual const int * getIndices() const = 0; 00033 virtual const double * getElements() const = 0; 00035 00051 void setTestForDuplicateIndex(bool test) const; 00060 void setTestForDuplicateIndexWhenTrue(bool test) const; 00063 bool testForDuplicateIndex() const { return testForDuplicateIndex_; } 00065 inline void setTestsOff() const 00066 { testForDuplicateIndex_=false; testedDuplicateIndex_=false;} 00068 00076 double * denseVector(int denseSize) const; 00084 double operator[](int i) const; 00086 00089 00090 int getMaxIndex() const; 00092 int getMinIndex() const; 00093 00095 void duplicateIndex(const char* methodName = NULL, 00096 const char * className = NULL) const; 00097 00100 bool isExistingIndex(int i) const; 00101 00104 int findIndex(int i) const; 00105 00107 00112 bool operator==(const CoinPackedVectorBase & rhs) const; 00114 bool operator!=(const CoinPackedVectorBase & rhs) const; 00115 00116 #if 0 00117 // LL: This should be implemented eventually. It is useful to have. 00121 int lexCompare(const CoinPackedVectorBase& rhs); 00122 #endif 00123 00130 int compare(const CoinPackedVectorBase& rhs) const; 00131 00139 template <class FloatEqual> bool 00140 isEquivalent(const CoinPackedVectorBase& rhs, const FloatEqual& eq) const 00141 { 00142 if (getNumElements() != rhs.getNumElements()) 00143 return false; 00144 00145 duplicateIndex("equivalent", "CoinPackedVector"); 00146 rhs.duplicateIndex("equivalent", "CoinPackedVector"); 00147 00148 std::map<int,double> mv; 00149 const int * inds = getIndices(); 00150 const double * elems = getElements(); 00151 int i; 00152 for ( i = getNumElements() - 1; i >= 0; --i) { 00153 mv.insert(std::make_pair(inds[i], elems[i])); 00154 } 00155 00156 std::map<int,double> mvRhs; 00157 inds = rhs.getIndices(); 00158 elems = rhs.getElements(); 00159 for ( i = getNumElements() - 1; i >= 0; --i) { 00160 mvRhs.insert(std::make_pair(inds[i], elems[i])); 00161 } 00162 00163 std::map<int,double>::const_iterator mvI = mv.begin(); 00164 std::map<int,double>::const_iterator mvIlast = mv.end(); 00165 std::map<int,double>::const_iterator mvIrhs = mvRhs.begin(); 00166 while (mvI != mvIlast) { 00167 if (mvI->first != mvIrhs->first || ! eq(mvI->second, mvIrhs->second)) 00168 return false; 00169 ++mvI; 00170 ++mvIrhs; 00171 } 00172 return true; 00173 } 00174 00175 bool isEquivalent(const CoinPackedVectorBase& rhs) const; 00177 00178 00181 00182 double dotProduct(const double* dense) const; 00183 00185 double oneNorm() const; 00186 00188 double normSquare() const; 00189 00191 double twoNorm() const; 00192 00194 double infNorm() const; 00195 00197 double sum() const; 00199 00200 protected: 00201 00207 CoinPackedVectorBase(); 00208 00209 public: 00211 virtual ~CoinPackedVectorBase(); 00213 00214 private: 00223 CoinPackedVectorBase(const CoinPackedVectorBase&); 00226 CoinPackedVectorBase& operator=(const CoinPackedVectorBase&); 00228 00229 protected: 00230 00233 00234 void findMaxMinIndices() const; 00235 00237 std::set<int> * indexSet(const char* methodName = NULL, 00238 const char * className = NULL) const; 00239 00241 void clearIndexSet() const; 00242 void clearBase() const; 00243 void copyMaxMinIndex(const CoinPackedVectorBase & x) const { 00244 maxIndex_ = x.maxIndex_; 00245 minIndex_ = x.minIndex_; 00246 } 00248 00249 private: 00252 00253 mutable int maxIndex_; 00255 mutable int minIndex_; 00259 mutable std::set<int> * indexSetPtr_; 00262 mutable bool testForDuplicateIndex_; 00265 mutable bool testedDuplicateIndex_; 00267 }; 00268 00269 #endif