$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rank_bool.hh
1 // Copyright (C) 2007, 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_ACCU_STAT_RANK_BOOL_HH
27 # define MLN_ACCU_STAT_RANK_BOOL_HH
28 
37 
38 # include <mln/accu/internal/base.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace accu
45  {
46 
47  namespace stat
48  {
49 
50  // Forward declaration.
51  template <typename T> struct rank;
52 
56  //
57  template <>
58  struct rank<bool> : public mln::accu::internal::base< bool, rank<bool> >
59  {
60  typedef bool argument;
61 
62  rank();
63  rank(unsigned k);
64 
67  void init();
68  void take_as_init_(const argument& t);
69  void take(const argument& t);
70  void take(const rank<bool>& other);
71  void untake(const argument& t);
72  void untake(const rank<bool>& other);
74 
76  bool to_result() const;
77 
80  bool is_valid() const;
81 
82  protected:
83  unsigned nfalse_;
84  unsigned k_; // 0 <= k_
85  };
86 
87 
88 # ifndef MLN_INCLUDE_ONLY
89 
90  inline
92  {
93  init();
94  }
95 
96  inline
97  rank<bool>::rank(unsigned k)
98  : k_(k)
99  {
100  init();
101  }
102 
103  inline
104  void
106  {
107  nfalse_ = 0;
108  }
109 
110  inline
111  void rank<bool>::take_as_init_(const argument& t)
112  {
113  nfalse_ = t ? 0 : 1;
114  }
115 
116  inline
117  void rank<bool>::take(const argument& t)
118  {
119  if (t == false)
120  ++nfalse_;
121  }
122 
123  inline
124  void rank<bool>::untake(const argument& t)
125  {
126  if (t == false)
127  {
128  mln_assertion(nfalse_ > 0);
129  --nfalse_;
130  }
131  }
132 
133  inline
134  void
135  rank<bool>::take(const rank<bool>& other)
136  {
137  nfalse_ += other.nfalse_;
138  }
139 
140  inline
141  void
142  rank<bool>::untake(const rank<bool>& other)
143  {
144  mln_precondition(other.nfalse_ <= nfalse_);
145  nfalse_ -= other.nfalse_;
146  }
147 
148  inline
149  bool
150  rank<bool>::to_result() const
151  {
152  return k_ >= nfalse_;
153  }
154 
155  inline
156  bool
157  rank<bool>::is_valid() const
158  {
159  return true;
160  }
161 
162 # endif // ! MLN_INCLUDE_ONLY
163 
164  } // end of namespace mln::accu::stat
165 
166  } // end of namespace mln::accu
167 
168 } // end of namespace mln
169 
170 
171 #endif // ! MLN_ACCU_STAT_RANK_BOOL_HH