1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/counter.h Mon Dec 05 17:03:31 2005 +0000
1.3 @@ -0,0 +1,171 @@
1.4 +/* -*- C++ -*-
1.5 + * lemon/counter.h -
1.6 + * Part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.9 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.10 + *
1.11 + * Permission to use, modify and distribute this software is granted
1.12 + * provided that this copyright notice appears in all copies. For
1.13 + * precise terms see the accompanying LICENSE file.
1.14 + *
1.15 + * This software is provided "AS IS" with no warranty of any kind,
1.16 + * express or implied, and with no claim as to its suitability for any
1.17 + * purpose.
1.18 + *
1.19 + */
1.20 +
1.21 +#ifndef LEMON_COUNTER_H
1.22 +#define LEMON_COUNTER_H
1.23 +
1.24 +#include <string>
1.25 +#include <iostream>
1.26 +
1.27 +///\ingroup timecount
1.28 +///\file
1.29 +///\brief Tools for counting steps and events
1.30 +
1.31 +namespace lemon
1.32 +{
1.33 +
1.34 + template<class P> class _SubNoCounter;
1.35 +
1.36 + template<class P>
1.37 + class _SubCounter
1.38 + {
1.39 + P &_parent;
1.40 + std::string _title;
1.41 + std::ostream &_os;
1.42 + int count;
1.43 + public:
1.44 +
1.45 + typedef _SubCounter<_SubCounter<P> > SubCounter;
1.46 + typedef _SubNoCounter<_SubCounter<P> > SubNoCounter;
1.47 +
1.48 + _SubCounter(P &parent)
1.49 + : _parent(parent), _title(), _os(std::cerr), count(0) {}
1.50 + _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
1.51 + : _parent(parent), _title(title), _os(os), count(0) {}
1.52 + _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
1.53 + : _parent(parent), _title(title), _os(os), count(0) {}
1.54 + ~_SubCounter() {
1.55 + _os << _title << count <<std::endl;
1.56 + _parent+=count;
1.57 + }
1.58 + _SubCounter &operator++() { count++; return *this;}
1.59 + int operator++(int) { return count++; }
1.60 + _SubCounter &operator--() { count--; return *this;}
1.61 + int operator--(int) { return count--; }
1.62 + _SubCounter &operator+=(int c) { count+=c; return *this;}
1.63 + _SubCounter &operator-=(int c) { count-=c; return *this;}
1.64 + void reset(int c=0) {count=c;}
1.65 + operator int() {return count;}
1.66 + };
1.67 +
1.68 + template<class P>
1.69 + class _SubNoCounter
1.70 + {
1.71 + P &_parent;
1.72 + public:
1.73 + typedef _SubNoCounter<_SubNoCounter<P> > SubCounter;
1.74 + typedef _SubNoCounter<_SubNoCounter<P> > SubNoCounter;
1.75 +
1.76 + _SubNoCounter(P &parent) :_parent(parent) {}
1.77 + _SubNoCounter(P &parent,std::string title,std::ostream &os=std::cerr)
1.78 + :_parent(parent) {}
1.79 + _SubNoCounter(P &parent,const char *title,std::ostream &os=std::cerr)
1.80 + :_parent(parent) {}
1.81 + ~_SubNoCounter() {}
1.82 + _SubNoCounter &operator++() { ++_parent; return *this;}
1.83 + int operator++(int) { _parent++; return 0;}
1.84 + _SubNoCounter &operator--() { --_parent; return *this;}
1.85 + int operator--(int) { _parent--; return 0;}
1.86 + _SubNoCounter &operator+=(int c) { _parent+=c; return *this;}
1.87 + _SubNoCounter &operator-=(int c) { _parent-=c; return *this;}
1.88 + void reset(int c=0) {}
1.89 + operator int() {return 0;}
1.90 + };
1.91 +
1.92 +
1.93 + /// \addtogroup timecount
1.94 + /// @{
1.95 +
1.96 + ///A counter class
1.97 +
1.98 + ///This class makes it easier to count certain events. You can increment
1.99 + ///or decrement the counter using operator++ and operator--.
1.100 + ///A report is automatically printed on destruction.
1.101 + class Counter
1.102 + {
1.103 + std::string _title;
1.104 + std::ostream &_os;
1.105 + int count;
1.106 + public:
1.107 + ///\e
1.108 +
1.109 + ///\todo document please.
1.110 + ///
1.111 + typedef _SubCounter<Counter> SubCounter;
1.112 + ///\e
1.113 +
1.114 + ///\todo document please.
1.115 + ///
1.116 + typedef _SubNoCounter<Counter> SubNoCounter;
1.117 +
1.118 + ///\e
1.119 + Counter() : _title(), _os(std::cerr), count(0) {}
1.120 + ///\e
1.121 + Counter(std::string title,std::ostream &os=std::cerr)
1.122 + : _title(title), _os(os), count(0) {}
1.123 + ///\e
1.124 + Counter(const char *title,std::ostream &os=std::cerr)
1.125 + : _title(title), _os(os), count(0) {}
1.126 + ///Destructor. Prints the given title and the value of the counter.
1.127 + ~Counter() {
1.128 + _os << _title << count <<std::endl;
1.129 + }
1.130 + ///\e
1.131 + Counter &operator++() { count++; return *this;}
1.132 + ///\e
1.133 + int operator++(int) { return count++;}
1.134 + ///\e
1.135 + Counter &operator--() { count--; return *this;}
1.136 + ///\e
1.137 + int operator--(int) { return count--;}
1.138 + ///\e
1.139 + Counter &operator+=(int c) { count+=c; return *this;}
1.140 + ///\e
1.141 + Counter &operator-=(int c) { count-=c; return *this;}
1.142 + ///\e
1.143 + void reset(int c=0) {count=c;}
1.144 + ///\e
1.145 + operator int() {return count;}
1.146 + };
1.147 +
1.148 + ///'Do nothing' version of \ref Counter
1.149 +
1.150 + ///'Do nothing' version of \ref Counter.
1.151 + ///\sa Counter
1.152 + class NoCounter
1.153 + {
1.154 + public:
1.155 + typedef _SubNoCounter<NoCounter> SubCounter;
1.156 + typedef _SubNoCounter<NoCounter> SubNoCounter;
1.157 +
1.158 + NoCounter() {}
1.159 + NoCounter(std::string title,std::ostream &os=std::cerr) {}
1.160 + NoCounter(const char *title,std::ostream &os=std::cerr) {}
1.161 + NoCounter &operator++() { return *this; }
1.162 + int operator++(int) { return 0; }
1.163 + NoCounter &operator--() { return *this; }
1.164 + int operator--(int) { return 0; }
1.165 + NoCounter &operator+=(int c) { return *this;}
1.166 + NoCounter &operator-=(int c) { return *this;}
1.167 + void reset(int c=0) {}
1.168 + operator int() {return 0;}
1.169 + };
1.170 +
1.171 + ///@}
1.172 +}
1.173 +
1.174 +#endif