apply.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_CORE_APPLY_HH
00029 # define OLENA_CORE_APPLY_HH
00030 
00031 # include <mlc/contract.hh>
00032 # include <oln/core/abstract/image.hh>
00033 # include <oln/core/image.hh>
00034 # include <oln/core/abstract/iter.hh>
00035 # include <oln/core/macros.hh>
00036 
00037 namespace oln {
00038 
00039   /*------.
00040   | Unary |
00041   `------*/
00042 
00073   template<class AdaptableUnaryFun, class I> inline
00074   typename mute<I, typename AdaptableUnaryFun::result_type>::ret
00075   apply(AdaptableUnaryFun f, const abstract::image<I>& input)
00076   {
00077     typename mute<I, typename AdaptableUnaryFun::result_type>::ret
00078       output(input.size());
00079     oln_iter_type(I) p(input);
00080     for_all(p) output[p] = f(input[p]);
00081     return output;
00082   }
00083 
00084 
00089   template<class AdaptableUnaryFun, class I> inline
00090   typename mute<I, typename AdaptableUnaryFun::result_type>::ret
00091   apply(const abstract::image<I>& input)
00092   {
00093     return apply(AdaptableUnaryFun(), input);
00094   }
00095 
00096 
00102   template<template<class> class AdaptableUnaryFun,
00103            class I> inline
00104   typename mute<I, typename AdaptableUnaryFun<oln_value_type(I)>::result_type>::ret
00105   apply(const abstract::image<I>& input)
00106   {
00107     // Workaround for g++-2.95 bug.
00108     AdaptableUnaryFun<oln_value_type(I)> tmp;
00109     return apply(tmp, input);
00110   }
00111 
00112 
00113   /*-------.
00114   | binary |
00115   `-------*/
00116 
00126   template<class AdaptableBinaryFun, class I1, class I2> inline
00127   typename mute<I1, typename AdaptableBinaryFun::result_type>::ret
00128   apply2(AdaptableBinaryFun f,
00129          const abstract::image<I1>& input1, const abstract::image<I2>& input2)
00130   {
00131     precondition(input1.size() == input2.size());
00132     typename mute<I1, typename AdaptableBinaryFun::result_type>::ret
00133       output(input1.size());
00134     oln_iter_type(I1) p(input1);
00135     for_all(p) output[p] = f(input1[p], input2[p]);
00136     return output;
00137   }
00138 
00139 
00146   template<class AdaptableBinaryFun, class I1, class I2> inline
00147   typename mute<I1, typename AdaptableBinaryFun::result_type>::ret
00148   apply2(const abstract::image<I1>& input1, const abstract::image<I2>& input2)
00149   {
00150     return apply2(AdaptableBinaryFun(), input1, input2);
00151   }
00152   
00161   template<template <class, class> class AdaptableBinaryFun,
00162            class I1, class I2> inline
00163   typename mute<I1,
00164     typename AdaptableBinaryFun<oln_value_type(I1),oln_value_type(I2)>::result_type>::ret
00165   apply2(const abstract::image<I1>& input1, const abstract::image<I2>& input2)
00166   {
00167     // Workaround for g++-2.95 bug.
00168     AdaptableBinaryFun<oln_value_type(I1),oln_value_type(I2)> tmp;
00169     return apply2(tmp, input1, input2);
00170   }
00171   
00172   
00181   template<template <class> class AdaptableBinaryFun,
00182            class I> inline
00183   typename mute<I,
00184     typename AdaptableBinaryFun<oln_value_type(I)>::result_type>::ret
00185   apply2(const abstract::image<I>& input1, const abstract::image<I>& input2)
00186   {
00187     // Workaround for g++-2.95 bug.
00188     AdaptableBinaryFun<oln_value_type(I)> tmp;
00189     return apply2(tmp, input1, input2);
00190   }
00191 
00192 
00193   /*-----------.
00194   | self unary |
00195   `-----------*/
00196 
00201   template<class UnaryFun, class I> inline
00202   abstract::image<I>& apply_self(UnaryFun f, abstract::image<I>& input)
00203   {
00204     oln_iter_type(I) p(input);
00205     for_all(p) input[p] = f(input[p]);
00206     return input;
00207   }
00208 
00209 
00214   template<class UnaryFun, class I> inline
00215   abstract::image<I>& apply_self(abstract::image<I>& input)
00216   {
00217     return apply_self(UnaryFun(), input);
00218   }
00219 
00220 
00225   template<template<class> class UnaryFun, class I> inline
00226   abstract::image<I>& apply_self(abstract::image<I>& input)
00227   {
00228     // Workaround for g++-2.95 bug.
00229     UnaryFun<oln_value_type(I)> tmp;
00230     return apply_self(tmp, input);
00231   }
00232 
00233 
00234   /*------------.
00235   | self binary |
00236   `------------*/
00237 
00242   template<class UnaryFun, class I1, class I2>
00243   abstract::image<I1>& apply2_self(UnaryFun f,
00244                           abstract::image<I1>& input1, const abstract::image<I2>& input2)
00245   {
00246     precondition(input1.size() == input2.size());
00247     oln_iter_type(I1) p(input1);
00248     for_all(p) input1[p] = f(input1[p], input2[p]);
00249     return input1;
00250   }
00251 
00252 
00257   template<class UnaryFun, class I1, class I2> inline
00258   abstract::image<I1>& apply2_self(abstract::image<I1>& input1, const abstract::image<I1>& input2)
00259   {
00260     return apply_self(UnaryFun(), input1, input2);
00261   }
00262 
00263 
00269   template<template<class, class> class UnaryFun, class I1, class I2> inline
00270   abstract::image<I1>& apply2_self(abstract::image<I1>& input1, const abstract::image<I2>& input2)
00271   {
00272     // Workaround for g++-2.95 bug.
00273     UnaryFun<oln_value_type(I1),oln_value_type(I2)> tmp;
00274     return apply2_self(tmp, input1, input2);
00275   }
00276 
00277   
00282   template<template<class> class UnaryFun, class I> inline
00283   abstract::image<I>& apply2_self(abstract::image<I>& input1, const abstract::image<I>& input2)
00284   {
00285     // Workaround for g++-2.95 bug.
00286     UnaryFun<oln_value_type(I)> tmp;
00287     return apply2_self(tmp, input1, input2);
00288   }
00289 
00290 } // end of oln
00291 
00292 #endif // ! OLENA_CORE_APPLY_HH

Generated on Thu Apr 15 20:13:05 2004 for Olena by doxygen 1.3.6-20040222