@@ -151,24 +151,26 @@ class State : public WithUniqueID {
151151 return this ->outgoingEdges ;
152152 }
153153
154- // TODO (Marc Geilen) get rid of const cast
155- void removeOutgoingEdge (const Edge &e) {
156- this ->outgoingEdges .erase (&e); }
157154 void insertOutgoingEdge (Edge &e) { this ->outgoingEdges .insert (&e); }
158155
156+ void removeOutgoingEdge (Edge &e) {
157+ this ->outgoingEdges .erase (&e); }
158+
159159private:
160160 SetOfEdgeRefs outgoingEdges;
161+
161162};
162163
163164
164165// A set of states
165166// the set is assumed to have unique ownership of the states
166167class SetOfStates : public std ::map<CId, std::shared_ptr<State>> {
167168public:
168- using CIter = SetOfStates::const_iterator;
169- using Iter = SetOfStates::iterator;
170169 void remove (const State &s) { this ->erase (s.getId ()); }
171170 virtual ~SetOfStates () = default ;
171+ State& withId (const CId id) {
172+ return *this ->at (id);
173+ }
172174};
173175
174176struct StateRefCompareLessThan {
@@ -180,6 +182,7 @@ class SetOfStateRefs : public std::set<const State *, StateRefCompareLessThan> {
180182public:
181183 using CIter = SetOfEdgeRefs::const_iterator;
182184 bool includesState (const State *s) { return this ->find (s) != this ->end (); }
185+ virtual ~SetOfStateRefs () = default ;
183186};
184187
185188// forward declaration of reachable states strategy
@@ -359,20 +362,25 @@ template <typename StateLabelType, typename EdgeLabelType> using StateRef = cons
359362template <typename StateLabelType, typename EdgeLabelType>
360363class SetOfStates : public Abstract ::SetOfStates {
361364private:
362- std::map<StateLabelType, StateRef<StateLabelType, EdgeLabelType>> stateIndex;
365+ std::map<StateLabelType, std::shared_ptr<State<StateLabelType, EdgeLabelType>>> labelIndex;
366+
367+ void addToStateIndex (StateLabelType l, std::shared_ptr<State<StateLabelType, EdgeLabelType>> s) {
368+ this ->labelIndex [l] = s;
369+ }
363370
364371public:
365- using CIter = typename SetOfStates::const_iterator;
366- StateRef <StateLabelType, EdgeLabelType> withLabel (StateLabelType l) {
367- if (this ->stateIndex .find (l) != this ->stateIndex .end ()) {
368- return this ->stateIndex [l];
372+
373+ State <StateLabelType, EdgeLabelType>& withLabel (StateLabelType l) {
374+ if (this ->labelIndex .find (l) != this ->labelIndex .end ()) {
375+ return * this ->labelIndex [l];
369376 }
370- return nullptr ;
377+ throw CException ( " error - state not found in FiniteStateMachine::_withLabel " ) ;
371378 }
372379
373- void addToStateIndex (StateLabelType l, State<StateLabelType, EdgeLabelType> * s) {
374- this ->stateIndex [l] = s ;
380+ void addState (std::shared_ptr< State<StateLabelType, EdgeLabelType>>& s) {
381+ this ->addToStateIndex (s-> getLabel (), s) ;
375382 }
383+
376384};
377385
378386template <typename StateLabelType, typename EdgeLabelType>
@@ -430,27 +438,9 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
430438 SetOfStateRefs<StateLabelType, EdgeLabelType> initialStates;
431439 SetOfStateRefs<StateLabelType, EdgeLabelType> finalStates;
432440
433- State<StateLabelType, EdgeLabelType> _getStateLabeled (const StateLabelType &s) {
441+ State<StateLabelType, EdgeLabelType>& _getStateLabeled (const StateLabelType &s) {
434442 // try the index first
435- StateRef<StateLabelType, EdgeLabelType> sp = this ->states .withLabel (s);
436- if (sp != nullptr ) {
437- return sp;
438- }
439- // for now just a linear search
440- // TODO: use index?
441- for (auto &it : this ->states ) {
442- auto &i = *(it.second );
443- auto &t = dynamic_cast <const State<StateLabelType, EdgeLabelType> &>(i);
444- // TODO: remove const_cast set of states provides only const iterator, but states are
445- // identified only by ID.
446- auto &ct = const_cast <State<StateLabelType, EdgeLabelType> &>(t);
447- if ((ct.stateLabel ) == s) {
448- // TODO: manage index inside SetOfStates
449- this ->states .addToStateIndex (s, &ct);
450- return &ct;
451- }
452- }
453- throw CException (" error - state not found in FiniteStateMachine::getStateLabeled" );
443+ return this ->states .withLabel (s);
454444 };
455445
456446
@@ -469,12 +459,11 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
469459 }
470460
471461 // add state with the given label
472- State <StateLabelType, EdgeLabelType> * addState (StateLabelType label) {
462+ StateRef <StateLabelType, EdgeLabelType> addState (StateLabelType label) {
473463 bool added = false ;
474- typename SetOfStates<StateLabelType, EdgeLabelType>::CIter i;
475464 auto sp = std::make_shared<State<StateLabelType, EdgeLabelType>>(label);
476465 auto &s = *sp;
477- this ->states [sp-> getId ()] = std::move (sp);
466+ this ->states . addState (sp);
478467 return &s;
479468 };
480469
@@ -483,9 +472,9 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
483472 const State<StateLabelType, EdgeLabelType> &dst) {
484473 // lookup state again to drop const qualifier
485474 auto &mySrc =
486- dynamic_cast <State<StateLabelType, EdgeLabelType> &>(*( this ->states [ src.getId ()] ));
475+ dynamic_cast <State<StateLabelType, EdgeLabelType> &>(this ->states . withId ( src.getId ()));
487476 auto &myDst =
488- dynamic_cast <State<StateLabelType, EdgeLabelType> &>(*( this ->states [ dst.getId ()] ));
477+ dynamic_cast <State<StateLabelType, EdgeLabelType> &>(this ->states . withId ( dst.getId ()));
489478 bool added = false ;
490479 auto ep = std::make_shared<Edge<StateLabelType, EdgeLabelType>>(mySrc, lbl, myDst);
491480 auto &e = *ep;
@@ -497,8 +486,8 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
497486 void removeEdge (const Edge<StateLabelType, EdgeLabelType> &e) {
498487 auto csrc = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(e.getSource ());
499488 // get a non-const version of the state
500- auto src = this ->getStateLabeled (csrc->getLabel ());
501- src-> removeOutgoingEdge (e);
489+ auto & src = this ->_getStateLabeled (csrc->getLabel ());
490+ src. removeOutgoingEdge (e);
502491 this ->edges .remove (e);
503492 }
504493
@@ -532,7 +521,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
532521
533522 void addInitialState (const State<StateLabelType, EdgeLabelType> &s) {
534523 // we are assuming s is one of our states
535- this ->initialStates .emplace (s. getId (), &s);
524+ this ->initialStates .insert ( &s);
536525 };
537526
538527 void addFinalState (const State<StateLabelType, EdgeLabelType> &s) {
@@ -558,26 +547,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
558547 };
559548
560549 const StateRef<StateLabelType, EdgeLabelType> getStateLabeled (const StateLabelType &s) {
561- // try the index first
562- StateRef<StateLabelType, EdgeLabelType> sp = this ->states .withLabel (s);
563- if (sp != nullptr ) {
564- return sp;
565- }
566- // for now just a linear search
567- // TODO: use index?
568- for (auto &it : this ->states ) {
569- auto &i = *(it.second );
570- auto &t = dynamic_cast <const State<StateLabelType, EdgeLabelType> &>(i);
571- // TODO: remove const_cast set of states provides only const iterator, but states are
572- // identified only by ID.
573- auto &ct = const_cast <State<StateLabelType, EdgeLabelType> &>(t);
574- if ((ct.stateLabel ) == s) {
575- // TODO: manage index inside SetOfStates
576- this ->states .addToStateIndex (s, &ct);
577- return &ct;
578- }
579- }
580- throw CException (" error - state not found in FiniteStateMachine::getStateLabeled" );
550+ return &(this ->_getStateLabeled (s));
581551 };
582552
583553 bool hasStateLabeled (const StateLabelType &s) {
0 commit comments