Cbc  2.9.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Friends | List of all members
CoinPackedVector Class Reference

Sparse Vector. More...

#include <CoinPackedVector.hpp>

+ Inheritance diagram for CoinPackedVector:
+ Collaboration diagram for CoinPackedVector:

Public Member Functions

Get methods.
virtual int getNumElements () const
 Get the size. More...
 
virtual const int * getIndices () const
 Get indices of elements. More...
 
virtual const double * getElements () const
 Get element values. More...
 
int * getIndices ()
 Get indices of elements. More...
 
int getVectorNumElements () const
 Get the size. More...
 
const int * getVectorIndices () const
 Get indices of elements. More...
 
const double * getVectorElements () const
 Get element values. More...
 
double * getElements ()
 Get element values. More...
 
const int * getOriginalPosition () const
 Get pointer to int * vector of original postions. More...
 
Set methods
void clear ()
 Reset the vector (as if were just created an empty vector) More...
 
CoinPackedVectoroperator= (const CoinPackedVector &)
 Assignment operator. More...
 
CoinPackedVectoroperator= (const CoinPackedVectorBase &rhs)
 Assignment operator from a CoinPackedVectorBase. More...
 
void assignVector (int size, int *&inds, double *&elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Assign the ownership of the arguments to this vector. More...
 
void setVector (int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Set vector size, indices, and elements. More...
 
void setConstant (int size, const int *inds, double elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Elements set to have the same scalar value. More...
 
void setFull (int size, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Indices are not specified and are taken to be 0,1,...,size-1. More...
 
void setFullNonZero (int size, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Indices are not specified and are taken to be 0,1,...,size-1, but only where non zero. More...
 
void setElement (int index, double element)
 Set an existing element in the packed vector The first argument is the "index" into the elements() array. More...
 
void insert (int index, double element)
 Insert an element into the vector. More...
 
void append (const CoinPackedVectorBase &caboose)
 Append a CoinPackedVector to the end. More...
 
void swap (int i, int j)
 Swap values in positions i and j of indices and elements. More...
 
void truncate (int newSize)
 Resize the packed vector to be the first newSize elements. More...
 
Arithmetic operators.
void operator+= (double value)
 add value to every entry More...
 
void operator-= (double value)
 subtract value from every entry More...
 
void operator*= (double value)
 multiply every entry by value More...
 
void operator/= (double value)
 divide every entry by value More...
 
Sorting
template<class CoinCompare3 >
void sort (const CoinCompare3 &tc)
 Sort the packed storage vector. More...
 
void sortIncrIndex ()
 
void sortDecrIndex ()
 
void sortIncrElement ()
 
void sortDecrElement ()
 
void sortOriginalOrder ()
 Sort in original order. More...
 
Memory usage
void reserve (int n)
 Reserve space. More...
 
int capacity () const
 capacity returns the size which could be accomodated without having to reallocate storage. More...
 
Constructors and destructors
 CoinPackedVector (bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Default constructor. More...
 
 CoinPackedVector (int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Alternate Constructors - set elements to vector of doubles. More...
 
 CoinPackedVector (int capacity, int size, int *&inds, double *&elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Alternate Constructors - set elements to vector of doubles. More...
 
 CoinPackedVector (int size, const int *inds, double element, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Alternate Constructors - set elements to same scalar value. More...
 
 CoinPackedVector (int size, const double *elements, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
 Alternate Constructors - construct full storage with indices 0 through size-1. More...
 
 CoinPackedVector (const CoinPackedVector &)
 Copy constructor. More...
 
 CoinPackedVector (const CoinPackedVectorBase &rhs)
 Copy constructor from a PackedVectorBase. More...
 
virtual ~CoinPackedVector ()
 Destructor. More...
 
- Public Member Functions inherited from CoinPackedVectorBase
void setTestForDuplicateIndex (bool test) const
 Set to the argument value whether to test for duplicate indices in the vector whenever they can occur. More...
 
void setTestForDuplicateIndexWhenTrue (bool test) const
 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. More...
 
bool testForDuplicateIndex () const
 Returns true if the vector should be tested for duplicate indices when they can occur. More...
 
void setTestsOff () const
 Just sets test stuff false without a try etc. More...
 
double * denseVector (int denseSize) const
 Get the vector as a dense vector. More...
 
double operator[] (int i) const
 Access the i'th element of the full storage vector. More...
 
int getMaxIndex () const
 Get value of maximum index. More...
 
int getMinIndex () const
 Get value of minimum index. More...
 
void duplicateIndex (const char *methodName=NULL, const char *className=NULL) const
 Throw an exception if there are duplicate indices. More...
 
bool isExistingIndex (int i) const
 Return true if the i'th element of the full storage vector exists in the packed storage vector. More...
 
int findIndex (int i) const
 Return the position of the i'th element of the full storage vector. More...
 
bool operator== (const CoinPackedVectorBase &rhs) const
 Equal. More...
 
bool operator!= (const CoinPackedVectorBase &rhs) const
 Not equal. More...
 
int compare (const CoinPackedVectorBase &rhs) const
 This method establishes an ordering on packed vectors. More...
 
template<class FloatEqual >
bool isEquivalent (const CoinPackedVectorBase &rhs, const FloatEqual &eq) const
 equivalent - If shallow packed vector A & B are equivalent, then they are still equivalent no matter how they are sorted. More...
 
bool isEquivalent (const CoinPackedVectorBase &rhs) const
 
double dotProduct (const double *dense) const
 Create the dot product with a full vector. More...
 
double oneNorm () const
 Return the 1-norm of the vector. More...
 
double normSquare () const
 Return the square of the 2-norm of the vector. More...
 
double twoNorm () const
 Return the 2-norm of the vector. More...
 
double infNorm () const
 Return the infinity-norm of the vector. More...
 
double sum () const
 Sum elements of vector. More...
 
virtual ~CoinPackedVectorBase ()
 Destructor. More...
 

Private Member Functions

Private methods
void gutsOfSetVector (int size, const int *inds, const double *elems, bool testForDuplicateIndex, const char *method)
 Copy internal date. More...
 
void gutsOfSetConstant (int size, const int *inds, double value, bool testForDuplicateIndex, const char *method)
 

Private Attributes

Private member data
int * indices_
 Vector indices. More...
 
double * elements_
 Vector elements. More...
 
int nElements_
 Size of indices and elements vectors. More...
 
int * origIndices_
 original unsorted indices More...
 
int capacity_
 Amount of memory allocated for indices_, origIndices_, and elements_. More...
 

Friends

void CoinPackedVectorUnitTest ()
 A function that tests the methods in the CoinPackedVector class. More...
 

Additional Inherited Members

- Protected Member Functions inherited from CoinPackedVectorBase
void findMaxMinIndices () const
 Find Maximum and Minimum Indices. More...
 
std::set< int > * indexSet (const char *methodName=NULL, const char *className=NULL) const
 Return indexSetPtr_ (create it if necessary). More...
 
void clearIndexSet () const
 Delete the indexSet. More...
 
void clearBase () const
 
void copyMaxMinIndex (const CoinPackedVectorBase &x) const
 
 CoinPackedVectorBase ()
 Default constructor. More...
 

Detailed Description

Sparse Vector.

Stores vector of indices and associated element values. Supports sorting of vector while maintaining the original indices.

Here is a sample usage:

    const int ne = 4;
    int inx[ne] =   {  1,   4,  0,   2 }
    double el[ne] = { 10., 40., 1., 50. }

    // Create vector and set its value
    CoinPackedVector r(ne,inx,el);

    // access each index and element
    assert( r.indices ()[0]== 1  );
    assert( r.elements()[0]==10. );
    assert( r.indices ()[1]== 4  );
    assert( r.elements()[1]==40. );
    assert( r.indices ()[2]== 0  );
    assert( r.elements()[2]== 1. );
    assert( r.indices ()[3]== 2  );
    assert( r.elements()[3]==50. );

    // access original position of index
    assert( r.originalPosition()[0]==0 );
    assert( r.originalPosition()[1]==1 );
    assert( r.originalPosition()[2]==2 );
    assert( r.originalPosition()[3]==3 );

    // access as a full storage vector
    assert( r[ 0]==1. );
    assert( r[ 1]==10.);
    assert( r[ 2]==50.);
    assert( r[ 3]==0. );
    assert( r[ 4]==40.);

    // sort Elements in increasing order
    r.sortIncrElement();

    // access each index and element
    assert( r.indices ()[0]== 0  );
    assert( r.elements()[0]== 1. );
    assert( r.indices ()[1]== 1  );
    assert( r.elements()[1]==10. );
    assert( r.indices ()[2]== 4  );
    assert( r.elements()[2]==40. );
    assert( r.indices ()[3]== 2  );
    assert( r.elements()[3]==50. );    

    // access original position of index    
    assert( r.originalPosition()[0]==2 );
    assert( r.originalPosition()[1]==0 );
    assert( r.originalPosition()[2]==1 );
    assert( r.originalPosition()[3]==3 );

    // access as a full storage vector
    assert( r[ 0]==1. );
    assert( r[ 1]==10.);
    assert( r[ 2]==50.);
    assert( r[ 3]==0. );
    assert( r[ 4]==40.);

    // Restore orignal sort order
    r.sortOriginalOrder();
    
    assert( r.indices ()[0]== 1  );
    assert( r.elements()[0]==10. );
    assert( r.indices ()[1]== 4  );
    assert( r.elements()[1]==40. );
    assert( r.indices ()[2]== 0  );
    assert( r.elements()[2]== 1. );
    assert( r.indices ()[3]== 2  );
    assert( r.elements()[3]==50. );

    // Tests for equality and equivalence
    CoinPackedVector r1;
    r1=r;
    assert( r==r1 );
    assert( r.equivalent(r1) );
    r.sortIncrElement();
    assert( r!=r1 );
    assert( r.equivalent(r1) );

    // Add packed vectors.
    // Similarly for subtraction, multiplication,
    // and division.
    CoinPackedVector add = r + r1;
    assert( add[0] ==  1.+ 1. );
    assert( add[1] == 10.+10. );
    assert( add[2] == 50.+50. );
    assert( add[3] ==  0.+ 0. );
    assert( add[4] == 40.+40. );

    assert( r.sum() == 10.+40.+1.+50. );

Definition at line 123 of file CoinPackedVector.hpp.

Constructor & Destructor Documentation

CoinPackedVector::CoinPackedVector ( bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE)

Default constructor.

CoinPackedVector::CoinPackedVector ( int  size,
const int *  inds,
const double *  elems,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Alternate Constructors - set elements to vector of doubles.

This constructor copies the vectors provided as parameters.

CoinPackedVector::CoinPackedVector ( int  capacity,
int  size,
int *&  inds,
double *&  elems,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Alternate Constructors - set elements to vector of doubles.

This constructor takes ownership of the vectors passed as parameters. inds and elems will be NULL on return.

CoinPackedVector::CoinPackedVector ( int  size,
const int *  inds,
double  element,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Alternate Constructors - set elements to same scalar value.

CoinPackedVector::CoinPackedVector ( int  size,
const double *  elements,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Alternate Constructors - construct full storage with indices 0 through size-1.

CoinPackedVector::CoinPackedVector ( const CoinPackedVector )

Copy constructor.

CoinPackedVector::CoinPackedVector ( const CoinPackedVectorBase rhs)

Copy constructor from a PackedVectorBase.

virtual CoinPackedVector::~CoinPackedVector ( )
virtual

Destructor.

Member Function Documentation

virtual int CoinPackedVector::getNumElements ( ) const
inlinevirtual

Get the size.

Implements CoinPackedVectorBase.

Definition at line 130 of file CoinPackedVector.hpp.

virtual const int* CoinPackedVector::getIndices ( ) const
inlinevirtual

Get indices of elements.

Implements CoinPackedVectorBase.

Definition at line 132 of file CoinPackedVector.hpp.

virtual const double* CoinPackedVector::getElements ( ) const
inlinevirtual

Get element values.

Implements CoinPackedVectorBase.

Definition at line 134 of file CoinPackedVector.hpp.

int* CoinPackedVector::getIndices ( )
inline

Get indices of elements.

Definition at line 136 of file CoinPackedVector.hpp.

int CoinPackedVector::getVectorNumElements ( ) const
inline

Get the size.

Definition at line 138 of file CoinPackedVector.hpp.

const int* CoinPackedVector::getVectorIndices ( ) const
inline

Get indices of elements.

Definition at line 140 of file CoinPackedVector.hpp.

const double* CoinPackedVector::getVectorElements ( ) const
inline

Get element values.

Definition at line 142 of file CoinPackedVector.hpp.

double* CoinPackedVector::getElements ( )
inline

Get element values.

Definition at line 144 of file CoinPackedVector.hpp.

const int* CoinPackedVector::getOriginalPosition ( ) const
inline

Get pointer to int * vector of original postions.

If the packed vector has not been sorted then this function returns the vector: 0, 1, 2, ..., size()-1.

Definition at line 148 of file CoinPackedVector.hpp.

void CoinPackedVector::clear ( )

Reset the vector (as if were just created an empty vector)

CoinPackedVector& CoinPackedVector::operator= ( const CoinPackedVector )

Assignment operator.


NOTE: This operator keeps the current testForDuplicateIndex setting, and affter copying the data it acts accordingly.

CoinPackedVector& CoinPackedVector::operator= ( const CoinPackedVectorBase rhs)

Assignment operator from a CoinPackedVectorBase.


NOTE: This operator keeps the current testForDuplicateIndex setting, and affter copying the data it acts accordingly.

void CoinPackedVector::assignVector ( int  size,
int *&  inds,
double *&  elems,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Assign the ownership of the arguments to this vector.

Size is the length of both the indices and elements vectors. The indices and elements vectors are copied into this class instance's member data. The last argument indicates whether this vector will have to be tested for duplicate indices.

void CoinPackedVector::setVector ( int  size,
const int *  inds,
const double *  elems,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Set vector size, indices, and elements.

Size is the length of both the indices and elements vectors. The indices and elements vectors are copied into this class instance's member data. The last argument specifies whether this vector will have to be checked for duplicate indices whenever that can happen.

void CoinPackedVector::setConstant ( int  size,
const int *  inds,
double  elems,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Elements set to have the same scalar value.

void CoinPackedVector::setFull ( int  size,
const double *  elems,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Indices are not specified and are taken to be 0,1,...,size-1.

void CoinPackedVector::setFullNonZero ( int  size,
const double *  elems,
bool  testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE 
)

Indices are not specified and are taken to be 0,1,...,size-1, but only where non zero.

void CoinPackedVector::setElement ( int  index,
double  element 
)

Set an existing element in the packed vector The first argument is the "index" into the elements() array.

void CoinPackedVector::insert ( int  index,
double  element 
)

Insert an element into the vector.

void CoinPackedVector::append ( const CoinPackedVectorBase caboose)

Append a CoinPackedVector to the end.

void CoinPackedVector::swap ( int  i,
int  j 
)

Swap values in positions i and j of indices and elements.

void CoinPackedVector::truncate ( int  newSize)

Resize the packed vector to be the first newSize elements.

Problem with truncate: what happens with origIndices_ ???

void CoinPackedVector::operator+= ( double  value)

add value to every entry

void CoinPackedVector::operator-= ( double  value)

subtract value from every entry

void CoinPackedVector::operator*= ( double  value)

multiply every entry by value

void CoinPackedVector::operator/= ( double  value)

divide every entry by value

template<class CoinCompare3 >
void CoinPackedVector::sort ( const CoinCompare3 &  tc)
inline

Sort the packed storage vector.

Typcical usages:

 
packedVector.sort(CoinIncrIndexOrdered());   //increasing indices
packedVector.sort(CoinIncrElementOrdered()); // increasing elements

Definition at line 239 of file CoinPackedVector.hpp.

void CoinPackedVector::sortIncrIndex ( )
inline

Definition at line 243 of file CoinPackedVector.hpp.

void CoinPackedVector::sortDecrIndex ( )
inline

Definition at line 247 of file CoinPackedVector.hpp.

void CoinPackedVector::sortIncrElement ( )
inline

Definition at line 251 of file CoinPackedVector.hpp.

void CoinPackedVector::sortDecrElement ( )
inline

Definition at line 255 of file CoinPackedVector.hpp.

void CoinPackedVector::sortOriginalOrder ( )

Sort in original order.

If the vector has been sorted, then this method restores to its orignal sort order.

void CoinPackedVector::reserve ( int  n)

Reserve space.

If one knows the eventual size of the packed vector, then it may be more efficient to reserve the space.

int CoinPackedVector::capacity ( ) const
inline

capacity returns the size which could be accomodated without having to reallocate storage.

Definition at line 277 of file CoinPackedVector.hpp.

void CoinPackedVector::gutsOfSetVector ( int  size,
const int *  inds,
const double *  elems,
bool  testForDuplicateIndex,
const char *  method 
)
private

Copy internal date.

void CoinPackedVector::gutsOfSetConstant ( int  size,
const int *  inds,
double  value,
bool  testForDuplicateIndex,
const char *  method 
)
private

Friends And Related Function Documentation

void CoinPackedVectorUnitTest ( )
friend

A function that tests the methods in the CoinPackedVector class.

The only reason for it not to be a member method is that this way it doesn't have to be compiled into the library. And that's a gain, because the library should be compiled with optimization on, but this method should be compiled with debugging.

Member Data Documentation

int* CoinPackedVector::indices_
private

Vector indices.

Definition at line 330 of file CoinPackedVector.hpp.

double* CoinPackedVector::elements_
private

Vector elements.

Definition at line 332 of file CoinPackedVector.hpp.

int CoinPackedVector::nElements_
private

Size of indices and elements vectors.

Definition at line 334 of file CoinPackedVector.hpp.

int* CoinPackedVector::origIndices_
private

original unsorted indices

Definition at line 336 of file CoinPackedVector.hpp.

int CoinPackedVector::capacity_
private

Amount of memory allocated for indices_, origIndices_, and elements_.

Definition at line 338 of file CoinPackedVector.hpp.


The documentation for this class was generated from the following file: