CoinPackedVectorBase.hpp 9.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
/* $Id$ */
// Copyright (C) 2000, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CoinPackedVectorBase_H
#define CoinPackedVectorBase_H

#include <set>
#include <map>
#include "CoinPragma.hpp"
#include "CoinError.hpp"

class CoinPackedVector;

/** Abstract base class for various sparse vectors.

    Since this class is abstract, no object of this type can be created. The
    sole purpose of this class is to provide access to a <em>constant</em>
    packed vector. All members of this class are const methods, they can't
    change the object. */

class CoinPackedVectorBase {

public:
  /**@name Virtual methods that the derived classes must provide */
  //@{
  /// Get length of indices and elements vectors
  virtual int getNumElements() const = 0;
  /// Get indices of elements
  virtual const int *getIndices() const = 0;
  /// Get element values
  virtual const double *getElements() const = 0;
  //@}

  /**@name Methods related to whether duplicate-index checking is performed.

       If the checking for duplicate indices is turned off, then
       some CoinPackedVector methods may not work correctly if there
       are duplicate indices.
       Turning off the checking for duplicate indices may result in
       better run time performance.
      */
  //@{
  /** \brief Set to the argument value whether to test for duplicate indices
	      in the vector whenever they can occur.
       
       Calling this method with \p test set to true will trigger an immediate
       check for duplicate indices.
   */
  void setTestForDuplicateIndex(bool test) const;
  /** \brief Set to the argument value whether to test for duplicate indices
	      in the vector whenever they can occur BUT we know that right
	      now the vector has no duplicate indices.

       Calling this method with \p test set to true will <em>not</em> trigger
       an immediate check for duplicate indices; instead, it's assumed that
       the result of the test will be true.
   */
  void setTestForDuplicateIndexWhenTrue(bool test) const;
  /** Returns true if the vector should be tested for duplicate indices when
       they can occur. */
  bool testForDuplicateIndex() const { return testForDuplicateIndex_; }
  /// Just sets test stuff false without a try etc
  inline void setTestsOff() const
  {
    testForDuplicateIndex_ = false;
    testedDuplicateIndex_ = false;
  }
  //@}

  /**@name Methods for getting info on the packed vector as a full vector */
  //@{
  /** Get the vector as a dense vector. The argument specifies how long this
       dense vector is. <br>
       <strong>NOTE</strong>: The user needs to <code>delete[]</code> this
       pointer after it's not needed anymore.
   */
  double *denseVector(int denseSize) const;
  /** Access the i'th element of the full storage vector.
       If the i'th is not stored, then zero is returned. The initial use of
       this method has some computational and storage overhead associated with
       it.<br>
       <strong>NOTE</strong>: This is <em>very</em> expensive. It is probably
       much better to use <code>denseVector()</code>.
   */
  double operator[](int i) const;
  //@}

  /**@name Index methods */
  //@{
  /// Get value of maximum index
  int getMaxIndex() const;
  /// Get value of minimum index
  int getMinIndex() const;

  /// Throw an exception if there are duplicate indices
  void duplicateIndex(const char *methodName = NULL,
    const char *className = NULL) const;

  /** Return true if the i'th element of the full storage vector exists in
       the packed storage vector.*/
  bool isExistingIndex(int i) const;

  /** Return the position of the i'th element of the full storage vector.
       If index does not exist then -1 is returned  */
  int findIndex(int i) const;

  //@}

  /**@name Comparison operators on two packed vectors */
  //@{
  /** Equal. Returns true if vectors have same length and corresponding
       element of each vector is equal. */
  bool operator==(const CoinPackedVectorBase &rhs) const;
  /// Not equal
  bool operator!=(const CoinPackedVectorBase &rhs) const;

#if 0
   // LL: This should be implemented eventually. It is useful to have.
   /** Lexicographic comparisons of two packed vectors. Returns
       negative/0/positive depending on whether \c this is
       smaller/equal.greater than \c rhs */
   int lexCompare(const CoinPackedVectorBase& rhs);
#endif

  /** This method establishes an ordering on packed vectors. It is complete
       ordering, but not the same as lexicographic ordering. However, it is
       quick and dirty to compute and thus it is useful to keep packed vectors
       in a heap when all we care is to quickly check whether a particular
       vector is already in the heap or not. Returns negative/0/positive
       depending on whether \c this is smaller/equal.greater than \c rhs. */
  int compare(const CoinPackedVectorBase &rhs) const;

  /** equivalent - If shallow packed vector A & B are equivalent, then they
       are still equivalent no matter how they are sorted.
       In this method the FloatEqual function operator can be specified. The
       default equivalence test is that the entries are relatively equal.<br> 
       <strong>NOTE</strong>: This is a relatively expensive method as it
       sorts the two shallow packed vectors.
   */
  template < class FloatEqual >
  bool
  isEquivalent(const CoinPackedVectorBase &rhs, const FloatEqual &eq) const
  {
    if (getNumElements() != rhs.getNumElements())
      return false;

    duplicateIndex("equivalent", "CoinPackedVector");
    rhs.duplicateIndex("equivalent", "CoinPackedVector");

    std::map< int, double > mv;
    const int *inds = getIndices();
    const double *elems = getElements();
    int i;
    for (i = getNumElements() - 1; i >= 0; --i) {
      mv.insert(std::make_pair(inds[i], elems[i]));
    }

    std::map< int, double > mvRhs;
    inds = rhs.getIndices();
    elems = rhs.getElements();
    for (i = getNumElements() - 1; i >= 0; --i) {
      mvRhs.insert(std::make_pair(inds[i], elems[i]));
    }

    std::map< int, double >::const_iterator mvI = mv.begin();
    std::map< int, double >::const_iterator mvIlast = mv.end();
    std::map< int, double >::const_iterator mvIrhs = mvRhs.begin();
    while (mvI != mvIlast) {
      if (mvI->first != mvIrhs->first || !eq(mvI->second, mvIrhs->second))
        return false;
      ++mvI;
      ++mvIrhs;
    }
    return true;
  }

  bool isEquivalent(const CoinPackedVectorBase &rhs) const;
  //@}

  /**@name Arithmetic operators. */
  //@{
  /// Create the dot product with a full vector
  double dotProduct(const double *dense) const;

  /// Return the 1-norm of the vector
  double oneNorm() const;

  /// Return the square of the 2-norm of the vector
  double normSquare() const;

  /// Return the 2-norm of the vector
  double twoNorm() const;

  /// Return the infinity-norm of the vector
  double infNorm() const;

  /// Sum elements of vector.
  double sum() const;
  //@}

protected:
  /**@name Constructors, destructor
      <strong>NOTE</strong>: All constructors are protected. There's no need
      to expose them, after all, this is an abstract class. */
  //@{
  /** Default constructor. */
  CoinPackedVectorBase();

public:
  /** Destructor */
  virtual ~CoinPackedVectorBase();
  //@}

private:
  /**@name Disabled methods */
  //@{
  /** The copy constructor. <br>
       This must be at least protected, but we make it private. The reason is
       that when, say, a shallow packed vector is created, first the
       underlying class, it this one is constructed. However, at that point we
       don't know how much of the data members of this class we need to copy
       over. Therefore the copy constructor is not used. */
  CoinPackedVectorBase(const CoinPackedVectorBase &);
  /** This class provides <em>const</em> access to packed vectors, so there's
       no need to provide an assignment operator. */
  CoinPackedVectorBase &operator=(const CoinPackedVectorBase &);
  //@}

protected:
  /**@name Protected methods */
  //@{
  /// Find Maximum and Minimum Indices
  void findMaxMinIndices() const;

  /// Return indexSetPtr_ (create it if necessary).
  std::set< int > *indexSet(const char *methodName = NULL,
    const char *className = NULL) const;

  /// Delete the indexSet
  void clearIndexSet() const;
  void clearBase() const;
  void copyMaxMinIndex(const CoinPackedVectorBase &x) const
  {
    maxIndex_ = x.maxIndex_;
    minIndex_ = x.minIndex_;
  }
  //@}

private:
  /**@name Protected member data */
  //@{
  /// Contains max index value or -infinity
  mutable int maxIndex_;
  /// Contains minimum index value or infinity
  mutable int minIndex_;
  /** Store the indices in a set. This set is only created if it is needed.
       Its primary use is testing for duplicate indices.
    */
  mutable std::set< int > *indexSetPtr_;
  /** True if the vector should be tested for duplicate indices when they can
       occur. */
  mutable bool testForDuplicateIndex_;
  /** True if the vector has already been tested for duplicate indices. Most
       of the operations in CoinPackedVector preserves this flag. */
  mutable bool testedDuplicateIndex_;
  //@}
};

#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/