Modelib: NumArray.h Source File

Page principale | Hiérarchie des classes | Liste des composants | Liste des fichiers | Composants

# NumArray.h

    00001 #ifndef __NUMARRAY_H_
    00002 #define __NUMARRAY_H_
    00003
    00004 #include "MuteVar.h"
    00005 #include "Num.h"
    00006
    00007 #include <vector>
    00008 #include <stdexcept>
    00009
    00010
    00012
    00021 class NumArray
    00022 {
    00023   protected:
    00024     std::vector<Num> cstArray;
    00025     MuteVar::VarType type;
    00026   public:
    00028     NumArray(MuteVar::VarType _type = MuteVar::FLOAT, unsigned _size = 0,float _value = 0.0f )
    00029     :cstArray( std::vector<Num>( _size, Num(_type,_value) ) ), type(_type){} //
    00031     NumArray(const NumArray & _n):cstArray(_n.cstArray),type(_n.type) {}
    00033     NumArray & operator= (const NumArray & _n)
    00034     {
    00035       cstArray = _n.cstArray;
    00036       type = _n.type;
    00037       return *this;
    00038     }
    00039
    00040     NumArray(const std::vector< Num > & _vect,
    00041              MuteVar::VarType _type = MuteVar::FLOAT)
    00042     :type(_type)
    00043     {
    00044         if(_vect.size())
    00045         {
    00046           type = _vect[0].GetType();
    00047           for(unsigned i = 0; i<_vect.size(); ++i)
    00048           {
    00049                 cstArray.push_back( Num(_type, _vect[i].GetValue()));
    00050           }
    00051         }
    00052     }
    00053
    00054     NumArray(const std::vector< float > & _vect)
    00055     :type(MuteVar::FLOAT)
    00056     {
    00057         if(_vect.size())
    00058         {
    00059           for(unsigned i = 0; i<_vect.size(); ++i)
    00060           {
    00061                 cstArray.push_back( Float(_vect[i]));
    00062           }
    00063         }
    00064     }
    00065
    00066     NumArray(const std::vector< double > & _vect)
    00067     :type(MuteVar::FLOAT)
    00068     {
    00069         if(_vect.size())
    00070         {
    00071           for(unsigned i = 0; i<_vect.size(); ++i)
    00072           {
    00073                 cstArray.push_back( Float(_vect[i]));
    00074           }
    00075         }
    00076     }
    00077     NumArray(const std::vector< unsigned > & _vect)
    00078     :type(MuteVar::INT)
    00079     {
    00080         if(_vect.size())
    00081         {
    00082           for(unsigned i = 0; i<_vect.size(); ++i)
    00083           {
    00084                 cstArray.push_back( Int(_vect[i]));
    00085           }
    00086         }
    00087     }
    00088
    00089     NumArray(const std::vector< int > & _vect)
    00090     :type(MuteVar::INT)
    00091     {
    00092         if(_vect.size())
    00093         {
    00094           for(unsigned i = 0; i<_vect.size(); ++i)
    00095           {
    00096                 cstArray.push_back( Int(_vect[i]));
    00097           }
    00098         }
    00099     }
    00100
    00105     float operator[] (unsigned i) const
    00106     {
    00107         if( i < cstArray.size())
    00108             return cstArray[i].GetValue();
    00109         else
    00110             throw std::out_of_range("NumArray::operator[] : index out of range");
    00111     }
    00116     Num & operator[] (unsigned i)
    00117     {
    00118         if( i < cstArray.size())
    00119             return cstArray[i];
    00120         else
    00121             throw std::out_of_range("NumArray::operator[] : index out of range");
    00122     }
    00126     unsigned Size() const { return cstArray.size();}
    00130     MuteVar::VarType GetType() const { return type;}
    00131
    00138     NumArray & operator+=( const NumArray & na)
    00139     {
    00140         if( na.Size() == cstArray.size())
    00141                 for(unsigned i=0; i<cstArray.size();++i)
    00142                cstArray[i].SetValue( cstArray[i].GetValue() + na.cstArray[i].GetValue());
    00143         else
    00144                 throw std::range_error("NumArray::operator+= : vector size doesn't match");
    00145         return *this;
    00146     }
    00147
    00154     NumArray & operator-=( const NumArray & na)
    00155     {
    00156         if( na.cstArray.size() == cstArray.size())
    00157                 for(unsigned i=0; i<cstArray.size();++i)
    00158                cstArray[i].SetValue( cstArray[i].GetValue() - na.cstArray[i].GetValue());
    00159         else
    00160                 throw std::range_error("NumArray::operator+= : vector size doesn't match");
    00161         return *this;
    00162     }
    00163
    00168     NumArray & operator*=( Num f)
    00169     {
    00170         for(unsigned i=0; i<cstArray.size();++i)
    00171                cstArray[i].SetValue( cstArray[i].GetValue()*f.GetValue());
    00172         return *this;
    00173     }
    00174
    00175     ~NumArray() {}
    00176
    00177 };
    00178
    00180 class IntArray:public NumArray
    00181 {
    00182   public:
    00183     IntArray(unsigned _size = 0,int _entier = 0)
    00184     :NumArray(MuteVar::INT, _size, static_cast<float>(_entier)) {}
    00185     IntArray(const std::vector< unsigned > & _vect)
    00186     :NumArray(_vect) {}
    00187     IntArray(const std::vector< int > & _vect)
    00188     :NumArray(_vect) {}
    00189
    00190 };
    00191
    00193 class FloatArray:public NumArray
    00194 {
    00195   public:
    00196     FloatArray(unsigned _size = 0, float flottant = 0.0f)
    00197     :NumArray(MuteVar::FLOAT, _size, flottant) {}
    00198     FloatArray(const std::vector< float > & _vect)
    00199     :NumArray(_vect) {}
    00200     FloatArray(const std::vector< double > & _vect)
    00201     :NumArray(_vect) {}
    00202 };
    00203
    00205 class BoolArray:public NumArray
    00206 {
    00207   public:
    00208     BoolArray(unsigned _size = 0, bool booleen = false)
    00209     :NumArray(MuteVar::BOOL, _size, (booleen?1.0f:0.0f)) {}
    00210     BoolArray(const std::vector< float > & _vect)
    00211     :NumArray(_vect) {}
    00212     BoolArray(const std::vector< double > & _vect)
    00213     :NumArray(_vect) {}
    00214     BoolArray(const std::vector< int > & _vect)
    00215     :NumArray(_vect) {}
    00216     BoolArray(const std::vector< unsigned > & _vect)
    00217     :NumArray(_vect) {}
    00218 };
    00219
    00220 NumArray operator+(const NumArray & na1, const NumArray & na2);
    00221
    00222 NumArray operator*(const NumArray & na1, Num f);
    00223
    00224 NumArray operator*( Num f, const NumArray & na1);
    00225
    00226 NumArray operator-(const NumArray & na1, const NumArray & na2);
    00227
    00228 #endif