summaryrefslogtreecommitdiffstats
path: root/ml/BitRateWindow.h
blob: 0d99008b853a91e679947e6064a239305181884e (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef BIT_RATE_WINDOW_H
#define BIT_RATE_WINDOW_H

#include "BitBufferCounter.h"
#include "ml-private.h"

namespace ml {

class BitRateWindow {
public:
    enum class State {
        NotFilled,
        BelowThreshold,
        AboveThreshold,
        Idle
    };

    using Edge = std::pair<State, State>;
    using Action = size_t (BitRateWindow::*)(State PrevState, bool NewBit);

private:
    std::map<Edge, Action> EdgeActions = {
        // From == To
        {
            Edge(State::NotFilled, State::NotFilled),
            &BitRateWindow::onRoundtripNotFilled,
        },
        {
            Edge(State::BelowThreshold, State::BelowThreshold),
            &BitRateWindow::onRoundtripBelowThreshold,
        },
        {
            Edge(State::AboveThreshold, State::AboveThreshold),
            &BitRateWindow::onRoundtripAboveThreshold,
        },
        {
            Edge(State::Idle, State::Idle),
            &BitRateWindow::onRoundtripIdle,
        },


        // NotFilled => {BelowThreshold, AboveThreshold}
        {
            Edge(State::NotFilled, State::BelowThreshold),
            &BitRateWindow::onNotFilledToBelowThreshold
        },
        {
            Edge(State::NotFilled, State::AboveThreshold),
            &BitRateWindow::onNotFilledToAboveThreshold
        },

        // BelowThreshold => AboveThreshold
        {
            Edge(State::BelowThreshold, State::AboveThreshold),
            &BitRateWindow::onBelowToAboveThreshold
        },

        // AboveThreshold => Idle
        {
            Edge(State::AboveThreshold, State::Idle),
            &BitRateWindow::onAboveThresholdToIdle
        },

        // Idle => NotFilled
        {
            Edge(State::Idle, State::NotFilled),
            &BitRateWindow::onIdleToNotFilled
        },
    };

public:
    BitRateWindow(size_t MinLength, size_t MaxLength, size_t IdleLength,
                  size_t SetBitsThreshold) :
        MinLength(MinLength), MaxLength(MaxLength), IdleLength(IdleLength),
        SetBitsThreshold(SetBitsThreshold),
        CurrState(State::NotFilled), CurrLength(0), BBC(MinLength) {}

    std::pair<Edge, size_t> insert(bool Bit);

    void print(std::ostream &OS) const;

private:
    size_t onRoundtripNotFilled(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        CurrLength += 1;
        return CurrLength;
    }

    size_t onRoundtripBelowThreshold(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        CurrLength = MinLength;
        return CurrLength;
    }

    size_t onRoundtripAboveThreshold(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        CurrLength += 1;
        return CurrLength;
    }

    size_t onRoundtripIdle(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        CurrLength += 1;
        return CurrLength;
    }

    size_t onNotFilledToBelowThreshold(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        CurrLength = MinLength;
        return CurrLength;
    }

    size_t onNotFilledToAboveThreshold(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        CurrLength += 1;
        return CurrLength;
    }

    size_t onBelowToAboveThreshold(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        CurrLength = MinLength;
        return CurrLength;
    }

    size_t onAboveThresholdToIdle(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        size_t PrevLength = CurrLength;
        CurrLength = 1;
        return PrevLength;
    }

    size_t onIdleToNotFilled(State PrevState, bool NewBit) {
        (void) PrevState, (void) NewBit;

        BBC = BitBufferCounter(MinLength);
        BBC.insert(NewBit);

        CurrLength = 1;
        return CurrLength;
    }

private:
    size_t MinLength;
    size_t MaxLength;
    size_t IdleLength;
    size_t SetBitsThreshold;

    State CurrState;
    size_t CurrLength;
    BitBufferCounter BBC;
};

} // namespace ml

inline std::ostream& operator<<(std::ostream &OS, const ml::BitRateWindow BRW) {
    BRW.print(OS);
    return OS;
}

#endif /* BIT_RATE_WINDOW_H */