count.hh 3.91 KB
Newer Older
Guillaume Lazzara's avatar
Guillaume Lazzara committed
1
2
// Copyright (C) 2007, 2008, 2009 EPITA Research and Development
// Laboratory (LRDE)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//
// This file is part of the Olena Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License version 2 as published by the
// Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
// Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

Thierry Geraud's avatar
Thierry Geraud committed
29
30
#ifndef MLN_ACCU_COUNT_HH
# define MLN_ACCU_COUNT_HH
31

32
/// \file mln/accu/count.hh
33
///
Guillaume Lazzara's avatar
Guillaume Lazzara committed
34
/// Define an accumulator that counts.
35

36
# include <mln/accu/internal/base.hh>
37
# include <mln/core/concept/meta_accumulator.hh>
38

39

40
41
42
namespace mln
{

43
  namespace accu
44
45
  {

Guillaume Lazzara's avatar
Guillaume Lazzara committed
46
    /// \brief Generic counter accumulator.
47
    /// The parameter \a T is the type to be count.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
48
49
50
    ///
    /// \ingroup modaccusiteset
    //
51
    template <typename T>
Ugo Jardonnet's avatar
Ugo Jardonnet committed
52
    struct count : public mln::accu::internal::base< unsigned , count<T> >
53
    {
54
      typedef T argument;
55

56
      count();
57

58
59
      /// Manipulators.
      /// \{
60
      void init();
61
      void take(const argument&);
62
      void take(const count<T>& other);
63

64
65
66
      void untake(const argument&);
      void untake(const count<T>& other);

67
      /// Force the value of the counter to \a c.
Ugo Jardonnet's avatar
Ugo Jardonnet committed
68
      void set_value(unsigned c);
69
      /// \}
Thierry Geraud's avatar
Thierry Geraud committed
70

71
      /// Get the value of the accumulator.
Ugo Jardonnet's avatar
Ugo Jardonnet committed
72
      unsigned to_result() const;
73

74
75
76
77
      /// Check whether this accu is able to return a result.
      /// Always true here.
      bool is_valid() const;

78
79
    protected:
      /// The value of the counter.
Ugo Jardonnet's avatar
Ugo Jardonnet committed
80
      unsigned count_;
81
    };
82

83

84
    namespace meta
85
    {
86

Guillaume Lazzara's avatar
Guillaume Lazzara committed
87
      /// Meta accumulator for count.
88
      struct count : public Meta_Accumulator< count >
89
      {
90
91
92
	template <typename T>
	struct with
	{
93
	  typedef accu::count<T> ret;
94
	};
95
      };
96
97

    } // end of namespace mln::accu::meta
98

99

100

101
102
# ifndef MLN_INCLUDE_ONLY

103
    template <typename T>
104
    inline
105
    count<T>::count()
106
107
108
109
    {
      init();
    }

110
    template <typename T>
111
    inline
112
    void
113
    count<T>::init()
114
    {
115
      count_ = 0;
116
117
    }

118
    template <typename T>
119
    inline
120
    void
121
    count<T>::take(const argument&)
122
    {
123
      ++count_;
124
125
    }

126
127
128
129
130
131
132
133
134
    template <typename T>
    inline
    void
    count<T>::untake(const argument&)
    {
      mln_precondition(count_ > 0);
      --count_;
    }

135
    template <typename T>
136
    inline
137
    void
138
    count<T>::take(const count<T>& other)
139
    {
140
      count_ += other.count_;
141
142
    }

143
144
145
146
147
148
149
150
151
    template <typename T>
    inline
    void
    count<T>::untake(const count<T>& other)
    {
      mln_precondition(other.count_ <= count_);
      count_ -= other.count_;
    }

152
    template <typename T>
153
    inline
Ugo Jardonnet's avatar
Ugo Jardonnet committed
154
    unsigned
155
    count<T>::to_result() const
156
    {
157
      return count_;
158
159
    }

160
    template <typename T>
161
    inline
162
    void
Ugo Jardonnet's avatar
Ugo Jardonnet committed
163
    count<T>::set_value(unsigned c)
164
    {
165
      count_ = c;
166
167
    }

168
169
170
    template <typename T>
    inline
    bool
171
    count<T>::is_valid() const
172
173
174
175
    {
      return true;
    }

176
177
# endif // ! MLN_INCLUDE_ONLY

178
  } // end of namespace mln::accu
179
180
181
182

} // end of namespace mln


Thierry Geraud's avatar
Thierry Geraud committed
183
#endif // ! MLN_ACCU_COUNT_HH