# CoinPackedVector Class Reference

Sparse Vector. More...

`#include <CoinPackedVector.hpp>`

Inheritance diagram for CoinPackedVector:
[legend]
Collaboration diagram for CoinPackedVector:
[legend]

List of all members.

## Public Member Functions
Get methods.
virtual int | getNumElements () const
| Get the size.
virtual const int * | getIndices () const
| Get indices of elements.
virtual const double * | getElements () const
| Get element values.
int * | getIndices ()
| Get indices of elements.
int | getVectorNumElements () const
| Get the size.
const int * | getVectorIndices () const
| Get indices of elements.
const double * | getVectorElements () const
| Get element values.
double * | getElements ()
| Get element values.
const int * | getOriginalPosition () const
| Get pointer to int * vector of original postions.
Set methods
void | clear ()
| Reset the vector (as if were just created an empty vector).
CoinPackedVector & | operator= (const CoinPackedVector &)
| Assignment operator.
CoinPackedVector & | operator= (const CoinPackedVectorBase &rhs)
| Assignment operator from a CoinPackedVectorBase.
void | assignVector (int size, int *&inds, double *&elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
| Assign the ownership of the arguments to this vector.
void | setVector (int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
| Set vector size, indices, and elements.
void | setConstant (int size, const int *inds, double elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
| Elements set to have the same scalar value.
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,.
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,.
void | setElement (int index, double element)
| Set an existing element in the packed vector The first argument is the "index" into the elements() array.
void | insert (int index, double element)
| Insert an element into the vector.
void | append (const CoinPackedVectorBase &caboose)
| Append a CoinPackedVector to the end.
void | swap (int i, int j)
| Swap values in positions i and j of indices and elements.
void | truncate (int newSize)
| Resize the packed vector to be the first newSize elements.
Arithmetic operators.
void | operator+= (double value)
| add `value` to every entry
void | operator-= (double value)
| subtract `value` from every entry
void | operator*= (double value)
| multiply every entry by `value`
void | operator/= (double value)
| divide every entry by `value`
Sorting
template<class CoinCompare3 >
void | sort (const CoinCompare3 &tc)
| Sort the packed storage vector.
void | sortIncrIndex ()
void | sortDecrIndex ()
void | sortIncrElement ()
void | sortDecrElement ()
void | sortOriginalOrder ()
| Sort in original order.
Memory usage
void | reserve (int n)
| Reserve space.
int | capacity () const
| capacity returns the size which could be accomodated without having to reallocate storage.
Constructors and destructors
| CoinPackedVector (bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
| Default constructor.
| CoinPackedVector (int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
| Alternate Constructors - set elements to vector of doubles.
| CoinPackedVector (int capacity, int size, int *&inds, double *&elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
| Alternate Constructors - set elements to vector of doubles.
| CoinPackedVector (int size, const int *inds, double element, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
| Alternate Constructors - set elements to same scalar value.
| 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 (const CoinPackedVector &)
| Copy constructor.
| CoinPackedVector (const CoinPackedVectorBase &rhs)
| Copy constructor from a PackedVectorBase.
virtual | ~CoinPackedVector ()
| Destructor.
## Private Member Functions
Private methods
void | gutsOfSetVector (int size, const int *inds, const double *elems, bool testForDuplicateIndex, const char *method)
| Copy internal date.
void | gutsOfSetConstant (int size, const int *inds, double value, bool testForDuplicateIndex, const char *method)
## Private Attributes
Private member data
int * | indices_
| Vector indices.
double * | elements_
| Vector elements.
int | nElements_
| Size of indices and elements vectors.
int * | origIndices_
| original unsorted indices
int | capacity_
| Amount of memory allocated for indices_, origIndices_, and elements_.
## Friends
void | CoinPackedVectorUnitTest ()
| A function that tests the methods in the CoinPackedVector class.

## 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`[inline, virtual]`

Get the size.

Implements CoinPackedVectorBase.

Definition at line 130 of file CoinPackedVector.hpp.

virtual const int* CoinPackedVector::getIndices | ( |  | ) | const`[inline, virtual]`

Get indices of elements.

Implements CoinPackedVectorBase.

Definition at line 132 of file CoinPackedVector.hpp.

virtual const double* CoinPackedVector::getElements | ( |  | ) | const`[inline, virtual]`

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.

Reimplemented from CoinPackedVectorBase.

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:

- /home/coin/svn-release/CoinUtils-2.9.4/CoinUtils/src/CoinPackedVector.hpp

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines