COIN-OR::LEMON - Graph Library

source: lemon/lemon/counter.h @ 1337:4add05447ca0

Last change on this file since 1337:4add05447ca0 was 1327:18c89646185e, checked in by Alpar Juttner <alpar@…>, 9 years ago

Suppress MSVC warning C4267 (#519)

C4267: conversion from 'size_t' to 'type', possible loss of data

File size: 7.9 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
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#include <lemon/core.h>
26
27///\ingroup timecount
28///\file
29///\brief Tools for counting steps and events
30
31namespace lemon
32{
33
34  template<class P> class _NoSubCounter;
35
36  template<class P>
37  class _SubCounter
38  {
39    P &_parent;
40    std::string _title;
41    std::ostream &_os;
42    int count;
43  public:
44
45    typedef _SubCounter<_SubCounter<P> > SubCounter;
46    typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
47
48    _SubCounter(P &parent)
49      : _parent(parent), _title(), _os(std::cerr), count(0) {}
50    _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
51      : _parent(parent), _title(title), _os(os), count(0) {}
52    _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
53      : _parent(parent), _title(title), _os(os), count(0) {}
54    ~_SubCounter() {
55      _os << _title << count <<std::endl;
56      _parent+=count;
57    }
58    _SubCounter &operator++() { count++; return *this;}
59    int operator++(int) { return count++; }
60    _SubCounter &operator--() { count--; return *this;}
61    int operator--(int) { return count--; }
62    _SubCounter &operator+=(int c) { count+=c; return *this;}
63    _SubCounter &operator-=(int c) { count-=c; return *this;}
64    operator int() {return count;}
65  };
66
67  template<class P>
68  class _NoSubCounter
69  {
70    P &_parent;
71  public:
72    typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
73    typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
74
75    _NoSubCounter(P &parent) :_parent(parent) {}
76    _NoSubCounter(P &parent,std::string,std::ostream &)
77      :_parent(parent) {}
78    _NoSubCounter(P &parent,std::string)
79      :_parent(parent) {}
80    _NoSubCounter(P &parent,const char *,std::ostream &)
81      :_parent(parent) {}
82    _NoSubCounter(P &parent,const char *)
83      :_parent(parent) {}
84    ~_NoSubCounter() {}
85    _NoSubCounter &operator++() { ++_parent; return *this;}
86    int operator++(int) { _parent++; return 0;}
87    _NoSubCounter &operator--() { --_parent; return *this;}
88    int operator--(int) { _parent--; return 0;}
89    _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
90    _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
91    operator int() {return 0;}
92  };
93
94
95  /// \addtogroup timecount
96  /// @{
97
98  /// A counter class
99
100  /// This class makes it easier to count certain events (e.g. for debug
101  /// reasons).
102  /// You can increment or decrement the counter using \c operator++,
103  /// \c operator--, \c operator+= and \c operator-=. You can also
104  /// define subcounters for the different phases of the algorithm or
105  /// for different types of operations.
106  /// A report containing the given title and the value of the counter
107  /// is automatically printed on destruction.
108  ///
109  /// The following example shows the usage of counters and subcounters.
110  /// \code
111  /// // Bubble sort
112  /// std::vector<T> v;
113  /// ...
114  /// Counter op("Operations: ");
115  /// Counter::SubCounter as(op, "Assignments: ");
116  /// Counter::SubCounter co(op, "Comparisons: ");
117  /// for (int i = v.size()-1; i > 0; --i) {
118  ///   for (int j = 0; j < i; ++j) {
119  ///     if (v[j] > v[j+1]) {
120  ///       T tmp = v[j];
121  ///       v[j] = v[j+1];
122  ///       v[j+1] = tmp;
123  ///       as += 3;          // three assignments
124  ///     }
125  ///     ++co;               // one comparison
126  ///   }
127  /// }
128  /// \endcode
129  ///
130  /// This code prints out something like that:
131  /// \code
132  /// Comparisons: 45
133  /// Assignments: 57
134  /// Operations: 102
135  /// \endcode
136  ///
137  /// \sa NoCounter
138  class Counter
139  {
140    std::string _title;
141    std::ostream &_os;
142    int count;
143  public:
144
145    /// SubCounter class
146
147    /// This class can be used to setup subcounters for a \ref Counter
148    /// to have finer reports. A subcounter provides exactly the same
149    /// operations as the main \ref Counter, but it also increments and
150    /// decrements the value of its parent.
151    /// Subcounters can also have subcounters.
152    ///
153    /// The parent counter must be given as the first parameter of the
154    /// constructor. Apart from that a title and an \c ostream object
155    /// can also be given just like for the main \ref Counter.
156    ///
157    /// A report containing the given title and the value of the
158    /// subcounter is automatically printed on destruction. If you
159    /// would like to turn off this report, use \ref NoSubCounter
160    /// instead.
161    ///
162    /// \sa NoSubCounter
163    typedef _SubCounter<Counter> SubCounter;
164
165    /// SubCounter class without printing report on destruction
166
167    /// This class can be used to setup subcounters for a \ref Counter.
168    /// It is the same as \ref SubCounter but it does not print report
169    /// on destruction. (It modifies the value of its parent, so 'No'
170    /// only means 'do not print'.)
171    ///
172    /// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter
173    /// "NoSubCounter"s makes it possible to turn off reporting
174    /// subcounter values without actually removing the definitions
175    /// and the increment or decrement operators.
176    ///
177    /// \sa SubCounter
178    typedef _NoSubCounter<Counter> NoSubCounter;
179
180    /// Constructor.
181    Counter() : _title(), _os(std::cerr), count(0) {}
182    /// Constructor.
183    Counter(std::string title,std::ostream &os=std::cerr)
184      : _title(title), _os(os), count(0) {}
185    /// Constructor.
186    Counter(const char *title,std::ostream &os=std::cerr)
187      : _title(title), _os(os), count(0) {}
188    /// Destructor. Prints the given title and the value of the counter.
189    ~Counter() {
190      _os << _title << count <<std::endl;
191    }
192    ///\e
193    Counter &operator++() { count++; return *this;}
194    ///\e
195    int operator++(int) { return count++;}
196    ///\e
197    Counter &operator--() { count--; return *this;}
198    ///\e
199    int operator--(int) { return count--;}
200    ///\e
201    Counter &operator+=(int c) { count+=c; return *this;}
202    ///\e
203    Counter &operator-=(int c) { count-=c; return *this;}
204    /// Resets the counter to the given value.
205
206    /// Resets the counter to the given value.
207    /// \note This function does not reset the values of
208    /// \ref SubCounter "SubCounter"s but it resets \ref NoSubCounter
209    /// "NoSubCounter"s along with the main counter.
210    void reset(int c=0) {count=c;}
211    /// Returns the value of the counter.
212    operator int() {return count;}
213  };
214
215  /// 'Do nothing' version of Counter.
216
217  /// This class can be used in the same way as \ref Counter, but it
218  /// does not count at all and does not print report on destruction.
219  ///
220  /// Replacing a \ref Counter with a \ref NoCounter makes it possible
221  /// to turn off all counting and reporting (SubCounters should also
222  /// be replaced with NoSubCounters), so it does not affect the
223  /// efficiency of the program at all.
224  ///
225  /// \sa Counter
226  class NoCounter
227  {
228  public:
229    typedef _NoSubCounter<NoCounter> SubCounter;
230    typedef _NoSubCounter<NoCounter> NoSubCounter;
231
232    NoCounter() {}
233    NoCounter(std::string,std::ostream &) {}
234    NoCounter(const char *,std::ostream &) {}
235    NoCounter(std::string) {}
236    NoCounter(const char *) {}
237    NoCounter &operator++() { return *this; }
238    int operator++(int) { return 0; }
239    NoCounter &operator--() { return *this; }
240    int operator--(int) { return 0; }
241    NoCounter &operator+=(int) { return *this;}
242    NoCounter &operator-=(int) { return *this;}
243    void reset(int) {}
244    void reset() {}
245    operator int() {return 0;}
246  };
247
248  ///@}
249}
250
251#endif
Note: See TracBrowser for help on using the repository browser.