aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/lex/DFAState.h
blob: b2c89fd4cb680e67102a22e15b4b4a4623a0f06f (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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_DFASTATE
#define SKSL_DFASTATE

struct DFAState {
    struct Label {
        std::vector<int> fStates;

        Label(std::vector<int> states)
        : fStates(std::move(states)) {}

        bool operator==(const Label& other) const {
            return fStates == other.fStates;
        }

        bool operator!=(const Label& other) const {
            return !(*this == other);
        }

        std::string description() const {
            std::string result = "<";
            const char* separator = "";
            for (int s : fStates) {
                result += separator;
                result += std::to_string(s);
                separator = ", ";
            }
            result += ">";
            return result;
        }
    };

    DFAState()
    : fId(-1)
    , fLabel({}) {}

    DFAState(int id, Label label)
    : fId(id)
    , fLabel(std::move(label)) {}

    DFAState(const DFAState& other) = delete;

    int fId;

    Label fLabel;

    bool fIsScanned = false;
};

namespace std {
    template<> struct hash<DFAState::Label> {
        size_t operator()(const DFAState::Label& s) const {
            int result = 0;
            for (int i : s.fStates) {
                result = result * 101 + i;
            }
            return result;
        }
    };
} // namespace

#endif