Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
Loading...
Searching...
No Matches
stabilized-value.h
Go to the documentation of this file.
1// License: Apache 2.0. See LICENSE file in root directory.
2// Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3
4#pragma once
5
6#include <string>
7#include <atomic>
8#include <mutex>
9#include <deque>
10#include <unordered_map>
11
12namespace utilities {
13namespace number {
14
15// Given a rapidly changing history of a value, we do not always want to show every minor
16// change right away. Rather, a "stable" value can be deduced from the history and changed
17// only when the history clearly reflects a new "stabilization percentage".
18//
19// The stabilized value is, in order of importance:
20// - the highest-occurring value in history that's over the required percentage
21// - the previous stabilized value, if it's still in history
22// - the value which occurs the most in history
23//
24// If the history is empty, no stable value exists and an exception will be thrown.
25//
26// Inputs: Type of value <T>
27// Required history size in the ctor
28//
29// The stabilized_value class is thread safe.
30//
31template < typename T >
33{
34public:
35 stabilized_value( size_t history_size )
36 : _history_size( history_size )
37 , _last_stable_value( 0 )
38 {
39 if( ! history_size )
40 throw std::runtime_error( "history size must be > 0" );
41 }
42
43 stabilized_value() = delete;
45
46
47 void add( T val )
48 {
49 const std::lock_guard< std::mutex > lock( _mutex );
50 if( _values.empty() )
51 {
52 _last_stable_value = val;
53 _last_stable_percentage = 100.f;
54 _values.push_back( val );
55 }
56 else
57 {
58 if( _values.size() >= _history_size )
59 _values.pop_front();
60 _last_stable_percentage = 0.f;
61 _values.push_back( val );
62 }
63 }
64
65 T get( float stabilization_percent = 0.75f ) const
66 {
67 if( stabilization_percent <= 0.f || stabilization_percent > 1.f )
68 throw std::runtime_error( "illegal stabilization percentage "
69 + std::to_string( stabilization_percent ));
70
71 std::lock_guard< std::mutex > lock( _mutex );
72
73 if( _values.empty() )
74 throw std::runtime_error( "history is empty; no stable value" );
75
76 if( _last_stable_percentage != stabilization_percent )
77 {
78 _last_stable_percentage = stabilization_percent;
79 std::unordered_map< T, int > values_count_map;
80 std::pair< T, int > most_stable_value = { 0.f, 0 };
81 for( T val : _values )
82 {
83 auto current_val = ++values_count_map[val];
84
85 if( most_stable_value.second < current_val )
86 {
87 most_stable_value.first = val;
88 most_stable_value.second = current_val;
89 }
90 }
91
92 auto new_value_percentage
93 = most_stable_value.second / static_cast< float >( _values.size() );
94
95 // The stabilized value is, in order of importance:
96 // - the highest-occurring value in history that's over the required percentage
97 // - the previous stabilized value, if it's still in history
98 // - the value which occurs the most in history
99 if( new_value_percentage >= _last_stable_percentage
100 || values_count_map.find( _last_stable_value ) == values_count_map.end() )
101 {
102 _last_stable_value = most_stable_value.first;
103 }
104 }
105
106 return _last_stable_value;
107 }
108
109 void clear()
110 {
111 const std::lock_guard< std::mutex > lock( _mutex );
112 _values.clear();
113 }
114
115 bool empty() const
116 {
117 const std::lock_guard< std::mutex > lock( _mutex );
118 return _values.empty();
119 }
120
121private:
122 std::deque< T > _values;
123 const size_t _history_size;
124 mutable T _last_stable_value;
125 mutable float _last_stable_percentage;
126 mutable std::mutex _mutex;
127};
128
129} // namespace number
130} // namespace utilities
Definition: stabilized-value.h:33
void clear()
Definition: stabilized-value.h:109
stabilized_value(const stabilized_value &)=delete
bool empty() const
Definition: stabilized-value.h:115
stabilized_value(size_t history_size)
Definition: stabilized-value.h:35
T get(float stabilization_percent=0.75f) const
Definition: stabilized-value.h:65
void add(T val)
Definition: stabilized-value.h:47
Definition: stabilized-value.h:12