gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Improved documentation for counters
0 1 0
default
1 file changed with 81 insertions and 15 deletions:
↑ Collapse diff ↑
Show white space 192 line context
... ...
@@ -5,177 +5,243 @@
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_COUNTER_H
20 20
#define LEMON_COUNTER_H
21 21

	
22 22
#include <string>
23 23
#include <iostream>
24 24

	
25 25
///\ingroup timecount
26 26
///\file
27 27
///\brief Tools for counting steps and events
28 28

	
29 29
namespace lemon 
30 30
{
31 31

	
32 32
  template<class P> class _NoSubCounter;
33 33

	
34 34
  template<class P>
35 35
  class _SubCounter 
36 36
  {
37 37
    P &_parent;
38 38
    std::string _title;
39 39
    std::ostream &_os;
40 40
    int count;
41 41
  public:
42 42

	
43 43
    typedef _SubCounter<_SubCounter<P> > SubCounter;
44 44
    typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
45 45

	
46 46
    _SubCounter(P &parent)
47 47
      : _parent(parent), _title(), _os(std::cerr), count(0) {}
48 48
    _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
49 49
      : _parent(parent), _title(title), _os(os), count(0) {}
50 50
    _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
51 51
      : _parent(parent), _title(title), _os(os), count(0) {}
52 52
    ~_SubCounter() { 
53 53
      _os << _title << count <<std::endl;
54 54
      _parent+=count;
55 55
    }
56 56
    _SubCounter &operator++() { count++; return *this;}
57 57
    int operator++(int) { return count++; }
58 58
    _SubCounter &operator--() { count--; return *this;}
59 59
    int operator--(int) { return count--; }
60 60
    _SubCounter &operator+=(int c) { count+=c; return *this;}
61 61
    _SubCounter &operator-=(int c) { count-=c; return *this;}
62 62
    void reset(int c=0) {count=c;}
63 63
    operator int() {return count;}
64 64
  };
65 65

	
66 66
  template<class P>
67 67
  class _NoSubCounter 
68 68
  {
69 69
    P &_parent;
70 70
  public:
71 71
    typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
72 72
    typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
73 73
  
74 74
    _NoSubCounter(P &parent) :_parent(parent) {}
75 75
    _NoSubCounter(P &parent,std::string,std::ostream &) 
76 76
      :_parent(parent) {}
77 77
    _NoSubCounter(P &parent,std::string) 
78 78
      :_parent(parent) {}
79 79
    _NoSubCounter(P &parent,const char *,std::ostream &)
80 80
      :_parent(parent) {}
81 81
    _NoSubCounter(P &parent,const char *)
82 82
      :_parent(parent) {}
83 83
    ~_NoSubCounter() {}
84 84
    _NoSubCounter &operator++() { ++_parent; return *this;}
85 85
    int operator++(int) { _parent++; return 0;}
86 86
    _NoSubCounter &operator--() { --_parent; return *this;}
87 87
    int operator--(int) { _parent--; return 0;}
88 88
    _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
89 89
    _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
90 90
    void reset(int) {}
91 91
    void reset() {}
92 92
    operator int() {return 0;}
93 93
  };
94 94

	
95 95

	
96 96
  /// \addtogroup timecount
97 97
  /// @{
98 98

	
99 99
  ///A counter class
100 100

	
101
  ///This class makes it easier to count certain events. You can increment
102
  ///or decrement the counter using operator++ and operator--.
103
  ///A report is automatically printed on destruction.
104
  ///\todo More doc
101
  /// This class makes it easier to count certain events (e.g. for debug
102
  /// reasons).
103
  /// You can increment or decrement the counter using \c operator++,
104
  /// \c operator--, \c operator+= and \c operator-=. You can also
105
  /// define subcounters for the different phases of the algorithm or
106
  /// for different types of operations.
107
  /// A report containing the given title and the value of the counter
108
  /// is automatically printed on destruction. 
109
  ///
110
  /// The following example shows the usage of counters and subcounters.
111
  /// \code
112
  /// // Bubble sort
113
  /// std::vector<T> v;
114
  /// ...
115
  /// Counter op("Operations: ");
116
  /// Counter::SubCounter as(op, "Assignments: ");
117
  /// Counter::SubCounter co(op, "Comparisons: ");
118
  /// for (int i = v.size()-1; i > 0; --i) {
119
  ///   for (int j = 0; j < i; ++j) {
120
  ///     if (v[j] > v[j+1]) {
121
  ///       T tmp = v[j];
122
  ///       v[j] = v[j+1];
123
  ///       v[j+1] = tmp;
124
  ///       as += 3;          // three assignments
125
  ///     }
126
  ///     ++co;               // one comparison
127
  ///   }
128
  /// }
129
  /// \endcode
130
  ///
131
  /// This code prints out something like that:
132
  /// \code
133
  /// Comparisons: 45
134
  /// Assignments: 57
135
  /// Operations: 102
136
  /// \endcode
137
  ///
138
  /// \sa NoCounter
105 139
  class Counter 
106 140
  {
107 141
    std::string _title;
108 142
    std::ostream &_os;
109 143
    int count;
110 144
  public:
111
    ///\e
112 145

	
113
    ///\todo document please.
146
    /// SubCounter class
147
    
148
    /// This class can be used to setup subcounters for a \ref Counter
149
    /// to have finer reports. A subcounter provides exactly the same
150
    /// operations as the main \ref Counter, but it also increments and
151
    /// decrements the value of its parent.
152
    /// Subcounters can also have subcounters.
114 153
    ///
154
    /// The parent counter must be given as the first parameter of the
155
    /// constructor. Apart from that a title and an \c ostream object
156
    /// can also be given just like for the main \ref Counter.
157
    ///
158
    /// A report containing the given title and the value of the
159
    /// subcounter is automatically printed on destruction. If you
160
    /// would like to turn off this report, use \ref NoSubCounter
161
    /// instead.
162
    /// 
163
    /// \sa NoSubCounter
115 164
    typedef _SubCounter<Counter> SubCounter;
116
    ///\e
117 165

	
118
    ///\todo document please.
166
    /// SubCounter class without printing report on destruction 
167
    
168
    /// This class can be used to setup subcounters for a \ref Counter.
169
    /// It is the same as \ref SubCounter but it does not print report
170
    /// on destruction. (It modifies the value of its parent, so 'No'
171
    /// only means 'do not print'.)
119 172
    ///
173
    /// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter
174
    /// "NoSubCounter"s makes it possible to turn off reporting 
175
    /// subcounter values without actually removing the definitions
176
    /// and the increment or decrement operators.
177
    ///
178
    /// \sa SubCounter
120 179
    typedef _NoSubCounter<Counter> NoSubCounter;
121 180

	
122
    ///\e
181
    /// Constructor.
123 182
    Counter() : _title(), _os(std::cerr), count(0) {}
124
    ///\e
183
    /// Constructor.
125 184
    Counter(std::string title,std::ostream &os=std::cerr) 
126 185
      : _title(title), _os(os), count(0) {}
127
    ///\e
186
    /// Constructor.
128 187
    Counter(const char *title,std::ostream &os=std::cerr)
129 188
      : _title(title), _os(os), count(0) {}
130 189
    ///Destructor. Prints the given title and the value of the counter.
131 190
    ~Counter() {
132 191
      _os << _title << count <<std::endl;
133 192
    }
134 193
    ///\e
135 194
    Counter &operator++() { count++; return *this;}
136 195
    ///\e
137 196
    int operator++(int) { return count++;}
138 197
    ///\e
139 198
    Counter &operator--() { count--; return *this;}
140 199
    ///\e
141 200
    int operator--(int) { return count--;}
142 201
    ///\e
143 202
    Counter &operator+=(int c) { count+=c; return *this;}
144 203
    ///\e
145 204
    Counter &operator-=(int c) { count-=c; return *this;}
146
    ///\e
205
    /// Resets the counter to the given value.
147 206
    void reset(int c=0) {count=c;}
148
    ///\e
207
    /// Returns the value of the counter.
149 208
    operator int() {return count;}
150 209
  };
151 210

	
152
  ///'Do nothing' version of \ref Counter
211
  /// 'Do nothing' version of Counter.
153 212

	
154
  ///'Do nothing' version of \ref Counter.
213
  /// This class can be used in the same way as \ref Counter however it
214
  /// does not count at all and does not print report on destruction.
215
  ///
216
  /// Replacing a \ref Counter with a \ref NoCounter makes it possible
217
  /// to turn off all counting and reporting (SubCounters should also
218
  /// be replaced with NoSubCounters), so it does not affect the
219
  /// efficiency of the program at all.
220
  ///
155 221
  ///\sa Counter
156 222
  class NoCounter
157 223
  {
158 224
  public:
159 225
    typedef _NoSubCounter<NoCounter> SubCounter;
160 226
    typedef _NoSubCounter<NoCounter> NoSubCounter;
161 227

	
162 228
    NoCounter() {}
163 229
    NoCounter(std::string,std::ostream &) {}
164 230
    NoCounter(const char *,std::ostream &) {}
165 231
    NoCounter(std::string) {}
166 232
    NoCounter(const char *) {}
167 233
    NoCounter &operator++() { return *this; }
168 234
    int operator++(int) { return 0; }
169 235
    NoCounter &operator--() { return *this; }
170 236
    int operator--(int) { return 0; }
171 237
    NoCounter &operator+=(int) { return *this;}
172 238
    NoCounter &operator-=(int) { return *this;}
173 239
    void reset(int) {}
174 240
    void reset() {}
175 241
    operator int() {return 0;}
176 242
  };
177 243

	
178 244
  ///@}
179 245
}
180 246

	
181 247
#endif
0 comments (0 inline)