• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

sum.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_ACCU_MATH_SUM_HH
00027 # define MLN_ACCU_MATH_SUM_HH
00028 
00032 
00033 # include <mln/core/concept/meta_accumulator.hh>
00034 # include <mln/accu/internal/base.hh>
00035 
00036 # include <mln/util/pix.hh> // To prevent accu::math::sum to work on pixels (ambiguous).
00037 
00038 # include <mln/trait/value_.hh>      // For mln_sum.
00039 # include <mln/value/builtin/all.hh> // In the case of summing builtin values.
00040 # include <mln/literal/zero.hh>      // For initialization.
00041 
00042 
00043 namespace mln
00044 {
00045 
00046   namespace accu
00047   {
00048 
00049     namespace math
00050     {
00051 
00052       // Forward declaration.
00053       template <typename T, typename S>
00054       struct sum;
00055 
00056     } // end of namespace mln::accu::math
00057 
00058     namespace meta
00059     {
00060 
00061       namespace math
00062       {
00063 
00065         struct sum : public Meta_Accumulator< sum >
00066         {
00067           template <typename T, typename S = mln_sum(T)>
00068           struct with
00069           {
00070             typedef accu::math::sum<T, S> ret;
00071           };
00072         };
00073 
00074       } // end of namespace mln::accu::meta::math
00075 
00076     } // end of namespace mln::accu::meta
00077 
00078   } // end of namespace mln::accu
00079 
00080   // Traits.
00081   namespace trait
00082   {
00083 
00084     template <typename T, typename S>
00085     struct accumulator_< accu::math::sum<T,S> >
00086     {
00087       typedef accumulator::has_untake::yes    has_untake;
00088       typedef accumulator::has_set_value::yes has_set_value;
00089       typedef accumulator::has_stop::no       has_stop;
00090       typedef accumulator::when_pix::not_ok   when_pix;
00091     };
00092 
00093   } // end of namespace mln::trait
00094 
00095 
00096   namespace accu
00097   {
00098 
00099     namespace math
00100     {
00101 
00109       //
00110       template <typename T, typename S = mln_sum(T)>
00111       struct sum : public mln::accu::internal::base< const S&, sum<T,S> >
00112       {
00113         typedef T argument;
00114 
00115         sum();
00116 
00119         void init();
00120         void take(const argument& t);
00121         void take_as_init_(const argument& t);
00122         void take(const sum<T,S>& other);
00123 
00124         void untake(const argument& t);
00125         void set_value(const S& s);
00127 
00129         const S& to_result() const;
00130 
00133         bool is_valid() const;
00134 
00135       protected:
00136 
00137         S s_;
00138       };
00139 
00140 
00141       template <typename I, typename S>
00142       struct sum< util::pix<I>, S >;
00143 
00144 
00145 # ifndef MLN_INCLUDE_ONLY
00146 
00147 
00148 
00149       template <typename T, typename S>
00150       inline
00151       sum<T,S>::sum()
00152       {
00153         init();
00154       }
00155 
00156       template <typename T, typename S>
00157       inline
00158       void
00159       sum<T,S>::init()
00160       {
00161         s_ = literal::zero;
00162       }
00163 
00164       template <typename T, typename S>
00165       inline
00166       void sum<T,S>::take(const argument& t)
00167       {
00168         s_ += static_cast<S>(t);
00169       }
00170 
00171       template <typename T, typename S>
00172       inline
00173       void sum<T,S>::untake(const argument& t)
00174       {
00175         s_ -= static_cast<S>(t);
00176       }
00177 
00178       template <typename T, typename S>
00179       inline
00180       void sum<T,S>::take_as_init_(const argument& t)
00181       {
00182         s_ = static_cast<S>(t);
00183       }
00184 
00185       template <typename T, typename S>
00186       inline
00187       void
00188       sum<T,S>::take(const sum<T,S>& other)
00189       {
00190         s_ += other.s_;
00191       }
00192 
00193       template <typename T, typename S>
00194       inline
00195       const S&
00196       sum<T,S>::to_result() const
00197       {
00198         return s_;
00199       }
00200 
00201       template <typename T, typename S>
00202       inline
00203       void
00204       sum<T,S>::set_value(const S& s)
00205       {
00206         s_ = s;
00207       }
00208 
00209       template <typename T, typename S>
00210       inline
00211       bool
00212       sum<T,S>::is_valid() const
00213       {
00214         return true;
00215       }
00216 
00217 # endif // ! MLN_INCLUDE_ONLY
00218 
00219     } // end of namespace mln::accu::math
00220 
00221   } // end of namespace mln::accu
00222 
00223 } // end of namespace mln
00224 
00225 
00226 #endif // ! MLN_ACCU_MATH_SUM_HH

Generated on Thu Sep 8 2011 18:32:40 for Milena (Olena) by  doxygen 1.7.1