BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
filters.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <cstddef>
12 
13 namespace bst {
14 
17 template <class T>
19 public:
24  ConsecutiveValuesFilter(T initial_value, size_t filter_threshold = 0)
25  : filter_threshold_{filter_threshold}, current_value_{initial_value},
26  new_value_{initial_value} {
27  }
28 
31 
34  void reset(T value) {
35  current_count_ = 0;
36  current_value_ = value;
37  }
38 
39  void setFilterThreshold(size_t filter_threshold) {
40  filter_threshold_ = filter_threshold;
41  }
42 
46  bool addValue(T value) {
47  // If the value is the same as the current value, decrement the count until
48  // it's back to zero.
49  if (value == current_value_) {
50  if (current_count_ != 0) {
52  }
53  return false;
54  }
55 
56  // If the value is the same as the currently being counted value, increment
57  // the count.
58  if (value == new_value_) {
60  } else {
61  // If the value is different from the current or the new value, reset the
62  // count and set the new, new value.
63  new_value_ = value;
64  current_count_ = 1;
65  }
66 
67  // If the count is greater than the filter threshold, set the current value
68  // to the new value and reset the count.
71  current_count_ = 0;
72  return true;
73  }
74 
75  return false;
76  }
77 
79  T getCurrentValue() const {
80  return current_value_;
81  }
82 
83 private:
86 
88  size_t current_count_{0};
89 
92 
95 };
96 
97 } // namespace bst
A simple counting filter that allows for some noise.
Definition: filters.hpp:18
bool addValue(T value)
The input stream for this filter.
Definition: filters.hpp:46
size_t filter_threshold_
The filter threshold.
Definition: filters.hpp:85
T current_value_
The current value.
Definition: filters.hpp:91
~ConsecutiveValuesFilter()=default
Destructor for the ConsecutiveValuesFilter.
void reset(T value)
Resets the filter's counter and sets the current value.
Definition: filters.hpp:34
ConsecutiveValuesFilter(T initial_value, size_t filter_threshold=0)
Constructor for the ConsecutiveValuesFilter.
Definition: filters.hpp:24
T new_value_
The new value that is being counted.
Definition: filters.hpp:94
void setFilterThreshold(size_t filter_threshold)
Definition: filters.hpp:39
T getCurrentValue() const
Returns the current value.
Definition: filters.hpp:79
size_t current_count_
The current count of consecutive new values.
Definition: filters.hpp:88
Definition: filesystem.cpp:34