Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CoinDenseVector.cpp

00001 // Copyright (C) 2003, International Business Machines
00002 // Corporation and others.  All Rights Resized.
00003 #if defined(_MSC_VER)
00004 // Turn off compiler warning about long names
00005 #  pragma warning(disable:4786)
00006 #endif
00007 
00008 #include <cassert>
00009 #include "CoinDenseVector.hpp"
00010 #include "CoinHelperFunctions.hpp"
00011 
00012 #define min(a,b)        ( (a) < (b) ? (a) : (b) )
00013 //#############################################################################
00014 
00015 template <typename T> void
00016 CoinDenseVector<T>::clear()
00017 {
00018    memset(elements_, 0, nElements_*sizeof(T));
00019 }
00020 
00021 //#############################################################################
00022 
00023 template <typename T> CoinDenseVector<T> &
00024 CoinDenseVector<T>::operator=(const CoinDenseVector<T> & rhs)
00025 {
00026    if (this != &rhs) {
00027      setVector(rhs.getNumElements(), rhs.getElements());
00028    }
00029    return *this;
00030 }
00031 
00032 //#############################################################################
00033 
00034 template <typename T> void
00035 CoinDenseVector<T>::setVector(int size, const T * elems)
00036 {
00037    resize(size);
00038    memcpy(elements_, elems, size*sizeof(T));
00039 }
00040 
00041 //#############################################################################
00042 
00043 template <typename T> void
00044 CoinDenseVector<T>::setConstant(int size, T value)
00045 {
00046    resize(size);
00047    for(int i=0; i<size; i++)
00048      elements_[i] = value;
00049 }
00050 
00051 //#############################################################################
00052 
00053 template <typename T> void
00054 CoinDenseVector<T>::resize(int newsize, T value)
00055 {
00056   if (newsize != nElements_){   
00057     assert(newsize > 0);
00058     T *newarray = new T[newsize];
00059     int cpysize = min(newsize, nElements_);
00060     memcpy(newarray, elements_, cpysize*sizeof(T));
00061     delete[] elements_;
00062     elements_ = newarray;
00063     nElements_ = newsize;
00064     for(int i=cpysize; i<newsize; i++)
00065       elements_[i] = value;
00066   }
00067 }
00068 
00069 //#############################################################################
00070 
00071 template <typename T> void
00072 CoinDenseVector<T>::setElement(int index, T element)
00073 {
00074   assert(index >= 0 && index < nElements_);
00075    elements_[index] = element;
00076 }
00077 
00078 //#############################################################################
00079 
00080 template <typename T> void
00081 CoinDenseVector<T>::append(const CoinDenseVector<T> & caboose)
00082 {
00083    const int s = nElements_;
00084    const int cs = caboose.getNumElements();
00085    int newsize = s + cs;
00086    resize(newsize);
00087    const T * celem = caboose.getElements();
00088    CoinDisjointCopyN(celem, cs, elements_ + s);
00089 }
00090 
00091 //#############################################################################
00092 
00093 template <typename T> void
00094 CoinDenseVector<T>::operator+=(T value) 
00095 {
00096   for(int i=0; i<nElements_; i++)
00097     elements_[i] += value;
00098 }
00099 
00100 //-----------------------------------------------------------------------------
00101 
00102 template <typename T> void
00103 CoinDenseVector<T>::operator-=(T value) 
00104 {
00105   for(int i=0; i<nElements_; i++)
00106     elements_[i] -= value;
00107 }
00108 
00109 //-----------------------------------------------------------------------------
00110 
00111 template <typename T> void
00112 CoinDenseVector<T>::operator*=(T value) 
00113 {
00114   for(int i=0; i<nElements_; i++)
00115     elements_[i] *= value;
00116 }
00117 
00118 //-----------------------------------------------------------------------------
00119 
00120 template <typename T> void
00121 CoinDenseVector<T>::operator/=(T value) 
00122 {
00123   for(int i=0; i<nElements_; i++)
00124     elements_[i] /= value;
00125 }
00126 
00127 //#############################################################################
00128 
00129 template <typename T> CoinDenseVector<T>::CoinDenseVector():
00130    nElements_(0),
00131    elements_(NULL)
00132 {}
00133   
00134 //#############################################################################
00135 
00136 template <typename T> 
00137 CoinDenseVector<T>::CoinDenseVector(int size, const T * elems):
00138    nElements_(0),
00139    elements_(NULL)
00140 {
00141   gutsOfSetVector(size, elems);
00142 }
00143 
00144 //-----------------------------------------------------------------------------
00145 
00146 template <typename T> CoinDenseVector<T>::CoinDenseVector(int size, T value):
00147    nElements_(0),
00148    elements_(NULL)
00149 {
00150   gutsOfSetConstant(size, value);
00151 }
00152 
00153 //-----------------------------------------------------------------------------
00154 
00155 template <typename T> 
00156 CoinDenseVector<T>::CoinDenseVector(const CoinDenseVector<T> & rhs):
00157    nElements_(0),
00158    elements_(NULL)
00159 {
00160      setVector(rhs.getNumElements(), rhs.getElements());
00161 }
00162 
00163 //-----------------------------------------------------------------------------
00164 
00165 template <typename T> CoinDenseVector<T>::~CoinDenseVector ()
00166 {
00167    delete [] elements_;
00168 }
00169 
00170 //#############################################################################
00171 
00172 template <typename T> void
00173 CoinDenseVector<T>::gutsOfSetVector(int size, const T * elems)
00174 {
00175    if ( size != 0 ) {
00176       resize(size);
00177       nElements_ = size;
00178       CoinDisjointCopyN(elems, size, elements_);
00179    }
00180 }
00181 
00182 //-----------------------------------------------------------------------------
00183 
00184 template <typename T> void
00185 CoinDenseVector<T>::gutsOfSetConstant(int size, T value)
00186 {
00187    if ( size != 0 ) {
00188       resize(size);
00189       nElements_ = size;
00190       CoinFillN(elements_, size, value);
00191    }
00192 }
00193 
00194 //#############################################################################
00196 template <typename T> T &
00197 CoinDenseVector<T>::operator[](int index) const
00198 {
00199   assert(index >= 0 && index < nElements_);
00200   T *where = elements_ + index;
00201   return *where;
00202 }
00203 //#############################################################################
00204 
00205 // template class CoinDenseVector<int>; This works but causes warning messages
00206 template class CoinDenseVector<float>;
00207 template class CoinDenseVector<double>;

Generated on Wed Dec 3 14:34:16 2003 for Coin by doxygen 1.3.5