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