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