$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fold.hh
1 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_FUN_P2P_FOLD_HH
27 # define MLN_FUN_P2P_FOLD_HH
28 
32 
33 # include <mln/core/concept/function.hh>
34 # include <mln/core/site_set/box.hh>
35 
36 
37 namespace mln
38 {
39 
40  namespace fun
41  {
42 
43  namespace p2p
44  {
45 
46  // Forward declaration.
47  namespace internal {
48  template <typename F, typename P>
49  P do_fold(const P& p, const box<P>& b);
50  }
51 
52 
53  template < typename P,
54  int dir_0 = -1,
55  int dir_1 = -1,
56  int dir_2 = -1 >
57  struct fold : Function_v2v< fold<P,dir_0,dir_1,dir_2> >
58  {
59  fold();
60  fold(const box<P>& b);
61 
62  typedef P result;
63  P operator()(const P& p) const;
64 
66  };
67 
68 
69 # ifndef MLN_INCLUDE_ONLY
70 
71  namespace internal
72  {
73 
74  template <int dim, typename F>
75  struct do_fold_helper;
76 
77  template <typename P>
78  struct do_fold_helper< 1, fold< P, -1, -1, -1 > >
79  {
80  static P run(const P& p, const box<P>& b)
81  {
82  P tmp(p[0] % b.len(0));
83  return tmp;
84  }
85  };
86 
87  template <typename P, int dir_0, int dir_1>
88  struct do_fold_helper< 2, fold< P, dir_0, dir_1, -1 > >
89  {
90  static P run(const P& p, const box<P>& b)
91  {
92  P tmp(dir_0 ? p[0] % b.len(0) : p[0],
93  dir_1 ? p[1] % b.len(1) : p[1]);
94  return tmp;
95  }
96  };
97 
98  template <typename P, int dir_0, int dir_1, int dir_2>
99  struct do_fold_helper< 3, fold< P, dir_0, dir_1, dir_2 > >
100  {
101  static P run(const P& p, const box<P>& b)
102  {
103  P tmp(dir_0 ? p[0] % b.len(0) : p[0],
104  dir_1 ? p[1] % b.len(1) : p[1],
105  dir_2 ? p[2] % b.len(2) : p[2]);
106  return tmp;
107  }
108  };
109 
110  template <typename F, typename P>
111  inline
112  P
113  do_fold(const F&, const P& p, const box<P>& b)
114  {
115  return do_fold_helper<P::dim, F>::run(p, b);
116  }
117 
118  } // end of namespace mln::fun::p2p::internal
119 
120 
121  // fold.
122 
123  template <typename P, int dir_0, int dir_1, int dir_2>
124  inline
126  {
127  }
128 
129  template <typename P, int dir_0, int dir_1, int dir_2>
130  inline
131  fold<P,dir_0,dir_1,dir_2>::fold(const box<P>& b)
132  : b(b)
133  {
134  }
135 
136  template <typename P, int dir_0, int dir_1, int dir_2>
137  inline
138  P
139  fold<P,dir_0,dir_1,dir_2>::operator()(const P& p) const
140  {
141  return internal::do_fold(*this, p, b);
142  }
143 
144 # endif // ! MLN_INCLUDE_ONLY
145 
146  } // end of namespace mln::fun::p2p
147 
148  } // end of namespace mln::fun
149 
150 } // end of namespace mln
151 
152 
153 #endif // ! MLN_FUN_P2P_FOLD_HH