alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@119:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@119:  *
alpar@119:  * Copyright (C) 2003-2008
alpar@119:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@119:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@119:  *
alpar@119:  * Permission to use, modify and distribute this software is granted
alpar@119:  * provided that this copyright notice appears in all copies. For
alpar@119:  * precise terms see the accompanying LICENSE file.
alpar@119:  *
alpar@119:  * This software is provided "AS IS" with no warranty of any kind,
alpar@119:  * express or implied, and with no claim as to its suitability for any
alpar@119:  * purpose.
alpar@119:  *
alpar@119:  */
alpar@119: 
alpar@119: #ifndef LEMON_COUNTER_H
alpar@119: #define LEMON_COUNTER_H
alpar@119: 
alpar@119: #include <string>
alpar@119: #include <iostream>
alpar@119: 
alpar@119: ///\ingroup timecount
alpar@119: ///\file
alpar@119: ///\brief Tools for counting steps and events
alpar@119: 
alpar@209: namespace lemon
alpar@119: {
alpar@119: 
alpar@121:   template<class P> class _NoSubCounter;
alpar@119: 
alpar@119:   template<class P>
alpar@209:   class _SubCounter
alpar@119:   {
alpar@119:     P &_parent;
alpar@119:     std::string _title;
alpar@119:     std::ostream &_os;
alpar@119:     int count;
alpar@119:   public:
alpar@119: 
alpar@119:     typedef _SubCounter<_SubCounter<P> > SubCounter;
alpar@121:     typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
alpar@119: 
alpar@119:     _SubCounter(P &parent)
alpar@119:       : _parent(parent), _title(), _os(std::cerr), count(0) {}
alpar@119:     _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
alpar@119:       : _parent(parent), _title(title), _os(os), count(0) {}
alpar@119:     _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
alpar@119:       : _parent(parent), _title(title), _os(os), count(0) {}
alpar@209:     ~_SubCounter() {
alpar@119:       _os << _title << count <<std::endl;
alpar@119:       _parent+=count;
alpar@119:     }
alpar@119:     _SubCounter &operator++() { count++; return *this;}
alpar@119:     int operator++(int) { return count++; }
alpar@119:     _SubCounter &operator--() { count--; return *this;}
alpar@119:     int operator--(int) { return count--; }
alpar@119:     _SubCounter &operator+=(int c) { count+=c; return *this;}
alpar@119:     _SubCounter &operator-=(int c) { count-=c; return *this;}
alpar@119:     operator int() {return count;}
alpar@119:   };
alpar@119: 
alpar@119:   template<class P>
alpar@209:   class _NoSubCounter
alpar@119:   {
alpar@119:     P &_parent;
alpar@119:   public:
alpar@121:     typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
alpar@121:     typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
alpar@209: 
alpar@121:     _NoSubCounter(P &parent) :_parent(parent) {}
alpar@209:     _NoSubCounter(P &parent,std::string,std::ostream &)
alpar@119:       :_parent(parent) {}
alpar@209:     _NoSubCounter(P &parent,std::string)
alpar@119:       :_parent(parent) {}
alpar@121:     _NoSubCounter(P &parent,const char *,std::ostream &)
alpar@119:       :_parent(parent) {}
alpar@121:     _NoSubCounter(P &parent,const char *)
alpar@119:       :_parent(parent) {}
alpar@121:     ~_NoSubCounter() {}
alpar@121:     _NoSubCounter &operator++() { ++_parent; return *this;}
alpar@119:     int operator++(int) { _parent++; return 0;}
alpar@121:     _NoSubCounter &operator--() { --_parent; return *this;}
alpar@119:     int operator--(int) { _parent--; return 0;}
alpar@121:     _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
alpar@121:     _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
alpar@119:     operator int() {return 0;}
alpar@119:   };
alpar@119: 
alpar@119: 
alpar@119:   /// \addtogroup timecount
alpar@119:   /// @{
alpar@119: 
kpeter@160:   /// A counter class
alpar@119: 
kpeter@160:   /// This class makes it easier to count certain events (e.g. for debug
kpeter@160:   /// reasons).
kpeter@160:   /// You can increment or decrement the counter using \c operator++,
kpeter@160:   /// \c operator--, \c operator+= and \c operator-=. You can also
kpeter@160:   /// define subcounters for the different phases of the algorithm or
kpeter@160:   /// for different types of operations.
kpeter@160:   /// A report containing the given title and the value of the counter
alpar@209:   /// is automatically printed on destruction.
kpeter@160:   ///
kpeter@160:   /// The following example shows the usage of counters and subcounters.
kpeter@160:   /// \code
kpeter@160:   /// // Bubble sort
kpeter@160:   /// std::vector<T> v;
kpeter@160:   /// ...
kpeter@160:   /// Counter op("Operations: ");
kpeter@160:   /// Counter::SubCounter as(op, "Assignments: ");
kpeter@160:   /// Counter::SubCounter co(op, "Comparisons: ");
kpeter@160:   /// for (int i = v.size()-1; i > 0; --i) {
kpeter@160:   ///   for (int j = 0; j < i; ++j) {
kpeter@160:   ///     if (v[j] > v[j+1]) {
kpeter@160:   ///       T tmp = v[j];
kpeter@160:   ///       v[j] = v[j+1];
kpeter@160:   ///       v[j+1] = tmp;
kpeter@160:   ///       as += 3;          // three assignments
kpeter@160:   ///     }
kpeter@160:   ///     ++co;               // one comparison
kpeter@160:   ///   }
kpeter@160:   /// }
kpeter@160:   /// \endcode
kpeter@160:   ///
kpeter@160:   /// This code prints out something like that:
kpeter@160:   /// \code
kpeter@160:   /// Comparisons: 45
kpeter@160:   /// Assignments: 57
kpeter@160:   /// Operations: 102
kpeter@160:   /// \endcode
kpeter@160:   ///
kpeter@160:   /// \sa NoCounter
alpar@209:   class Counter
alpar@119:   {
alpar@119:     std::string _title;
alpar@119:     std::ostream &_os;
alpar@119:     int count;
alpar@119:   public:
alpar@119: 
kpeter@160:     /// SubCounter class
alpar@209: 
kpeter@160:     /// This class can be used to setup subcounters for a \ref Counter
kpeter@160:     /// to have finer reports. A subcounter provides exactly the same
kpeter@160:     /// operations as the main \ref Counter, but it also increments and
kpeter@160:     /// decrements the value of its parent.
kpeter@160:     /// Subcounters can also have subcounters.
alpar@209:     ///
kpeter@160:     /// The parent counter must be given as the first parameter of the
kpeter@160:     /// constructor. Apart from that a title and an \c ostream object
kpeter@160:     /// can also be given just like for the main \ref Counter.
alpar@119:     ///
kpeter@160:     /// A report containing the given title and the value of the
kpeter@160:     /// subcounter is automatically printed on destruction. If you
kpeter@160:     /// would like to turn off this report, use \ref NoSubCounter
kpeter@160:     /// instead.
alpar@209:     ///
kpeter@160:     /// \sa NoSubCounter
alpar@119:     typedef _SubCounter<Counter> SubCounter;
alpar@119: 
alpar@209:     /// SubCounter class without printing report on destruction
alpar@209: 
kpeter@160:     /// This class can be used to setup subcounters for a \ref Counter.
kpeter@160:     /// It is the same as \ref SubCounter but it does not print report
kpeter@160:     /// on destruction. (It modifies the value of its parent, so 'No'
kpeter@160:     /// only means 'do not print'.)
alpar@119:     ///
kpeter@160:     /// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter
alpar@209:     /// "NoSubCounter"s makes it possible to turn off reporting
kpeter@160:     /// subcounter values without actually removing the definitions
kpeter@160:     /// and the increment or decrement operators.
kpeter@160:     ///
kpeter@160:     /// \sa SubCounter
alpar@121:     typedef _NoSubCounter<Counter> NoSubCounter;
alpar@119: 
kpeter@160:     /// Constructor.
alpar@119:     Counter() : _title(), _os(std::cerr), count(0) {}
kpeter@160:     /// Constructor.
alpar@209:     Counter(std::string title,std::ostream &os=std::cerr)
alpar@119:       : _title(title), _os(os), count(0) {}
kpeter@160:     /// Constructor.
alpar@119:     Counter(const char *title,std::ostream &os=std::cerr)
alpar@119:       : _title(title), _os(os), count(0) {}
kpeter@160:     /// Destructor. Prints the given title and the value of the counter.
alpar@119:     ~Counter() {
alpar@119:       _os << _title << count <<std::endl;
alpar@119:     }
alpar@119:     ///\e
alpar@119:     Counter &operator++() { count++; return *this;}
alpar@119:     ///\e
alpar@119:     int operator++(int) { return count++;}
alpar@119:     ///\e
alpar@119:     Counter &operator--() { count--; return *this;}
alpar@119:     ///\e
alpar@119:     int operator--(int) { return count--;}
alpar@119:     ///\e
alpar@119:     Counter &operator+=(int c) { count+=c; return *this;}
alpar@119:     ///\e
alpar@119:     Counter &operator-=(int c) { count-=c; return *this;}
kpeter@160:     /// Resets the counter to the given value.
kpeter@168: 
kpeter@168:     /// Resets the counter to the given value.
kpeter@168:     /// \note This function does not reset the values of
kpeter@168:     /// \ref SubCounter "SubCounter"s but it resets \ref NoSubCounter
alpar@209:     /// "NoSubCounter"s along with the main counter.
alpar@119:     void reset(int c=0) {count=c;}
kpeter@160:     /// Returns the value of the counter.
alpar@119:     operator int() {return count;}
alpar@119:   };
alpar@119: 
kpeter@160:   /// 'Do nothing' version of Counter.
alpar@119: 
kpeter@160:   /// This class can be used in the same way as \ref Counter however it
kpeter@160:   /// does not count at all and does not print report on destruction.
kpeter@160:   ///
kpeter@160:   /// Replacing a \ref Counter with a \ref NoCounter makes it possible
kpeter@160:   /// to turn off all counting and reporting (SubCounters should also
kpeter@160:   /// be replaced with NoSubCounters), so it does not affect the
kpeter@160:   /// efficiency of the program at all.
kpeter@160:   ///
kpeter@160:   /// \sa Counter
alpar@119:   class NoCounter
alpar@119:   {
alpar@119:   public:
alpar@121:     typedef _NoSubCounter<NoCounter> SubCounter;
alpar@121:     typedef _NoSubCounter<NoCounter> NoSubCounter;
alpar@119: 
alpar@119:     NoCounter() {}
alpar@119:     NoCounter(std::string,std::ostream &) {}
alpar@119:     NoCounter(const char *,std::ostream &) {}
alpar@119:     NoCounter(std::string) {}
alpar@119:     NoCounter(const char *) {}
alpar@119:     NoCounter &operator++() { return *this; }
alpar@119:     int operator++(int) { return 0; }
alpar@119:     NoCounter &operator--() { return *this; }
alpar@119:     int operator--(int) { return 0; }
alpar@119:     NoCounter &operator+=(int) { return *this;}
alpar@119:     NoCounter &operator-=(int) { return *this;}
alpar@119:     void reset(int) {}
alpar@119:     void reset() {}
alpar@119:     operator int() {return 0;}
alpar@119:   };
alpar@119: 
alpar@119:   ///@}
alpar@119: }
alpar@119: 
alpar@119: #endif