lemon/counter.h
changeset 124 ae7785fe8431
parent 119 82a2639a05bb
child 160 b1bd0c2a7f57
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/counter.h	Sat Apr 12 20:38:51 2008 +0100
     1.3 @@ -0,0 +1,181 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_COUNTER_H
    1.23 +#define LEMON_COUNTER_H
    1.24 +
    1.25 +#include <string>
    1.26 +#include <iostream>
    1.27 +
    1.28 +///\ingroup timecount
    1.29 +///\file
    1.30 +///\brief Tools for counting steps and events
    1.31 +
    1.32 +namespace lemon 
    1.33 +{
    1.34 +
    1.35 +  template<class P> class _NoSubCounter;
    1.36 +
    1.37 +  template<class P>
    1.38 +  class _SubCounter 
    1.39 +  {
    1.40 +    P &_parent;
    1.41 +    std::string _title;
    1.42 +    std::ostream &_os;
    1.43 +    int count;
    1.44 +  public:
    1.45 +
    1.46 +    typedef _SubCounter<_SubCounter<P> > SubCounter;
    1.47 +    typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
    1.48 +
    1.49 +    _SubCounter(P &parent)
    1.50 +      : _parent(parent), _title(), _os(std::cerr), count(0) {}
    1.51 +    _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
    1.52 +      : _parent(parent), _title(title), _os(os), count(0) {}
    1.53 +    _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
    1.54 +      : _parent(parent), _title(title), _os(os), count(0) {}
    1.55 +    ~_SubCounter() { 
    1.56 +      _os << _title << count <<std::endl;
    1.57 +      _parent+=count;
    1.58 +    }
    1.59 +    _SubCounter &operator++() { count++; return *this;}
    1.60 +    int operator++(int) { return count++; }
    1.61 +    _SubCounter &operator--() { count--; return *this;}
    1.62 +    int operator--(int) { return count--; }
    1.63 +    _SubCounter &operator+=(int c) { count+=c; return *this;}
    1.64 +    _SubCounter &operator-=(int c) { count-=c; return *this;}
    1.65 +    void reset(int c=0) {count=c;}
    1.66 +    operator int() {return count;}
    1.67 +  };
    1.68 +
    1.69 +  template<class P>
    1.70 +  class _NoSubCounter 
    1.71 +  {
    1.72 +    P &_parent;
    1.73 +  public:
    1.74 +    typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
    1.75 +    typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
    1.76 +  
    1.77 +    _NoSubCounter(P &parent) :_parent(parent) {}
    1.78 +    _NoSubCounter(P &parent,std::string,std::ostream &) 
    1.79 +      :_parent(parent) {}
    1.80 +    _NoSubCounter(P &parent,std::string) 
    1.81 +      :_parent(parent) {}
    1.82 +    _NoSubCounter(P &parent,const char *,std::ostream &)
    1.83 +      :_parent(parent) {}
    1.84 +    _NoSubCounter(P &parent,const char *)
    1.85 +      :_parent(parent) {}
    1.86 +    ~_NoSubCounter() {}
    1.87 +    _NoSubCounter &operator++() { ++_parent; return *this;}
    1.88 +    int operator++(int) { _parent++; return 0;}
    1.89 +    _NoSubCounter &operator--() { --_parent; return *this;}
    1.90 +    int operator--(int) { _parent--; return 0;}
    1.91 +    _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
    1.92 +    _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
    1.93 +    void reset(int) {}
    1.94 +    void reset() {}
    1.95 +    operator int() {return 0;}
    1.96 +  };
    1.97 +
    1.98 +
    1.99 +  /// \addtogroup timecount
   1.100 +  /// @{
   1.101 +
   1.102 +  ///A counter class
   1.103 +
   1.104 +  ///This class makes it easier to count certain events. You can increment
   1.105 +  ///or decrement the counter using operator++ and operator--.
   1.106 +  ///A report is automatically printed on destruction.
   1.107 +  ///\todo More doc
   1.108 +  class Counter 
   1.109 +  {
   1.110 +    std::string _title;
   1.111 +    std::ostream &_os;
   1.112 +    int count;
   1.113 +  public:
   1.114 +    ///\e
   1.115 +
   1.116 +    ///\todo document please.
   1.117 +    ///
   1.118 +    typedef _SubCounter<Counter> SubCounter;
   1.119 +    ///\e
   1.120 +
   1.121 +    ///\todo document please.
   1.122 +    ///
   1.123 +    typedef _NoSubCounter<Counter> NoSubCounter;
   1.124 +
   1.125 +    ///\e
   1.126 +    Counter() : _title(), _os(std::cerr), count(0) {}
   1.127 +    ///\e
   1.128 +    Counter(std::string title,std::ostream &os=std::cerr) 
   1.129 +      : _title(title), _os(os), count(0) {}
   1.130 +    ///\e
   1.131 +    Counter(const char *title,std::ostream &os=std::cerr)
   1.132 +      : _title(title), _os(os), count(0) {}
   1.133 +    ///Destructor. Prints the given title and the value of the counter.
   1.134 +    ~Counter() {
   1.135 +      _os << _title << count <<std::endl;
   1.136 +    }
   1.137 +    ///\e
   1.138 +    Counter &operator++() { count++; return *this;}
   1.139 +    ///\e
   1.140 +    int operator++(int) { return count++;}
   1.141 +    ///\e
   1.142 +    Counter &operator--() { count--; return *this;}
   1.143 +    ///\e
   1.144 +    int operator--(int) { return count--;}
   1.145 +    ///\e
   1.146 +    Counter &operator+=(int c) { count+=c; return *this;}
   1.147 +    ///\e
   1.148 +    Counter &operator-=(int c) { count-=c; return *this;}
   1.149 +    ///\e
   1.150 +    void reset(int c=0) {count=c;}
   1.151 +    ///\e
   1.152 +    operator int() {return count;}
   1.153 +  };
   1.154 +
   1.155 +  ///'Do nothing' version of \ref Counter
   1.156 +
   1.157 +  ///'Do nothing' version of \ref Counter.
   1.158 +  ///\sa Counter
   1.159 +  class NoCounter
   1.160 +  {
   1.161 +  public:
   1.162 +    typedef _NoSubCounter<NoCounter> SubCounter;
   1.163 +    typedef _NoSubCounter<NoCounter> NoSubCounter;
   1.164 +
   1.165 +    NoCounter() {}
   1.166 +    NoCounter(std::string,std::ostream &) {}
   1.167 +    NoCounter(const char *,std::ostream &) {}
   1.168 +    NoCounter(std::string) {}
   1.169 +    NoCounter(const char *) {}
   1.170 +    NoCounter &operator++() { return *this; }
   1.171 +    int operator++(int) { return 0; }
   1.172 +    NoCounter &operator--() { return *this; }
   1.173 +    int operator--(int) { return 0; }
   1.174 +    NoCounter &operator+=(int) { return *this;}
   1.175 +    NoCounter &operator-=(int) { return *this;}
   1.176 +    void reset(int) {}
   1.177 +    void reset() {}
   1.178 +    operator int() {return 0;}
   1.179 +  };
   1.180 +
   1.181 +  ///@}
   1.182 +}
   1.183 +
   1.184 +#endif