Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator 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  trace::entering("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  trace::exiting("set::impl::generic::card");
73  return n;
74  }
75 
76  } // end of namespace mln::set::impl::generic
77 
78 
79  // A single specialization.
80 
81  template <typename S>
82  inline
83  unsigned card_from_method(const Site_Set<S>& s)
84  {
85  trace::entering("set::impl::card_from_method");
86  unsigned n = exact(s).nsites();
87  trace::exiting("set::impl::card_from_method");
88  return n;
89  }
90 
91  } // end of namespace mln::set::impl
92 
93 
94 
95  // Dispatch.
96 
97  namespace internal
98  {
99 
100  template <typename S>
101  inline
102  unsigned card_dispatch(mln::trait::site_set::nsites::any,
103  const Site_Set<S>& s)
104  {
105  return impl::generic::card(s);
106  }
107 
108  template <typename S>
109  inline
110  unsigned card_dispatch(mln::trait::site_set::nsites::known,
111  const Site_Set<S>& s)
112  {
113  return impl::card_from_method(s);
114  }
115 
116  // Dispatch facade.
117 
118  template <typename S>
119  inline
120  unsigned card_dispatch(const Site_Set<S>& s)
121  {
122  return card_dispatch(mln_trait_site_set_nsites(S)(),
123  s);
124  }
125 
126  } // end of namespace mln::set::internal
127 
128 
129 
130  // Facade.
131 
132  template <typename S>
133  inline
134  unsigned card(const Site_Set<S>& s)
135  {
136  trace::entering("set::card");
137  mln_precondition(exact(s).is_valid());
138 
139  unsigned n = internal::card_dispatch(s);
140 
141  trace::exiting("set::card");
142  return n;
143  }
144 
145 # endif // ! MLN_INCLUDE_ONLY
146 
147  } // end of namespace mln::set
148 
149 } // end of namespace mln
150 
151 
152 #endif // ! MLN_SET_CARD_HH