core/Vector.hpp
Go to the documentation of this file.
00001 /*
00002   This file is part of OpenFLUID software
00003   Copyright (c) 2007-2010 INRA-Montpellier SupAgro
00004 
00005 
00006  == GNU General Public License Usage ==
00007 
00008   OpenFLUID is free software: you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation, either version 3 of the License, or
00011   (at your option) any later version.
00012 
00013   OpenFLUID is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License
00019   along with OpenFLUID.  If not, see <http://www.gnu.org/licenses/>.
00020 
00021   In addition, as a special exception, INRA gives You the additional right
00022   to dynamically link the code of OpenFLUID with code not covered
00023   under the GNU General Public License ("Non-GPL Code") and to distribute
00024   linked combinations including the two, subject to the limitations in this
00025   paragraph. Non-GPL Code permitted under this exception must only link to
00026   the code of OpenFLUID dynamically through the OpenFLUID libraries
00027   interfaces, and only for building OpenFLUID plugins. The files of
00028   Non-GPL Code may be link to the OpenFLUID libraries without causing the
00029   resulting work to be covered by the GNU General Public License. You must
00030   obey the GNU General Public License in all respects for all of the
00031   OpenFLUID code and other code used in conjunction with OpenFLUID
00032   except the Non-GPL Code covered by this exception. If you modify
00033   this OpenFLUID, you may extend this exception to your version of the file,
00034   but you are not obligated to do so. If you do not wish to provide this
00035   exception without modification, you must delete this exception statement
00036   from your version and license this OpenFLUID solely under the GPL without
00037   exception.
00038 
00039 
00040  == Other Usage ==
00041 
00042   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00043   license, and requires a written agreement between You and INRA.
00044   Licensees for Other Usage of OpenFLUID may use this file in accordance
00045   with the terms contained in the written agreement between You and INRA.
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; // in case somebody tries assign array to itself
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

Generated using Doxygen 1.7.6.1
Creative Commons License Creative Commons By-NC-ND license