Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
probability::windowed_median::WindowedMedian Class Reference

A class to calculate the median of a leading sliding window at the back of a stream of integer values. More...

Collaboration diagram for probability::windowed_median::WindowedMedian:
[legend]

Public Member Functions

 WindowedMedian (size_type windowSize)
 Constructs a WindowedMedian object.
 
void insert (int value)
 Insert a new value to the stream.
 
float getMedian () const
 Gets the median of the values in the sliding window.
 
float getMedianNaive () const
 A naive and inefficient method to obtain the median of the sliding window. Used for testing!
 

Private Member Functions

void insertToSorted (int value)
 Inserts a value to a sorted multi-value BST.
 
void eraseFromSorted (int value)
 Erases a value from a sorted multi-value BST.
 

Private Attributes

const size_type _windowSize
 sliding window size
 
Window _window
 a sliding window of values along the stream
 
std::multiset< int > _sortedValues
 
std::multiset< int >::const_iterator _itMedian
 

Detailed Description

A class to calculate the median of a leading sliding window at the back of a stream of integer values.

Constructor & Destructor Documentation

◆ WindowedMedian()

probability::windowed_median::WindowedMedian::WindowedMedian ( size_type windowSize)
inlineexplicit

Constructs a WindowedMedian object.

Parameters
windowSizeSliding window size
123: _windowSize(windowSize){};
const size_type _windowSize
sliding window size
Definition windowed_median.cpp:57

Member Function Documentation

◆ eraseFromSorted()

void probability::windowed_median::WindowedMedian::eraseFromSorted ( int value)
inlineprivate

Erases a value from a sorted multi-value BST.

Parameters
valueValue to insert

If the erased value is on the left branch or the median itself and the number of elements is even, the new median will be the right child of the current one

O(1) - traversing one step to the right child

However, if the erased value is on the right branch or the median itself, and the number of elements is odd, the new median will be the left child of the current one

Find the (first) position of the value we want to erase, and erase it

95 {
96 const auto sz = _sortedValues.size();
97
98 /// If the erased value is on the left branch or the median itself and
99 /// the number of elements is even, the new median will be the right
100 /// child of the current one
101 if (value <= *_itMedian && sz % 2 == 0) {
102 ++_itMedian; /// O(1) - traversing one step to the right child
103 }
104
105 /// However, if the erased value is on the right branch or the median
106 /// itself, and the number of elements is odd, the new median will be
107 /// the left child of the current one
108 else if (value >= *_itMedian && sz % 2 != 0) {
109 --_itMedian; // O(1) - traversing one step to the left child
110 }
111
112 /// Find the (first) position of the value we want to erase, and erase
113 /// it
114 const auto it = _sortedValues.find(value); // O(logN)
115 _sortedValues.erase(it); // O(logN)
116 }
std::multiset< int >::const_iterator _itMedian
Definition windowed_median.cpp:62
std::multiset< int > _sortedValues
Definition windowed_median.cpp:59
T erase(T... args)
T find(T... args)
T size(T... args)
Here is the call graph for this function:

◆ getMedian()

float probability::windowed_median::WindowedMedian::getMedian ( ) const
inline

Gets the median of the values in the sliding window.

Returns
Median of sliding window. For even window size return the average between the two values in the middle

O(1)

148 {
149 if (_sortedValues.size() % 2 != 0) {
150 return *_itMedian; // O(1)
151 }
152 return 0.5f * *_itMedian + 0.5f * *next(_itMedian); /// O(1)
153 }
T next(T... args)
Here is the call graph for this function:

◆ getMedianNaive()

float probability::windowed_median::WindowedMedian::getMedianNaive ( ) const
inline

A naive and inefficient method to obtain the median of the sliding window. Used for testing!

Returns
Median of sliding window. For even window size return the average between the two values in the middle

Sort window - O(NlogN)

Find value in the middle - O(N)

O(N)

161 {
162 auto window = _window;
163 window.sort(); /// Sort window - O(NlogN)
164 auto median =
165 *next(window.begin(),
166 window.size() / 2); /// Find value in the middle - O(N)
167 if (window.size() % 2 != 0) {
168 return median;
169 }
170 return 0.5f * median +
171 0.5f * *next(window.begin(), window.size() / 2 - 1); /// O(N)
172 }
Window _window
a sliding window of values along the stream
Definition windowed_median.cpp:58
T sort(T... args)
Here is the call graph for this function:

◆ insert()

void probability::windowed_median::WindowedMedian::insert ( int value)
inline

Insert a new value to the stream.

Parameters
valueNew value to insert

Push new value to the back of the sliding window - O(1)

If exceeding size of window, pop from its left side

Erase from the multi-value BST the window left side value

Pop the left side value from the window - O(1)

129 {
130 /// Push new value to the back of the sliding window - O(1)
131 _window.push_back(value);
132 insertToSorted(value); // Insert value to the multi-value BST - O(logN)
133 if (_window.size() > _windowSize) { /// If exceeding size of window,
134 /// pop from its left side
136 _window.front()); /// Erase from the multi-value BST
137 /// the window left side value
138 _window.pop_front(); /// Pop the left side value from the window -
139 /// O(1)
140 }
141 }
void insertToSorted(int value)
Inserts a value to a sorted multi-value BST.
Definition windowed_median.cpp:69
void eraseFromSorted(int value)
Erases a value from a sorted multi-value BST.
Definition windowed_median.cpp:95
T front(T... args)
T pop_front(T... args)
T push_back(T... args)
Here is the call graph for this function:

◆ insertToSorted()

void probability::windowed_median::WindowedMedian::insertToSorted ( int value)
inlineprivate

Inserts a value to a sorted multi-value BST.

Parameters
valueValue to insert

Insert value to BST - O(logN)

For the first value, set median iterator to BST root

If new value goes to left tree branch, and number of elements is even, the new median in the balanced tree is the left child of the median before the insertion

However, if the new value goes to the right branch, the previous median's right child is the new median in the balanced tree

O(1) - traversing one step to the right child

69 {
70 _sortedValues.insert(value); /// Insert value to BST - O(logN)
71 const auto sz = _sortedValues.size();
72 if (sz == 1) { /// For the first value, set median iterator to BST root
74 return;
75 }
76
77 /// If new value goes to left tree branch, and number of elements is
78 /// even, the new median in the balanced tree is the left child of the
79 /// median before the insertion
80 if (value < *_itMedian && sz % 2 == 0) {
81 --_itMedian; // O(1) - traversing one step to the left child
82 }
83
84 /// However, if the new value goes to the right branch, the previous
85 /// median's right child is the new median in the balanced tree
86 else if (value >= *_itMedian && sz % 2 != 0) {
87 ++_itMedian; /// O(1) - traversing one step to the right child
88 }
89 }
T begin(T... args)
T insert(T... args)
Here is the call graph for this function:

Member Data Documentation

◆ _itMedian

std::multiset<int>::const_iterator probability::windowed_median::WindowedMedian::_itMedian
private

an iterator that points to the root of the multi-value BST

◆ _sortedValues

std::multiset<int> probability::windowed_median::WindowedMedian::_sortedValues
private

a DS to represent a balanced multi-value binary search tree (BST)


The documentation for this class was generated from the following file: