00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef __ARRAYVALUE_HPP__
00050 #define __ARRAYVALUE_HPP__
00051
00052
00053 #include <iostream>
00054 #include <openfluid/dllexport.hpp>
00055 #include <openfluid/base/OFException.hpp>
00056
00057
00058 namespace openfluid { namespace core {
00059
00060
00064 template <class T>
00065 class DLLEXPORT Vector
00066 {
00067 private :
00068 T* m_Data;
00069 unsigned long m_Size;
00070
00071 bool allocate(unsigned long Size);
00072
00073 void init();
00074
00075 static void copy(const Vector& Source, Vector& Dest);
00076
00077 public :
00078
00082 Vector();
00083
00087 Vector(const Vector &Vector);
00088
00092 Vector(unsigned long Size);
00093
00097 Vector(unsigned long Size, T InitValue);
00098
00102 Vector(T* Data, unsigned long Size);
00103
00107 ~Vector();
00108
00112 unsigned long getSize() const { return m_Size; };
00113
00117 unsigned long size() const { return getSize(); };
00118
00122 T* getData() const { return m_Data; };
00123
00127 void setData(T* Data, unsigned long Size);
00128
00132 T getElement(unsigned long Index) const;
00133
00137 T at(unsigned long Index) const { return getElement(Index); };
00138
00142 void setElement(unsigned long Index, T Element);
00143
00147 T& operator[](unsigned long Index);
00148
00152 Vector<T>& operator = (const Vector &A);
00153
00157 void clear();
00158
00159 };
00160
00161
00162
00163
00164 template <class T>
00165 Vector<T>::Vector()
00166 {
00167 init();
00168 }
00169
00170
00171
00172
00173
00174 template <class T>
00175 Vector<T>::Vector(const Vector &A)
00176 {
00177 init();
00178
00179 if (!allocate(A.m_Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(const Vector)","Cannot allocate memory");
00180
00181 std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
00182
00183 }
00184
00185
00186
00187
00188
00189 template <class T>
00190 Vector<T>::Vector(unsigned long Size)
00191 {
00192 init();
00193
00194 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(Size)","Cannot allocate memory");
00195 }
00196
00197
00198
00199
00200
00201 template <class T>
00202 Vector<T>::Vector(unsigned long Size, T InitValue)
00203 {
00204 init();
00205
00206
00207 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(Size,T)","Cannot allocate memory");
00208
00209
00210 if (m_Data != NULL)
00211 {
00212 unsigned long i;
00213 for (i=0;i<m_Size;i++) m_Data[i] = InitValue;
00214 }
00215
00216 }
00217
00218
00219
00220
00221
00222
00223 template <class T>
00224 Vector<T>::Vector(T* Data, unsigned long Size)
00225 {
00226 init();
00227
00228 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(T*,Size)","Cannot allocate memory");
00229
00230 std::copy(Data, Data + Size, m_Data);
00231
00232 }
00233
00234
00235
00236
00237
00238 template <class T>
00239 Vector<T>::~Vector()
00240 {
00241 if (m_Data != NULL) clear();
00242 }
00243
00244
00245
00246
00247
00248 template <class T>
00249 bool Vector<T>::allocate(unsigned long Size)
00250 {
00251
00252 if (Size > 0)
00253 {
00254 m_Data = new T[Size];
00255 if (m_Data != NULL) m_Size = Size;
00256 else
00257 {
00258 return false;
00259 }
00260 }
00261
00262 return true;
00263
00264
00265 }
00266
00267
00268
00269
00270 template <class T>
00271 void Vector<T>::setData(T* Data, unsigned long Size)
00272 {
00273 clear();
00274
00275 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::setData","Cannot allocate memory");
00276
00277 if (m_Data != NULL) std::copy(Data, Data + Size, m_Data);
00278
00279 }
00280
00281
00282
00283
00284
00285 template <class T>
00286 T Vector<T>::getElement(unsigned long Index) const
00287 {
00288 if (Index >= m_Size) throw openfluid::base::OFException("OpenFLUID framework","Vector::getElement","element access range error");
00289 return m_Data[Index];
00290 }
00291
00292
00293
00294
00295
00296 template <class T>
00297 void Vector<T>::setElement(unsigned long Index, T Element)
00298 {
00299 if (Index >= m_Size) throw openfluid::base::OFException("OpenFLUID framework","Vector::setElement","element access range error");
00300 m_Data[Index] = Element;
00301 }
00302
00303
00304
00305
00306
00307
00308 template <class T>
00309 T& Vector<T>::operator[](unsigned long Index)
00310 {
00311 if (Index >= m_Size) throw openfluid::base::OFException("OpenFLUID framework","Vector::operator[]","element access range error");
00312 return m_Data[Index];
00313 }
00314
00315
00316
00317
00318 template <class T>
00319 Vector<T>& Vector<T>::operator=(const Vector &A)
00320 {
00321
00322 if (this == &A) return *this;
00323
00324 clear();
00325
00326 allocate(A.m_Size);
00327 std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
00328
00329 return *this;
00330 }
00331
00332
00333
00334
00335
00336 template <class T>
00337 void Vector<T>::init()
00338 {
00339 m_Data = NULL;
00340 m_Size = 0;
00341 }
00342
00343
00344
00345
00346 template <class T>
00347 void Vector<T>::clear()
00348 {
00349
00350 delete [] m_Data;
00351 init();
00352 }
00353
00354
00355
00356
00357 template <class T>
00358 void Vector<T>::copy(const Vector& Source, Vector& Dest)
00359 {
00360 Dest.clear;
00361 Dest.allocate(Source.m_Size);
00362 for (unsigned long i = 0; i < Source.m_Size;i++)
00363 {
00364 Dest.m_Data[i] = Source.m_Data[i];
00365 }
00366 }
00367
00368
00369
00370 } }
00371
00372
00373 #endif