COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/counter.h @ 1847:7cbc12e42482

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