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
17template <typename T> void
18CoinDenseVector<T>::clear()
19{
20 memset(elements_, 0, nElements_*sizeof(T));
21}
22
23//#############################################################################
24
25template <typename T> CoinDenseVector<T> &
26CoinDenseVector<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
36template <typename T> void
37CoinDenseVector<T>::setVector(int size, const T * elems)
38{
39 resize(size);
40 CoinMemcpyN( elems,size,elements_);
41}
42
43//#############################################################################
44
45template <typename T> void
46CoinDenseVector<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
55template <typename T> void
56CoinDenseVector<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
73template <typename T> void
74CoinDenseVector<T>::setElement(int index, T element)
75{
76 assert(index >= 0 && index < nElements_);
77 elements_[index] = element;
78}
79
80//#############################################################################
81
82template <typename T> void
83CoinDenseVector<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
95template <typename T> void
96CoinDenseVector<T>::operator+=(T value)
97{
98 for(int i=0; i<nElements_; i++)
99 elements_[i] += value;
100}
101
102//-----------------------------------------------------------------------------
103
104template <typename T> void
105CoinDenseVector<T>::operator-=(T value)
106{
107 for(int i=0; i<nElements_; i++)
108 elements_[i] -= value;
109}
110
111//-----------------------------------------------------------------------------
112
113template <typename T> void
114CoinDenseVector<T>::operator*=(T value)
115{
116 for(int i=0; i<nElements_; i++)
117 elements_[i] *= value;
118}
119
120//-----------------------------------------------------------------------------
121
122template <typename T> void
123CoinDenseVector<T>::operator/=(T value)
124{
125 for(int i=0; i<nElements_; i++)
126 elements_[i] /= value;
127}
128
129//#############################################################################
130
131template <typename T> CoinDenseVector<T>::CoinDenseVector():
132 nElements_(0),
133 elements_(NULL)
134{}
135
136//#############################################################################
137
138template <typename T>
139CoinDenseVector<T>::CoinDenseVector(int size, const T * elems):
140 nElements_(0),
141 elements_(NULL)
142{
143 gutsOfSetVector(size, elems);
144}
145
146//-----------------------------------------------------------------------------
147
148template <typename T> CoinDenseVector<T>::CoinDenseVector(int size, T value):
149 nElements_(0),
150 elements_(NULL)
151{
152 gutsOfSetConstant(size, value);
153}
154
155//-----------------------------------------------------------------------------
156
157template <typename T>
158CoinDenseVector<T>::CoinDenseVector(const CoinDenseVector<T> & rhs):
159 nElements_(0),
160 elements_(NULL)
161{
162 setVector(rhs.getNumElements(), rhs.getElements());
163}
164
165//-----------------------------------------------------------------------------
166
167template <typename T> CoinDenseVector<T>::~CoinDenseVector ()
168{
169 delete [] elements_;
170}
171
172//#############################################################################
173
174template <typename T> void
175CoinDenseVector<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
186template <typename T> void
187CoinDenseVector<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. */
198template <typename T> T &
199CoinDenseVector<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
208template class CoinDenseVector<float>;
209template class CoinDenseVector<double>;
210