| 1 | /* $Id: CoinShallowPackedVector.hpp 1372 2011-01-03 23:31:00Z lou $ */ |
| 2 | // Copyright (C) 2000, International Business Machines |
| 3 | // Corporation and others. All Rights Reserved. |
| 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
| 5 | |
| 6 | #ifndef CoinShallowPackedVector_H |
| 7 | #define CoinShallowPackedVector_H |
| 8 | |
| 9 | #if defined(_MSC_VER) |
| 10 | // Turn off compiler warning about long names |
| 11 | # pragma warning(disable:4786) |
| 12 | #endif |
| 13 | |
| 14 | #include "CoinError.hpp" |
| 15 | #include "CoinPackedVectorBase.hpp" |
| 16 | |
| 17 | /** Shallow Sparse Vector |
| 18 | |
| 19 | This class is for sparse vectors where the indices and |
| 20 | elements are stored elsewhere. This class only maintains |
| 21 | pointers to the indices and elements. Since this class |
| 22 | does not own the index and element data it provides |
| 23 | read only access to to the data. An CoinSparsePackedVector |
| 24 | must be used when the sparse vector's data will be altered. |
| 25 | |
| 26 | This class stores pointers to the vectors. |
| 27 | It does not actually contain the vectors. |
| 28 | |
| 29 | Here is a sample usage: |
| 30 | @verbatim |
| 31 | const int ne = 4; |
| 32 | int inx[ne] = { 1, 4, 0, 2 }; |
| 33 | double el[ne] = { 10., 40., 1., 50. }; |
| 34 | |
| 35 | // Create vector and set its value |
| 36 | CoinShallowPackedVector r(ne,inx,el); |
| 37 | |
| 38 | // access each index and element |
| 39 | assert( r.indices ()[0]== 1 ); |
| 40 | assert( r.elements()[0]==10. ); |
| 41 | assert( r.indices ()[1]== 4 ); |
| 42 | assert( r.elements()[1]==40. ); |
| 43 | assert( r.indices ()[2]== 0 ); |
| 44 | assert( r.elements()[2]== 1. ); |
| 45 | assert( r.indices ()[3]== 2 ); |
| 46 | assert( r.elements()[3]==50. ); |
| 47 | |
| 48 | // access as a full storage vector |
| 49 | assert( r[ 0]==1. ); |
| 50 | assert( r[ 1]==10.); |
| 51 | assert( r[ 2]==50.); |
| 52 | assert( r[ 3]==0. ); |
| 53 | assert( r[ 4]==40.); |
| 54 | |
| 55 | // Tests for equality and equivalence |
| 56 | CoinShallowPackedVector r1; |
| 57 | r1=r; |
| 58 | assert( r==r1 ); |
| 59 | r.sort(CoinIncrElementOrdered()); |
| 60 | assert( r!=r1 ); |
| 61 | |
| 62 | // Add packed vectors. |
| 63 | // Similarly for subtraction, multiplication, |
| 64 | // and division. |
| 65 | CoinPackedVector add = r + r1; |
| 66 | assert( add[0] == 1.+ 1. ); |
| 67 | assert( add[1] == 10.+10. ); |
| 68 | assert( add[2] == 50.+50. ); |
| 69 | assert( add[3] == 0.+ 0. ); |
| 70 | assert( add[4] == 40.+40. ); |
| 71 | assert( r.sum() == 10.+40.+1.+50. ); |
| 72 | @endverbatim |
| 73 | */ |
| 74 | class CoinShallowPackedVector : public CoinPackedVectorBase { |
| 75 | friend void CoinShallowPackedVectorUnitTest(); |
| 76 | |
| 77 | public: |
| 78 | |
| 79 | /**@name Get methods */ |
| 80 | //@{ |
| 81 | /// Get length of indices and elements vectors |
| 82 | virtual int getNumElements() const override { return nElements_; } |
| 83 | /// Get indices of elements |
| 84 | virtual const int * getIndices() const override { return indices_; } |
| 85 | /// Get element values |
| 86 | virtual const double * getElements() const override { return elements_; } |
| 87 | //@} |
| 88 | |
| 89 | /**@name Set methods */ |
| 90 | //@{ |
| 91 | /// Reset the vector (as if were just created an empty vector) |
| 92 | void clear(); |
| 93 | /** Assignment operator. */ |
| 94 | CoinShallowPackedVector& operator=(const CoinShallowPackedVector & x); |
| 95 | /** Assignment operator from a CoinPackedVectorBase. */ |
| 96 | CoinShallowPackedVector& operator=(const CoinPackedVectorBase & x); |
| 97 | /** just like the explicit constructor */ |
| 98 | void setVector(int size, const int * indices, const double * elements, |
| 99 | bool testForDuplicateIndex = true); |
| 100 | //@} |
| 101 | |
| 102 | /**@name Methods to create, set and destroy */ |
| 103 | //@{ |
| 104 | /** Default constructor. */ |
| 105 | CoinShallowPackedVector(bool testForDuplicateIndex = true); |
| 106 | /** Explicit Constructor. |
| 107 | Set vector size, indices, and elements. Size is the length of both the |
| 108 | indices and elements vectors. The indices and elements vectors are not |
| 109 | copied into this class instance. The ShallowPackedVector only maintains |
| 110 | the pointers to the indices and elements vectors. <br> |
| 111 | The last argument specifies whether the creator of the object knows in |
| 112 | advance that there are no duplicate indices. |
| 113 | */ |
| 114 | CoinShallowPackedVector(int size, |
| 115 | const int * indices, const double * elements, |
| 116 | bool testForDuplicateIndex = true); |
| 117 | /** Copy constructor from the base class. */ |
| 118 | CoinShallowPackedVector(const CoinPackedVectorBase &); |
| 119 | /** Copy constructor. */ |
| 120 | CoinShallowPackedVector(const CoinShallowPackedVector &); |
| 121 | /** Destructor. */ |
| 122 | ~CoinShallowPackedVector() {} |
| 123 | /// Print vector information. |
| 124 | void print(); |
| 125 | //@} |
| 126 | |
| 127 | private: |
| 128 | /**@name Private member data */ |
| 129 | //@{ |
| 130 | /// Vector indices |
| 131 | const int * indices_; |
| 132 | ///Vector elements |
| 133 | const double * elements_; |
| 134 | /// Size of indices and elements vectors |
| 135 | int nElements_; |
| 136 | //@} |
| 137 | }; |
| 138 | |
| 139 | //############################################################################# |
| 140 | /** A function that tests the methods in the CoinShallowPackedVector class. The |
| 141 | only reason for it not to be a member method is that this way it doesn't |
| 142 | have to be compiled into the library. And that's a gain, because the |
| 143 | library should be compiled with optimization on, but this method should be |
| 144 | compiled with debugging. */ |
| 145 | void |
| 146 | CoinShallowPackedVectorUnitTest(); |
| 147 | |
| 148 | #endif |
| 149 | |