Go to the documentation of this file.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 __VECTOR_HPP__
00050 #define __VECTOR_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 protected :
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
00113 unsigned long getSize() const { return m_Size; };
00114
00119 unsigned long size() const { return getSize(); };
00120
00124 T* getData() const { return m_Data; };
00125
00129 void setData(T* Data, unsigned long Size);
00130
00134 T getElement(unsigned long Index) const;
00135
00139 inline T at(unsigned long Index) const { return getElement(Index); };
00140
00144 inline T get(unsigned long Index) const { return getElement(Index); };
00145
00146
00150 void setElement(unsigned long Index, T Element);
00151
00155 inline void set(unsigned long Index, T Element) { setElement(Index,Element); };
00156
00157
00161 T& operator[](unsigned long Index);
00162
00166 Vector<T>& operator = (const Vector &A);
00167
00171 void fill(const T& Val);
00172
00173
00177 void clear();
00178
00179 };
00180
00181
00182
00183
00184 template <class T>
00185 Vector<T>::Vector()
00186 {
00187 init();
00188 }
00189
00190
00191
00192
00193
00194 template <class T>
00195 Vector<T>::Vector(const Vector &A)
00196 {
00197 init();
00198
00199 if (!allocate(A.m_Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(const Vector)","Cannot allocate memory");
00200
00201 std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
00202
00203 }
00204
00205
00206
00207
00208
00209 template <class T>
00210 Vector<T>::Vector(unsigned long Size)
00211 {
00212 init();
00213
00214 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(Size)","Cannot allocate memory");
00215 }
00216
00217
00218
00219
00220
00221 template <class T>
00222 Vector<T>::Vector(unsigned long Size, T InitValue)
00223 {
00224 init();
00225
00226
00227 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(Size,T)","Cannot allocate memory");
00228
00229
00230 if (m_Data != NULL)
00231 {
00232 unsigned long i;
00233 for (i=0;i<m_Size;i++) m_Data[i] = InitValue;
00234 }
00235
00236 }
00237
00238
00239
00240
00241
00242
00243 template <class T>
00244 Vector<T>::Vector(T* Data, unsigned long Size)
00245 {
00246 init();
00247
00248 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::Vector(T*,Size)","Cannot allocate memory");
00249
00250 std::copy(Data, Data + Size, m_Data);
00251
00252 }
00253
00254
00255
00256
00257
00258 template <class T>
00259 Vector<T>::~Vector()
00260 {
00261 if (m_Data != NULL) clear();
00262 }
00263
00264
00265
00266
00267
00268 template <class T>
00269 bool Vector<T>::allocate(unsigned long Size)
00270 {
00271
00272 if (Size > 0)
00273 {
00274 m_Data = new T[Size];
00275 if (m_Data != NULL) m_Size = Size;
00276 else
00277 {
00278 return false;
00279 }
00280 }
00281
00282 return true;
00283
00284
00285 }
00286
00287
00288
00289
00290 template <class T>
00291 void Vector<T>::setData(T* Data, unsigned long Size)
00292 {
00293 clear();
00294
00295 if (!allocate(Size)) throw openfluid::base::OFException("OpenFLUID framework","Vector::setData","Cannot allocate memory");
00296
00297 if (m_Data != NULL) std::copy(Data, Data + Size, m_Data);
00298
00299 }
00300
00301
00302
00303
00304
00305 template <class T>
00306 T Vector<T>::getElement(unsigned long Index) const
00307 {
00308 if (Index >= m_Size) throw openfluid::base::OFException("OpenFLUID framework","Vector::getElement","element access range error");
00309 return m_Data[Index];
00310 }
00311
00312
00313
00314
00315
00316 template <class T>
00317 void Vector<T>::setElement(unsigned long Index, T Element)
00318 {
00319 if (Index >= m_Size) throw openfluid::base::OFException("OpenFLUID framework","Vector::setElement","element access range error");
00320 m_Data[Index] = Element;
00321 }
00322
00323
00324
00325
00326
00327
00328 template <class T>
00329 T& Vector<T>::operator[](unsigned long Index)
00330 {
00331 if (Index >= m_Size) throw openfluid::base::OFException("OpenFLUID framework","Vector::operator[]","element access range error");
00332 return m_Data[Index];
00333 }
00334
00335
00336
00337
00338 template <class T>
00339 Vector<T>& Vector<T>::operator=(const Vector &A)
00340 {
00341
00342 if (this == &A) return *this;
00343
00344 clear();
00345
00346 allocate(A.m_Size);
00347 std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
00348
00349 return *this;
00350 }
00351
00352
00353
00354
00355
00356 template <class T>
00357 void Vector<T>::init()
00358 {
00359 m_Data = NULL;
00360 m_Size = 0;
00361 }
00362
00363
00364
00365
00366
00367
00368 template <class T>
00369 void Vector<T>::fill(const T& Val)
00370 {
00371 std::fill(m_Data, m_Data + m_Size,Val);
00372 }
00373
00374
00375
00376
00377
00378
00379 template <class T>
00380 void Vector<T>::clear()
00381 {
00382 delete [] m_Data;
00383 init();
00384 }
00385
00386
00387
00388
00389 template <class T>
00390 void Vector<T>::copy(const Vector& Source, Vector& Dest)
00391 {
00392 Dest.clear;
00393 Dest.allocate(Source.m_Size);
00394 for (unsigned long i = 0; i < Source.m_Size;i++)
00395 {
00396 Dest.m_Data[i] = Source.m_Data[i];
00397 }
00398 }
00399
00400
00401
00402 } }
00403
00404
00405 #endif