Modelib: Fichier source de NumArray.h

Page principale | Liste des namespaces | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

# NumArray.h

Aller à la documentation de ce fichier.

    00001 /*
    00002     Copyright (c) 2005, Quentin Lequy and Romain Gaucher
    00003     All rights reserved.
    00004
    00005     Redistribution and use in source and binary forms, with or
    00006     without modification, are permitted provided that the following
    00007     conditions are met:
    00008
    00009         * Redistributions of source code must retain the above copyright
    00010           notice, this list of conditions and the following disclaimer.
    00011
    00012         * Redistributions in binary form must reproduce the above copyright
    00013           notice, this list of conditions and the following disclaimer in the
    00014           documentation and/or other materials provided with the distribution.
    00015
    00016         * Neither the name of Quentin Lequy or Romain Gaucher nor the names
    00017           of its contributors may be used to endorse or promote products
    00018           derived from this software without specific prior written permission.
    00019
    00020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00021     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00022     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00023     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00024     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00025     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    00026     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    00027     AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00030 */
    00031
    00032 #ifndef __NUMARRAY_H_
    00033 #define __NUMARRAY_H_
    00034
    00035 #include "MuteVar.h"
    00036 #include "Num.h"
    00037
    00038 #include <vector>
    00039 #include <stdexcept>
    00040
    00041 namespace Modelib {
    00042
    00044
    00053 class NumArray
    00054 {
    00055   protected:
    00056     std::vector<Num> cstArray;
    00057     MuteVar::VarType type;
    00058   public:
    00060     NumArray(MuteVar::VarType _type = MuteVar::FLOAT, unsigned _size = 0,float _value = 0.0f )
    00061     :cstArray( std::vector<Num>( _size, Num(_type,_value) ) ), type(_type){} //
    00063     NumArray(const NumArray & _n):cstArray(_n.cstArray),type(_n.type) {}
    00065     NumArray & operator= (const NumArray & _n)
    00066     {
    00067       cstArray = _n.cstArray;
    00068       type = _n.type;
    00069       return *this;
    00070     }
    00071
    00072     NumArray(const std::vector< Num > & _vect,
    00073              MuteVar::VarType _type = MuteVar::FLOAT)
    00074     :type(_type)
    00075     {
    00076         if(_vect.size())
    00077         {
    00078           type = _vect[0].GetType();
    00079           for(unsigned i = 0; i<_vect.size(); ++i)
    00080           {
    00081             cstArray.push_back( Num(_type, _vect[i].GetValue()));
    00082           }
    00083         }
    00084     }
    00085
    00086     NumArray(const std::vector< float > & _vect)
    00087     :type(MuteVar::FLOAT)
    00088     {
    00089         if(_vect.size())
    00090         {
    00091           for(unsigned i = 0; i<_vect.size(); ++i)
    00092           {
    00093             cstArray.push_back( Float(_vect[i]));
    00094           }
    00095         }
    00096     }
    00097
    00098     NumArray(const std::vector< double > & _vect)
    00099     :type(MuteVar::FLOAT)
    00100     {
    00101         if(_vect.size())
    00102         {
    00103           for(unsigned i = 0; i<_vect.size(); ++i)
    00104           {
    00105             cstArray.push_back( Float(_vect[i]));
    00106           }
    00107         }
    00108     }
    00109     NumArray(const std::vector< unsigned > & _vect)
    00110     :type(MuteVar::INT)
    00111     {
    00112         if(_vect.size())
    00113         {
    00114           for(unsigned i = 0; i<_vect.size(); ++i)
    00115           {
    00116             cstArray.push_back( Int(_vect[i]));
    00117           }
    00118         }
    00119     }
    00120
    00121     NumArray(const std::vector< int > & _vect)
    00122     :type(MuteVar::INT)
    00123     {
    00124         if(_vect.size())
    00125         {
    00126           for(unsigned i = 0; i<_vect.size(); ++i)
    00127           {
    00128             cstArray.push_back( Int(_vect[i]));
    00129           }
    00130         }
    00131     }
    00132
    00137     float operator[] (unsigned i) const
    00138     {
    00139         if( i < cstArray.size())
    00140             return cstArray[i].GetValue();
    00141         else
    00142             throw std::out_of_range("NumArray::operator[] : index out of range");
    00143     }
    00148     Num & operator[] (unsigned i)
    00149     {
    00150         if( i < cstArray.size())
    00151             return cstArray[i];
    00152         else
    00153             throw std::out_of_range("NumArray::operator[] : index out of range");
    00154     }
    00158     unsigned Size() const { return cstArray.size();}
    00162     MuteVar::VarType GetType() const { return type;}
    00163
    00170     NumArray & operator+=( const NumArray & na)
    00171     {
    00172         if( na.Size() == cstArray.size())
    00173             for(unsigned i=0; i<cstArray.size();++i)
    00174                cstArray[i].SetValue( cstArray[i].GetValue() + na.cstArray[i].GetValue());
    00175         else
    00176             throw std::range_error("NumArray::operator+= : vector size doesn't match");
    00177         return *this;
    00178     }
    00179
    00186     NumArray & operator-=( const NumArray & na)
    00187     {
    00188         if( na.cstArray.size() == cstArray.size())
    00189             for(unsigned i=0; i<cstArray.size();++i)
    00190                cstArray[i].SetValue( cstArray[i].GetValue() - na.cstArray[i].GetValue());
    00191         else
    00192             throw std::range_error("NumArray::operator+= : vector size doesn't match");
    00193         return *this;
    00194     }
    00195
    00200     NumArray & operator*=( Num f)
    00201     {
    00202         for(unsigned i=0; i<cstArray.size();++i)
    00203                cstArray[i].SetValue( cstArray[i].GetValue()*f.GetValue());
    00204         return *this;
    00205     }
    00206
    00207     ~NumArray() {}
    00208
    00209 };
    00210
    00212 class IntArray:public NumArray
    00213 {
    00214   public:
    00215     IntArray(unsigned _size = 0,int _entier = 0)
    00216     :NumArray(MuteVar::INT, _size, static_cast<float>(_entier)) {}
    00217     IntArray(const std::vector< unsigned > & _vect)
    00218     :NumArray(_vect) {}
    00219     IntArray(const std::vector< int > & _vect)
    00220     :NumArray(_vect) {}
    00221
    00222 };
    00223
    00225 class FloatArray:public NumArray
    00226 {
    00227   public:
    00228     FloatArray(unsigned _size = 0, float flottant = 0.0f)
    00229     :NumArray(MuteVar::FLOAT, _size, flottant) {}
    00230     FloatArray(const std::vector< float > & _vect)
    00231     :NumArray(_vect) {}
    00232     FloatArray(const std::vector< double > & _vect)
    00233     :NumArray(_vect) {}
    00234 };
    00235
    00237 class BoolArray:public NumArray
    00238 {
    00239   public:
    00240     BoolArray(unsigned _size = 0, bool booleen = false)
    00241     :NumArray(MuteVar::BOOL, _size, (booleen?1.0f:0.0f)) {}
    00242     BoolArray(const std::vector< float > & _vect)
    00243     :NumArray(_vect) {}
    00244     BoolArray(const std::vector< double > & _vect)
    00245     :NumArray(_vect) {}
    00246     BoolArray(const std::vector< int > & _vect)
    00247     :NumArray(_vect) {}
    00248     BoolArray(const std::vector< unsigned > & _vect)
    00249     :NumArray(_vect) {}
    00250 };
    00251
    00252 NumArray operator+(const NumArray & na1, const NumArray & na2);
    00253
    00254 NumArray operator*(const NumArray & na1, Num f);
    00255
    00256 NumArray operator*( Num f, const NumArray & na1);
    00257
    00258 NumArray operator-(const NumArray & na1, const NumArray & na2);
    00259
    00260 }
    00261
    00262 #endif