@@ -161,10 +161,13 @@ template <typename ActionT> class Automaton {
161
161
// / FIXME: This uses a std::map because ActionT can be a pair type including
162
162
// / an enum. In particular DenseMapInfo<ActionT> must be defined to use
163
163
// / DenseMap here.
164
- std::map<std::pair<uint64_t , ActionT>, std::pair<uint64_t , unsigned >> M;
164
+ // / This is a shared_ptr to allow very quick copy-construction of Automata; this
165
+ // / state is immutable after construction so this is safe.
166
+ using MapTy = std::map<std::pair<uint64_t , ActionT>, std::pair<uint64_t , unsigned >>;
167
+ std::shared_ptr<MapTy> M;
165
168
// / An optional transcription object. This uses much more state than simply
166
169
// / traversing the DFA for acceptance, so is heap allocated.
167
- std::unique_ptr <internal::NfaTranscriber> Transcriber;
170
+ std::shared_ptr <internal::NfaTranscriber> Transcriber;
168
171
// / The initial DFA state is 1.
169
172
uint64_t State = 1 ;
170
173
// / True if we should transcribe and false if not (even if Transcriber is defined).
@@ -187,13 +190,15 @@ template <typename ActionT> class Automaton {
187
190
ArrayRef<NfaStatePair> TranscriptionTable = {}) {
188
191
if (!TranscriptionTable.empty ())
189
192
Transcriber =
190
- std::make_unique <internal::NfaTranscriber>(TranscriptionTable);
193
+ std::make_shared <internal::NfaTranscriber>(TranscriptionTable);
191
194
Transcribe = Transcriber != nullptr ;
195
+ M = std::make_shared<MapTy>();
192
196
for (const auto &I : Transitions)
193
197
// Greedily read and cache the transition table.
194
- M. emplace (std::make_pair (I.FromDfaState , I.Action ),
195
- std::make_pair (I.ToDfaState , I.InfoIdx ));
198
+ M-> emplace (std::make_pair (I.FromDfaState , I.Action ),
199
+ std::make_pair (I.ToDfaState , I.InfoIdx ));
196
200
}
201
+ Automaton (const Automaton &) = default ;
197
202
198
203
// / Reset the automaton to its initial state.
199
204
void reset () {
@@ -218,8 +223,8 @@ template <typename ActionT> class Automaton {
218
223
// / If this function returns false, all methods are undefined until reset() is
219
224
// / called.
220
225
bool add (const ActionT &A) {
221
- auto I = M. find ({State, A});
222
- if (I == M. end ())
226
+ auto I = M-> find ({State, A});
227
+ if (I == M-> end ())
223
228
return false ;
224
229
if (Transcriber && Transcribe)
225
230
Transcriber->transition (I->second .second );
@@ -229,8 +234,8 @@ template <typename ActionT> class Automaton {
229
234
230
235
// / Return true if the automaton can be transitioned based on input symbol A.
231
236
bool canAdd (const ActionT &A) {
232
- auto I = M. find ({State, A});
233
- return I != M. end ();
237
+ auto I = M-> find ({State, A});
238
+ return I != M-> end ();
234
239
}
235
240
236
241
// / Obtain a set of possible paths through the input nondeterministic
0 commit comments