@@ -153,8 +153,9 @@ class State : public WithUniqueID {
153153
154154 void insertOutgoingEdge (Edge &e) { this ->outgoingEdges .insert (&e); }
155155
156- void removeOutgoingEdge (Edge &e) {
157- this ->outgoingEdges .erase (&e); }
156+ void removeOutgoingEdge (EdgeRef e) {
157+ this ->outgoingEdges .erase (e);
158+ }
158159
159160private:
160161 SetOfEdgeRefs outgoingEdges;
@@ -203,6 +204,7 @@ class FiniteStateMachine {
203204 [[nodiscard]] virtual StateRef getInitialState () const = 0;
204205 [[nodiscard]] virtual const SetOfStateRefs &getInitialStates () const = 0;
205206 [[nodiscard]] virtual const SetOfStateRefs &getFinalStates () const = 0;
207+
206208};
207209
208210//
@@ -377,6 +379,10 @@ class SetOfStates : public Abstract::SetOfStates {
377379 throw CException (" error - state not found in FiniteStateMachine::_withLabel" );
378380 }
379381
382+ bool hasStateWithLabel (StateLabelType l) {
383+ return this ->labelIndex .find (l) != this ->labelIndex .end ();
384+ }
385+
380386 void addState (std::shared_ptr<State<StateLabelType, EdgeLabelType>>& s) {
381387 this ->addToStateIndex (s->getLabel (), s);
382388 }
@@ -386,7 +392,6 @@ class SetOfStates : public Abstract::SetOfStates {
386392template <typename StateLabelType, typename EdgeLabelType>
387393class SetOfStateRefs : public Abstract ::SetOfStateRefs {
388394public:
389- using CIter = typename SetOfStateRefs::const_iterator;
390395};
391396
392397template <typename StateLabelType, typename EdgeLabelType> class State : public Abstract ::State {
@@ -439,10 +444,13 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
439444 SetOfStateRefs<StateLabelType, EdgeLabelType> finalStates;
440445
441446 State<StateLabelType, EdgeLabelType>& _getStateLabeled (const StateLabelType &s) {
442- // try the index first
443447 return this ->states .withLabel (s);
444448 };
445449
450+ State<StateLabelType, EdgeLabelType>& _getState (const State<StateLabelType, EdgeLabelType> &s) {
451+ return dynamic_cast <State<StateLabelType, EdgeLabelType>&>(this ->states .withId (s.getId ()));
452+ };
453+
446454
447455public:
448456 FiniteStateMachine () : Abstract::FiniteStateMachine() {};
@@ -486,8 +494,8 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
486494 void removeEdge (const Edge<StateLabelType, EdgeLabelType> &e) {
487495 auto csrc = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(e.getSource ());
488496 // get a non-const version of the state
489- auto & src = this ->_getStateLabeled ( csrc-> getLabel () );
490- src.removeOutgoingEdge (e);
497+ auto & src = this ->_getState (* csrc);
498+ src.removeOutgoingEdge (& e);
491499 this ->edges .remove (e);
492500 }
493501
@@ -510,7 +518,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
510518
511519 // set initial state to state with label;
512520 void setInitialState (StateLabelType label) {
513- this ->setInitialState (* this ->states .withLabel (label));
521+ this ->setInitialState (this ->states .withLabel (label));
514522 };
515523
516524 void setInitialState (const State<StateLabelType, EdgeLabelType> &s) {
@@ -526,7 +534,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
526534
527535 void addFinalState (const State<StateLabelType, EdgeLabelType> &s) {
528536 // we are assuming s is one of our states
529- this ->finalStates .emplace (s. getId (), &s);
537+ this ->finalStates .insert ( &s);
530538 };
531539
532540
@@ -598,20 +606,9 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
598606 return nullptr ;
599607 };
600608
601- StateRef<StateLabelType, EdgeLabelType> checkStateLabeled (const StateLabelType &s) {
602- // try the index first
603- if (this ->states .withLabel (s) != nullptr ) {
604- return this ->states .withLabel (s);
605- }
606- // for now just a linear search
607- auto i = this ->states .begin ();
608- while (i != this ->states .end ()) {
609- auto &t = dynamic_cast <State<StateLabelType, EdgeLabelType> &>(*((*i).second ));
610- if ((t.stateLabel ) == s) {
611- this ->states .addToStateIndex (s, &t);
612- return &t;
613- }
614- i++;
609+ StateRef<StateLabelType, EdgeLabelType> checkStateLabeled (const StateLabelType &l) {
610+ if (this ->states .hasStateWithLabel (l)) {
611+ return &(this ->states .withLabel (l));
615612 }
616613 return nullptr ;
617614 };
@@ -642,9 +639,8 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
642639
643640 for (const auto &it : outgoingEdges) {
644641 auto e = dynamic_cast <const Edge<StateLabelType, EdgeLabelType> *>(it);
645- auto dstState = dynamic_cast <const State<StateLabelType, EdgeLabelType> &>(
646- e->getDestination ());
647- if (e->getLabel () == lbl && dstState.getLabel () == dst) {
642+ auto dstState = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(e->getDestination ());
643+ if (e->getLabel () == lbl && dstState->getLabel () == dst) {
648644 return e;
649645 }
650646 }
@@ -861,7 +857,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
861857 this ->newInstance ());
862858
863859 // make a state for every equivalence class
864- std::map<std::shared_ptr<Abstract::SetOfStateRefs>, State <StateLabelType, EdgeLabelType> * >
860+ std::map<std::shared_ptr<Abstract::SetOfStateRefs>, StateRef <StateLabelType, EdgeLabelType>>
865861 newStateMap;
866862 CId sid = 0 ;
867863 for (const auto &cli : eqClasses) {
@@ -883,7 +879,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
883879 auto ed = dynamic_cast <EdgeRef<StateLabelType, EdgeLabelType>>(edi);
884880 result->addEdge (*(newStateMap[cli]),
885881 ed->getLabel (),
886- *(newStateMap[eqMap[&( ed->getDestination () )]]));
882+ *(newStateMap[eqMap[ed->getDestination ()]]));
887883 }
888884 }
889885
0 commit comments