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 #ifndef __VECTOR_HPP__
00035 #define __VECTOR_HPP__
00036
00037
00038 #include <iostream>
00039 #include <openfluid/dllexport.hpp>
00040 #include <openfluid/base/FrameworkException.hpp>
00041
00042
00043 namespace openfluid { namespace core {
00044
00045
00046
00047
00048
00049 template <class T>
00050 class DLLEXPORT Vector
00051 {
00052 protected :
00053 T* m_Data;
00054 unsigned long m_Size;
00055
00056 bool allocate(unsigned long Size);
00057
00058 void init();
00059
00060 static void copy(const Vector& Source, Vector& Dest);
00061
00062 public :
00063
00064
00065
00066
00067 Vector();
00068
00069
00070
00071
00072 Vector(const Vector &Vector);
00073
00074
00075
00076
00077 Vector(unsigned long Size);
00078
00079
00080
00081
00082 Vector(unsigned long Size, T InitValue);
00083
00084
00085
00086
00087 Vector(T* Data, unsigned long Size);
00088
00089
00090
00091
00092 ~Vector();
00093
00094
00095
00096
00097
00098 unsigned long getSize() const { return m_Size; };
00099
00100
00101
00102
00103
00104 unsigned long size() const { return getSize(); };
00105
00106
00107
00108
00109 T* getData() const { return m_Data; };
00110
00111
00112
00113
00114 void setData(T* Data, unsigned long Size);
00115
00116
00117
00118
00119 T getElement(unsigned long Index) const;
00120
00121
00122
00123
00124 inline T at(unsigned long Index) const { return getElement(Index); };
00125
00126
00127
00128
00129 inline T get(unsigned long Index) const { return getElement(Index); };
00130
00131
00132
00133
00134
00135 void setElement(unsigned long Index, T Element);
00136
00137
00138
00139
00140 inline void set(unsigned long Index, T Element) { setElement(Index,Element); };
00141
00142
00143
00144
00145
00146 T& operator[](unsigned long Index);
00147
00148
00149
00150
00151 Vector<T>& operator = (const Vector &A);
00152
00153
00154
00155
00156 void fill(const T& Val);
00157
00158
00159
00160
00161
00162 void clear();
00163
00164 };
00165
00166
00167
00168
00169 template <class T>
00170 Vector<T>::Vector()
00171 {
00172 init();
00173 }
00174
00175
00176
00177
00178
00179 template <class T>
00180 Vector<T>::Vector(const Vector &A)
00181 {
00182 init();
00183
00184 if (!allocate(A.m_Size)) throw openfluid::base::FrameworkException("Vector::Vector(const Vector)","Cannot allocate memory");
00185
00186 std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
00187
00188 }
00189
00190
00191
00192
00193
00194 template <class T>
00195 Vector<T>::Vector(unsigned long Size)
00196 {
00197 init();
00198
00199 if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::Vector(Size)","Cannot allocate memory");
00200 }
00201
00202
00203
00204
00205
00206 template <class T>
00207 Vector<T>::Vector(unsigned long Size, T InitValue)
00208 {
00209 init();
00210
00211
00212 if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::Vector(Size,T)","Cannot allocate memory");
00213
00214
00215 if (m_Data != NULL)
00216 {
00217 unsigned long i;
00218 for (i=0;i<m_Size;i++) m_Data[i] = InitValue;
00219 }
00220
00221 }
00222
00223
00224
00225
00226
00227
00228 template <class T>
00229 Vector<T>::Vector(T* Data, unsigned long Size)
00230 {
00231 init();
00232
00233 if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::Vector(T*,Size)","Cannot allocate memory");
00234
00235 std::copy(Data, Data + Size, m_Data);
00236
00237 }
00238
00239
00240
00241
00242
00243 template <class T>
00244 Vector<T>::~Vector()
00245 {
00246 if (m_Data != NULL) clear();
00247 }
00248
00249
00250
00251
00252
00253 template <class T>
00254 bool Vector<T>::allocate(unsigned long Size)
00255 {
00256
00257 if (Size > 0)
00258 {
00259 m_Data = new T[Size];
00260 if (m_Data != NULL) m_Size = Size;
00261 else
00262 {
00263 return false;
00264 }
00265 }
00266
00267 return true;
00268
00269
00270 }
00271
00272
00273
00274
00275 template <class T>
00276 void Vector<T>::setData(T* Data, unsigned long Size)
00277 {
00278 clear();
00279
00280 if (!allocate(Size)) throw openfluid::base::FrameworkException("Vector::setData","Cannot allocate memory");
00281
00282 if (m_Data != NULL) std::copy(Data, Data + Size, m_Data);
00283
00284 }
00285
00286
00287
00288
00289
00290 template <class T>
00291 T Vector<T>::getElement(unsigned long Index) const
00292 {
00293 if (Index >= m_Size) throw openfluid::base::FrameworkException("Vector::getElement","element access range error");
00294 return m_Data[Index];
00295 }
00296
00297
00298
00299
00300
00301 template <class T>
00302 void Vector<T>::setElement(unsigned long Index, T Element)
00303 {
00304 if (Index >= m_Size) throw openfluid::base::FrameworkException("Vector::setElement","element access range error");
00305 m_Data[Index] = Element;
00306 }
00307
00308
00309
00310
00311
00312
00313 template <class T>
00314 T& Vector<T>::operator[](unsigned long Index)
00315 {
00316 if (Index >= m_Size) throw openfluid::base::FrameworkException("Vector::operator[]","element access range error");
00317 return m_Data[Index];
00318 }
00319
00320
00321
00322
00323 template <class T>
00324 Vector<T>& Vector<T>::operator=(const Vector &A)
00325 {
00326
00327 if (this == &A) return *this;
00328
00329 clear();
00330
00331 allocate(A.m_Size);
00332 std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
00333
00334 return *this;
00335 }
00336
00337
00338
00339
00340
00341 template <class T>
00342 void Vector<T>::init()
00343 {
00344 m_Data = NULL;
00345 m_Size = 0;
00346 }
00347
00348
00349
00350
00351
00352
00353 template <class T>
00354 void Vector<T>::fill(const T& Val)
00355 {
00356 std::fill(m_Data, m_Data + m_Size,Val);
00357 }
00358
00359
00360
00361
00362
00363
00364 template <class T>
00365 void Vector<T>::clear()
00366 {
00367 delete [] m_Data;
00368 init();
00369 }
00370
00371
00372
00373
00374 template <class T>
00375 void Vector<T>::copy(const Vector& Source, Vector& Dest)
00376 {
00377 Dest.clear;
00378 Dest.allocate(Source.m_Size);
00379 for (unsigned long i = 0; i < Source.m_Size;i++)
00380 {
00381 Dest.m_Data[i] = Source.m_Data[i];
00382 }
00383 }
00384
00385
00386
00387 } }
00388
00389
00390 #endif