Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

general-functions.hh

00001 /*
00002 *  Name:      general-functions.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   General functions
00005 *  Date:      $Date: 2003/04/14 00:18:35 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1999-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _MPCL_UTIL_GENERAL_FUNCTIONS__
00027 #define _MPCL_UTIL_GENERAL_FUNCTIONS__
00028 
00029 #include <cstdlib>
00030 #include <functional>
00031 
00032 
00034 namespace mpcl
00035 {
00036 
00038   namespace util
00039   {
00040 
00041     //
00042     //  E X P O R T E D   F U N C T I O N S
00043     //
00044 
00049     template <typename TTargetItem, typename TSourceItem>
00050     struct MAppend : public std::unary_function<TTargetItem, void>
00051     {
00052 
00053       public:
00054 
00056         const TSourceItem&   rktSourceItem;
00057 
00058 
00059       public:
00060 
00065         MAppend (const TSourceItem& rktSOURCE_ITEM) :
00066           rktSourceItem (rktSOURCE_ITEM)            {}
00067 
00073         void operator () (TTargetItem& rtTARGET_ITEM) const
00074         {
00075           operator << (*rtTARGET_ITEM, rktSourceItem);
00076         }
00077 
00078     };  // struct MAppend
00079 
00081     template < typename TItem      ,
00082                typename TSizeType  ,
00083                typename MOperation ,
00084                typename MFunction  >
00085     struct MCompare : public std::unary_function<TItem, void>
00086     {
00087 
00088       public:
00089 
00091         MFunction   mFunction;
00092 
00094         MOperation   mOperation;
00095 
00097         TSizeType   zComputedValue;
00098 
00099 
00100       public:
00101 
00103         MCompare ( const TSizeType&  rkzVALUE     ,
00104                    const MOperation& rkmOPERATION ,
00105                    const MFunction&  rkmFUNCTION  ) :
00106           mFunction      (rkmFUNCTION)              ,
00107           mOperation     (rkmOPERATION)             ,
00108           zComputedValue (rkzVALUE)                 {}
00109 
00111         MCompare (const TSizeType& rkzVALUE, const MFunction& rkmFUNCTION) :
00112           mFunction      (rkmFUNCTION)                                     ,
00113           mOperation     ()                                                ,
00114           zComputedValue (rkzVALUE)                                        {}
00115 
00121         void operator () (const TItem& rktITEM)
00122         {
00123           TSizeType   zItemResult (mFunction (rktITEM));
00124 
00125           if ( mOperation (zComputedValue, zItemResult) )
00126           {
00127             zComputedValue = zItemResult;
00128           }
00129         }
00130 
00135         operator TSizeType (void) const
00136         {
00137           return zComputedValue;
00138         }
00139 
00140     };  // struct MCompare
00141 
00143     template < typename TItem      ,
00144                typename TSizeType  ,
00145                typename MOperation ,
00146                typename MFunction  >
00147     struct MComparePointer : public std::unary_function<TItem, void>
00148     {
00149 
00150       public:
00151 
00153         MFunction   mFunction;
00154 
00156         MOperation   mOperation;
00157 
00159         TSizeType   zComputedValue;
00160 
00161 
00162       public:
00163 
00165         MComparePointer ( const TSizeType&  rkzVALUE     ,
00166                           const MOperation& rkmOPERATION ,
00167                           const MFunction&  rkmFUNCTION  ) :
00168           mFunction      (rkmFUNCTION)                     ,
00169           mOperation     (rkmOPERATION)                    ,
00170           zComputedValue (rkzVALUE)                        {}
00171 
00173         MComparePointer (const TSizeType& rkzVALUE, const MFunction& rkmFUNCTION) :
00174           mFunction      (rkmFUNCTION)                                            ,
00175           mOperation     ()                                                       ,
00176           zComputedValue (rkzVALUE)                                               {}
00177 
00183         void operator () (const TItem& rktITEM)
00184         {
00185           TSizeType   zItemResult (mFunction (*rktITEM));
00186 
00187           if ( mOperation (zComputedValue, zItemResult) )
00188           {
00189             zComputedValue = zItemResult;
00190           }
00191         }
00192 
00198         void operator () (TItem& rtITEM)
00199         {
00200           TSizeType   zItemResult (mFunction (*rtITEM));
00201 
00202           if ( mOperation (zComputedValue, zItemResult) )
00203           {
00204             zComputedValue = zItemResult;
00205           }
00206         }
00207 
00212         operator TSizeType (void) const
00213         {
00214           return zComputedValue;
00215         }
00216 
00217     };  // struct MComparePointer
00218 
00220     template < typename TItem      ,
00221                typename TSizeType  ,
00222                typename MFunction  >
00223     struct MSum : public std::unary_function<TItem, void>
00224     {
00225 
00226       private:
00227 
00229         MFunction   mFunction;
00230 
00232         TSizeType   zSum;
00233 
00234 
00235       public:
00236 
00241         MSum (const MFunction& rkmFUNCTION) :
00242           mFunction (rkmFUNCTION)           ,
00243           zSum      (0)                     {}
00244 
00249         void operator () (const TItem& rktITEM)
00250         {
00251           zSum += mFunction (rktITEM);
00252         }
00253 
00258         operator TSizeType (void) const
00259         {
00260           return zSum;
00261         }
00262 
00263     };  // struct MSum
00264 
00269     template < typename TItem      ,
00270                typename TSizeType  ,
00271                typename MFunction  >
00272     struct MSumPointer : public std::unary_function<TItem, void>
00273     {
00274 
00275       private:
00276 
00278         MFunction   mFunction;
00279 
00281         TSizeType   zSum;
00282 
00283 
00284       public:
00285 
00290         MSumPointer (const MFunction& rkmFUNCTION) :
00291           mFunction (rkmFUNCTION)                  ,
00292           zSum      (0)                            {}
00293 
00298         void operator () (const TItem& rktITEM)
00299         {
00300           zSum += mFunction (*rktITEM);
00301         }
00302 
00307         operator TSizeType (void) const
00308         {
00309           return zSum;
00310         }
00311 
00312     };  // struct MSumPointer
00313 
00315     template <typename TItem>
00316     struct MDelete : public std::unary_function<TItem, void>
00317     {
00318 
00323         void operator () (const TItem& rktITEM) const
00324         {
00325           delete rktITEM;
00326         }
00327 
00328     };  // struct MDelete
00329 
00331     template <typename TItem>
00332     struct MRandomNumberGenerator
00333     {
00334 
00352         TItem operator () (const size_t kzMAX) const
00353         {
00354           return TItem (((kzMAX + 1.0) / (RAND_MAX + 1.0)) * std::rand());
00355         }
00356 
00357     };  // struct MRandomNumberGenerator
00358 
00359   }  // namespace util
00360 
00361 }  // namespace mpcl
00362 
00363 
00364 #endif  // not _MPCL_UTIL_GENERAL_FUNCTIONS__

Generated on Mon Oct 13 02:35:23 2003 for MPCL by doxygen1.2.18