lemon/counter.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 24 Apr 2008 20:26:14 +0100
changeset 151 4d79daa40e9b
parent 119 82a2639a05bb
child 160 b1bd0c2a7f57
permissions -rw-r--r--
Turn on built in Doxygen STL support
alpar@119
     1
/* -*- C++ -*-
alpar@119
     2
 *
alpar@119
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@119
     4
 *
alpar@119
     5
 * Copyright (C) 2003-2008
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@119
    25
///\ingroup timecount
alpar@119
    26
///\file
alpar@119
    27
///\brief Tools for counting steps and events
alpar@119
    28
alpar@119
    29
namespace lemon 
alpar@119
    30
{
alpar@119
    31
alpar@121
    32
  template<class P> class _NoSubCounter;
alpar@119
    33
alpar@119
    34
  template<class P>
alpar@119
    35
  class _SubCounter 
alpar@119
    36
  {
alpar@119
    37
    P &_parent;
alpar@119
    38
    std::string _title;
alpar@119
    39
    std::ostream &_os;
alpar@119
    40
    int count;
alpar@119
    41
  public:
alpar@119
    42
alpar@119
    43
    typedef _SubCounter<_SubCounter<P> > SubCounter;
alpar@121
    44
    typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
alpar@119
    45
alpar@119
    46
    _SubCounter(P &parent)
alpar@119
    47
      : _parent(parent), _title(), _os(std::cerr), count(0) {}
alpar@119
    48
    _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
alpar@119
    49
      : _parent(parent), _title(title), _os(os), count(0) {}
alpar@119
    50
    _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
alpar@119
    51
      : _parent(parent), _title(title), _os(os), count(0) {}
alpar@119
    52
    ~_SubCounter() { 
alpar@119
    53
      _os << _title << count <<std::endl;
alpar@119
    54
      _parent+=count;
alpar@119
    55
    }
alpar@119
    56
    _SubCounter &operator++() { count++; return *this;}
alpar@119
    57
    int operator++(int) { return count++; }
alpar@119
    58
    _SubCounter &operator--() { count--; return *this;}
alpar@119
    59
    int operator--(int) { return count--; }
alpar@119
    60
    _SubCounter &operator+=(int c) { count+=c; return *this;}
alpar@119
    61
    _SubCounter &operator-=(int c) { count-=c; return *this;}
alpar@119
    62
    void reset(int c=0) {count=c;}
alpar@119
    63
    operator int() {return count;}
alpar@119
    64
  };
alpar@119
    65
alpar@119
    66
  template<class P>
alpar@121
    67
  class _NoSubCounter 
alpar@119
    68
  {
alpar@119
    69
    P &_parent;
alpar@119
    70
  public:
alpar@121
    71
    typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
alpar@121
    72
    typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
alpar@119
    73
  
alpar@121
    74
    _NoSubCounter(P &parent) :_parent(parent) {}
alpar@121
    75
    _NoSubCounter(P &parent,std::string,std::ostream &) 
alpar@119
    76
      :_parent(parent) {}
alpar@121
    77
    _NoSubCounter(P &parent,std::string) 
alpar@119
    78
      :_parent(parent) {}
alpar@121
    79
    _NoSubCounter(P &parent,const char *,std::ostream &)
alpar@119
    80
      :_parent(parent) {}
alpar@121
    81
    _NoSubCounter(P &parent,const char *)
alpar@119
    82
      :_parent(parent) {}
alpar@121
    83
    ~_NoSubCounter() {}
alpar@121
    84
    _NoSubCounter &operator++() { ++_parent; return *this;}
alpar@119
    85
    int operator++(int) { _parent++; return 0;}
alpar@121
    86
    _NoSubCounter &operator--() { --_parent; return *this;}
alpar@119
    87
    int operator--(int) { _parent--; return 0;}
alpar@121
    88
    _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
alpar@121
    89
    _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
alpar@119
    90
    void reset(int) {}
alpar@119
    91
    void reset() {}
alpar@119
    92
    operator int() {return 0;}
alpar@119
    93
  };
alpar@119
    94
alpar@119
    95
alpar@119
    96
  /// \addtogroup timecount
alpar@119
    97
  /// @{
alpar@119
    98
alpar@119
    99
  ///A counter class
alpar@119
   100
alpar@119
   101
  ///This class makes it easier to count certain events. You can increment
alpar@119
   102
  ///or decrement the counter using operator++ and operator--.
alpar@119
   103
  ///A report is automatically printed on destruction.
alpar@119
   104
  ///\todo More doc
alpar@119
   105
  class Counter 
alpar@119
   106
  {
alpar@119
   107
    std::string _title;
alpar@119
   108
    std::ostream &_os;
alpar@119
   109
    int count;
alpar@119
   110
  public:
alpar@119
   111
    ///\e
alpar@119
   112
alpar@119
   113
    ///\todo document please.
alpar@119
   114
    ///
alpar@119
   115
    typedef _SubCounter<Counter> SubCounter;
alpar@119
   116
    ///\e
alpar@119
   117
alpar@119
   118
    ///\todo document please.
alpar@119
   119
    ///
alpar@121
   120
    typedef _NoSubCounter<Counter> NoSubCounter;
alpar@119
   121
alpar@119
   122
    ///\e
alpar@119
   123
    Counter() : _title(), _os(std::cerr), count(0) {}
alpar@119
   124
    ///\e
alpar@119
   125
    Counter(std::string title,std::ostream &os=std::cerr) 
alpar@119
   126
      : _title(title), _os(os), count(0) {}
alpar@119
   127
    ///\e
alpar@119
   128
    Counter(const char *title,std::ostream &os=std::cerr)
alpar@119
   129
      : _title(title), _os(os), count(0) {}
alpar@119
   130
    ///Destructor. Prints the given title and the value of the counter.
alpar@119
   131
    ~Counter() {
alpar@119
   132
      _os << _title << count <<std::endl;
alpar@119
   133
    }
alpar@119
   134
    ///\e
alpar@119
   135
    Counter &operator++() { count++; return *this;}
alpar@119
   136
    ///\e
alpar@119
   137
    int operator++(int) { return count++;}
alpar@119
   138
    ///\e
alpar@119
   139
    Counter &operator--() { count--; return *this;}
alpar@119
   140
    ///\e
alpar@119
   141
    int operator--(int) { return count--;}
alpar@119
   142
    ///\e
alpar@119
   143
    Counter &operator+=(int c) { count+=c; return *this;}
alpar@119
   144
    ///\e
alpar@119
   145
    Counter &operator-=(int c) { count-=c; return *this;}
alpar@119
   146
    ///\e
alpar@119
   147
    void reset(int c=0) {count=c;}
alpar@119
   148
    ///\e
alpar@119
   149
    operator int() {return count;}
alpar@119
   150
  };
alpar@119
   151
alpar@119
   152
  ///'Do nothing' version of \ref Counter
alpar@119
   153
alpar@119
   154
  ///'Do nothing' version of \ref Counter.
alpar@119
   155
  ///\sa Counter
alpar@119
   156
  class NoCounter
alpar@119
   157
  {
alpar@119
   158
  public:
alpar@121
   159
    typedef _NoSubCounter<NoCounter> SubCounter;
alpar@121
   160
    typedef _NoSubCounter<NoCounter> NoSubCounter;
alpar@119
   161
alpar@119
   162
    NoCounter() {}
alpar@119
   163
    NoCounter(std::string,std::ostream &) {}
alpar@119
   164
    NoCounter(const char *,std::ostream &) {}
alpar@119
   165
    NoCounter(std::string) {}
alpar@119
   166
    NoCounter(const char *) {}
alpar@119
   167
    NoCounter &operator++() { return *this; }
alpar@119
   168
    int operator++(int) { return 0; }
alpar@119
   169
    NoCounter &operator--() { return *this; }
alpar@119
   170
    int operator--(int) { return 0; }
alpar@119
   171
    NoCounter &operator+=(int) { return *this;}
alpar@119
   172
    NoCounter &operator-=(int) { return *this;}
alpar@119
   173
    void reset(int) {}
alpar@119
   174
    void reset() {}
alpar@119
   175
    operator int() {return 0;}
alpar@119
   176
  };
alpar@119
   177
alpar@119
   178
  ///@}
alpar@119
   179
}
alpar@119
   180
alpar@119
   181
#endif