lemon/counter.h
changeset 479 55111a054b2e
parent 168 8ceb318224b1
child 440 88ed40ad0d4f
equal deleted inserted replaced
3:6dfd509bede3 4:470266d21278
     1 /* -*- C++ -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2008
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
    24 
    24 
    25 ///\ingroup timecount
    25 ///\ingroup timecount
    26 ///\file
    26 ///\file
    27 ///\brief Tools for counting steps and events
    27 ///\brief Tools for counting steps and events
    28 
    28 
    29 namespace lemon 
    29 namespace lemon
    30 {
    30 {
    31 
    31 
    32   template<class P> class _NoSubCounter;
    32   template<class P> class _NoSubCounter;
    33 
    33 
    34   template<class P>
    34   template<class P>
    35   class _SubCounter 
    35   class _SubCounter
    36   {
    36   {
    37     P &_parent;
    37     P &_parent;
    38     std::string _title;
    38     std::string _title;
    39     std::ostream &_os;
    39     std::ostream &_os;
    40     int count;
    40     int count;
    47       : _parent(parent), _title(), _os(std::cerr), count(0) {}
    47       : _parent(parent), _title(), _os(std::cerr), count(0) {}
    48     _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
    48     _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
    49       : _parent(parent), _title(title), _os(os), count(0) {}
    49       : _parent(parent), _title(title), _os(os), count(0) {}
    50     _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
    50     _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
    51       : _parent(parent), _title(title), _os(os), count(0) {}
    51       : _parent(parent), _title(title), _os(os), count(0) {}
    52     ~_SubCounter() { 
    52     ~_SubCounter() {
    53       _os << _title << count <<std::endl;
    53       _os << _title << count <<std::endl;
    54       _parent+=count;
    54       _parent+=count;
    55     }
    55     }
    56     _SubCounter &operator++() { count++; return *this;}
    56     _SubCounter &operator++() { count++; return *this;}
    57     int operator++(int) { return count++; }
    57     int operator++(int) { return count++; }
    61     _SubCounter &operator-=(int c) { count-=c; return *this;}
    61     _SubCounter &operator-=(int c) { count-=c; return *this;}
    62     operator int() {return count;}
    62     operator int() {return count;}
    63   };
    63   };
    64 
    64 
    65   template<class P>
    65   template<class P>
    66   class _NoSubCounter 
    66   class _NoSubCounter
    67   {
    67   {
    68     P &_parent;
    68     P &_parent;
    69   public:
    69   public:
    70     typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
    70     typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
    71     typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
    71     typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
    72   
    72 
    73     _NoSubCounter(P &parent) :_parent(parent) {}
    73     _NoSubCounter(P &parent) :_parent(parent) {}
    74     _NoSubCounter(P &parent,std::string,std::ostream &) 
    74     _NoSubCounter(P &parent,std::string,std::ostream &)
    75       :_parent(parent) {}
    75       :_parent(parent) {}
    76     _NoSubCounter(P &parent,std::string) 
    76     _NoSubCounter(P &parent,std::string)
    77       :_parent(parent) {}
    77       :_parent(parent) {}
    78     _NoSubCounter(P &parent,const char *,std::ostream &)
    78     _NoSubCounter(P &parent,const char *,std::ostream &)
    79       :_parent(parent) {}
    79       :_parent(parent) {}
    80     _NoSubCounter(P &parent,const char *)
    80     _NoSubCounter(P &parent,const char *)
    81       :_parent(parent) {}
    81       :_parent(parent) {}
   100   /// You can increment or decrement the counter using \c operator++,
   100   /// You can increment or decrement the counter using \c operator++,
   101   /// \c operator--, \c operator+= and \c operator-=. You can also
   101   /// \c operator--, \c operator+= and \c operator-=. You can also
   102   /// define subcounters for the different phases of the algorithm or
   102   /// define subcounters for the different phases of the algorithm or
   103   /// for different types of operations.
   103   /// for different types of operations.
   104   /// A report containing the given title and the value of the counter
   104   /// A report containing the given title and the value of the counter
   105   /// is automatically printed on destruction. 
   105   /// is automatically printed on destruction.
   106   ///
   106   ///
   107   /// The following example shows the usage of counters and subcounters.
   107   /// The following example shows the usage of counters and subcounters.
   108   /// \code
   108   /// \code
   109   /// // Bubble sort
   109   /// // Bubble sort
   110   /// std::vector<T> v;
   110   /// std::vector<T> v;
   131   /// Assignments: 57
   131   /// Assignments: 57
   132   /// Operations: 102
   132   /// Operations: 102
   133   /// \endcode
   133   /// \endcode
   134   ///
   134   ///
   135   /// \sa NoCounter
   135   /// \sa NoCounter
   136   class Counter 
   136   class Counter
   137   {
   137   {
   138     std::string _title;
   138     std::string _title;
   139     std::ostream &_os;
   139     std::ostream &_os;
   140     int count;
   140     int count;
   141   public:
   141   public:
   142 
   142 
   143     /// SubCounter class
   143     /// SubCounter class
   144     
   144 
   145     /// This class can be used to setup subcounters for a \ref Counter
   145     /// This class can be used to setup subcounters for a \ref Counter
   146     /// to have finer reports. A subcounter provides exactly the same
   146     /// to have finer reports. A subcounter provides exactly the same
   147     /// operations as the main \ref Counter, but it also increments and
   147     /// operations as the main \ref Counter, but it also increments and
   148     /// decrements the value of its parent.
   148     /// decrements the value of its parent.
   149     /// Subcounters can also have subcounters.
   149     /// Subcounters can also have subcounters.
   150     /// 
   150     ///
   151     /// The parent counter must be given as the first parameter of the
   151     /// The parent counter must be given as the first parameter of the
   152     /// constructor. Apart from that a title and an \c ostream object
   152     /// constructor. Apart from that a title and an \c ostream object
   153     /// can also be given just like for the main \ref Counter.
   153     /// can also be given just like for the main \ref Counter.
   154     ///
   154     ///
   155     /// A report containing the given title and the value of the
   155     /// A report containing the given title and the value of the
   156     /// subcounter is automatically printed on destruction. If you
   156     /// subcounter is automatically printed on destruction. If you
   157     /// would like to turn off this report, use \ref NoSubCounter
   157     /// would like to turn off this report, use \ref NoSubCounter
   158     /// instead.
   158     /// instead.
   159     /// 
   159     ///
   160     /// \sa NoSubCounter
   160     /// \sa NoSubCounter
   161     typedef _SubCounter<Counter> SubCounter;
   161     typedef _SubCounter<Counter> SubCounter;
   162 
   162 
   163     /// SubCounter class without printing report on destruction 
   163     /// SubCounter class without printing report on destruction
   164     
   164 
   165     /// This class can be used to setup subcounters for a \ref Counter.
   165     /// This class can be used to setup subcounters for a \ref Counter.
   166     /// It is the same as \ref SubCounter but it does not print report
   166     /// It is the same as \ref SubCounter but it does not print report
   167     /// on destruction. (It modifies the value of its parent, so 'No'
   167     /// on destruction. (It modifies the value of its parent, so 'No'
   168     /// only means 'do not print'.)
   168     /// only means 'do not print'.)
   169     ///
   169     ///
   170     /// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter
   170     /// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter
   171     /// "NoSubCounter"s makes it possible to turn off reporting 
   171     /// "NoSubCounter"s makes it possible to turn off reporting
   172     /// subcounter values without actually removing the definitions
   172     /// subcounter values without actually removing the definitions
   173     /// and the increment or decrement operators.
   173     /// and the increment or decrement operators.
   174     ///
   174     ///
   175     /// \sa SubCounter
   175     /// \sa SubCounter
   176     typedef _NoSubCounter<Counter> NoSubCounter;
   176     typedef _NoSubCounter<Counter> NoSubCounter;
   177 
   177 
   178     /// Constructor.
   178     /// Constructor.
   179     Counter() : _title(), _os(std::cerr), count(0) {}
   179     Counter() : _title(), _os(std::cerr), count(0) {}
   180     /// Constructor.
   180     /// Constructor.
   181     Counter(std::string title,std::ostream &os=std::cerr) 
   181     Counter(std::string title,std::ostream &os=std::cerr)
   182       : _title(title), _os(os), count(0) {}
   182       : _title(title), _os(os), count(0) {}
   183     /// Constructor.
   183     /// Constructor.
   184     Counter(const char *title,std::ostream &os=std::cerr)
   184     Counter(const char *title,std::ostream &os=std::cerr)
   185       : _title(title), _os(os), count(0) {}
   185       : _title(title), _os(os), count(0) {}
   186     /// Destructor. Prints the given title and the value of the counter.
   186     /// Destructor. Prints the given title and the value of the counter.
   202     /// Resets the counter to the given value.
   202     /// Resets the counter to the given value.
   203 
   203 
   204     /// Resets the counter to the given value.
   204     /// Resets the counter to the given value.
   205     /// \note This function does not reset the values of
   205     /// \note This function does not reset the values of
   206     /// \ref SubCounter "SubCounter"s but it resets \ref NoSubCounter
   206     /// \ref SubCounter "SubCounter"s but it resets \ref NoSubCounter
   207     /// "NoSubCounter"s along with the main counter. 
   207     /// "NoSubCounter"s along with the main counter.
   208     void reset(int c=0) {count=c;}
   208     void reset(int c=0) {count=c;}
   209     /// Returns the value of the counter.
   209     /// Returns the value of the counter.
   210     operator int() {return count;}
   210     operator int() {return count;}
   211   };
   211   };
   212 
   212