[1847] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1847] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_COUNTER_H |
---|
| 20 | #define LEMON_COUNTER_H |
---|
| 21 | |
---|
| 22 | #include <string> |
---|
| 23 | #include <iostream> |
---|
| 24 | |
---|
| 25 | ///\ingroup timecount |
---|
| 26 | ///\file |
---|
| 27 | ///\brief Tools for counting steps and events |
---|
| 28 | |
---|
| 29 | namespace lemon |
---|
| 30 | { |
---|
| 31 | |
---|
| 32 | template<class P> class _SubNoCounter; |
---|
| 33 | |
---|
| 34 | template<class P> |
---|
| 35 | class _SubCounter |
---|
| 36 | { |
---|
| 37 | P &_parent; |
---|
| 38 | std::string _title; |
---|
| 39 | std::ostream &_os; |
---|
| 40 | int count; |
---|
| 41 | public: |
---|
| 42 | |
---|
| 43 | typedef _SubCounter<_SubCounter<P> > SubCounter; |
---|
| 44 | typedef _SubNoCounter<_SubCounter<P> > SubNoCounter; |
---|
| 45 | |
---|
| 46 | _SubCounter(P &parent) |
---|
| 47 | : _parent(parent), _title(), _os(std::cerr), count(0) {} |
---|
| 48 | _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr) |
---|
| 49 | : _parent(parent), _title(title), _os(os), count(0) {} |
---|
| 50 | _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr) |
---|
| 51 | : _parent(parent), _title(title), _os(os), count(0) {} |
---|
| 52 | ~_SubCounter() { |
---|
| 53 | _os << _title << count <<std::endl; |
---|
| 54 | _parent+=count; |
---|
| 55 | } |
---|
| 56 | _SubCounter &operator++() { count++; return *this;} |
---|
| 57 | int operator++(int) { return count++; } |
---|
| 58 | _SubCounter &operator--() { count--; return *this;} |
---|
| 59 | int operator--(int) { return count--; } |
---|
| 60 | _SubCounter &operator+=(int c) { count+=c; return *this;} |
---|
| 61 | _SubCounter &operator-=(int c) { count-=c; return *this;} |
---|
| 62 | void reset(int c=0) {count=c;} |
---|
| 63 | operator int() {return count;} |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | template<class P> |
---|
| 67 | class _SubNoCounter |
---|
| 68 | { |
---|
| 69 | P &_parent; |
---|
| 70 | public: |
---|
| 71 | typedef _SubNoCounter<_SubNoCounter<P> > SubCounter; |
---|
| 72 | typedef _SubNoCounter<_SubNoCounter<P> > SubNoCounter; |
---|
| 73 | |
---|
| 74 | _SubNoCounter(P &parent) :_parent(parent) {} |
---|
[1855] | 75 | _SubNoCounter(P &parent,std::string,std::ostream &) |
---|
[1847] | 76 | :_parent(parent) {} |
---|
[1855] | 77 | _SubNoCounter(P &parent,std::string) |
---|
| 78 | :_parent(parent) {} |
---|
| 79 | _SubNoCounter(P &parent,const char *,std::ostream &) |
---|
| 80 | :_parent(parent) {} |
---|
| 81 | _SubNoCounter(P &parent,const char *) |
---|
[1847] | 82 | :_parent(parent) {} |
---|
| 83 | ~_SubNoCounter() {} |
---|
| 84 | _SubNoCounter &operator++() { ++_parent; return *this;} |
---|
| 85 | int operator++(int) { _parent++; return 0;} |
---|
| 86 | _SubNoCounter &operator--() { --_parent; return *this;} |
---|
| 87 | int operator--(int) { _parent--; return 0;} |
---|
| 88 | _SubNoCounter &operator+=(int c) { _parent+=c; return *this;} |
---|
| 89 | _SubNoCounter &operator-=(int c) { _parent-=c; return *this;} |
---|
[1855] | 90 | void reset(int) {} |
---|
| 91 | void reset() {} |
---|
[1847] | 92 | operator int() {return 0;} |
---|
| 93 | }; |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | /// \addtogroup timecount |
---|
| 97 | /// @{ |
---|
| 98 | |
---|
| 99 | ///A counter class |
---|
| 100 | |
---|
| 101 | ///This class makes it easier to count certain events. You can increment |
---|
| 102 | ///or decrement the counter using operator++ and operator--. |
---|
| 103 | ///A report is automatically printed on destruction. |
---|
[1851] | 104 | ///\todo More doc |
---|
[1847] | 105 | class Counter |
---|
| 106 | { |
---|
| 107 | std::string _title; |
---|
| 108 | std::ostream &_os; |
---|
| 109 | int count; |
---|
| 110 | public: |
---|
| 111 | ///\e |
---|
| 112 | |
---|
| 113 | ///\todo document please. |
---|
| 114 | /// |
---|
| 115 | typedef _SubCounter<Counter> SubCounter; |
---|
| 116 | ///\e |
---|
| 117 | |
---|
| 118 | ///\todo document please. |
---|
| 119 | /// |
---|
| 120 | typedef _SubNoCounter<Counter> SubNoCounter; |
---|
| 121 | |
---|
| 122 | ///\e |
---|
| 123 | Counter() : _title(), _os(std::cerr), count(0) {} |
---|
| 124 | ///\e |
---|
| 125 | Counter(std::string title,std::ostream &os=std::cerr) |
---|
| 126 | : _title(title), _os(os), count(0) {} |
---|
| 127 | ///\e |
---|
| 128 | Counter(const char *title,std::ostream &os=std::cerr) |
---|
| 129 | : _title(title), _os(os), count(0) {} |
---|
| 130 | ///Destructor. Prints the given title and the value of the counter. |
---|
| 131 | ~Counter() { |
---|
| 132 | _os << _title << count <<std::endl; |
---|
| 133 | } |
---|
| 134 | ///\e |
---|
| 135 | Counter &operator++() { count++; return *this;} |
---|
| 136 | ///\e |
---|
| 137 | int operator++(int) { return count++;} |
---|
| 138 | ///\e |
---|
| 139 | Counter &operator--() { count--; return *this;} |
---|
| 140 | ///\e |
---|
| 141 | int operator--(int) { return count--;} |
---|
| 142 | ///\e |
---|
| 143 | Counter &operator+=(int c) { count+=c; return *this;} |
---|
| 144 | ///\e |
---|
| 145 | Counter &operator-=(int c) { count-=c; return *this;} |
---|
| 146 | ///\e |
---|
| 147 | void reset(int c=0) {count=c;} |
---|
| 148 | ///\e |
---|
| 149 | operator int() {return count;} |
---|
| 150 | }; |
---|
| 151 | |
---|
| 152 | ///'Do nothing' version of \ref Counter |
---|
| 153 | |
---|
| 154 | ///'Do nothing' version of \ref Counter. |
---|
| 155 | ///\sa Counter |
---|
| 156 | class NoCounter |
---|
| 157 | { |
---|
| 158 | public: |
---|
| 159 | typedef _SubNoCounter<NoCounter> SubCounter; |
---|
| 160 | typedef _SubNoCounter<NoCounter> SubNoCounter; |
---|
| 161 | |
---|
| 162 | NoCounter() {} |
---|
[1855] | 163 | NoCounter(std::string,std::ostream &) {} |
---|
| 164 | NoCounter(const char *,std::ostream &) {} |
---|
| 165 | NoCounter(std::string) {} |
---|
| 166 | NoCounter(const char *) {} |
---|
[1847] | 167 | NoCounter &operator++() { return *this; } |
---|
| 168 | int operator++(int) { return 0; } |
---|
| 169 | NoCounter &operator--() { return *this; } |
---|
| 170 | int operator--(int) { return 0; } |
---|
[1855] | 171 | NoCounter &operator+=(int) { return *this;} |
---|
| 172 | NoCounter &operator-=(int) { return *this;} |
---|
| 173 | void reset(int) {} |
---|
| 174 | void reset() {} |
---|
[1847] | 175 | operator int() {return 0;} |
---|
| 176 | }; |
---|
| 177 | |
---|
| 178 | ///@} |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | #endif |
---|