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