CoinPackedVectorBase.hpp
Go to the documentation of this file.
1 /* $Id: CoinPackedVectorBase.hpp 1215 2009-11-05 11:03:04Z forrest $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CoinPackedVectorBase_H
5 #define CoinPackedVectorBase_H
6 
7 #if defined(_MSC_VER)
8 // Turn off compiler warning about long names
9 # pragma warning(disable:4786)
10 #endif
11 
12 #include <set>
13 #include <map>
14 #include "CoinError.hpp"
15 #include "CoinFloatEqual.hpp"
16 
17 class CoinPackedVector;
18 
27 
28 public:
31 
32  virtual int getNumElements() const = 0;
34  virtual const int * getIndices() const = 0;
36  virtual const double * getElements() const = 0;
38 
54  void setTestForDuplicateIndex(bool test) const;
63  void setTestForDuplicateIndexWhenTrue(bool test) const;
68  inline void setTestsOff() const
71 
79  double * denseVector(int denseSize) const;
87  double operator[](int i) const;
89 
92 
93  int getMaxIndex() const;
95  int getMinIndex() const;
96 
98  void duplicateIndex(const char* methodName = NULL,
99  const char * className = NULL) const;
100 
103  bool isExistingIndex(int i) const;
104 
107  int findIndex(int i) const;
108 
110 
115  bool operator==(const CoinPackedVectorBase & rhs) const;
117  bool operator!=(const CoinPackedVectorBase & rhs) const;
118 
119 #if 0
120  // LL: This should be implemented eventually. It is useful to have.
124  int lexCompare(const CoinPackedVectorBase& rhs);
125 #endif
126 
133  int compare(const CoinPackedVectorBase& rhs) const;
134 
142  template <class FloatEqual> bool
143  isEquivalent(const CoinPackedVectorBase& rhs, const FloatEqual& eq) const
144  {
145  if (getNumElements() != rhs.getNumElements())
146  return false;
147 
148  duplicateIndex("equivalent", "CoinPackedVector");
149  rhs.duplicateIndex("equivalent", "CoinPackedVector");
150 
151  std::map<int,double> mv;
152  const int * inds = getIndices();
153  const double * elems = getElements();
154  int i;
155  for ( i = getNumElements() - 1; i >= 0; --i) {
156  mv.insert(std::make_pair(inds[i], elems[i]));
157  }
158 
159  std::map<int,double> mvRhs;
160  inds = rhs.getIndices();
161  elems = rhs.getElements();
162  for ( i = getNumElements() - 1; i >= 0; --i) {
163  mvRhs.insert(std::make_pair(inds[i], elems[i]));
164  }
165 
166  std::map<int,double>::const_iterator mvI = mv.begin();
167  std::map<int,double>::const_iterator mvIlast = mv.end();
168  std::map<int,double>::const_iterator mvIrhs = mvRhs.begin();
169  while (mvI != mvIlast) {
170  if (mvI->first != mvIrhs->first || ! eq(mvI->second, mvIrhs->second))
171  return false;
172  ++mvI;
173  ++mvIrhs;
174  }
175  return true;
176  }
177  bool isEquivalent(const CoinPackedVectorBase& rhs) const
178  {
179  return isEquivalent(rhs, CoinRelFltEq());
180  }
182 
183 
186 
187  double dotProduct(const double* dense) const;
188 
190  double oneNorm() const;
191 
193  double normSquare() const;
194 
196  inline double twoNorm() const { return sqrt(normSquare()); }
197 
199  double infNorm() const;
200 
202  double sum() const;
204 
205 protected:
206 
213 
214 public:
216  virtual ~CoinPackedVectorBase();
218 
219 private:
233 
234 protected:
235 
238 
239  void findMaxMinIndices() const;
240 
242  std::set<int> * indexSet(const char* methodName = NULL,
243  const char * className = NULL) const;
244 
246  void clearIndexSet() const;
247  void clearBase() const;
248  void copyMaxMinIndex(const CoinPackedVectorBase & x) const {
249  maxIndex_ = x.maxIndex_;
250  minIndex_ = x.minIndex_;
251  }
253 
254 private:
257 
258  mutable int maxIndex_;
260  mutable int minIndex_;
264  mutable std::set<int> * indexSetPtr_;
270  mutable bool testedDuplicateIndex_;
272 };
273 
274 #endif