summaryrefslogtreecommitdiffstats
path: root/ml/BitRateWindow.cc
blob: c4c994c42d5babc00fd0232e74ea08e5d48df053 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "BitRateWindow.h"

using namespace ml;

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

    BBC.insert(Bit);
    switch (CurrState) {
        case State::NotFilled: {
            if (BBC.isFilled()) {
                if (BBC.numSetBits() < SetBitsThreshold) {
                    CurrState = State::BelowThreshold;
                } else {
                    CurrState = State::AboveThreshold;
                }
            } else {
                CurrState = State::NotFilled;
            }

            E = {State::NotFilled, CurrState};
            break;
        } case State::BelowThreshold: {
            if (BBC.numSetBits() >= SetBitsThreshold) {
                CurrState = State::AboveThreshold;
            }

            E = {State::BelowThreshold, CurrState};
            break;
        } case State::AboveThreshold: {
            if ((BBC.numSetBits() < SetBitsThreshold) ||
                (CurrLength == MaxLength)) {
                CurrState = State::Idle;
            }

            E = {State::AboveThreshold, CurrState};
            break;
        } case State::Idle: {
            if (CurrLength == IdleLength) {
                CurrState = State::NotFilled;
            }

            E = {State::Idle, CurrState};
            break;
        }
    }

    Action A = EdgeActions[E];
    size_t L = (this->*A)(E.first, Bit);
    return {E, L};
}

void BitRateWindow::print(std::ostream &OS) const {
    switch (CurrState) {
        case State::NotFilled:
            OS << "NotFilled";
            break;
        case State::BelowThreshold:
            OS << "BelowThreshold";
            break;
        case State::AboveThreshold:
            OS << "AboveThreshold";
            break;
        case State::Idle:
            OS << "Idle";
            break;
        default:
            OS << "UnknownState";
            break;
    }

    OS << ": " << BBC << " (Current Length: " << CurrLength << ")";
}