alpar@1847
|
1 |
/* -*- C++ -*-
|
alpar@1847
|
2 |
* lemon/counter.h -
|
alpar@1847
|
3 |
* Part of LEMON, a generic C++ optimization library
|
alpar@1847
|
4 |
*
|
alpar@1847
|
5 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1847
|
6 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@1847
|
7 |
*
|
alpar@1847
|
8 |
* Permission to use, modify and distribute this software is granted
|
alpar@1847
|
9 |
* provided that this copyright notice appears in all copies. For
|
alpar@1847
|
10 |
* precise terms see the accompanying LICENSE file.
|
alpar@1847
|
11 |
*
|
alpar@1847
|
12 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@1847
|
13 |
* express or implied, and with no claim as to its suitability for any
|
alpar@1847
|
14 |
* purpose.
|
alpar@1847
|
15 |
*
|
alpar@1847
|
16 |
*/
|
alpar@1847
|
17 |
|
alpar@1847
|
18 |
#ifndef LEMON_COUNTER_H
|
alpar@1847
|
19 |
#define LEMON_COUNTER_H
|
alpar@1847
|
20 |
|
alpar@1847
|
21 |
#include <string>
|
alpar@1847
|
22 |
#include <iostream>
|
alpar@1847
|
23 |
|
alpar@1847
|
24 |
///\ingroup timecount
|
alpar@1847
|
25 |
///\file
|
alpar@1847
|
26 |
///\brief Tools for counting steps and events
|
alpar@1847
|
27 |
|
alpar@1847
|
28 |
namespace lemon
|
alpar@1847
|
29 |
{
|
alpar@1847
|
30 |
|
alpar@1847
|
31 |
template<class P> class _SubNoCounter;
|
alpar@1847
|
32 |
|
alpar@1847
|
33 |
template<class P>
|
alpar@1847
|
34 |
class _SubCounter
|
alpar@1847
|
35 |
{
|
alpar@1847
|
36 |
P &_parent;
|
alpar@1847
|
37 |
std::string _title;
|
alpar@1847
|
38 |
std::ostream &_os;
|
alpar@1847
|
39 |
int count;
|
alpar@1847
|
40 |
public:
|
alpar@1847
|
41 |
|
alpar@1847
|
42 |
typedef _SubCounter<_SubCounter<P> > SubCounter;
|
alpar@1847
|
43 |
typedef _SubNoCounter<_SubCounter<P> > SubNoCounter;
|
alpar@1847
|
44 |
|
alpar@1847
|
45 |
_SubCounter(P &parent)
|
alpar@1847
|
46 |
: _parent(parent), _title(), _os(std::cerr), count(0) {}
|
alpar@1847
|
47 |
_SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
|
alpar@1847
|
48 |
: _parent(parent), _title(title), _os(os), count(0) {}
|
alpar@1847
|
49 |
_SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
|
alpar@1847
|
50 |
: _parent(parent), _title(title), _os(os), count(0) {}
|
alpar@1847
|
51 |
~_SubCounter() {
|
alpar@1847
|
52 |
_os << _title << count <<std::endl;
|
alpar@1847
|
53 |
_parent+=count;
|
alpar@1847
|
54 |
}
|
alpar@1847
|
55 |
_SubCounter &operator++() { count++; return *this;}
|
alpar@1847
|
56 |
int operator++(int) { return count++; }
|
alpar@1847
|
57 |
_SubCounter &operator--() { count--; return *this;}
|
alpar@1847
|
58 |
int operator--(int) { return count--; }
|
alpar@1847
|
59 |
_SubCounter &operator+=(int c) { count+=c; return *this;}
|
alpar@1847
|
60 |
_SubCounter &operator-=(int c) { count-=c; return *this;}
|
alpar@1847
|
61 |
void reset(int c=0) {count=c;}
|
alpar@1847
|
62 |
operator int() {return count;}
|
alpar@1847
|
63 |
};
|
alpar@1847
|
64 |
|
alpar@1847
|
65 |
template<class P>
|
alpar@1847
|
66 |
class _SubNoCounter
|
alpar@1847
|
67 |
{
|
alpar@1847
|
68 |
P &_parent;
|
alpar@1847
|
69 |
public:
|
alpar@1847
|
70 |
typedef _SubNoCounter<_SubNoCounter<P> > SubCounter;
|
alpar@1847
|
71 |
typedef _SubNoCounter<_SubNoCounter<P> > SubNoCounter;
|
alpar@1847
|
72 |
|
alpar@1847
|
73 |
_SubNoCounter(P &parent) :_parent(parent) {}
|
alpar@1847
|
74 |
_SubNoCounter(P &parent,std::string title,std::ostream &os=std::cerr)
|
alpar@1847
|
75 |
:_parent(parent) {}
|
alpar@1847
|
76 |
_SubNoCounter(P &parent,const char *title,std::ostream &os=std::cerr)
|
alpar@1847
|
77 |
:_parent(parent) {}
|
alpar@1847
|
78 |
~_SubNoCounter() {}
|
alpar@1847
|
79 |
_SubNoCounter &operator++() { ++_parent; return *this;}
|
alpar@1847
|
80 |
int operator++(int) { _parent++; return 0;}
|
alpar@1847
|
81 |
_SubNoCounter &operator--() { --_parent; return *this;}
|
alpar@1847
|
82 |
int operator--(int) { _parent--; return 0;}
|
alpar@1847
|
83 |
_SubNoCounter &operator+=(int c) { _parent+=c; return *this;}
|
alpar@1847
|
84 |
_SubNoCounter &operator-=(int c) { _parent-=c; return *this;}
|
alpar@1847
|
85 |
void reset(int c=0) {}
|
alpar@1847
|
86 |
operator int() {return 0;}
|
alpar@1847
|
87 |
};
|
alpar@1847
|
88 |
|
alpar@1847
|
89 |
|
alpar@1847
|
90 |
/// \addtogroup timecount
|
alpar@1847
|
91 |
/// @{
|
alpar@1847
|
92 |
|
alpar@1847
|
93 |
///A counter class
|
alpar@1847
|
94 |
|
alpar@1847
|
95 |
///This class makes it easier to count certain events. You can increment
|
alpar@1847
|
96 |
///or decrement the counter using operator++ and operator--.
|
alpar@1847
|
97 |
///A report is automatically printed on destruction.
|
alpar@1851
|
98 |
///\todo More doc
|
alpar@1847
|
99 |
class Counter
|
alpar@1847
|
100 |
{
|
alpar@1847
|
101 |
std::string _title;
|
alpar@1847
|
102 |
std::ostream &_os;
|
alpar@1847
|
103 |
int count;
|
alpar@1847
|
104 |
public:
|
alpar@1847
|
105 |
///\e
|
alpar@1847
|
106 |
|
alpar@1847
|
107 |
///\todo document please.
|
alpar@1847
|
108 |
///
|
alpar@1847
|
109 |
typedef _SubCounter<Counter> SubCounter;
|
alpar@1847
|
110 |
///\e
|
alpar@1847
|
111 |
|
alpar@1847
|
112 |
///\todo document please.
|
alpar@1847
|
113 |
///
|
alpar@1847
|
114 |
typedef _SubNoCounter<Counter> SubNoCounter;
|
alpar@1847
|
115 |
|
alpar@1847
|
116 |
///\e
|
alpar@1847
|
117 |
Counter() : _title(), _os(std::cerr), count(0) {}
|
alpar@1847
|
118 |
///\e
|
alpar@1847
|
119 |
Counter(std::string title,std::ostream &os=std::cerr)
|
alpar@1847
|
120 |
: _title(title), _os(os), count(0) {}
|
alpar@1847
|
121 |
///\e
|
alpar@1847
|
122 |
Counter(const char *title,std::ostream &os=std::cerr)
|
alpar@1847
|
123 |
: _title(title), _os(os), count(0) {}
|
alpar@1847
|
124 |
///Destructor. Prints the given title and the value of the counter.
|
alpar@1847
|
125 |
~Counter() {
|
alpar@1847
|
126 |
_os << _title << count <<std::endl;
|
alpar@1847
|
127 |
}
|
alpar@1847
|
128 |
///\e
|
alpar@1847
|
129 |
Counter &operator++() { count++; return *this;}
|
alpar@1847
|
130 |
///\e
|
alpar@1847
|
131 |
int operator++(int) { return count++;}
|
alpar@1847
|
132 |
///\e
|
alpar@1847
|
133 |
Counter &operator--() { count--; return *this;}
|
alpar@1847
|
134 |
///\e
|
alpar@1847
|
135 |
int operator--(int) { return count--;}
|
alpar@1847
|
136 |
///\e
|
alpar@1847
|
137 |
Counter &operator+=(int c) { count+=c; return *this;}
|
alpar@1847
|
138 |
///\e
|
alpar@1847
|
139 |
Counter &operator-=(int c) { count-=c; return *this;}
|
alpar@1847
|
140 |
///\e
|
alpar@1847
|
141 |
void reset(int c=0) {count=c;}
|
alpar@1847
|
142 |
///\e
|
alpar@1847
|
143 |
operator int() {return count;}
|
alpar@1847
|
144 |
};
|
alpar@1847
|
145 |
|
alpar@1847
|
146 |
///'Do nothing' version of \ref Counter
|
alpar@1847
|
147 |
|
alpar@1847
|
148 |
///'Do nothing' version of \ref Counter.
|
alpar@1847
|
149 |
///\sa Counter
|
alpar@1847
|
150 |
class NoCounter
|
alpar@1847
|
151 |
{
|
alpar@1847
|
152 |
public:
|
alpar@1847
|
153 |
typedef _SubNoCounter<NoCounter> SubCounter;
|
alpar@1847
|
154 |
typedef _SubNoCounter<NoCounter> SubNoCounter;
|
alpar@1847
|
155 |
|
alpar@1847
|
156 |
NoCounter() {}
|
alpar@1847
|
157 |
NoCounter(std::string title,std::ostream &os=std::cerr) {}
|
alpar@1847
|
158 |
NoCounter(const char *title,std::ostream &os=std::cerr) {}
|
alpar@1847
|
159 |
NoCounter &operator++() { return *this; }
|
alpar@1847
|
160 |
int operator++(int) { return 0; }
|
alpar@1847
|
161 |
NoCounter &operator--() { return *this; }
|
alpar@1847
|
162 |
int operator--(int) { return 0; }
|
alpar@1847
|
163 |
NoCounter &operator+=(int c) { return *this;}
|
alpar@1847
|
164 |
NoCounter &operator-=(int c) { return *this;}
|
alpar@1847
|
165 |
void reset(int c=0) {}
|
alpar@1847
|
166 |
operator int() {return 0;}
|
alpar@1847
|
167 |
};
|
alpar@1847
|
168 |
|
alpar@1847
|
169 |
///@}
|
alpar@1847
|
170 |
}
|
alpar@1847
|
171 |
|
alpar@1847
|
172 |
#endif
|