array.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_ARRAY_H
00023 #define MECHSYS_ARRAY_H
00024 
00025 #include <vector>
00026 
00027 #include "util/exception.h"
00028 
00029 template <typename Type>
00030 class Array : public std::vector<Type>
00031 {
00032 public:
00033     // operator [] -- write
00034     Type & operator[] (size_t i)
00035     {
00036 #ifndef NDEBUG
00037         if (!(i>=0 && i<this->size()))
00038             throw new Fatal(_("Array::operator[] failed. Subscript <i=%d> out of range"),i);
00039         else
00040             return this->std::vector<Type>::operator[](i);
00041 #else
00042         return this->std::vector<Type>::operator[](i);
00043 #endif
00044     }
00045     // operator [] -- read
00046     const Type & operator[] (size_t i) const
00047     {
00048 #ifndef NDEBUG
00049         if (!(i>=0 && i<this->size()))
00050             throw new Fatal(_("Array::operator[] failed. Subscript <i=%d> out of range"),i);
00051         else
00052             return this->std::vector<Type>::operator[](i);
00053 #else
00054         return this->std::vector<Type>::operator[](i);
00055 #endif
00056     }
00057     Array<Type> Copy(size_t Begin, size_t Len) const
00058     {
00059 #ifndef NDEBUG
00060         if (Begin<0 || Begin>=this->std::vector<Type>::size())
00061             throw new Fatal(_("Array::Copy failed. Begin variable is out of range"));
00062         if (Len<=0)
00063             throw new Fatal(_("Array::Copy failed. Len variable is null or negative"));
00064 #endif
00065         Array<Type> Result;
00066         Result.resize(Len);
00067         size_t End = Begin+Len;
00068         size_t Counter=0;
00069         for (size_t i=Begin; i<End; ++i)
00070         {
00071             Result[Counter] = this->std::vector<Type>::operator[](i);
00072             Counter++;  
00073         }
00074         return Result;
00075     }
00076 
00077 };
00078 
00079 #endif // MECHSYS_ARRAY_H
00080 
00081 // vim:fdm=marker

Generated on Wed Jan 24 15:56:27 2007 for MechSys by  doxygen 1.4.7