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

Algorithms.h

00001 #ifndef __ALGORITHMS_H
00002 #define __ALGORITHMS_H
00003 
00004 #include "Modelib.h"
00005 #include <iostream>
00006 
00016 template<typename Elmt, class Container, class Prop>
00017 struct for_all_var
00018 {
00019   for_all_var() {};
00020   void operator()(const Container& _c) const
00021   {
00022        const unsigned len = _c.Size();
00023        Prop MaProp;
00024        unsigned i=0;
00025        for (;i<len;++i)
00026        {
00027          Elmt *t = reinterpret_cast<Elmt *>(&_c[i]);
00028          MaProp(*t);
00029        }
00030   }
00031 };
00032 
00033 template<typename Elmt, class Container, class Prop>
00034 struct for_all_num
00035 {
00036   for_all_num() {};
00037   void operator()(const Container& _c) const
00038   {
00039        const unsigned len = _c.Size();
00040        Prop MaProp;
00041        unsigned i=0;
00042        for (;i<len;++i)
00043        {
00044          Elmt t(static_cast<int>(_c[i]));
00045          //float val = _c[i];
00046          //t = val;
00047          MaProp(t);
00048        }
00049   }
00050 };
00051 
00054 template<typename Elmt, class Container, class PropOper, class PropVerify>
00055 struct while_prop
00056 {
00057   while_prop() {};
00058 
00059   void operator()(const Container& _c) const
00060   {
00061        const unsigned len = _c.Size();
00062        Elmt *_e = 0;
00063        PropOper   MaProp;
00064        PropVerify Condition;
00065        unsigned i=0;
00066        for (;i<len;++i)
00067        {
00068          _e = reinterpret_cast<Elmt*>(&_c[i]);
00069          if (Condition(*_e) == true)
00070            MaProp(*_e);
00071        }
00072   }
00073 };
00074 
00077 template<typename Elmt, class Container>
00078 struct sum
00079 {
00080   sum(){};
00081   float operator()(const Container& _c) const
00082   {
00083        const unsigned len = _c.Size();
00084        Elmt *_e = 0;
00085        float _res=0,t=0;
00086        unsigned i=0;
00087        for (;i<len;++i)
00088        {
00089          _e = reinterpret_cast<Elmt*>(&_c[i]);
00090          if ((t=_e->GetValue()) != 0)
00091            _res = t + _res;
00092        }
00093        return _res;
00094   }
00095 };
00096 
00097 
00100 template<typename Elmt>
00101 struct BoolIT
00102 {
00103   BoolIT(){};
00104   void operator()(Elmt& _e) const
00105   {
00106     if (_e.GetValue() <= 0) _e.SetValue(0);
00107     else                    _e.SetValue(1);
00108   }
00109 };
00110 
00113 template<typename Elmt>
00114 struct NotNull
00115 {
00116   NotNull(){};
00117   bool operator()(Elmt& _e) const
00118   {
00119     if (_e.GetValue() != 0) return true;
00120     else                    return false ;
00121   }
00122 };
00123 
00126 template<typename Elmt>
00127 struct Affiche
00128 {
00129   Affiche() {};
00130   void operator()(Elmt& _e)
00131   {
00132     std::cout << _e.GetValue() << ',' ;
00133   }
00134 };
00135 
00136 template<typename Elmt, class Container>
00137 Elmt Min(Container& _c)
00138 {
00139   unsigned i=1;
00140   Elmt min=_c[0];
00141   while (_c[i])
00142   {
00143     if (_c[i] < min.GetValue())
00144        min = _c[i];
00145     i++;
00146   }
00147   return min;
00148 }
00149 
00150 template<typename Elmt, class Container>
00151 Elmt Max(Container& _c)
00152 {
00153   unsigned i=1;
00154   Elmt max=_c[0];
00155   while (_c[i])
00156   {
00157     if (_c[i] > max.GetValue())
00158        max = _c[i];
00159     i++;
00160   }
00161   return max;
00162 }
00163 
00164 
00165 template<class Container>
00166 struct OBJECTIVE_SUM
00167 {
00168   Expr operator()(Container& _c)
00169   {
00170     return Sum(_c);
00171   }
00172 };
00173 
00174 template<class Container_VAR, class Container_NUM>
00175 struct OBJECTIVE_PROD // on fait le produit scalaire
00176 {
00177   Expr operator()(Container_VAR& _c_var, Container_NUM& _c_num)
00178   {
00179     return Expr(_c_var * _c_num);
00180   }
00181 };
00182 
00183 
00184 
00185 template<class Container_to, class Container_from>
00186 void Set(Container_to& _to, Container_from& _from)
00187 {
00188   unsigned i=0;
00189   while (_from[i])
00190   {
00191     _to[i] = _from[i];
00192     i++;
00193   }
00194 }
00195 
00196 
00197 #endif
00198 
00199 /*
00200 
00201   exemple d'utilisation:
00202       
00203       FloatVarArray t(m,NOMBRE_DE_VAR,-Infinity,Infinity,"vect");
00204       
00205       // Applique PropType() a tous les éléments de t              
00206       for_all<FloatVar,FloatVarArray,PropType<FloatVar> >()(t);
00207 
00208       // Applique PropType() a tous les éléments de t tant que ceux-ci
00209       // vérifient la condition Superieur()
00210       while_prop<FloatVar,FloatVarArray,PropType<FloatVar>,Superieur<FloatVar> >()(t);
00211       
00212       // Calculer la "somme" de t
00213       FloatVar somme = sum<FloatVar,FloatVarArray>()(t);
00214 */
00215 
00216 

Généré le Mon Jul 18 23:07:40 2005 pour Modelib par doxygen 1.3.6