7#ifndef NFAtoDFA_DEFINED
8#define NFAtoDFA_DEFINED
18#include <unordered_map>
48 std::sort(startStates.begin(), startStates.end());
51 this->scanState(
start);
53 this->computeMappings();
56 for (
const auto& row : fTransitions) {
57 stateCount =
std::max(stateCount, (
int) row.size());
59 return DFA(fCharMappings, fTransitions, fAccepts);
67 auto found = fStates.find(label);
68 if (found == fStates.end()) {
69 int id = fStates.size();
70 fStates[label] = std::unique_ptr<DFAState>(
new DFAState(
id, label));
71 return fStates[label].get();
73 return found->second.get();
76 void add(
int nfaState, std::vector<int>* states) {
80 this->add(
next, states);
83 for (
int entry : *states) {
84 if (nfaState == entry) {
88 states->push_back(nfaState);
92 void addTransition(
char c,
int start,
int next) {
93 while (fTransitions.size() <= (
size_t) c) {
94 fTransitions.push_back(std::vector<int>());
96 std::vector<int>& row = fTransitions[c];
97 while (row.size() <= (
size_t)
start) {
104 state->fIsScanned =
true;
106 std::vector<int>
next;
107 int bestAccept = INT_MAX;
108 for (
int idx :
state->fLabel.fStates) {
111 for (
int nextState : nfaState.
fNext) {
115 this->add(nextState, &
next);
121 this->addTransition(c,
state->fId, nextState->
fId);
122 if (bestAccept != INT_MAX) {
123 while (fAccepts.size() <= (
size_t) nextState->
fId) {
126 fAccepts[nextState->
fId] = bestAccept;
129 this->scanState(nextState);
137 void computeMappings() {
139 std::vector<std::vector<int>*> uniques;
141 for (
size_t i = 0;
i < fTransitions.size(); ++
i) {
143 for (
size_t j = 0; j < uniques.size(); ++j) {
144 if (*uniques[j] == fTransitions[
i]) {
150 found = (
int) uniques.size();
151 uniques.push_back(&fTransitions[
i]);
153 fCharMappings.push_back(found);
155 std::vector<std::vector<int>> newTransitions;
156 for (std::vector<int>* row : uniques) {
157 newTransitions.push_back(*row);
159 fTransitions = newTransitions;
163 std::unordered_map<DFAState::Label, std::unique_ptr<DFAState>> fStates;
164 std::vector<std::vector<int>> fTransitions;
165 std::vector<int> fCharMappings;
166 std::vector<int> fAccepts;
static float next(float f)
static std::vector< SkPDFIndirectReference > sort(const THashSet< SkPDFIndirectReference > &src)
static constexpr char START_CHAR
static constexpr char END_CHAR
static float max(float r, float g, float b)
static float min(float r, float g, float b)
bool accept(char c) const
std::vector< int > fStartStates
std::vector< NFAState > fStates