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