COIN-OR::LEMON - Graph Library

source: lemon/lemon/counter.h

Last change on this file was 1327:18c89646185e, checked in by Alpar Juttner <alpar@…>, 5 years ago

Suppress MSVC warning C4267 (#519)

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

File size: 7.9 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[119]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[119]4 *
[463]5 * Copyright (C) 2003-2009
[119]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
[1327]25#include <lemon/core.h>
26
[119]27///\ingroup timecount
28///\file
29///\brief Tools for counting steps and events
30
[209]31namespace lemon
[119]32{
33
[121]34  template<class P> class _NoSubCounter;
[119]35
36  template<class P>
[209]37  class _SubCounter
[119]38  {
39    P &_parent;
40    std::string _title;
41    std::ostream &_os;
42    int count;
43  public:
44
45    typedef _SubCounter<_SubCounter<P> > SubCounter;
[121]46    typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
[119]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) {}
[209]54    ~_SubCounter() {
[119]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>
[209]68  class _NoSubCounter
[119]69  {
70    P &_parent;
71  public:
[121]72    typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
73    typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
[209]74
[121]75    _NoSubCounter(P &parent) :_parent(parent) {}
[209]76    _NoSubCounter(P &parent,std::string,std::ostream &)
[119]77      :_parent(parent) {}
[209]78    _NoSubCounter(P &parent,std::string)
[119]79      :_parent(parent) {}
[121]80    _NoSubCounter(P &parent,const char *,std::ostream &)
[119]81      :_parent(parent) {}
[121]82    _NoSubCounter(P &parent,const char *)
[119]83      :_parent(parent) {}
[121]84    ~_NoSubCounter() {}
85    _NoSubCounter &operator++() { ++_parent; return *this;}
[119]86    int operator++(int) { _parent++; return 0;}
[121]87    _NoSubCounter &operator--() { --_parent; return *this;}
[119]88    int operator--(int) { _parent--; return 0;}
[121]89    _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
90    _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
[119]91    operator int() {return 0;}
92  };
93
94
95  /// \addtogroup timecount
96  /// @{
97
[160]98  /// A counter class
[119]99
[160]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
[209]107  /// is automatically printed on destruction.
[160]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
[209]138  class Counter
[119]139  {
140    std::string _title;
141    std::ostream &_os;
142    int count;
143  public:
144
[160]145    /// SubCounter class
[209]146
[160]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.
[209]152    ///
[160]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.
[119]156    ///
[160]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.
[209]161    ///
[160]162    /// \sa NoSubCounter
[119]163    typedef _SubCounter<Counter> SubCounter;
164
[209]165    /// SubCounter class without printing report on destruction
166
[160]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'.)
[119]171    ///
[160]172    /// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter
[209]173    /// "NoSubCounter"s makes it possible to turn off reporting
[160]174    /// subcounter values without actually removing the definitions
175    /// and the increment or decrement operators.
176    ///
177    /// \sa SubCounter
[121]178    typedef _NoSubCounter<Counter> NoSubCounter;
[119]179
[160]180    /// Constructor.
[119]181    Counter() : _title(), _os(std::cerr), count(0) {}
[160]182    /// Constructor.
[209]183    Counter(std::string title,std::ostream &os=std::cerr)
[119]184      : _title(title), _os(os), count(0) {}
[160]185    /// Constructor.
[119]186    Counter(const char *title,std::ostream &os=std::cerr)
187      : _title(title), _os(os), count(0) {}
[160]188    /// Destructor. Prints the given title and the value of the counter.
[119]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;}
[160]204    /// Resets the counter to the given value.
[168]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]209    /// "NoSubCounter"s along with the main counter.
[119]210    void reset(int c=0) {count=c;}
[160]211    /// Returns the value of the counter.
[119]212    operator int() {return count;}
213  };
214
[160]215  /// 'Do nothing' version of Counter.
[119]216
[833]217  /// This class can be used in the same way as \ref Counter, but it
[160]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
[119]226  class NoCounter
227  {
228  public:
[121]229    typedef _NoSubCounter<NoCounter> SubCounter;
230    typedef _NoSubCounter<NoCounter> NoSubCounter;
[119]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.