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 __ALGORITHMS_H
00033 #define __ALGORITHMS_H
00034
00035 #include "Modelib.h"
00036 #include <iostream>
00037
00038 namespace Modelib {
00039
00049 template<typename Elmt, class Container, class Prop>
00050 struct for_all_var
00051 {
00052 for_all_var() {};
00053 void operator()(const Container& _c) const
00054 {
00055 const unsigned len = _c.Size();
00056 Prop MaProp;
00057 unsigned i=0;
00058 for (;i<len;++i)
00059 {
00060 Elmt *t = reinterpret_cast<Elmt *>(&_c[i]);
00061 MaProp(*t);
00062 }
00063 }
00064 };
00065
00066 template<typename Elmt, class Container, class Prop>
00067 struct for_all_num
00068 {
00069 for_all_num() {};
00070 void operator()(const Container& _c) const
00071 {
00072 const unsigned len = _c.Size();
00073 Prop MaProp;
00074 unsigned i=0;
00075 for (;i<len;++i)
00076 {
00077 Elmt t(static_cast<int>(_c[i]));
00078
00079
00080 MaProp(t);
00081 }
00082 }
00083 };
00084
00087 template<typename Elmt, class Container, class PropOper, class PropVerify>
00088 struct while_prop
00089 {
00090 while_prop() {};
00091
00092 void operator()(const Container& _c) const
00093 {
00094 const unsigned len = _c.Size();
00095 Elmt *_e = 0;
00096 PropOper MaProp;
00097 PropVerify Condition;
00098 unsigned i=0;
00099 for (;i<len;++i)
00100 {
00101 _e = reinterpret_cast<Elmt*>(&_c[i]);
00102 if (Condition(*_e) == true)
00103 MaProp(*_e);
00104 }
00105 }
00106 };
00107
00110 template<typename Elmt, class Container>
00111 struct sum
00112 {
00113 sum(){};
00114 float operator()(const Container& _c) const
00115 {
00116 const unsigned len = _c.Size();
00117 Elmt *_e = 0;
00118 float _res=0,t=0;
00119 unsigned i=0;
00120 for (;i<len;++i)
00121 {
00122 _e = reinterpret_cast<Elmt*>(&_c[i]);
00123 if ((t=_e->GetValue()) != 0)
00124 _res = t + _res;
00125 }
00126 return _res;
00127 }
00128 };
00129
00130
00133 template<typename Elmt>
00134 struct BoolIT
00135 {
00136 BoolIT(){};
00137 void operator()(Elmt& _e) const
00138 {
00139 if (_e.GetValue() <= 0) _e.SetValue(0);
00140 else _e.SetValue(1);
00141 }
00142 };
00143
00146 template<typename Elmt>
00147 struct NotNull
00148 {
00149 NotNull(){};
00150 bool operator()(Elmt& _e) const
00151 {
00152 if (_e.GetValue() != 0) return true;
00153 else return false ;
00154 }
00155 };
00156
00159 template<typename Elmt>
00160 struct Affiche
00161 {
00162 Affiche() {};
00163 void operator()(Elmt& _e)
00164 {
00165 std::cout << _e.GetValue() << ',' ;
00166 }
00167 };
00168
00169 template<typename Elmt, class Container>
00170 Elmt Min(Container& _c)
00171 {
00172 unsigned i=1;
00173 Elmt min=_c[0];
00174 while (_c[i])
00175 {
00176 if (_c[i] < min.GetValue())
00177 min = _c[i];
00178 i++;
00179 }
00180 return min;
00181 }
00182
00183 template<typename Elmt, class Container>
00184 Elmt Max(Container& _c)
00185 {
00186 unsigned i=1;
00187 Elmt max=_c[0];
00188 while (_c[i])
00189 {
00190 if (_c[i] > max.GetValue())
00191 max = _c[i];
00192 i++;
00193 }
00194 return max;
00195 }
00196
00197
00198 template<class Container>
00199 struct OBJECTIVE_SUM
00200 {
00201 Expr operator()(Container& _c)
00202 {
00203 return Sum(_c);
00204 }
00205 };
00206
00207 template<class Container_VAR, class Container_NUM>
00208 struct OBJECTIVE_PROD
00209 {
00210 Expr operator()(Container_VAR& _c_var, Container_NUM& _c_num)
00211 {
00212 return Expr(_c_var * _c_num);
00213 }
00214 };
00215
00216
00217
00218 template<class Container_to, class Container_from>
00219 void Set(Container_to& _to, Container_from& _from)
00220 {
00221 unsigned i=0;
00222 while (_from[i])
00223 {
00224 _to[i] = _from[i];
00225 i++;
00226 }
00227 }
00228
00229 }
00230
00231 #endif
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250