alpar@119: /* -*- C++ -*- alpar@119: * alpar@119: * 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 alpar@119: #include alpar@119: alpar@119: ///\ingroup timecount alpar@119: ///\file alpar@119: ///\brief Tools for counting steps and events alpar@119: alpar@119: namespace lemon alpar@119: { alpar@119: alpar@121: template class _NoSubCounter; alpar@119: alpar@119: template alpar@119: 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

> SubCounter; alpar@121: typedef _NoSubCounter<_SubCounter

> 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@119: ~_SubCounter() { alpar@119: _os << _title << count < alpar@121: class _NoSubCounter alpar@119: { alpar@119: P &_parent; alpar@119: public: alpar@121: typedef _NoSubCounter<_NoSubCounter

> SubCounter; alpar@121: typedef _NoSubCounter<_NoSubCounter

> NoSubCounter; alpar@119: alpar@121: _NoSubCounter(P &parent) :_parent(parent) {} alpar@121: _NoSubCounter(P &parent,std::string,std::ostream &) alpar@119: :_parent(parent) {} alpar@121: _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: void reset(int) {} alpar@119: void reset() {} 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 kpeter@160: /// 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 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@119: 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 kpeter@160: 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. kpeter@160: /// 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. kpeter@160: /// kpeter@160: /// \sa NoSubCounter alpar@119: typedef _SubCounter SubCounter; alpar@119: kpeter@160: /// SubCounter class without printing report on destruction kpeter@160: 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 kpeter@160: /// "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 NoSubCounter; alpar@119: kpeter@160: /// Constructor. alpar@119: Counter() : _title(), _os(std::cerr), count(0) {} kpeter@160: /// Constructor. alpar@119: 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 < SubCounter; alpar@121: typedef _NoSubCounter 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