4 #ifndef CoinPackedVector_H
5 #define CoinPackedVector_H
9 # pragma warning(disable:4786)
16 #ifndef COIN_NOTEST_DUPLICATE
17 #define COIN_DEFAULT_VALUE_FOR_DUPLICATE true
19 #define COIN_DEFAULT_VALUE_FOR_DUPLICATE false
172 void setVector(
int size,
const int * inds,
const double * elems,
176 void setConstant(
int size,
const int * inds,
double elems,
180 void setFull(
int size,
const double * elems,
194 void insert(
int index,
double element);
199 void swap(
int i,
int j);
227 template <
class CoinCompare3>
228 void sort(
const CoinCompare3 & tc)
305 const int * inds,
const double * elems,
307 const char * method);
310 const int * inds,
double value,
312 const char * method);
348 template <
class BinaryFunction>
void
359 for (
int i=0; i<s; ++i ) {
360 retVal.
insert(inds[i], bf(value, elems[i]));
365 template <
class BinaryFunction>
inline void
373 template <
class BinaryFunction>
void
387 if (s1 == 0 && s2 == 0)
399 for ( i=0; i<s1; ++i ) {
400 const int index = inds1[i];
402 const double val = bf(elems1[i], pos2 == -1 ? 0.0 : elems2[pos2]);
404 retVal.
insert(index, val);
407 for ( i=0; i<s2; ++i ) {
408 const int index = inds2[i];
413 const double val = bf(0.0, elems2[i]);
415 retVal.
insert(index, val);
458 binaryOp(retVal, op1, op2, std::plus<double>());
468 binaryOp(retVal, op1, op2, std::minus<double>());
478 binaryOp(retVal, op1, op2, std::multiplies<double>());
488 binaryOp(retVal, op1, op2, std::divides<double>());
505 for(i = 0; i < len; i++){
516 int i, j, len1, len2;
530 while(i < len1 && j < len2){
531 if(v1ind[i] == v2ind[j]){
532 acc += v1val[i] * v2val[j];
536 else if(v2ind[j] < v1ind[i]){
608 for (
int i = 0; i < size; ++i) {
609 elems[i] = value - elems[i];
630 for (
int i = 0; i < size; ++i) {
631 elems[i] = value / elems[i];
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 operator*=(double value)
multiply every entry by value
int capacity_
Amount of memory allocated for indices_, origIndices_, and elements_.
void gutsOfSetVector(int size, const int *inds, const double *elems, bool testForDuplicateIndex, const char *method)
Copy internal date.
int nElements_
Size of indices and elements vectors.
virtual const int * getIndices() const
Get indices of elements.
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.
virtual const double * getElements() const
Get element values.
void setVector(int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Set vector size, indices, and elements.
virtual int getNumElements() const =0
Get length of indices and elements vectors.
int * indices_
Vector indices.
CoinPackedVector & operator=(const CoinPackedVector &)
Assignment operator.
void insert(int index, double element)
Insert an element into the vector.
void sortIncrIndex()
Sort the packed storage vector.
int * origIndices_
original unsorted indices
void clear()
Reset the vector (as if were just created an empty vector)
void setElement(int index, double element)
Set an existing element in the packed vector The first argument is the "index" into the elements() ar...
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.
void setTestForDuplicateIndex(bool test) const
Set to the argument value whether to test for duplicate indices in the vector whenever they can occur...
double * elements_
Vector elements.
Abstract base class for various sparse vectors.
virtual int getNumElements() const
Get the size.
double * getElements()
Get element values.
void sortIncrElement()
Sort the packed storage vector.
void CoinSort_3(S *sfirst, S *slast, T *tfirst, U *ufirst, const CoinCompare3 &tc)
Sort a triple of containers.
void sortOriginalOrder()
Sort in original order.
double sparseDotProduct(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Returns the dot product of two CoinPackedVector objects whose elements are doubles.
bool isExistingIndex(int i) const
Return true if the i'th element of the full storage vector exists in the packed storage vector...
#define COIN_DEFAULT_VALUE_FOR_DUPLICATE
void gutsOfSetConstant(int size, const int *inds, double value, bool testForDuplicateIndex, const char *method)
Copy internal date.
void operator-=(double value)
subtract value from every entry
int capacity() const
capacity returns the size which could be accomodated without having to reallocate storage...
void sortDecrElement()
Sort the packed storage vector.
void truncate(int newSize)
Resize the packed vector to be the first newSize elements.
virtual const double * getElements() const =0
Get element values.
virtual ~CoinPackedVector()
Destructor.
CoinPackedVector operator/(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Return the element-wise ratio of two packed vectors.
CoinPackedVector(bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Default constructor.
void append(const CoinPackedVectorBase &caboose)
Append a CoinPackedVector to the end.
CoinPackedVector operator+(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Return the sum of two packed vectors.
bool testForDuplicateIndex() const
Returns true if the vector should be tested for duplicate indices when they can occur.
int * getIndices()
Get indices of elements.
friend void CoinPackedVectorUnitTest()
A function that tests the methods in the CoinPackedVector class.
void operator/=(double value)
divide every entry by value
void CoinPackedVectorUnitTest()
A function that tests the methods in the CoinPackedVector class.
void operator+=(double value)
add value to every entry
void reserve(int n)
Reserve space.
virtual const int * getIndices() const =0
Get indices of elements.
CoinPackedVector operator-(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Return the difference of two packed vectors.
CoinPackedVector operator*(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Return the element-wise product of two packed vectors.
int findIndex(int i) const
Return the position of the i'th element of the full storage vector.
double sortedSparseDotProduct(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Returns the dot product of two sorted CoinPackedVector objects.
void sortDecrIndex()
Sort the packed storage vector.
void assignVector(int size, int *&inds, double *&elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Assign the ownership of the arguments to this vector.
const int * getOriginalPosition() const
Get pointer to int * vector of original postions.
void swap(int i, int j)
Swap values in positions i and j of indices and elements.
void sort(const CoinCompare3 &tc)
Sort the packed storage vector.
void binaryOp(CoinPackedVector &retVal, const CoinPackedVectorBase &op1, double value, BinaryFunction bf)
Return the sum of two packed vectors.