DFAState.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef SKSL_DFASTATE
  8. #define SKSL_DFASTATE
  9. #include "src/sksl/lex/LexUtil.h"
  10. #include <vector>
  11. #include <string>
  12. struct DFAState {
  13. struct Label {
  14. std::vector<int> fStates;
  15. Label(std::vector<int> states)
  16. : fStates(std::move(states)) {}
  17. bool operator==(const Label& other) const {
  18. return fStates == other.fStates;
  19. }
  20. bool operator!=(const Label& other) const {
  21. return !(*this == other);
  22. }
  23. std::string description() const {
  24. std::string result = "<";
  25. const char* separator = "";
  26. for (int s : fStates) {
  27. result += separator;
  28. result += std::to_string(s);
  29. separator = ", ";
  30. }
  31. result += ">";
  32. return result;
  33. }
  34. };
  35. DFAState()
  36. : fId(INVALID)
  37. , fLabel({}) {}
  38. DFAState(int id, Label label)
  39. : fId(id)
  40. , fLabel(std::move(label)) {}
  41. DFAState(const DFAState& other) = delete;
  42. int fId;
  43. Label fLabel;
  44. bool fIsScanned = false;
  45. };
  46. namespace std {
  47. template<> struct hash<DFAState::Label> {
  48. size_t operator()(const DFAState::Label& s) const {
  49. size_t result = 0;
  50. for (int i : s.fStates) {
  51. result = result * 101 + i;
  52. }
  53. return result;
  54. }
  55. };
  56. } // namespace
  57. #endif