-
Notifications
You must be signed in to change notification settings - Fork 156
/
RunningMinMax.h
118 lines (96 loc) · 3.84 KB
/
RunningMinMax.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/************************************************************************
* Copyright(c) 2010, One Unified. All rights reserved. *
* email: [email protected] *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
#pragma once
#include <map>
#include <stdexcept>
namespace ou { // One Unified
namespace tf { // TradeFrame
template<typename CRTP, typename value_t>
class RunningMinMax {
public:
RunningMinMax();
RunningMinMax( const RunningMinMax& );
RunningMinMax( RunningMinMax&& );
virtual ~RunningMinMax();
void Add( const value_t& );
void Remove( const value_t& );
value_t Min() const {
if ( 0 == m_mapValueCount.size() ) throw std::runtime_error( "no value available" );
return m_mapValueCount.begin()->first;
};
value_t Max() const {
if ( 0 == m_mapValueCount.size() ) throw std::runtime_error( "no value available" );
return m_mapValueCount.rbegin()->first;
};
void Reset();
protected:
void UpdateOnAdd( const value_t min, const value_t max ) {} // CRTP callback
void UpdateOnDel( const value_t min, const value_t max ) {} // CRTP callback
private:
using mapValueCount_t = std::map<value_t,unsigned int>;
mapValueCount_t m_mapValueCount;
};
template<typename CRTP, typename value_t>
RunningMinMax<CRTP,value_t>::RunningMinMax() {}
template<typename CRTP, typename value_t>
RunningMinMax<CRTP,value_t>::RunningMinMax( const RunningMinMax& rhs )
: m_mapValueCount( rhs.m_mapValueCount )
{
}
template<typename CRTP, typename value_t>
RunningMinMax<CRTP,value_t>::RunningMinMax( RunningMinMax&& rhs )
: m_mapValueCount( std::move( rhs.m_mapValueCount ) )
{
}
template<typename CRTP, typename value_t>
RunningMinMax<CRTP,value_t>::~RunningMinMax() {
m_mapValueCount.clear();
}
template<typename CRTP, typename value_t>
void RunningMinMax<CRTP,value_t>::Add(const value_t& value) {
typename mapValueCount_t::iterator iter = m_mapValueCount.find( value );
if ( m_mapValueCount.end() == iter ) {
m_mapValueCount.insert( typename mapValueCount_t::value_type( value, 1 ) );
}
else {
(iter->second)++;
}
if ( &RunningMinMax<CRTP,value_t>::UpdateOnAdd != &CRTP::UpdateOnAdd ) {
static_cast<CRTP*>(this)->UpdateOnAdd( m_mapValueCount.begin()->first, m_mapValueCount.rbegin()->first );
}
}
template<typename CRTP, typename value_t>
void RunningMinMax<CRTP,value_t>::Remove( const value_t& value ) {
if ( &RunningMinMax<CRTP,value_t>::UpdateOnDel != &CRTP::UpdateOnDel ) {
static_cast<CRTP*>(this)->UpdateOnDel( m_mapValueCount.begin()->first, m_mapValueCount.rbegin()->first );
}
typename mapValueCount_t::iterator iter = m_mapValueCount.find( value );
if ( (m_mapValueCount.end() == iter) ) {
int i = 1; // shouldn't land here, a bug if we do
}
else {
(iter->second)--;
if ( 0 == iter->second ) {
m_mapValueCount.erase( iter );
if ( !m_mapValueCount.empty() ) {
}
}
}
}
template<typename CRTP, typename value_t>
void RunningMinMax<CRTP,value_t>::Reset() {
m_mapValueCount.clear();
}
} // namespace tf
} // namespace ou