Counter

Declared in lemon/counter.h, in the group Time Measuring and Counting; the same class on the Doxygen site.

class Counter

A counter class.

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

Public Types

typedef _SubCounter< Counter > SubCounter

SubCounter class.

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

SubCounter class without printing report on destruction.

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

Public Functions

inline Counter()

Constructor.

inline Counter( std::string title , std::ostream & os = std::cerr )

Constructor.

inline Counter( const char * title , std::ostream & os = std::cerr )

Constructor.

inline ~Counter()

Destructor. Prints the given title and the value of the counter.

inline Counter & operator++()

inline int operator++(int)

inline Counter & operator--()

inline int operator--(int)

inline Counter & operator+=( int c )

inline Counter & operator-=( int c )

inline void reset( int c = 0 )

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.

inline operator int()

Returns the value of the counter.