libmonaa  0.5.2
sunday_skip_value.hh
1 #pragma once
2 
3 #include <array>
4 #include <climits>
5 #include <iostream>
6 
7 #include "ta2za.hh"
8 #include "timed_automaton.hh"
9 #include "zone_automaton.hh"
10 
18 private:
20  int m;
21  std::array<unsigned int, CHAR_MAX> delta{};
23  std::unordered_set<char> endChars;
24 
25 public:
26  explicit SundaySkipValue(const TimedAutomaton &TA) {
27  ZoneAutomaton ZA;
28  ta2za(TA, ZA);
29  ZA.removeDeadStates();
30 
31  std::vector<std::unordered_set<char>> charSet;
32  bool accepted = false;
33  m = 0;
34  std::vector<std::shared_ptr<ZAState>> CStates = ZA.initialStates;
35  while (!accepted) {
36  if (CStates.empty()) {
37  std::cerr << "monaa: empty pattern" << std::endl;
38  exit(10);
39  }
40  std::vector<std::shared_ptr<ZAState>> NStates;
41  m++;
42  charSet.resize(m);
43  for (const auto& zaState : CStates) {
44  std::unordered_set<std::shared_ptr<ZAState>> closure;
45  closure.insert(zaState);
46  epsilonClosure(closure);
47  for (const auto& state : closure) {
48  for (char c = 1; c < CHAR_MAX; c++) {
49  for (const auto& nextState : state->next[c]) {
50  auto sharedNext = nextState.lock();
51  if (!sharedNext) {
52  continue;
53  }
54  accepted = accepted || sharedNext->isMatch;
55  NStates.push_back(sharedNext);
56  charSet[m - 1].insert(c);
57  }
58  }
59  }
60  }
61  CStates = NStates;
62  }
63 
64  // Construct the table of Sunday's skip value
65  delta.fill(m + 1);
66  for (int i = 0; i <= m - 1; i++) {
67  for (char s : charSet[i]) {
68  delta[s] = m - i;
69  }
70  }
71  endChars = charSet[m - 1];
72  }
73  unsigned int at(std::size_t n) const { return delta.at(n); }
74  unsigned int operator[](std::size_t n) const { return delta[n]; }
76  int getM() const { return m; }
77  void getEndChars(std::unordered_set<char> &endCharsHolder) const {
78  endCharsHolder = this->endChars;
79  }
80 };
The skip value function based on Sunday's quick search.
Definition: sunday_skip_value.hh:17
int getM() const
Minimum length of the recognized language.
Definition: sunday_skip_value.hh:76
std::vector< std::shared_ptr< State > > initialStates
The initial states of this automaton.
Definition: common_types.hh:19
A timed automaton.
Definition: timed_automaton.hh:54
Definition: zone_automaton.hh:55
void removeDeadStates()
remove states unreachable to any accepting states
Definition: zone_automaton.hh:59