3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_COUNTER_H
20 #define LEMON_COUNTER_H
27 ///\brief Tools for counting steps and events
32 template<class P> class _NoSubCounter;
43 typedef _SubCounter<_SubCounter<P> > SubCounter;
44 typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
46 _SubCounter(P &parent)
47 : _parent(parent), _title(), _os(std::cerr), count(0) {}
48 _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
49 : _parent(parent), _title(title), _os(os), count(0) {}
50 _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
51 : _parent(parent), _title(title), _os(os), count(0) {}
53 _os << _title << count <<std::endl;
56 _SubCounter &operator++() { count++; return *this;}
57 int operator++(int) { return count++; }
58 _SubCounter &operator--() { count--; return *this;}
59 int operator--(int) { return count--; }
60 _SubCounter &operator+=(int c) { count+=c; return *this;}
61 _SubCounter &operator-=(int c) { count-=c; return *this;}
62 void reset(int c=0) {count=c;}
63 operator int() {return count;}
71 typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
72 typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
74 _NoSubCounter(P &parent) :_parent(parent) {}
75 _NoSubCounter(P &parent,std::string,std::ostream &)
77 _NoSubCounter(P &parent,std::string)
79 _NoSubCounter(P &parent,const char *,std::ostream &)
81 _NoSubCounter(P &parent,const char *)
84 _NoSubCounter &operator++() { ++_parent; return *this;}
85 int operator++(int) { _parent++; return 0;}
86 _NoSubCounter &operator--() { --_parent; return *this;}
87 int operator--(int) { _parent--; return 0;}
88 _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
89 _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
92 operator int() {return 0;}
96 /// \addtogroup timecount
101 /// This class makes it easier to count certain events (e.g. for debug
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.
110 /// The following example shows the usage of counters and subcounters.
113 /// std::vector<T> v;
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]) {
124 /// as += 3; // three assignments
126 /// ++co; // one comparison
131 /// This code prints out something like that:
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.
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.
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
164 typedef _SubCounter<Counter> SubCounter;
166 /// SubCounter class without printing report on destruction
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'.)
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.
179 typedef _NoSubCounter<Counter> NoSubCounter;
182 Counter() : _title(), _os(std::cerr), count(0) {}
184 Counter(std::string title,std::ostream &os=std::cerr)
185 : _title(title), _os(os), count(0) {}
187 Counter(const char *title,std::ostream &os=std::cerr)
188 : _title(title), _os(os), count(0) {}
189 /// Destructor. Prints the given title and the value of the counter.
191 _os << _title << count <<std::endl;
194 Counter &operator++() { count++; return *this;}
196 int operator++(int) { return count++;}
198 Counter &operator--() { count--; return *this;}
200 int operator--(int) { return count--;}
202 Counter &operator+=(int c) { count+=c; return *this;}
204 Counter &operator-=(int c) { count-=c; return *this;}
205 /// Resets the counter to the given value.
206 void reset(int c=0) {count=c;}
207 /// Returns the value of the counter.
208 operator int() {return count;}
211 /// 'Do nothing' version of 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.
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.
225 typedef _NoSubCounter<NoCounter> SubCounter;
226 typedef _NoSubCounter<NoCounter> NoSubCounter;
229 NoCounter(std::string,std::ostream &) {}
230 NoCounter(const char *,std::ostream &) {}
231 NoCounter(std::string) {}
232 NoCounter(const char *) {}
233 NoCounter &operator++() { return *this; }
234 int operator++(int) { return 0; }
235 NoCounter &operator--() { return *this; }
236 int operator--(int) { return 0; }
237 NoCounter &operator+=(int) { return *this;}
238 NoCounter &operator-=(int) { return *this;}
241 operator int() {return 0;}