• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SKSL_DFASTATE
9 #define SKSL_DFASTATE
10 
11 #include "src/sksl/lex/LexUtil.h"
12 
13 #include <vector>
14 #include <string>
15 
16 struct DFAState {
17     struct Label {
18         std::vector<int> fStates;
19 
LabelDFAState::Label20         Label(std::vector<int> states)
21         : fStates(std::move(states)) {}
22 
23         bool operator==(const Label& other) const {
24             return fStates == other.fStates;
25         }
26 
27         bool operator!=(const Label& other) const {
28             return !(*this == other);
29         }
30 
descriptionDFAState::Label31         std::string description() const {
32             std::string result = "<";
33             const char* separator = "";
34             for (int s : fStates) {
35                 result += separator;
36                 result += std::to_string(s);
37                 separator = ", ";
38             }
39             result += ">";
40             return result;
41         }
42     };
43 
DFAStateDFAState44     DFAState()
45     : fId(INVALID)
46     , fLabel({}) {}
47 
DFAStateDFAState48     DFAState(int id, Label label)
49     : fId(id)
50     , fLabel(std::move(label)) {}
51 
52     DFAState(const DFAState& other) = delete;
53 
54     int fId;
55 
56     Label fLabel;
57 
58     bool fIsScanned = false;
59 };
60 
61 namespace std {
62     template<> struct hash<DFAState::Label> {
63         size_t operator()(const DFAState::Label& s) const {
64             size_t result = 0;
65             for (int i : s.fStates) {
66                 result = result * 101 + i;
67             }
68             return result;
69         }
70     };
71 } // namespace
72 
73 #endif
74