00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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