-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostTagSearcher.cpp
135 lines (115 loc) · 5.87 KB
/
PostTagSearcher.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "PostTagSearcher.hpp"
#include "PostTagHistory.hpp"
namespace PostTagSystem {
class PostTagSearcher::Implementation {
public:
Implementation() {}
std::vector<EvaluationResult> evaluateRange(uint64_t tapeLength,
uint64_t tapeBegin,
uint64_t tapeEnd,
const EvaluationParameters& parameters) {
return evaluateRange(
TagState(tapeLength, tapeBegin, 0),
TagState(tapeEnd == static_cast<uint64_t>(1) << tapeLength ? tapeLength + 1 : tapeLength, tapeEnd, 0),
parameters);
}
std::vector<EvaluationResult> evaluateRange(const TagState& begin,
const TagState& end,
const EvaluationParameters& parameters) {
std::vector<TagState> states;
constexpr uint8_t postPhaseCount = 3;
for (auto state = begin; state != end; state.increment(postPhaseCount)) {
states.push_back(state);
}
return evaluateGroup(states, parameters);
}
std::vector<EvaluationResult> evaluateGroup(const std::vector<SmallState>& headsAndTapes,
const EvaluationParameters& parameters) {
std::vector<TagState> states;
for (const auto& smallState : headsAndTapes) {
states.push_back(fromSmallState(smallState));
}
return evaluateGroup(states, parameters);
}
std::vector<EvaluationResult> evaluateGroup(const std::vector<TagState>& states,
const EvaluationParameters& parameters) {
std::vector<EvaluationResult> results;
results.reserve(states.size());
PostTagHistory evaluator;
PostTagHistory::EvaluationLimits limits;
limits.maxEventCount = parameters.maxEventCount;
limits.maxTapeLength = parameters.maxTapeLength;
limits.maxTimeNs = parameters.groupTimeConstraintNs;
const auto singleInitResults =
evaluator.evaluate(PostTagHistory::NamedRule::Post,
states,
limits,
{parameters.checkpoints, {true, parameters.automaticCheckpointsEveryEvents}});
for (size_t initIndex = 0; initIndex < states.size(); ++initIndex) {
EvaluationResult result;
result.eventCount = singleInitResults[initIndex].eventCount;
result.maxTapeLength = singleInitResults[initIndex].maxIntermediateTapeLength;
result.finalTapeLength = singleInitResults[initIndex].finalState.tape.size();
result.initialState = states[initIndex];
result.finalState = singleInitResults[initIndex].finalState;
switch (singleInitResults[initIndex].conclusionReason) {
case PostTagHistory::ConclusionReason::Terminated:
result.conclusionReason = ConclusionReason::Terminated;
break;
case PostTagHistory::ConclusionReason::ReachedAutomaticCheckpoint:
result.conclusionReason = ConclusionReason::ReachedCycle;
break;
case PostTagHistory::ConclusionReason::ReachedExplicitCheckpoint:
result.conclusionReason = ConclusionReason::ReachedKnownCheckpoint;
break;
case PostTagHistory::ConclusionReason::MaxEventCountExceeded:
result.conclusionReason = ConclusionReason::MaxEventCountExceeded;
break;
case PostTagHistory::ConclusionReason::TimeConstraintExceeded:
result.conclusionReason = ConclusionReason::TimeConstraintExceeded;
break;
case PostTagHistory::ConclusionReason::NotEvaluated:
result.conclusionReason = ConclusionReason::NotEvaluated;
break;
case PostTagHistory::ConclusionReason::ReachedPreviousInitCheckpoint:
result.conclusionReason = ConclusionReason::MergedWithAnotherInit;
break;
default:
result.conclusionReason = ConclusionReason::InvalidInput;
}
if ((result.conclusionReason != ConclusionReason::NotEvaluated || parameters.includeUnevaluatedStates) &&
(result.conclusionReason != ConclusionReason::MergedWithAnotherInit || parameters.includeMergedStates)) {
results.push_back(result);
}
}
return results;
}
private:
TagState fromSmallState(const SmallState& smallState) {
return TagState(smallState.tapeLength, smallState.tape, smallState.headState);
}
};
bool PostTagSearcher::EvaluationResult::operator==(const PostTagSearcher::EvaluationResult& other) const {
return conclusionReason == other.conclusionReason && eventCount == other.eventCount &&
maxTapeLength == other.maxTapeLength && finalTapeLength == other.finalTapeLength &&
initialState == other.initialState && finalState == other.finalState;
}
PostTagSearcher::PostTagSearcher() : implementation_(std::make_shared<Implementation>()) {}
std::vector<PostTagSearcher::EvaluationResult> PostTagSearcher::evaluateRange(
const TagState& begin, const TagState& end, const PostTagSearcher::EvaluationParameters& parameters) {
return implementation_->evaluateRange(begin, end, parameters);
}
std::vector<PostTagSearcher::EvaluationResult> PostTagSearcher::evaluateRange(
uint8_t tapeLength, uint64_t tapeBegin, uint64_t tapeEnd, const PostTagSearcher::EvaluationParameters& parameters) {
return implementation_->evaluateRange(tapeLength, tapeBegin, tapeEnd, parameters);
}
std::vector<PostTagSearcher::EvaluationResult> PostTagSearcher::evaluateGroup(
const std::vector<TagState>& states, const PostTagSearcher::EvaluationParameters& parameters) {
return implementation_->evaluateGroup(states, parameters);
}
std::vector<PostTagSearcher::EvaluationResult> PostTagSearcher::evaluateGroup(
const std::vector<PostTagSearcher::SmallState>& headsAndTapes,
const PostTagSearcher::EvaluationParameters& parameters) {
return implementation_->evaluateGroup(headsAndTapes, parameters);
}
} // namespace PostTagSystem