alpar@1847: /* -*- C++ -*- alpar@1847: * lemon/counter.h - alpar@1847: * Part of LEMON, a generic C++ optimization library alpar@1847: * alpar@1847: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1847: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@1847: * alpar@1847: * Permission to use, modify and distribute this software is granted alpar@1847: * provided that this copyright notice appears in all copies. For alpar@1847: * precise terms see the accompanying LICENSE file. alpar@1847: * alpar@1847: * This software is provided "AS IS" with no warranty of any kind, alpar@1847: * express or implied, and with no claim as to its suitability for any alpar@1847: * purpose. alpar@1847: * alpar@1847: */ alpar@1847: alpar@1847: #ifndef LEMON_COUNTER_H alpar@1847: #define LEMON_COUNTER_H alpar@1847: alpar@1847: #include alpar@1847: #include alpar@1847: alpar@1847: ///\ingroup timecount alpar@1847: ///\file alpar@1847: ///\brief Tools for counting steps and events alpar@1847: alpar@1847: namespace lemon alpar@1847: { alpar@1847: alpar@1847: template class _SubNoCounter; alpar@1847: alpar@1847: template alpar@1847: class _SubCounter alpar@1847: { alpar@1847: P &_parent; alpar@1847: std::string _title; alpar@1847: std::ostream &_os; alpar@1847: int count; alpar@1847: public: alpar@1847: alpar@1847: typedef _SubCounter<_SubCounter

> SubCounter; alpar@1847: typedef _SubNoCounter<_SubCounter

> SubNoCounter; alpar@1847: alpar@1847: _SubCounter(P &parent) alpar@1847: : _parent(parent), _title(), _os(std::cerr), count(0) {} alpar@1847: _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr) alpar@1847: : _parent(parent), _title(title), _os(os), count(0) {} alpar@1847: _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr) alpar@1847: : _parent(parent), _title(title), _os(os), count(0) {} alpar@1847: ~_SubCounter() { alpar@1847: _os << _title << count < alpar@1847: class _SubNoCounter alpar@1847: { alpar@1847: P &_parent; alpar@1847: public: alpar@1847: typedef _SubNoCounter<_SubNoCounter

> SubCounter; alpar@1847: typedef _SubNoCounter<_SubNoCounter

> SubNoCounter; alpar@1847: alpar@1847: _SubNoCounter(P &parent) :_parent(parent) {} alpar@1847: _SubNoCounter(P &parent,std::string title,std::ostream &os=std::cerr) alpar@1847: :_parent(parent) {} alpar@1847: _SubNoCounter(P &parent,const char *title,std::ostream &os=std::cerr) alpar@1847: :_parent(parent) {} alpar@1847: ~_SubNoCounter() {} alpar@1847: _SubNoCounter &operator++() { ++_parent; return *this;} alpar@1847: int operator++(int) { _parent++; return 0;} alpar@1847: _SubNoCounter &operator--() { --_parent; return *this;} alpar@1847: int operator--(int) { _parent--; return 0;} alpar@1847: _SubNoCounter &operator+=(int c) { _parent+=c; return *this;} alpar@1847: _SubNoCounter &operator-=(int c) { _parent-=c; return *this;} alpar@1847: void reset(int c=0) {} alpar@1847: operator int() {return 0;} alpar@1847: }; alpar@1847: alpar@1847: alpar@1847: /// \addtogroup timecount alpar@1847: /// @{ alpar@1847: alpar@1847: ///A counter class alpar@1847: alpar@1847: ///This class makes it easier to count certain events. You can increment alpar@1847: ///or decrement the counter using operator++ and operator--. alpar@1847: ///A report is automatically printed on destruction. alpar@1847: class Counter alpar@1847: { alpar@1847: std::string _title; alpar@1847: std::ostream &_os; alpar@1847: int count; alpar@1847: public: alpar@1847: ///\e alpar@1847: alpar@1847: ///\todo document please. alpar@1847: /// alpar@1847: typedef _SubCounter SubCounter; alpar@1847: ///\e alpar@1847: alpar@1847: ///\todo document please. alpar@1847: /// alpar@1847: typedef _SubNoCounter SubNoCounter; alpar@1847: alpar@1847: ///\e alpar@1847: Counter() : _title(), _os(std::cerr), count(0) {} alpar@1847: ///\e alpar@1847: Counter(std::string title,std::ostream &os=std::cerr) alpar@1847: : _title(title), _os(os), count(0) {} alpar@1847: ///\e alpar@1847: Counter(const char *title,std::ostream &os=std::cerr) alpar@1847: : _title(title), _os(os), count(0) {} alpar@1847: ///Destructor. Prints the given title and the value of the counter. alpar@1847: ~Counter() { alpar@1847: _os << _title << count < SubCounter; alpar@1847: typedef _SubNoCounter SubNoCounter; alpar@1847: alpar@1847: NoCounter() {} alpar@1847: NoCounter(std::string title,std::ostream &os=std::cerr) {} alpar@1847: NoCounter(const char *title,std::ostream &os=std::cerr) {} alpar@1847: NoCounter &operator++() { return *this; } alpar@1847: int operator++(int) { return 0; } alpar@1847: NoCounter &operator--() { return *this; } alpar@1847: int operator--(int) { return 0; } alpar@1847: NoCounter &operator+=(int c) { return *this;} alpar@1847: NoCounter &operator-=(int c) { return *this;} alpar@1847: void reset(int c=0) {} alpar@1847: operator int() {return 0;} alpar@1847: }; alpar@1847: alpar@1847: ///@} alpar@1847: } alpar@1847: alpar@1847: #endif