Modelib: Fichier source de Algorithms.h

Page principale | Liste des namespaces | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

# Algorithms.h

Aller à la documentation de ce fichier.

    00001 /*
    00002     Copyright (c) 2005, Quentin Lequy and Romain Gaucher
    00003     All rights reserved.
    00004
    00005     Redistribution and use in source and binary forms, with or
    00006     without modification, are permitted provided that the following
    00007     conditions are met:
    00008
    00009         * Redistributions of source code must retain the above copyright
    00010           notice, this list of conditions and the following disclaimer.
    00011
    00012         * Redistributions in binary form must reproduce the above copyright
    00013           notice, this list of conditions and the following disclaimer in the
    00014           documentation and/or other materials provided with the distribution.
    00015
    00016         * Neither the name of Quentin Lequy or Romain Gaucher nor the names
    00017           of its contributors may be used to endorse or promote products
    00018           derived from this software without specific prior written permission.
    00019
    00020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00021     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00022     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00023     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00024     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00025     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    00026     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    00027     AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    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          //float val = _c[i];
    00079          //t = val;
    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 // on fait le produit scalaire
    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   exemple d'utilisation:
    00236
    00237       FloatVarArray t(m,NOMBRE_DE_VAR,-Infinity,Infinity,"vect");
    00238
    00239       // Applique PropType() a tous les éléments de t
    00240       for_all<FloatVar,FloatVarArray,PropType<FloatVar> >()(t);
    00241
    00242       // Applique PropType() a tous les éléments de t tant que ceux-ci
    00243       // vérifient la condition Superieur()
    00244       while_prop<FloatVar,FloatVarArray,PropType<FloatVar>,Superieur<FloatVar> >()(t);
    00245
    00246       // Calculer la "somme" de t
    00247       FloatVar somme = sum<FloatVar,FloatVarArray>()(t);
    00248 */
    00249
    00250