Thrill  0.1
stats_counter.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/stats_counter.hpp
3  *
5  * (See accompanying file LICENSE_1_0.txt or copy at
7  *
8  * Part of Project Thrill - http://project-thrill.org
9  *
10  * Copyright (C) 2014 Thomas Keh <[email protected]>
11  * Copyright (C) 2015 Timo Bingmann <[email protected]>
12  *
14  ******************************************************************************/
15
16 #pragma once
19
20 #include <algorithm>
21
22 namespace thrill {
23 namespace common {
24
25 /*!
26  * This class provides a statistical counter that can easily be deactivated
27  * using a boolean template switch. It's basically a wrapper for an counter
28  * type, like unsigned long long value. If further operators are needed, they
30  */
31 template <typename ValueType_, bool Active = true>
33 { };
34
35 template <typename ValueType_>
36 class StatsCounter<ValueType_, true>
37 {
38 public:
39  //! The counter's value type
40  using ValueType = ValueType_;
41
42 private:
43  //! The counter's value
45
46 public:
47  //! The constructor. Initializes the counter to 0.
48  StatsCounter(const ValueType& initial = ValueType()) // NOLINT
49  : value_(initial)
50  { }
51
52  //! Whether the counter is active
53  bool Real() const { return true; }
54
55  //! Increases the counter by right.
57  value_ += right;
58  return *this;
59  }
60
61  //! Increases the counter by 1 (prefix).
63  ++value_;
64  return *this;
65  }
66
67  //! Increases the counter by 1 (postfix).
68  StatsCounter operator ++ (int) { // NOLINT
69  StatsCounter copy = *this;
70  ++value_;
71  return copy;
72  }
73
74  //! Set the counter to other if other is larger than the current counter
75  //! value.
76  void set_max(const ValueType& other) {
77  value_ = std::max(value_, other);
78  }
79
80  /*!
81  * Cast to counter_type: Returns the counter's value as a regular integer
82  * value. This can be used as a getter as well as for printing with
83  * std::out.
84  */
85  operator ValueType () const
86  {
87  return value_;
88  }
89
90  ValueType value() const {
91  return value_;
92  }
93 };
94
95 template <typename ValueType_>
96 class StatsCounter<ValueType_, false>
97 {
98 public:
99  //! The counter's value type
100  using ValueType = ValueType_;
101
102 public:
103  StatsCounter(const ValueType& = ValueType()) // NOLINT
104  { }
105
106  //! Whether the counter is active
107  bool Real() const { return false; }
108
110  { return *this; }
111
113  { return *this; }
114
115  StatsCounter& operator ++ (int) // NOLINT
116  { return *this; }
117
118  void set_max(const ValueType&)
119  { }
120
121  operator ValueType () const
122  {
123  return ValueType();
124  }
125
126  ValueType value() const {
127  return ValueType();
128  }
129 };
130
131 } // namespace common
132 } // namespace thrill
133
135
136 /******************************************************************************/
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
This class provides a statistical counter that can easily be deactivated using a boolean template swi...
StatsCounter(const ValueType &initial=ValueType())
The constructor. Initializes the counter to 0.
uint_pair & operator+=(const uint_pair &b)