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 __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; // in case somebody tries assign array to itself
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 //  if (m_Data != NULL) free(m_Data);
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

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