| 1 | /* $Id: CoinDenseVector.cpp 1373 2011-01-03 23:57:44Z lou $ */ | 
|---|
| 2 | // Copyright (C) 2003, International Business Machines | 
|---|
| 3 | // Corporation and others.  All Rights Resized. | 
|---|
| 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). | 
|---|
| 5 |  | 
|---|
| 6 | #if defined(_MSC_VER) | 
|---|
| 7 | // Turn off compiler warning about long names | 
|---|
| 8 | #  pragma warning(disable:4786) | 
|---|
| 9 | #endif | 
|---|
| 10 |  | 
|---|
| 11 | #include <cassert> | 
|---|
| 12 | #include "CoinDenseVector.hpp" | 
|---|
| 13 | #include "CoinHelperFunctions.hpp" | 
|---|
| 14 |  | 
|---|
| 15 | //############################################################################# | 
|---|
| 16 |  | 
|---|
| 17 | template <typename T> void | 
|---|
| 18 | CoinDenseVector<T>::clear() | 
|---|
| 19 | { | 
|---|
| 20 | memset(elements_, 0, nElements_*sizeof(T)); | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | //############################################################################# | 
|---|
| 24 |  | 
|---|
| 25 | template <typename T> CoinDenseVector<T> & | 
|---|
| 26 | CoinDenseVector<T>::operator=(const CoinDenseVector<T> & rhs) | 
|---|
| 27 | { | 
|---|
| 28 | if (this != &rhs) { | 
|---|
| 29 | setVector(rhs.getNumElements(), rhs.getElements()); | 
|---|
| 30 | } | 
|---|
| 31 | return *this; | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | //############################################################################# | 
|---|
| 35 |  | 
|---|
| 36 | template <typename T> void | 
|---|
| 37 | CoinDenseVector<T>::setVector(int size, const T * elems) | 
|---|
| 38 | { | 
|---|
| 39 | resize(size); | 
|---|
| 40 | CoinMemcpyN( elems,size,elements_); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | //############################################################################# | 
|---|
| 44 |  | 
|---|
| 45 | template <typename T> void | 
|---|
| 46 | CoinDenseVector<T>::setConstant(int size, T value) | 
|---|
| 47 | { | 
|---|
| 48 | resize(size); | 
|---|
| 49 | for(int i=0; i<size; i++) | 
|---|
| 50 | elements_[i] = value; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | //############################################################################# | 
|---|
| 54 |  | 
|---|
| 55 | template <typename T> void | 
|---|
| 56 | CoinDenseVector<T>::resize(int newsize, T value) | 
|---|
| 57 | { | 
|---|
| 58 | if (newsize != nElements_){ | 
|---|
| 59 | assert(newsize > 0); | 
|---|
| 60 | T *newarray = new T[newsize]; | 
|---|
| 61 | int cpysize = CoinMin(newsize, nElements_); | 
|---|
| 62 | CoinMemcpyN( elements_,cpysize,newarray); | 
|---|
| 63 | delete[] elements_; | 
|---|
| 64 | elements_ = newarray; | 
|---|
| 65 | nElements_ = newsize; | 
|---|
| 66 | for(int i=cpysize; i<newsize; i++) | 
|---|
| 67 | elements_[i] = value; | 
|---|
| 68 | } | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | //############################################################################# | 
|---|
| 72 |  | 
|---|
| 73 | template <typename T> void | 
|---|
| 74 | CoinDenseVector<T>::setElement(int index, T element) | 
|---|
| 75 | { | 
|---|
| 76 | assert(index >= 0 && index < nElements_); | 
|---|
| 77 | elements_[index] = element; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | //############################################################################# | 
|---|
| 81 |  | 
|---|
| 82 | template <typename T> void | 
|---|
| 83 | CoinDenseVector<T>::append(const CoinDenseVector<T> & caboose) | 
|---|
| 84 | { | 
|---|
| 85 | const int s = nElements_; | 
|---|
| 86 | const int cs = caboose.getNumElements(); | 
|---|
| 87 | int newsize = s + cs; | 
|---|
| 88 | resize(newsize); | 
|---|
| 89 | const T * celem = caboose.getElements(); | 
|---|
| 90 | CoinDisjointCopyN(celem, cs, elements_ + s); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | //############################################################################# | 
|---|
| 94 |  | 
|---|
| 95 | template <typename T> void | 
|---|
| 96 | CoinDenseVector<T>::operator+=(T value) | 
|---|
| 97 | { | 
|---|
| 98 | for(int i=0; i<nElements_; i++) | 
|---|
| 99 | elements_[i] += value; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | //----------------------------------------------------------------------------- | 
|---|
| 103 |  | 
|---|
| 104 | template <typename T> void | 
|---|
| 105 | CoinDenseVector<T>::operator-=(T value) | 
|---|
| 106 | { | 
|---|
| 107 | for(int i=0; i<nElements_; i++) | 
|---|
| 108 | elements_[i] -= value; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | //----------------------------------------------------------------------------- | 
|---|
| 112 |  | 
|---|
| 113 | template <typename T> void | 
|---|
| 114 | CoinDenseVector<T>::operator*=(T value) | 
|---|
| 115 | { | 
|---|
| 116 | for(int i=0; i<nElements_; i++) | 
|---|
| 117 | elements_[i] *= value; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | //----------------------------------------------------------------------------- | 
|---|
| 121 |  | 
|---|
| 122 | template <typename T> void | 
|---|
| 123 | CoinDenseVector<T>::operator/=(T value) | 
|---|
| 124 | { | 
|---|
| 125 | for(int i=0; i<nElements_; i++) | 
|---|
| 126 | elements_[i] /= value; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | //############################################################################# | 
|---|
| 130 |  | 
|---|
| 131 | template <typename T> CoinDenseVector<T>::CoinDenseVector(): | 
|---|
| 132 | nElements_(0), | 
|---|
| 133 | elements_(NULL) | 
|---|
| 134 | {} | 
|---|
| 135 |  | 
|---|
| 136 | //############################################################################# | 
|---|
| 137 |  | 
|---|
| 138 | template <typename T> | 
|---|
| 139 | CoinDenseVector<T>::CoinDenseVector(int size, const T * elems): | 
|---|
| 140 | nElements_(0), | 
|---|
| 141 | elements_(NULL) | 
|---|
| 142 | { | 
|---|
| 143 | gutsOfSetVector(size, elems); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | //----------------------------------------------------------------------------- | 
|---|
| 147 |  | 
|---|
| 148 | template <typename T> CoinDenseVector<T>::CoinDenseVector(int size, T value): | 
|---|
| 149 | nElements_(0), | 
|---|
| 150 | elements_(NULL) | 
|---|
| 151 | { | 
|---|
| 152 | gutsOfSetConstant(size, value); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | //----------------------------------------------------------------------------- | 
|---|
| 156 |  | 
|---|
| 157 | template <typename T> | 
|---|
| 158 | CoinDenseVector<T>::CoinDenseVector(const CoinDenseVector<T> & rhs): | 
|---|
| 159 | nElements_(0), | 
|---|
| 160 | elements_(NULL) | 
|---|
| 161 | { | 
|---|
| 162 | setVector(rhs.getNumElements(), rhs.getElements()); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | //----------------------------------------------------------------------------- | 
|---|
| 166 |  | 
|---|
| 167 | template <typename T> CoinDenseVector<T>::~CoinDenseVector () | 
|---|
| 168 | { | 
|---|
| 169 | delete [] elements_; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | //############################################################################# | 
|---|
| 173 |  | 
|---|
| 174 | template <typename T> void | 
|---|
| 175 | CoinDenseVector<T>::gutsOfSetVector(int size, const T * elems) | 
|---|
| 176 | { | 
|---|
| 177 | if ( size != 0 ) { | 
|---|
| 178 | resize(size); | 
|---|
| 179 | nElements_ = size; | 
|---|
| 180 | CoinDisjointCopyN(elems, size, elements_); | 
|---|
| 181 | } | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | //----------------------------------------------------------------------------- | 
|---|
| 185 |  | 
|---|
| 186 | template <typename T> void | 
|---|
| 187 | CoinDenseVector<T>::gutsOfSetConstant(int size, T value) | 
|---|
| 188 | { | 
|---|
| 189 | if ( size != 0 ) { | 
|---|
| 190 | resize(size); | 
|---|
| 191 | nElements_ = size; | 
|---|
| 192 | CoinFillN(elements_, size, value); | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | //############################################################################# | 
|---|
| 197 | /** Access the i'th element of the dense vector.  */ | 
|---|
| 198 | template <typename T> T & | 
|---|
| 199 | CoinDenseVector<T>::operator[](int index) const | 
|---|
| 200 | { | 
|---|
| 201 | assert(index >= 0 && index < nElements_); | 
|---|
| 202 | T *where = elements_ + index; | 
|---|
| 203 | return *where; | 
|---|
| 204 | } | 
|---|
| 205 | //############################################################################# | 
|---|
| 206 |  | 
|---|
| 207 | // template class CoinDenseVector<int>; This works but causes warning messages | 
|---|
| 208 | template class CoinDenseVector<float>; | 
|---|
| 209 | template class CoinDenseVector<double>; | 
|---|
| 210 |  | 
|---|