#include <CoinIndexedVector.hpp>
Public Member Functions | |
Get methods. | |
int | getNumElements () const |
Get the size. | |
const int * | getIndices () const |
Get indices of elements. | |
int * | getIndices () |
Get indices of elements. | |
double * | denseVector () const |
void | setDenseVector (double *array) |
For very temporary use when user needs to borrow a dense vector. | |
double & | operator[] (int i) const |
Set methods | |
void | setNumElements (int value) |
Set the size. | |
void | clear () |
Reset the vector (as if were just created an empty vector). This leaves arrays! | |
void | empty () |
Reset the vector (as if were just created an empty vector). | |
CoinIndexedVector & | operator= (const CoinIndexedVector &) |
CoinIndexedVector & | operator= (const CoinPackedVectorBase &rhs) |
void | borrowVector (int size, int numberIndices, int *inds, double *elems) |
void | returnVector () |
void | setVector (int numberIndices, const int *inds, const double *elems) |
void | setVector (int size, int numberIndices, const int *inds, const double *elems) |
void | setConstant (int size, const int *inds, double elems) |
void | setFull (int size, const double *elems) |
void | setElement (int index, double element) |
void | insert (int index, double element) |
Insert an element into the vector. | |
void | add (int index, double element) |
void | quickAdd (int index, double element) |
void | zero (int index) |
int | clean (double tolerance) |
int | cleanAndPack (double tolerance) |
Same but packs down. | |
int | cleanAndPackSafe (double tolerance) |
Same but packs down and is safe (i.e. if order is odd). | |
void | setPacked () |
Mark as packed. | |
void | checkClear () |
For debug check vector is clear i.e. no elements. | |
void | checkClean () |
For debug check vector is clean i.e. elements match indices. | |
int | scan () |
Scan dense region and set up indices (returns number found). | |
int | scan (int start, int end) |
int | scan (double tolerance) |
int | scan (int start, int end, double tolerance) |
int | scanAndPack () |
These are same but pack down. | |
int | scanAndPack (int start, int end) |
int | scanAndPack (double tolerance) |
int | scanAndPack (int start, int end, double tolerance) |
void | createPacked (int number, const int *indices, const double *elements) |
Create packed array. | |
void | expand () |
This is mainly for testing - goes from packed to indexed. | |
void | append (const CoinPackedVectorBase &caboose) |
Append a CoinPackedVector to the end. | |
void | append (const CoinIndexedVector &caboose) |
Append a CoinIndexedVector to the end. | |
void | swap (int i, int j) |
Swap values in positions i and j of indices and elements. | |
void | truncate (int newSize) |
Throw away all entries in rows >= newSize. | |
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 (** 0 vanishes) | |
Comparison operators on two indexed vectors | |
bool | operator== (const CoinPackedVectorBase &rhs) const |
bool | operator!= (const CoinPackedVectorBase &rhs) const |
Not equal. | |
bool | operator== (const CoinIndexedVector &rhs) const |
bool | operator!= (const CoinIndexedVector &rhs) const |
Not equal. | |
Index methods | |
int | getMaxIndex () const |
Get value of maximum index. | |
int | getMinIndex () const |
Get value of minimum index. | |
Sorting | |
void | sort () |
void | sortIncrIndex () |
void | sortDecrIndex () |
void | sortIncrElement () |
void | sortDecrElement () |
Arithmetic operators on packed vectors. | |
NOTE: These methods operate on those positions where at least one of the arguments has a value listed. At those positions the appropriate operation is executed, Otherwise the result of the operation is considered 0. NOTE 2: Because these methods return an object (they can't return a reference, though they could return a pointer...) they are very inefficient... | |
CoinIndexedVector | operator+ (const CoinIndexedVector &op2) |
Return the sum of two indexed vectors. | |
CoinIndexedVector | operator- (const CoinIndexedVector &op2) |
Return the difference of two indexed vectors. | |
CoinIndexedVector | operator * (const CoinIndexedVector &op2) |
Return the element-wise product of two indexed vectors. | |
CoinIndexedVector | operator/ (const CoinIndexedVector &op2) |
Return the element-wise ratio of two indexed vectors (0.0/0.0 => 0.0) (0 vanishes). | |
Memory usage | |
void | reserve (int n) |
int | capacity () const |
void | setPackedMode (bool yesNo) |
Sets packed mode. | |
bool | packedMode () const |
Gets packed mode. | |
Constructors and destructors | |
CoinIndexedVector () | |
CoinIndexedVector (int size, const int *inds, const double *elems) | |
CoinIndexedVector (int size, const int *inds, double element) | |
CoinIndexedVector (int size, const double *elements) | |
CoinIndexedVector (const CoinIndexedVector &) | |
CoinIndexedVector (const CoinIndexedVector *) | |
CoinIndexedVector (const CoinPackedVectorBase &rhs) | |
~CoinIndexedVector () | |
Private Member Functions | |
Private methods | |
void | gutsOfSetVector (int size, const int *inds, const double *elems) |
Copy internal date. | |
void | gutsOfSetVector (int size, int numberIndices, const int *inds, const double *elems) |
void | gutsOfSetConstant (int size, const int *inds, double value) |
Private Attributes | |
Private member data | |
int * | indices_ |
Vector indices. | |
double * | elements_ |
Vector elements. | |
int | nElements_ |
Size of indices and packed elements vectors. | |
int | capacity_ |
Amount of memory allocated for indices_, and elements_. | |
int | offset_ |
Offset to get where new allocated array. | |
bool | packedMode_ |
If true then is operating in packed mode. | |
Friends | |
void | CoinIndexedVectorUnitTest () |
This stores values unpacked but apart from that is a bit like CoinPackedVector. It is designed to be lightweight in normal use.
It now has a "packed" mode when it is even more like CoinPackedVector
Indices array has capacity_ extra chars which are zeroed and can be used for any purpose - but must be re-zeroed
Stores vector of indices and associated element values. Supports sorting of indices.
Does not support negative indices.
Does NOT support testing for duplicates
getElements is no longer supported
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 valuex1 CoinIndexedVector r(ne,inx,el); // 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.getIndices ()[0]== 0 ); assert( r.getIndices ()[1]== 1 ); assert( r.getIndices ()[2]== 4 ); assert( r.getIndices ()[3]== 2 ); // 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.); // Tests for equality and equivalence CoinIndexedVector r1; r1=r; assert( r==r1 ); assert( r.equivalent(r1) ); r.sortIncrElement(); assert( r!=r1 ); assert( r.equivalent(r1) ); // Add indexed vectors. // Similarly for subtraction, multiplication, // and division. CoinIndexedVector 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 91 of file CoinIndexedVector.hpp.
|
Default constructor Definition at line 486 of file CoinIndexedVector.cpp.
00486 : 00487 indices_(NULL), 00488 elements_(NULL), 00489 nElements_(0), 00490 capacity_(0), 00491 offset_(0), 00492 packedMode_(false) 00493 { 00494 } |
|
Alternate Constructors - set elements to vector of doubles Definition at line 498 of file CoinIndexedVector.cpp. References gutsOfSetVector().
00499 : 00500 indices_(NULL), 00501 elements_(NULL), 00502 nElements_(0), 00503 capacity_(0), 00504 offset_(0), 00505 packedMode_(false) 00506 { 00507 gutsOfSetVector(size, inds, elems); 00508 } |
|
Alternate Constructors - set elements to same scalar value Definition at line 512 of file CoinIndexedVector.cpp.
00513 : 00514 indices_(NULL), 00515 elements_(NULL), 00516 nElements_(0), 00517 capacity_(0), 00518 offset_(0), 00519 packedMode_(false) 00520 { 00521 gutsOfSetConstant(size, inds, value); 00522 } |
|
Alternate Constructors - construct full storage with indices 0 through size-1. Definition at line 526 of file CoinIndexedVector.cpp. References setFull().
00526 : 00527 indices_(NULL), 00528 elements_(NULL), 00529 nElements_(0), 00530 capacity_(0), 00531 offset_(0), 00532 packedMode_(false) 00533 { 00534 setFull(size, element); 00535 } |
|
Copy constructor. Definition at line 553 of file CoinIndexedVector.cpp. References gutsOfSetVector().
00553 : 00554 indices_(NULL), 00555 elements_(NULL), 00556 nElements_(0), 00557 capacity_(0), 00558 offset_(0), 00559 packedMode_(false) 00560 { 00561 gutsOfSetVector(rhs.capacity_,rhs.nElements_, rhs.indices_, rhs.elements_); 00562 } |
|
Copy constructor.2 Definition at line 566 of file CoinIndexedVector.cpp. References gutsOfSetVector().
00566 : 00567 indices_(NULL), 00568 elements_(NULL), 00569 nElements_(0), 00570 capacity_(0), 00571 offset_(0), 00572 packedMode_(false) 00573 { 00574 gutsOfSetVector(rhs->capacity_,rhs->nElements_, rhs->indices_, rhs->elements_); 00575 } |
|
Copy constructor from a PackedVectorBase. Definition at line 539 of file CoinIndexedVector.cpp. References gutsOfSetVector().
00539 : 00540 indices_(NULL), 00541 elements_(NULL), 00542 nElements_(0), 00543 capacity_(0), 00544 offset_(0), 00545 packedMode_(false) 00546 { 00547 gutsOfSetVector(rhs.getNumElements(), 00548 rhs.getIndices(), rhs.getElements()); 00549 } |
|
Destructor Definition at line 579 of file CoinIndexedVector.cpp. References elements_, indices_, and offset_.
|
|
Insert or if exists add an element into the vector Any resulting zero elements will be made tiny Definition at line 198 of file CoinIndexedVector.cpp. References capacity_, elements_, indices_, nElements_, and reserve().
00199 { 00200 if ( index < 0 ) 00201 throw CoinError("index < 0" , "setElement", "CoinIndexedVector"); 00202 if (index >= capacity_) 00203 reserve(index+1); 00204 if (elements_[index]) { 00205 element += elements_[index]; 00206 if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) { 00207 elements_[index] = element; 00208 } else { 00209 elements_[index] = 1.0e-100; 00210 } 00211 } else if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) { 00212 indices_[nElements_++] = index; 00213 elements_[index] = element; 00214 } 00215 } |
|
Borrow ownership of the arguments to this vector. Size is the length of the unpacked elements vector. Definition at line 82 of file CoinIndexedVector.cpp. References capacity_, elements_, empty(), indices_, and nElements_.
00083 { 00084 empty(); 00085 capacity_=size; 00086 nElements_ = numberIndices; 00087 indices_ = inds; 00088 elements_ = elems; 00089 00090 // whole point about borrowvector is that it is lightweight so no testing is done 00091 } |
|
capacity returns the size which could be accomodated without having to reallocate storage. Definition at line 337 of file CoinIndexedVector.hpp. References capacity_. Referenced by operator *(), operator+(), operator-(), and operator/().
00337 { return capacity_; } |
|
set all small values to zero and return number remaining
Definition at line 220 of file CoinIndexedVector.cpp. References elements_, indices_, nElements_, and packedMode_.
00221 { 00222 int number = nElements_; 00223 int i; 00224 nElements_=0; 00225 assert(!packedMode_); 00226 for (i=0;i<number;i++) { 00227 int indexValue = indices_[i]; 00228 if (fabs(elements_[indexValue])>=tolerance) { 00229 indices_[nElements_++]=indexValue; 00230 } else { 00231 elements_[indexValue]=0.0; 00232 } 00233 } 00234 return nElements_; 00235 } |
|
Get the vector as a dense vector. This is normal storage method. The user should not not delete [] this. Definition at line 108 of file CoinIndexedVector.hpp. References elements_. Referenced by append().
00108 { return elements_; } |
|
Assignment operator from a CoinPackedVectorBase. Definition at line 71 of file CoinIndexedVector.cpp. References clear(), CoinPackedVectorBase::getElements(), CoinPackedVectorBase::getIndices(), CoinPackedVectorBase::getNumElements(), and gutsOfSetVector().
00072 { 00073 clear(); 00074 gutsOfSetVector(rhs.getNumElements(), 00075 rhs.getIndices(), rhs.getElements()); 00076 return *this; 00077 } |
|
Assignment operator. Definition at line 58 of file CoinIndexedVector.cpp. References capacity_, clear(), elements_, gutsOfSetVector(), indices_, nElements_, and packedMode_.
00059 { 00060 if (this != &rhs) { 00061 clear(); 00062 packedMode_=rhs.packedMode_; 00063 gutsOfSetVector(rhs.capacity_,rhs.nElements_, rhs.indices_, rhs.elements_); 00064 } 00065 return *this; 00066 } |
|
Equal. Returns true if vectors have same length and corresponding element of each vector is equal. Definition at line 1078 of file CoinIndexedVector.cpp. References elements_, indices_, and nElements_.
01079 { 01080 const int cs = rhs.nElements_; 01081 01082 const int * cind = rhs.indices_; 01083 const double * celem = rhs.elements_; 01084 if (nElements_!=cs) 01085 return false; 01086 int i; 01087 bool okay=true; 01088 for (i=0;i<cs;i++) { 01089 int iRow = cind[i]; 01090 if (celem[iRow]!=elements_[iRow]) { 01091 okay=false; 01092 break; 01093 } 01094 } 01095 return okay; 01096 } |
|
Equal. Returns true if vectors have same length and corresponding element of each vector is equal. Definition at line 1035 of file CoinIndexedVector.cpp. References CoinPackedVectorBase::getElements(), CoinPackedVectorBase::getIndices(), and CoinPackedVectorBase::getNumElements().
01036 { 01037 const int cs = rhs.getNumElements(); 01038 01039 const int * cind = rhs.getIndices(); 01040 const double * celem = rhs.getElements(); 01041 if (nElements_!=cs) 01042 return false; 01043 int i; 01044 bool okay=true; 01045 for (i=0;i<cs;i++) { 01046 int iRow = cind[i]; 01047 if (celem[i]!=elements_[iRow]) { 01048 okay=false; 01049 break; 01050 } 01051 } 01052 return okay; 01053 } |
|
Access the i'th element of the full storage vector. Definition at line 158 of file CoinIndexedVector.cpp. References capacity_, and elements_.
|
|
Insert or if exists add an element into the vector Any resulting zero elements will be made tiny. This version does no checking Definition at line 176 of file CoinIndexedVector.hpp. References elements_, indices_, and nElements_.
00177 { 00178 if (elements_[index]) { 00179 element += elements_[index]; 00180 if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) { 00181 elements_[index] = element; 00182 } else { 00183 elements_[index] = 1.0e-100; 00184 } 00185 } else if (fabs(element)>= COIN_INDEXED_TINY_ELEMENT) { 00186 indices_[nElements_++] = index; 00187 elements_[index] = element; 00188 } 00189 }; |
|
Reserve space. If one knows the eventual size of the indexed vector, then it may be more efficient to reserve the space. Definition at line 417 of file CoinIndexedVector.cpp. References capacity_, elements_, indices_, nElements_, and offset_. Referenced by add(), append(), gutsOfSetVector(), insert(), operator *(), operator+(), operator-(), operator/(), setFull(), and truncate().
00418 { 00419 int i; 00420 // don't make allocated space smaller but do take off values 00421 if ( n < capacity_ ) { 00422 if (n<0) 00423 throw CoinError("negative capacity", "reserve", "CoinIndexedVector"); 00424 00425 int nNew=0; 00426 for (i=0;i<nElements_;i++) { 00427 int indexValue=indices_[i]; 00428 if (indexValue<n) { 00429 indices_[nNew++]=indexValue; 00430 } else { 00431 elements_[indexValue]=0.0; 00432 } 00433 } 00434 nElements_=nNew; 00435 } else if (n>capacity_) { 00436 00437 // save pointers to existing data 00438 int * tempIndices = indices_; 00439 double * tempElements = elements_; 00440 double * delTemp = elements_-offset_; 00441 00442 // allocate new space 00443 int nPlus; 00444 if (sizeof(int)==4*sizeof(char)) 00445 nPlus=(n+3)>>2; 00446 else 00447 nPlus=(n+7)>>4; 00448 indices_ = new int [n+nPlus]; 00449 memset(indices_+n,0,nPlus*sizeof(int)); 00450 // align on 64 byte boundary 00451 double * temp = new double [n+7]; 00452 offset_ = 0; 00453 #ifndef __64BIT__ 00454 int xx = (int) temp; 00455 int iBottom = xx & 63; 00456 if (iBottom) 00457 offset_ = (64-iBottom)>>3; 00458 #else 00459 long xx = (long) temp; 00460 long iBottom = xx & 63; 00461 if (iBottom) 00462 offset_ = (64-iBottom)>>3; 00463 #endif 00464 elements_ = temp + offset_;; 00465 00466 // copy data to new space 00467 // and zero out part of array 00468 if (nElements_ > 0) { 00469 CoinMemcpyN(tempIndices, nElements_, indices_); 00470 CoinMemcpyN(tempElements, capacity_, elements_); 00471 CoinZeroN(elements_+capacity_,n-capacity_); 00472 } else { 00473 CoinZeroN(elements_,n); 00474 } 00475 capacity_ = n; 00476 00477 // free old data 00478 if (tempElements) 00479 delete [] delTemp; 00480 delete [] tempIndices; 00481 } 00482 } |
|
Return ownership of the arguments to this vector. State after is empty . Definition at line 96 of file CoinIndexedVector.cpp. References capacity_, elements_, indices_, nElements_, and packedMode_.
00097 { 00098 indices_=NULL; 00099 elements_=NULL; 00100 nElements_ = 0; 00101 capacity_=0; 00102 packedMode_=false; 00103 } |
|
Scan dense region from start to < end and set up indices returns number found. Only >= tolerance Definition at line 1170 of file CoinIndexedVector.cpp. References capacity_, and packedMode_.
01171 { 01172 assert(!packedMode_); 01173 end = min(end,capacity_); 01174 start = max(start,0); 01175 int i; 01176 int number = 0; 01177 int * indices = indices_+nElements_; 01178 for (i=start;i<end;i++) { 01179 double value = elements_[i]; 01180 if (value) { 01181 if (fabs(value)>=tolerance) 01182 indices[number++] = i; 01183 else 01184 elements_[i]=0.0; 01185 } 01186 } 01187 nElements_ += number; 01188 return number; 01189 } |
|
Scan dense region and set up indices (returns number found). Only ones >= tolerance Definition at line 1163 of file CoinIndexedVector.cpp. References capacity_, and scan().
01164 { 01165 nElements_=0; 01166 return scan(0,capacity_,tolerance); 01167 } |
|
Scan dense region from start to < end and set up indices returns number found Definition at line 1147 of file CoinIndexedVector.cpp. References capacity_, and packedMode_.
01148 { 01149 assert(!packedMode_); 01150 end = min(end,capacity_); 01151 start = max(start,0); 01152 int i; 01153 int number = 0; 01154 int * indices = indices_+nElements_; 01155 for (i=start;i<end;i++) 01156 if (elements_[i]) 01157 indices[number++] = i; 01158 nElements_ += number; 01159 return number; 01160 } |
|
Elements set to have the same scalar value Definition at line 125 of file CoinIndexedVector.cpp. References clear().
00126 { 00127 clear(); 00128 gutsOfSetConstant(size, inds, value); 00129 } |
|
Set an existing element in the indexed vector The first argument is the "index" into the elements() array Definition at line 171 of file CoinIndexedVector.cpp. References elements_, indices_, and nElements_.
00172 { 00173 if ( index >= nElements_ ) 00174 throw CoinError("index >= size()", "setElement", "CoinIndexedVector"); 00175 if ( index < 0 ) 00176 throw CoinError("index < 0" , "setElement", "CoinIndexedVector"); 00177 elements_[indices_[index]] = element; 00178 } |
|
Indices are not specified and are taken to be 0,1,...,size-1 Definition at line 134 of file CoinIndexedVector.cpp. References clear(), elements_, indices_, nElements_, and reserve(). Referenced by CoinIndexedVector().
00135 { 00136 // Clear out any values presently stored 00137 clear(); 00138 00139 if (size<0) 00140 throw CoinError("negative number of indices", "setFull", "CoinIndexedVector"); 00141 00142 reserve(size); 00143 nElements_ = 0; 00144 // elements_ array is all zero 00145 int i; 00146 for (i=0;i<size;i++) { 00147 int indexValue=i; 00148 if (fabs(elems[i])>=COIN_INDEXED_TINY_ELEMENT) { 00149 elements_[indexValue]=elems[i]; 00150 indices_[nElements_++]=indexValue; 00151 } 00152 } 00153 } |
|
Set vector size, indices, and elements. Size is the length of the unpacked elements vector. The indices and elements vectors are copied into this class instance's member data. We do not check for duplicate indices Definition at line 117 of file CoinIndexedVector.cpp. References clear(), and gutsOfSetVector().
00118 { 00119 clear(); 00120 gutsOfSetVector(size, numberIndices, inds, elems); 00121 } |
|
Set vector numberIndices, indices, and elements. NumberIndices is the length of both the indices and elements vectors. The indices and elements vectors are copied into this class instance's member data. Assumed to have no duplicates Definition at line 108 of file CoinIndexedVector.cpp. References clear(), and gutsOfSetVector().
00109 { 00110 clear(); 00111 gutsOfSetVector(size, inds, elems); 00112 } |
|
Sort the indexed storage vector (increasing indices). Definition at line 283 of file CoinIndexedVector.hpp. References indices_, and nElements_.
00284 { std::sort(indices_,indices_+nElements_); } |
|
Makes nonzero tiny. This version does no checking Definition at line 192 of file CoinIndexedVector.hpp. References elements_.
|
|
A function that tests the methods in the CoinIndexedVector 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. Definition at line 19 of file CoinIndexedVectorTest.cpp.
00020 { 00021 00022 int i; 00023 // Test default constructor 00024 { 00025 CoinIndexedVector r; 00026 assert( r.indices_==NULL ); 00027 assert( r.elements_==NULL ); 00028 assert( r.getNumElements()==0 ); 00029 assert( r.capacity_==0); 00030 } 00031 00032 // Test set and get methods 00033 const int ne = 4; 00034 int inx[ne] = { 1, 3, 4, 7 }; 00035 double el[ne] = { 1.2, 3.4, 5.6, 7.8 }; 00036 { 00037 CoinIndexedVector r; 00038 assert( r.getNumElements()==0 ); 00039 00040 // Test setting/getting elements with int* & float* vectors 00041 r.setVector( ne, inx, el ); 00042 assert( r.getNumElements()==ne ); 00043 for ( i=0; i<ne; i++ ) { 00044 assert( r.getIndices()[i] == inx[i] ); 00045 assert( r[inx[i]] == el[i] ); 00046 } 00047 00048 // Test setting/getting elements with indices out of order 00049 const int ne2 = 5; 00050 int inx2[ne2] = { 2, 4, 8, 14, 3 }; 00051 double el2[ne2] = { 2.2, 4.4, 6.6, 8.8, 3.3 }; 00052 00053 r.setVector(ne2,inx2,el2); 00054 00055 assert( r.getNumElements()==ne2 ); 00056 00057 assert( r.getIndices()[0]==inx2[0] ); 00058 00059 assert( r.getIndices()[1]==inx2[1] ); 00060 00061 assert( r.getIndices()[2]==inx2[2] ); 00062 00063 assert( r.getIndices()[3]==inx2[3] ); 00064 00065 assert( r.getIndices()[4]==inx2[4] ); 00066 00067 00068 CoinIndexedVector r1(ne2,inx2,el2); 00069 assert( r == r1 ); 00070 } 00071 CoinIndexedVector r; 00072 00073 00074 { 00075 CoinIndexedVector r; 00076 const int ne = 3; 00077 int inx[ne] = { 1, 2, 3 }; 00078 double el[ne] = { 2.2, 4.4, 8.8}; 00079 r.setVector(ne,inx,el); 00080 int c = r.capacity(); 00081 // Test swap function 00082 r.swap(0,2); 00083 assert( r.getIndices()[0]==3 ); 00084 assert( r.getIndices()[1]==2 ); 00085 assert( r.getIndices()[2]==1 ); 00086 assert( r.capacity() == c ); 00087 00088 // Test the append function 00089 CoinIndexedVector s; 00090 const int nes = 4; 00091 int inxs[nes] = { 11, 12, 13, 14 }; 00092 double els[nes] = { .122, 14.4, 18.8, 19.9}; 00093 s.setVector(nes,inxs,els); 00094 r.append(s); 00095 assert( r.getNumElements()==7 ); 00096 assert( r.getIndices()[0]==3 ); 00097 assert( r.getIndices()[1]==2 ); 00098 assert( r.getIndices()[2]==1 ); 00099 assert( r.getIndices()[3]==11 ); 00100 assert( r.getIndices()[4]==12 ); 00101 assert( r.getIndices()[5]==13 ); 00102 assert( r.getIndices()[6]==14 ); 00103 00104 // Test the resize function 00105 c = r.capacity(); 00106 r.truncate(4); 00107 // we will lose 11 00108 assert( r.getNumElements()==3 ); 00109 assert( r.getIndices()[0]==3 ); 00110 assert( r.getIndices()[1]==2 ); 00111 assert( r.getIndices()[2]==1 ); 00112 assert( r.getMaxIndex() == 3 ); 00113 assert( r.getMinIndex() == 1 ); 00114 assert( r.capacity() == c ); 00115 } 00116 00117 // Test borrow and return vector 00118 { 00119 CoinIndexedVector r,r2; 00120 const int ne = 3; 00121 int inx[ne] = { 1, 2, 3 }; 00122 double el[ne] = { 2.2, 4.4, 8.8}; 00123 double els[4] = { 0.0,2.2, 4.4, 8.8}; 00124 r.setVector(ne,inx,el); 00125 r2.borrowVector(4,ne,inx,els); 00126 assert (r==r2); 00127 r2.returnVector(); 00128 assert (!r2.capacity()); 00129 assert (!r2.getNumElements()); 00130 assert (!r2.denseVector()); 00131 assert (!r2.getIndices()); 00132 } 00133 00134 // Test copy constructor and assignment operator 00135 { 00136 CoinIndexedVector rhs; 00137 { 00138 CoinIndexedVector r; 00139 { 00140 CoinIndexedVector rC1(r); 00141 assert( 0==r.getNumElements() ); 00142 assert( 0==rC1.getNumElements() ); 00143 00144 00145 r.setVector( ne, inx, el ); 00146 00147 assert( ne==r.getNumElements() ); 00148 assert( 0==rC1.getNumElements() ); 00149 } 00150 00151 CoinIndexedVector rC2(r); 00152 00153 assert( ne==r.getNumElements() ); 00154 assert( ne==rC2.getNumElements() ); 00155 00156 for ( i=0; i<ne; i++ ) { 00157 assert( r.getIndices()[i] == rC2.getIndices()[i] ); 00158 } 00159 00160 rhs=rC2; 00161 } 00162 // Test that rhs has correct values even though lhs has gone out of scope 00163 assert( rhs.getNumElements()==ne ); 00164 00165 for ( i=0; i<ne; i++ ) { 00166 assert( inx[i] == rhs.getIndices()[i] ); 00167 } 00168 } 00169 00170 // Test operator== 00171 { 00172 CoinIndexedVector v1,v2; 00173 assert( v1==v2 ); 00174 assert( v2==v1 ); 00175 assert( v1==v1 ); 00176 assert( !(v1!=v2) ); 00177 00178 v1.setVector( ne, inx, el ); 00179 assert ( !(v1==v2) ); 00180 assert ( v1!=v2 ); 00181 00182 CoinIndexedVector v3(v1); 00183 assert( v3==v1 ); 00184 assert( v3!=v2 ); 00185 00186 CoinIndexedVector v4(v2); 00187 assert( v4!=v1 ); 00188 assert( v4==v2 ); 00189 } 00190 00191 { 00192 // Test sorting of indexed vectors 00193 const int ne = 4; 00194 int inx[ne] = { 1, 4, 0, 2 }; 00195 double el[ne] = { 10., 40., 1., 20. }; 00196 CoinIndexedVector r; 00197 r.setVector(ne,inx,el); 00198 00199 // Test that indices are in increasing order 00200 r.sort(); 00201 for ( i=1; i<ne; i++ ) assert( r.getIndices()[i-1] < r.getIndices()[i] ); 00202 00203 } 00204 { 00205 // Test operator[] and indexExists() 00206 const int ne = 4; 00207 int inx[ne] = { 1, 4, 0, 2 }; 00208 double el[ne] = { 10., 40., 1., 50. }; 00209 CoinIndexedVector r; 00210 bool errorThrown = false; 00211 try { 00212 assert( r[1]==0. ); 00213 } 00214 catch (CoinError e) { 00215 errorThrown = true; 00216 } 00217 assert( errorThrown ); 00218 00219 r.setVector(ne,inx,el); 00220 00221 errorThrown = false; 00222 try { 00223 assert( r[-1]==0. ); 00224 } 00225 catch (CoinError e) { 00226 errorThrown = true; 00227 } 00228 assert( errorThrown ); 00229 00230 assert( r[ 0]==1. ); 00231 assert( r[ 1]==10.); 00232 assert( r[ 2]==50.); 00233 assert( r[ 3]==0. ); 00234 assert( r[ 4]==40.); 00235 errorThrown = false; 00236 try { 00237 assert( r[5]==0. ); 00238 } 00239 catch (CoinError e) { 00240 errorThrown = true; 00241 } 00242 assert( errorThrown ); 00243 00244 assert ( r.getMaxIndex()==4 ); 00245 assert ( r.getMinIndex()==0 ); 00246 } 00247 00248 // Test that attemping to get min/max index of a 0, 00249 // length vector 00250 { 00251 CoinIndexedVector nullVec; 00252 assert( nullVec.getMaxIndex() == 00253 /*std::numeric_limits<int>::max()*/INT_MIN/*0*/ ); 00254 assert( nullVec.getMinIndex() == 00255 /*std::numeric_limits<int>::min()*/INT_MAX/*0*/ ); 00256 } 00257 00258 // Test CoinFltEq with equivalent method 00259 { 00260 const int ne = 4; 00261 int inx1[ne] = { 1, 3, 4, 7 }; 00262 double el1[ne] = { 1.2, 3.4, 5.6, 7.8 }; 00263 int inx2[ne] = { 7, 4, 3, 1 }; 00264 double el2[ne] = { 7.8+.5, 5.6+.5, 3.4+.5, 1.2+.5 }; 00265 CoinIndexedVector v1,v2; 00266 v1.setVector(ne,inx1,el1); 00267 v2.setVector(ne,inx2,el2); 00268 } 00269 00270 { 00271 // Test reserve 00272 CoinIndexedVector v1,v2; 00273 assert( v1.capacity()==0 ); 00274 v1.reserve(6); 00275 assert( v1.capacity()==6 ); 00276 assert( v1.getNumElements()==0 ); 00277 v2=v1; 00278 assert( v2.capacity() == 6 ); 00279 assert( v2.getNumElements()==0 ); 00280 assert( v2==v1 ); 00281 v1.setVector(0,NULL,NULL); 00282 assert( v1.capacity()==6 ); 00283 assert( v1.getNumElements()==0 ); 00284 assert( v2==v1 ); 00285 v2=v1; 00286 assert( v2.capacity() == 6 ); 00287 assert( v2.getNumElements()==0 ); 00288 assert( v2==v1 ); 00289 00290 const int ne = 2; 00291 int inx[ne] = { 1, 3 }; 00292 double el[ne] = { 1.2, 3.4 }; 00293 v1.setVector(ne,inx,el); 00294 assert( v1.capacity()==6 ); 00295 assert( v1.getNumElements()==2 ); 00296 v2=v1; 00297 assert( v2.capacity()==6 ); 00298 assert( v2.getNumElements()==2 ); 00299 assert( v2==v1 ); 00300 00301 const int ne1 = 5; 00302 int inx1[ne1] = { 1, 3, 4, 5, 6 }; 00303 double el1[ne1] = { 1.2, 3.4, 5., 6., 7. }; 00304 v1.setVector(ne1,inx1,el1); 00305 assert( v1.capacity()==7 ); 00306 assert( v1.getNumElements()==5 ); 00307 v2=v1; 00308 assert( v2.capacity()==7 ); 00309 assert( v2.getNumElements()==5 ); 00310 assert( v2==v1 ); 00311 00312 const int ne2 = 8; 00313 int inx2[ne2] = { 1, 3, 4, 5, 6, 7, 8, 9 }; 00314 double el2[ne2] = { 1.2, 3.4, 5., 6., 7., 8., 9., 10. }; 00315 v1.setVector(ne2,inx2,el2); 00316 assert( v1.capacity()==10 ); 00317 assert( v1.getNumElements()==8 ); 00318 v2=v1; 00319 assert( v2.getNumElements()==8 ); 00320 assert( v2==v1 ); 00321 00322 v1.setVector(ne1,inx1,el1); 00323 assert( v1.capacity()==10 ); 00324 assert( v1.getNumElements()==5 ); 00325 v2=v1; 00326 assert( v2.capacity()==10 ); 00327 assert( v2.getNumElements()==5 ); 00328 assert( v2==v1 ); 00329 00330 v1.reserve(7); 00331 assert( v1.capacity()==10 ); 00332 assert( v1.getNumElements()==5 ); 00333 v2=v1; 00334 assert( v2.capacity()==10 ); 00335 assert( v2.getNumElements()==5 ); 00336 assert( v2==v1 ); 00337 00338 } 00339 00340 // Test the insert method 00341 { 00342 CoinIndexedVector v1; 00343 assert( v1.getNumElements()==0 ); 00344 assert( v1.capacity()==0 ); 00345 00346 v1.insert(1,1.); 00347 assert( v1.getNumElements()==1 ); 00348 assert( v1.capacity()==2 ); 00349 assert( v1.getIndices()[0] == 1 ); 00350 00351 v1.insert(10,10.); 00352 assert( v1.getNumElements()==2 ); 00353 assert( v1.capacity()==11 ); 00354 assert( v1.getIndices()[1] == 10 ); 00355 00356 v1.insert(20,20.); 00357 assert( v1.getNumElements()==3 ); 00358 assert( v1.capacity()==21 ); 00359 assert( v1.getIndices()[2] == 20 ); 00360 00361 v1.insert(30,30.); 00362 assert( v1.getNumElements()==4 ); 00363 assert( v1.capacity()==31 ); 00364 assert( v1.getIndices()[3] == 30 ); 00365 00366 v1.insert(40,40.); 00367 assert( v1.getNumElements()==5 ); 00368 assert( v1.capacity()==41 ); 00369 assert( v1.getIndices()[4] == 40 ); 00370 00371 v1.insert(50,50.); 00372 assert( v1.getNumElements()==6 ); 00373 assert( v1.capacity()==51 ); 00374 assert( v1.getIndices()[5] == 50 ); 00375 00376 CoinIndexedVector v2; 00377 const int ne1 = 3; 00378 int inx1[ne1] = { 1, 3, 4 }; 00379 double el1[ne1] = { 1.2, 3.4, 5. }; 00380 v2.setVector(ne1,inx1,el1); 00381 assert( v2.getNumElements()==3 ); 00382 assert( v2.capacity()==5 ); 00383 00384 // Test clean method - get rid of 1.2 00385 assert(v2.clean(3.0)==2); 00386 assert(v2.denseVector()[1]==0.0); 00387 00388 // Below are purely for debug - so use assert 00389 // so we won't try with false 00390 // Test checkClean 00391 v2.checkClean(); 00392 assert( v2.getNumElements()==2 ); 00393 00394 // Get rid of all 00395 assert(v2.clean(10.0)==0); 00396 v2.checkClear(); 00397 00398 } 00399 00400 { 00401 //Test setConstant and setElement 00402 CoinIndexedVector v2; 00403 const int ne1 = 3; 00404 int inx1[ne1] = { 1, 3, 4 }; 00405 v2.setConstant(ne1,inx1,3.14); 00406 assert( v2.getNumElements()==3 ); 00407 assert( v2.capacity()==5 ); 00408 assert( v2.getIndices()[0]==1 ); 00409 assert( v2.getIndices()[1]==3 ); 00410 assert( v2.getIndices()[2]==4 ); 00411 00412 assert( v2[3] == 3.14 ); 00413 00414 CoinIndexedVector v2X(ne1,inx1,3.14); 00415 assert( v2 == v2X ); 00416 00417 } 00418 00419 { 00420 //Test setFull 00421 CoinIndexedVector v2; 00422 const int ne2 = 3; 00423 double el2[ne2] = { 1., 3., 4. }; 00424 v2.setFull(ne2,el2); 00425 assert( v2.getNumElements()==3 ); 00426 assert( v2.capacity()==3 ); 00427 assert( v2.getIndices()[0]==0 ); 00428 assert( v2.getIndices()[1]==1 ); 00429 assert( v2.getIndices()[2]==2 ); 00430 00431 assert( v2[1] == 3. ); 00432 00433 CoinIndexedVector v2X(ne2,el2); 00434 assert( v2 == v2X ); 00435 00436 v2.setFull(0,el2); 00437 assert( v2[2] == 0. ); 00438 } 00439 00440 00441 #if 0 00442 // what happens when someone sets 00443 // the number of elements to be a negative number 00444 { 00445 const int ne = 4; 00446 int inx1[ne] = { 1, 3, 4, 7 }; 00447 double el1[ne] = { 1.2, 3.4, 5.6, 7.8 }; 00448 CoinIndexedVector v1; 00449 v1.set(-ne,inx1,el1); 00450 } 00451 #endif 00452 00453 00454 // Test copy constructor from ShallowPackedVector 00455 { 00456 const int ne = 4; 00457 int inx[ne] = { 1, 4, 0, 2 }; 00458 double el[ne] = { 10., 40., 1., 50. }; 00459 CoinIndexedVector std(ne,inx,el); 00460 CoinShallowPackedVector * spvP = new CoinShallowPackedVector(ne,inx,el); 00461 CoinIndexedVector pv(*spvP); 00462 assert( pv == std ); 00463 delete spvP; 00464 assert( pv == std ); 00465 } 00466 00467 // Test assignment from ShallowPackedVector 00468 { 00469 const int ne = 4; 00470 int inx[ne] = { 1, 4, 0, 2 }; 00471 double el[ne] = { 10., 40., 1., 50. }; 00472 CoinIndexedVector std(ne,inx,el); 00473 CoinShallowPackedVector * spvP = new CoinShallowPackedVector(ne,inx,el); 00474 CoinIndexedVector pv; 00475 pv = *spvP; 00476 assert( pv == std ); 00477 delete spvP; 00478 assert( pv == std ); 00479 } 00480 00481 { 00482 // Test that sample usage works 00483 00484 const int ne = 4; 00485 int inx[ne] = { 1, 4, 0, 2 }; 00486 double el[ne] = { 10., 40., 1., 50. }; 00487 CoinIndexedVector r(ne,inx,el); 00488 00489 assert( r.getIndices()[0]== 1 ); 00490 assert( r.getIndices()[1]== 4 ); 00491 assert( r.getIndices()[2]== 0 ); 00492 assert( r.getIndices()[3]== 2 ); 00493 00494 assert( r[ 0]==1. ); 00495 assert( r[ 1]==10.); 00496 assert( r[ 2]==50.); 00497 assert( r[ 3]==0. ); 00498 assert( r[ 4]==40.); 00499 00500 r.sortIncrElement(); 00501 00502 assert( r.getIndices()[0]== 0 ); 00503 assert( r.getIndices()[1]== 1 ); 00504 assert( r.getIndices()[2]== 4 ); 00505 assert( r.getIndices()[3]== 2 ); 00506 00507 assert( r[ 0]==1. ); 00508 assert( r[ 1]==10.); 00509 assert( r[ 2]==50.); 00510 assert( r[ 3]==0. ); 00511 assert( r[ 4]==40.); 00512 00513 CoinIndexedVector r1; 00514 r1=r; 00515 assert( r==r1 ); 00516 00517 CoinIndexedVector add = r + r1; 00518 assert( add[0] == 1.+ 1. ); 00519 assert( add[1] == 10.+10. ); 00520 assert( add[2] == 50.+50. ); 00521 assert( add[3] == 0.+ 0. ); 00522 assert( add[4] == 40.+40. ); 00523 00524 } 00525 00526 } |