1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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
25 #include <lemon/core.h>
29 ///\brief Tools for counting steps and events
34 template<class P> class _NoSubCounter;
45 typedef _SubCounter<_SubCounter<P> > SubCounter;
46 typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
48 _SubCounter(P &parent)
49 : _parent(parent), _title(), _os(std::cerr), count(0) {}
50 _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
51 : _parent(parent), _title(title), _os(os), count(0) {}
52 _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
53 : _parent(parent), _title(title), _os(os), count(0) {}
55 _os << _title << count <<std::endl;
58 _SubCounter &operator++() { count++; return *this;}
59 int operator++(int) { return count++; }
60 _SubCounter &operator--() { count--; return *this;}
61 int operator--(int) { return count--; }
62 _SubCounter &operator+=(int c) { count+=c; return *this;}
63 _SubCounter &operator-=(int c) { count-=c; return *this;}
64 operator int() {return count;}
72 typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
73 typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
75 _NoSubCounter(P &parent) :_parent(parent) {}
76 _NoSubCounter(P &parent,std::string,std::ostream &)
78 _NoSubCounter(P &parent,std::string)
80 _NoSubCounter(P &parent,const char *,std::ostream &)
82 _NoSubCounter(P &parent,const char *)
85 _NoSubCounter &operator++() { ++_parent; return *this;}
86 int operator++(int) { _parent++; return 0;}
87 _NoSubCounter &operator--() { --_parent; return *this;}
88 int operator--(int) { _parent--; return 0;}
89 _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
90 _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
91 operator int() {return 0;}
95 /// \addtogroup timecount
100 /// This class makes it easier to count certain events (e.g. for debug
102 /// You can increment or decrement the counter using \c operator++,
103 /// \c operator--, \c operator+= and \c operator-=. You can also
104 /// define subcounters for the different phases of the algorithm or
105 /// for different types of operations.
106 /// A report containing the given title and the value of the counter
107 /// is automatically printed on destruction.
109 /// The following example shows the usage of counters and subcounters.
112 /// std::vector<T> v;
114 /// Counter op("Operations: ");
115 /// Counter::SubCounter as(op, "Assignments: ");
116 /// Counter::SubCounter co(op, "Comparisons: ");
117 /// for (int i = v.size()-1; i > 0; --i) {
118 /// for (int j = 0; j < i; ++j) {
119 /// if (v[j] > v[j+1]) {
123 /// as += 3; // three assignments
125 /// ++co; // one comparison
130 /// This code prints out something like that:
147 /// This class can be used to setup subcounters for a \ref Counter
148 /// to have finer reports. A subcounter provides exactly the same
149 /// operations as the main \ref Counter, but it also increments and
150 /// decrements the value of its parent.
151 /// Subcounters can also have subcounters.
153 /// The parent counter must be given as the first parameter of the
154 /// constructor. Apart from that a title and an \c ostream object
155 /// can also be given just like for the main \ref Counter.
157 /// A report containing the given title and the value of the
158 /// subcounter is automatically printed on destruction. If you
159 /// would like to turn off this report, use \ref NoSubCounter
163 typedef _SubCounter<Counter> SubCounter;
165 /// SubCounter class without printing report on destruction
167 /// This class can be used to setup subcounters for a \ref Counter.
168 /// It is the same as \ref SubCounter but it does not print report
169 /// on destruction. (It modifies the value of its parent, so 'No'
170 /// only means 'do not print'.)
172 /// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter
173 /// "NoSubCounter"s makes it possible to turn off reporting
174 /// subcounter values without actually removing the definitions
175 /// and the increment or decrement operators.
178 typedef _NoSubCounter<Counter> NoSubCounter;
181 Counter() : _title(), _os(std::cerr), count(0) {}
183 Counter(std::string title,std::ostream &os=std::cerr)
184 : _title(title), _os(os), count(0) {}
186 Counter(const char *title,std::ostream &os=std::cerr)
187 : _title(title), _os(os), count(0) {}
188 /// Destructor. Prints the given title and the value of the counter.
190 _os << _title << count <<std::endl;
193 Counter &operator++() { count++; return *this;}
195 int operator++(int) { return count++;}
197 Counter &operator--() { count--; return *this;}
199 int operator--(int) { return count--;}
201 Counter &operator+=(int c) { count+=c; return *this;}
203 Counter &operator-=(int c) { count-=c; return *this;}
204 /// Resets the counter to the given value.
206 /// Resets the counter to the given value.
207 /// \note This function does not reset the values of
208 /// \ref SubCounter "SubCounter"s but it resets \ref NoSubCounter
209 /// "NoSubCounter"s along with the main counter.
210 void reset(int c=0) {count=c;}
211 /// Returns the value of the counter.
212 operator int() {return count;}
215 /// 'Do nothing' version of Counter.
217 /// This class can be used in the same way as \ref Counter, but it
218 /// does not count at all and does not print report on destruction.
220 /// Replacing a \ref Counter with a \ref NoCounter makes it possible
221 /// to turn off all counting and reporting (SubCounters should also
222 /// be replaced with NoSubCounters), so it does not affect the
223 /// efficiency of the program at all.
229 typedef _NoSubCounter<NoCounter> SubCounter;
230 typedef _NoSubCounter<NoCounter> NoSubCounter;
233 NoCounter(std::string,std::ostream &) {}
234 NoCounter(const char *,std::ostream &) {}
235 NoCounter(std::string) {}
236 NoCounter(const char *) {}
237 NoCounter &operator++() { return *this; }
238 int operator++(int) { return 0; }
239 NoCounter &operator--() { return *this; }
240 int operator--(int) { return 0; }
241 NoCounter &operator+=(int) { return *this;}
242 NoCounter &operator-=(int) { return *this;}
245 operator int() {return 0;}