Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CoinPackedVectorBase Class Reference

#include <CoinPackedVectorBase.hpp>

Inheritance diagram for CoinPackedVectorBase:

CoinPackedVector CoinShallowPackedVector List of all members.

Constructors, destructor

NOTE: All constructors are protected. There's no need to expose them, after all, this is an abstract class.

virtual ~CoinPackedVectorBase ()
 CoinPackedVectorBase ()

Public Member Functions

Virtual methods that the derived classes must provide
virtual int getNumElements () const=0
 Get length of indices and elements vectors.

virtual const int * getIndices () const=0
 Get indices of elements.

virtual const double * getElements () const=0
 Get element values.

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.

void setTestForDuplicateIndex (bool test) const
bool testForDuplicateIndex () const
Methods for getting info on the packed vector as a full vector
double * denseVector (int denseSize) const throw (CoinError)
double operator[] (int i) const throw (CoinError)
Index methods
int getMaxIndex () const
 Get value of maximum index.

int getMinIndex () const
 Get value of minimum index.

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

bool isExistingIndex (int i) const
int findIndex (int i) const
Comparison operators on two packed vectors
bool operator== (const CoinPackedVectorBase &rhs) const
bool operator!= (const CoinPackedVectorBase &rhs) const
 Not equal.

template<class FloatEqual> bool isEquivalent (const CoinPackedVectorBase &rhs, const FloatEqual &eq) const throw (CoinError)
bool isEquivalent (const CoinPackedVectorBase &rhs) const
Arithmetic operators.
double dotProduct (const double *dense) const
 Create the dot product with a full vector.

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

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

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

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

double sum () const
 Sum elements of vector.


Protected Member Functions

Protected methods
void findMaxMinIndices () const
 Find Maximum and Minimum Indices.

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

void clearIndexSet () const
 Delete the indexSet.

void clearBase () const
void copyMaxMinIndex (const CoinPackedVectorBase &x) const

Private Member Functions

Disabled methods
 CoinPackedVectorBase (const CoinPackedVectorBase &)
CoinPackedVectorBaseoperator= (const CoinPackedVectorBase &)

Private Attributes

Protected member data
int maxIndex_
 Contains max index value or -infinity.

int minIndex_
 Contains minimum index value or infinity.

std::set< int > * indexSetPtr_
bool testForDuplicateIndex_
bool testedDuplicateIndex_

Detailed Description

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 constant packed vector. All members of this class are const methods, they can't change the object.

Definition at line 25 of file CoinPackedVectorBase.hpp.


Constructor & Destructor Documentation

CoinPackedVectorBase::CoinPackedVectorBase  )  [protected]
 

Default constructor.

Definition at line 200 of file CoinPackedVectorBase.cpp.

00200                                            :
00201    maxIndex_(/*std::numeric_limits<int>::max()*/INT_MIN/*0*/),
00202    minIndex_(/*std::numeric_limits<int>::min()*/INT_MAX/*0*/),
00203    indexSetPtr_(NULL),
00204    testForDuplicateIndex_(true),
00205    testedDuplicateIndex_(false) {}

CoinPackedVectorBase::~CoinPackedVectorBase  )  [virtual]
 

Destructor

Definition at line 209 of file CoinPackedVectorBase.cpp.

References indexSetPtr_.

00210 {
00211    delete indexSetPtr_;
00212 }

CoinPackedVectorBase::CoinPackedVectorBase const CoinPackedVectorBase  )  [private]
 

The copy constructor.
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.


Member Function Documentation

double * CoinPackedVectorBase::denseVector int  denseSize  )  const throw (CoinError)
 

Get the vector as a dense vector. The argument specifies how long this dense vector is.
NOTE: The user needs to delete[] this pointer after it's not needed anymore.

Definition at line 17 of file CoinPackedVectorBase.cpp.

00018 {
00019    if (getMaxIndex() >= denseSize)
00020       throw CoinError("Dense vector size is less than max index",
00021                      "denseVector", "CoinPackedVectorBase");
00022 
00023    double * dv = new double[denseSize];
00024    CoinFillN(dv, denseSize, 0.0);
00025    const int s = getNumElements();
00026    const int * inds = getIndices();
00027    const double * elems = getElements();
00028    for (int i = 0; i < s; ++i)
00029       dv[inds[i]] = elems[i];
00030    return dv;
00031 }

int CoinPackedVectorBase::findIndex 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

Definition at line 114 of file CoinPackedVectorBase.cpp.

References getIndices(), and getNumElements().

00115 {   
00116    const int * inds = getIndices();
00117    int retVal = std::find(inds, inds + getNumElements(), i) - inds;
00118    if (retVal == getNumElements() ) retVal = -1;
00119    return retVal;
00120 }

template<class FloatEqual>
bool CoinPackedVectorBase::isEquivalent const CoinPackedVectorBase rhs,
const FloatEqual &  eq
const throw (CoinError) [inline]
 

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.
NOTE: This is a relatively expensive method as it sorts the two shallow packed vectors.

Definition at line 110 of file CoinPackedVectorBase.hpp.

References duplicateIndex(), getElements(), getIndices(), and getNumElements().

00112    {
00113       if (getNumElements() != rhs.getNumElements())
00114          return false;
00115 
00116       duplicateIndex("equivalent", "CoinPackedVector");
00117       rhs.duplicateIndex("equivalent", "CoinPackedVector");
00118 
00119       std::map<int,double> mv;
00120       const int * inds = getIndices();
00121       const double * elems = getElements();
00122       int i;
00123       for ( i = getNumElements() - 1; i >= 0; --i) {
00124          mv.insert(std::make_pair(inds[i], elems[i]));
00125       }
00126 
00127       std::map<int,double> mvRhs;
00128       inds = rhs.getIndices();
00129       elems = rhs.getElements();
00130       for ( i = getNumElements() - 1; i >= 0; --i) {
00131          mvRhs.insert(std::make_pair(inds[i], elems[i]));
00132       }
00133 
00134       std::map<int,double>::const_iterator mvI = mv.begin();
00135       std::map<int,double>::const_iterator mvIlast = mv.end();
00136       std::map<int,double>::const_iterator mvIrhs = mvRhs.begin();
00137       while (mvI != mvIlast) {
00138          if (mvI->first != mvIrhs->first || ! eq(mvI->second, mvIrhs->second))
00139             return false;
00140          ++mvI;
00141          ++mvIrhs;
00142       }
00143       return true;
00144    }

bool CoinPackedVectorBase::isExistingIndex int  i  )  const
 

Return true if the i'th element of the full storage vector exists in the packed storage vector.

Definition at line 103 of file CoinPackedVectorBase.cpp.

References duplicateIndex(), indexSet(), and testedDuplicateIndex_.

00104 {
00105    if (! testedDuplicateIndex_)
00106       duplicateIndex("indexExists", "CoinPackedVectorBase");
00107 
00108    const std::set<int> & sv = *indexSet("indexExists", "CoinPackedVectorBase");
00109    return sv.find(i) != sv.end();
00110 }

CoinPackedVectorBase& CoinPackedVectorBase::operator= const CoinPackedVectorBase  )  [private]
 

This class provides const access to packed vectors, so there's no need to provide an assignment operator.

Reimplemented in CoinPackedVector, and CoinShallowPackedVector.

bool CoinPackedVectorBase::operator== const CoinPackedVectorBase rhs  )  const
 

Equal. Returns true if vectors have same length and corresponding element of each vector is equal.

Definition at line 125 of file CoinPackedVectorBase.cpp.

References getElements(), getIndices(), and getNumElements().

00126 {
00127    return (getNumElements()==rhs.getNumElements() &&
00128            std::equal(getIndices(), getIndices() + getNumElements(),
00129                       rhs.getIndices()) &&
00130            std::equal(getElements(), getElements() + getNumElements(),
00131                       rhs.getElements()));
00132 }

double CoinPackedVectorBase::operator[] int  i  )  const throw (CoinError)
 

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.
NOTE: This is very expensive. It is probably much better to use denseVector().

Definition at line 36 of file CoinPackedVectorBase.cpp.

00037 {
00038    if (! testedDuplicateIndex_)
00039       duplicateIndex("operator[]", "CoinPackedVectorBase");
00040 
00041    // Get a reference to a map of full storage indices to 
00042    // packed storage location.
00043    const std::set<int> & sv = *indexSet("operator[]", "CoinPackedVectorBase");
00044 #if 1
00045    if (sv.find(i) == sv.end())
00046       return 0.0;
00047    return getElements()[findIndex(i)];
00048 #else
00049    // LL: suggested change, somthing is wrong with this
00050    const size_t ind = std::distance(sv.begin(), sv.find(i));
00051    return (ind == sv.size()) ? 0.0 : getElements()[ind];
00052 #endif
00053       
00054 }

void CoinPackedVectorBase::setTestForDuplicateIndex bool  test  )  const
 

Set to the argument value whether to test for duplicate indices in the vector whenever they can occur.

Definition at line 59 of file CoinPackedVectorBase.cpp.

References duplicateIndex(), testedDuplicateIndex_, and testForDuplicateIndex_.

Referenced by CoinPackedVector::assignVector(), CoinPackedVector::CoinPackedVector(), CoinShallowPackedVector::CoinShallowPackedVector(), CoinPackedVector::gutsOfSetVector(), CoinPackedVector::setFull(), CoinPackedVector::setFullNonZero(), and CoinShallowPackedVector::setVector().

00060 {
00061    if (test == true) {
00062       testForDuplicateIndex_ = true;
00063       duplicateIndex("setTestForDuplicateIndex", "CoinPackedVectorBase");
00064    } else {
00065       testForDuplicateIndex_ = false;
00066       testedDuplicateIndex_ = false;
00067    }
00068 }

bool CoinPackedVectorBase::testForDuplicateIndex  )  const [inline]
 

Returns true if the vector should be tested for duplicate indices when they can occur.

Definition at line 52 of file CoinPackedVectorBase.hpp.

References testForDuplicateIndex_.

Referenced by CoinPackedVector::operator=().

00052 { return testForDuplicateIndex_; }


Member Data Documentation

std::set<int>* CoinPackedVectorBase::indexSetPtr_ [mutable, private]
 

Store the indices in a set. This set is only created if it is needed. Its primary use is testing for duplicate indices.

Definition at line 233 of file CoinPackedVectorBase.hpp.

Referenced by clearIndexSet(), findMaxMinIndices(), and ~CoinPackedVectorBase().

bool CoinPackedVectorBase::testedDuplicateIndex_ [mutable, private]
 

True if the vector has already been tested for duplicate indices. Most of the operations in CoinPackedVector preserves this flag.

Definition at line 239 of file CoinPackedVectorBase.hpp.

Referenced by isExistingIndex(), and setTestForDuplicateIndex().

bool CoinPackedVectorBase::testForDuplicateIndex_ [mutable, private]
 

True if the vector should be tested for duplicate indices when they can occur.

Definition at line 236 of file CoinPackedVectorBase.hpp.

Referenced by setTestForDuplicateIndex(), and testForDuplicateIndex().


The documentation for this class was generated from the following files:
Generated on Wed Dec 3 14:34:29 2003 for Coin by doxygen 1.3.5