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
00046
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
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
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216