3 * Part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
6 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 * Permission to use, modify and distribute this software is granted
9 * provided that this copyright notice appears in all copies. For
10 * precise terms see the accompanying LICENSE file.
12 * This software is provided "AS IS" with no warranty of any kind,
13 * express or implied, and with no claim as to its suitability for any
18 #ifndef LEMON_COUNTER_H
19 #define LEMON_COUNTER_H
26 ///\brief Tools for counting steps and events
31 template<class P> class _SubNoCounter;
42 typedef _SubCounter<_SubCounter<P> > SubCounter;
43 typedef _SubNoCounter<_SubCounter<P> > SubNoCounter;
45 _SubCounter(P &parent)
46 : _parent(parent), _title(), _os(std::cerr), count(0) {}
47 _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
48 : _parent(parent), _title(title), _os(os), count(0) {}
49 _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
50 : _parent(parent), _title(title), _os(os), count(0) {}
52 _os << _title << count <<std::endl;
55 _SubCounter &operator++() { count++; return *this;}
56 int operator++(int) { return count++; }
57 _SubCounter &operator--() { count--; return *this;}
58 int operator--(int) { return count--; }
59 _SubCounter &operator+=(int c) { count+=c; return *this;}
60 _SubCounter &operator-=(int c) { count-=c; return *this;}
61 void reset(int c=0) {count=c;}
62 operator int() {return count;}
70 typedef _SubNoCounter<_SubNoCounter<P> > SubCounter;
71 typedef _SubNoCounter<_SubNoCounter<P> > SubNoCounter;
73 _SubNoCounter(P &parent) :_parent(parent) {}
74 _SubNoCounter(P &parent,std::string title,std::ostream &os=std::cerr)
76 _SubNoCounter(P &parent,const char *title,std::ostream &os=std::cerr)
79 _SubNoCounter &operator++() { ++_parent; return *this;}
80 int operator++(int) { _parent++; return 0;}
81 _SubNoCounter &operator--() { --_parent; return *this;}
82 int operator--(int) { _parent--; return 0;}
83 _SubNoCounter &operator+=(int c) { _parent+=c; return *this;}
84 _SubNoCounter &operator-=(int c) { _parent-=c; return *this;}
85 void reset(int c=0) {}
86 operator int() {return 0;}
90 /// \addtogroup timecount
95 ///This class makes it easier to count certain events. You can increment
96 ///or decrement the counter using operator++ and operator--.
97 ///A report is automatically printed on destruction.
106 ///\todo document please.
108 typedef _SubCounter<Counter> SubCounter;
111 ///\todo document please.
113 typedef _SubNoCounter<Counter> SubNoCounter;
116 Counter() : _title(), _os(std::cerr), count(0) {}
118 Counter(std::string title,std::ostream &os=std::cerr)
119 : _title(title), _os(os), count(0) {}
121 Counter(const char *title,std::ostream &os=std::cerr)
122 : _title(title), _os(os), count(0) {}
123 ///Destructor. Prints the given title and the value of the counter.
125 _os << _title << count <<std::endl;
128 Counter &operator++() { count++; return *this;}
130 int operator++(int) { return count++;}
132 Counter &operator--() { count--; return *this;}
134 int operator--(int) { return count--;}
136 Counter &operator+=(int c) { count+=c; return *this;}
138 Counter &operator-=(int c) { count-=c; return *this;}
140 void reset(int c=0) {count=c;}
142 operator int() {return count;}
145 ///'Do nothing' version of \ref Counter
147 ///'Do nothing' version of \ref Counter.
152 typedef _SubNoCounter<NoCounter> SubCounter;
153 typedef _SubNoCounter<NoCounter> SubNoCounter;
156 NoCounter(std::string title,std::ostream &os=std::cerr) {}
157 NoCounter(const char *title,std::ostream &os=std::cerr) {}
158 NoCounter &operator++() { return *this; }
159 int operator++(int) { return 0; }
160 NoCounter &operator--() { return *this; }
161 int operator--(int) { return 0; }
162 NoCounter &operator+=(int c) { return *this;}
163 NoCounter &operator-=(int c) { return *this;}
164 void reset(int c=0) {}
165 operator int() {return 0;}