$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
set/card.hh
1 // Copyright (C) 2008, 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_SET_CARD_HH
27 # define MLN_SET_CARD_HH
28 
32 
33 # include <mln/core/concept/site_set.hh>
34 
35 
36 namespace mln
37 {
38 
39  namespace set
40  {
41 
43  template <typename S>
44  unsigned card(const Site_Set<S>& s);
45 
46 
47 # ifndef MLN_INCLUDE_ONLY
48 
49 
50  // Implementations.
51 
52  namespace impl
53  {
54 
55  // Generic version.
56 
57  namespace generic
58  {
59 
60  template <typename S>
61  unsigned card(const Site_Set<S>& s_)
62  {
63  mln_trace("set::impl::generic::card");
64  const S& s = exact(s_);
65  mln_precondition(s.is_valid());
66 
67  unsigned n = 0;
68  mln_piter(S) p(s);
69  for_all(p)
70  ++n;
71 
72  return n;
73  }
74 
75  } // end of namespace mln::set::impl::generic
76 
77 
78  // A single specialization.
79 
80  template <typename S>
81  inline
82  unsigned card_from_method(const Site_Set<S>& s)
83  {
84  mln_trace("set::impl::card_from_method");
85  unsigned n = exact(s).nsites();
86  return n;
87  }
88 
89  } // end of namespace mln::set::impl
90 
91 
92 
93  // Dispatch.
94 
95  namespace internal
96  {
97 
98  template <typename S>
99  inline
100  unsigned card_dispatch(mln::trait::site_set::nsites::any,
101  const Site_Set<S>& s)
102  {
103  return impl::generic::card(s);
104  }
105 
106  template <typename S>
107  inline
108  unsigned card_dispatch(mln::trait::site_set::nsites::known,
109  const Site_Set<S>& s)
110  {
111  return impl::card_from_method(s);
112  }
113 
114  // Dispatch facade.
115 
116  template <typename S>
117  inline
118  unsigned card_dispatch(const Site_Set<S>& s)
119  {
120  return card_dispatch(mln_trait_site_set_nsites(S)(),
121  s);
122  }
123 
124  } // end of namespace mln::set::internal
125 
126 
127 
128  // Facade.
129 
130  template <typename S>
131  inline
132  unsigned card(const Site_Set<S>& s)
133  {
134  mln_trace("set::card");
135  mln_precondition(exact(s).is_valid());
136 
137  unsigned n = internal::card_dispatch(s);
138 
139  return n;
140  }
141 
142 # endif // ! MLN_INCLUDE_ONLY
143 
144  } // end of namespace mln::set
145 
146 } // end of namespace mln
147 
148 
149 #endif // ! MLN_SET_CARD_HH