All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
List of all members | Public Types | Public Member Functions
Counter Class Reference

Detailed Description

This class makes it easier to count certain events (e.g. for debug reasons). You can increment or decrement the counter using operator++, operator–, operator+= and operator-=. You can also define subcounters for the different phases of the algorithm or for different types of operations. A report containing the given title and the value of the counter is automatically printed on destruction.

The following example shows the usage of counters and subcounters.

// Bubble sort
std::vector<T> v;
...
Counter op("Operations: ");
Counter::SubCounter as(op, "Assignments: ");
Counter::SubCounter co(op, "Comparisons: ");
for (int i = v.size()-1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (v[j] > v[j+1]) {
T tmp = v[j];
v[j] = v[j+1];
v[j+1] = tmp;
as += 3; // three assignments
}
++co; // one comparison
}
}

This code prints out something like that:

Comparisons: 45
Assignments: 57
Operations: 102
See Also
NoCounter

#include <lemon/counter.h>

Public Types

typedef _SubCounter< CounterSubCounter
 SubCounter class.
 
typedef _NoSubCounter< CounterNoSubCounter
 SubCounter class without printing report on destruction.
 

Public Member Functions

 Counter ()
 Constructor.
 
 Counter (std::string title, std::ostream &os=std::cerr)
 Constructor.
 
 Counter (const char *title, std::ostream &os=std::cerr)
 Constructor.
 
 ~Counter ()
 Destructor. Prints the given title and the value of the counter.
 
Counteroperator++ ()
 
 
int operator++ (int)
 
 
Counteroperator-- ()
 
 
int operator-- (int)
 
 
Counteroperator+= (int c)
 
 
Counteroperator-= (int c)
 
 
void reset (int c=0)
 Resets the counter to the given value.
 
 operator int ()
 Returns the value of the counter.
 

Member Typedef Documentation

typedef _SubCounter<Counter> SubCounter

This class can be used to setup subcounters for a Counter to have finer reports. A subcounter provides exactly the same operations as the main Counter, but it also increments and decrements the value of its parent. Subcounters can also have subcounters.

The parent counter must be given as the first parameter of the constructor. Apart from that a title and an ostream object can also be given just like for the main Counter.

A report containing the given title and the value of the subcounter is automatically printed on destruction. If you would like to turn off this report, use NoSubCounter instead.

See Also
NoSubCounter
typedef _NoSubCounter<Counter> NoSubCounter

This class can be used to setup subcounters for a Counter. It is the same as SubCounter but it does not print report on destruction. (It modifies the value of its parent, so 'No' only means 'do not print'.)

Replacing SubCounters with NoSubCounters makes it possible to turn off reporting subcounter values without actually removing the definitions and the increment or decrement operators.

See Also
SubCounter

Member Function Documentation

void reset ( int  c = 0)
inline

Resets the counter to the given value.

Note
This function does not reset the values of SubCounters but it resets NoSubCounters along with the main counter.