4141#ifndef MAXPLUS_BASE_FSM_FSM_H
4242#define MAXPLUS_BASE_FSM_FSM_H
4343
44- #include " maxplus/base/exception/exception.h"
4544#include " maxplus/base/basic_types.h"
45+ #include " maxplus/base/exception/exception.h"
4646#include " maxplus/base/string/cstring.h"
4747#include < list>
4848#include < map>
@@ -71,7 +71,7 @@ class WithUniqueID {
7171
7272// forward declaration of FSM state class
7373class State ;
74- using StateRef = const State*;
74+ using StateRef = const State *;
7575
7676// Edge of an FSM
7777class Edge : public WithUniqueID {
@@ -112,7 +112,7 @@ class SetOfEdges : public std::map<CId, std::shared_ptr<Edge>> {
112112 void remove (const Edge &e) { this ->erase (e.getId ()); }
113113};
114114
115- using EdgeRef = const Edge*;
115+ using EdgeRef = const Edge *;
116116
117117struct EdgeRefCompareLessThan {
118118 bool operator ()(EdgeRef lhs, EdgeRef rhs) const { return lhs->lessThan (*rhs); };
@@ -136,7 +136,7 @@ class State : public WithUniqueID {
136136
137137 State (const State &) = default ;
138138 State &operator =(const State &other) = delete ;
139- State (State &&) noexcept = default ;
139+ State (State &&) noexcept = default ;
140140 State &operator =(State &&) = delete ;
141141
142142 // add an outgoing edge to the state
@@ -152,29 +152,21 @@ class State : public WithUniqueID {
152152
153153 void insertOutgoingEdge (Edge &e) { this ->outgoingEdges .insert (&e); }
154154
155- void removeOutgoingEdge (EdgeRef e) {
156- this ->outgoingEdges .erase (e);
157- }
155+ void removeOutgoingEdge (EdgeRef e) { this ->outgoingEdges .erase (e); }
158156
159157private:
160158 SetOfEdgeRefs outgoingEdges;
161-
162159};
163160
164-
165161// A set of states
166162// the set is assumed to have unique ownership of the states
167163class SetOfStates : public std ::map<CId, std::shared_ptr<State>> {
168164public:
169165 void remove (const State &s) { this ->erase (s.getId ()); }
170166 virtual ~SetOfStates () = default ;
171- void addState (const std::shared_ptr<State> &s) {
172- this ->insert (std::make_pair (s->getId (), s));
173- }
167+ void addState (const std::shared_ptr<State> &s) { this ->insert (std::make_pair (s->getId (), s)); }
174168
175- State& withId (const CId id) {
176- return *this ->at (id);
177- }
169+ State &withId (const CId id) { return *this ->at (id); }
178170};
179171
180172struct StateRefCompareLessThan {
@@ -207,7 +199,6 @@ class FiniteStateMachine {
207199 [[nodiscard]] virtual StateRef getInitialState () const = 0;
208200 [[nodiscard]] virtual const SetOfStateRefs &getInitialStates () const = 0;
209201 [[nodiscard]] virtual const SetOfStateRefs &getFinalStates () const = 0;
210-
211202};
212203
213204//
@@ -347,16 +338,17 @@ template <typename StateLabelType, typename EdgeLabelType> class Edge : public A
347338 EdgeLabelType label;
348339};
349340
350- template <typename StateLabelType, typename EdgeLabelType> using EdgeRef = const Edge<StateLabelType, EdgeLabelType>*;
341+ template <typename StateLabelType, typename EdgeLabelType>
342+ using EdgeRef = const Edge<StateLabelType, EdgeLabelType> *;
351343
352344template <typename StateLabelType, typename EdgeLabelType>
353345class SetOfEdges : public Abstract ::SetOfEdges {
354346public:
355347 using CIter = typename Abstract::SetOfEdges::const_iterator;
356348};
357349
358- template <typename StateLabelType, typename EdgeLabelType> using StateRef = const State<StateLabelType,EdgeLabelType>*;
359-
350+ template <typename StateLabelType, typename EdgeLabelType>
351+ using StateRef = const State<StateLabelType, EdgeLabelType> *;
360352
361353// template <typename StateLabelType, typename EdgeLabelType>
362354// class SetOfEdgeRefs : public Abstract::SetOfEdgeRefs {
@@ -369,13 +361,13 @@ class SetOfStates : public Abstract::SetOfStates {
369361private:
370362 std::map<StateLabelType, std::shared_ptr<State<StateLabelType, EdgeLabelType>>> labelIndex;
371363
372- void addToStateIndex (StateLabelType l, std::shared_ptr<State<StateLabelType, EdgeLabelType>> s) {
364+ void addToStateIndex (StateLabelType l,
365+ std::shared_ptr<State<StateLabelType, EdgeLabelType>> s) {
373366 this ->labelIndex [l] = s;
374367 }
375368
376369public:
377-
378- State<StateLabelType, EdgeLabelType>& withLabel (StateLabelType l) {
370+ State<StateLabelType, EdgeLabelType> &withLabel (StateLabelType l) {
379371 if (this ->labelIndex .find (l) != this ->labelIndex .end ()) {
380372 return *this ->labelIndex [l];
381373 }
@@ -386,11 +378,10 @@ class SetOfStates : public Abstract::SetOfStates {
386378 return this ->labelIndex .find (l) != this ->labelIndex .end ();
387379 }
388380
389- void addState (std::shared_ptr<State<StateLabelType, EdgeLabelType>>& s) {
381+ void addState (std::shared_ptr<State<StateLabelType, EdgeLabelType>> & s) {
390382 Abstract::SetOfStates::addState (std::dynamic_pointer_cast<Abstract::State>(s));
391383 this ->addToStateIndex (s->getLabel (), s);
392384 }
393-
394385};
395386
396387template <typename StateLabelType, typename EdgeLabelType>
@@ -447,17 +438,16 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
447438 SetOfStateRefs<StateLabelType, EdgeLabelType> initialStates;
448439 SetOfStateRefs<StateLabelType, EdgeLabelType> finalStates;
449440
450- State<StateLabelType, EdgeLabelType>& _getStateLabeled (const StateLabelType &s) {
441+ State<StateLabelType, EdgeLabelType> & _getStateLabeled (const StateLabelType &s) {
451442 return this ->states .withLabel (s);
452443 };
453444
454- State<StateLabelType, EdgeLabelType>& _getState (const State<StateLabelType, EdgeLabelType> &s) {
455- return dynamic_cast <State<StateLabelType, EdgeLabelType>&>(this ->states .withId (s.getId ()));
445+ State<StateLabelType, EdgeLabelType> & _getState (const State<StateLabelType, EdgeLabelType> &s) {
446+ return dynamic_cast <State<StateLabelType, EdgeLabelType> &>(this ->states .withId (s.getId ()));
456447 };
457448
458-
459449public:
460- FiniteStateMachine () : Abstract::FiniteStateMachine() {};
450+ FiniteStateMachine () : Abstract::FiniteStateMachine(){};
461451
462452 ~FiniteStateMachine () override = default ;
463453
@@ -483,10 +473,10 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
483473 EdgeLabelType lbl,
484474 const State<StateLabelType, EdgeLabelType> &dst) {
485475 // lookup state again to drop const qualifier
486- auto &mySrc =
487- dynamic_cast <State<StateLabelType, EdgeLabelType> &>( this ->states .withId (src.getId ()));
488- auto &myDst =
489- dynamic_cast <State<StateLabelType, EdgeLabelType> &>( this ->states .withId (dst.getId ()));
476+ auto &mySrc = dynamic_cast <State<StateLabelType, EdgeLabelType> &>(
477+ this ->states .withId (src.getId ()));
478+ auto &myDst = dynamic_cast <State<StateLabelType, EdgeLabelType> &>(
479+ this ->states .withId (dst.getId ()));
490480 bool added = false ;
491481 auto ep = std::make_shared<Edge<StateLabelType, EdgeLabelType>>(mySrc, lbl, myDst);
492482 auto &e = *ep;
@@ -498,7 +488,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
498488 void removeEdge (const Edge<StateLabelType, EdgeLabelType> &e) {
499489 auto csrc = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(e.getSource ());
500490 // get a non-const version of the state
501- auto & src = this ->_getState (*csrc);
491+ auto & src = this ->_getState (*csrc);
502492 src.removeOutgoingEdge (&e);
503493 this ->edges .remove (e);
504494 }
@@ -541,7 +531,6 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
541531 this ->finalStates .insert (&s);
542532 };
543533
544-
545534 [[nodiscard]] StateRef<StateLabelType, EdgeLabelType> getInitialState () const override {
546535 if (this ->initialStates .empty ()) {
547536 throw MaxPlus::MPException (" FSM has no initial state." );
@@ -584,7 +573,9 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
584573 return false ;
585574 };
586575
587- [[nodiscard]] const SetOfStates<StateLabelType, EdgeLabelType> &getStates () const { return this ->states ; };
576+ [[nodiscard]] const SetOfStates<StateLabelType, EdgeLabelType> &getStates () const {
577+ return this ->states ;
578+ };
588579 Abstract::SetOfStateRefs getStateRefs () {
589580 Abstract::SetOfStateRefs result;
590581 for (auto i : this ->states ) {
@@ -643,7 +634,8 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
643634
644635 for (const auto &it : outgoingEdges) {
645636 auto e = dynamic_cast <const Edge<StateLabelType, EdgeLabelType> *>(it);
646- auto dstState = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(e->getDestination ());
637+ auto dstState = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(
638+ e->getDestination ());
647639 if (e->getLabel () == lbl && dstState->getLabel () == dst) {
648640 return e;
649641 }
@@ -957,7 +949,8 @@ namespace StateStringLabeled {
957949
958950class State : public Labeled ::State<MaxPlus::MPString, char > {
959951public:
960- explicit State (const MaxPlus::MPString &withLabel) : Labeled::State<MaxPlus::MPString, char>(withLabel) {}
952+ explicit State (const MaxPlus::MPString &withLabel) :
953+ Labeled::State<MaxPlus::MPString, char>(withLabel) {}
961954
962955 const Abstract::SetOfEdgeRefs &getOutgoingEdges () {
963956 return dynamic_cast <const Abstract::SetOfEdgeRefs &>(
@@ -968,7 +961,8 @@ class State : public Labeled::State<MaxPlus::MPString, char> {
968961class FiniteStateMachine : public Labeled ::FiniteStateMachine<MaxPlus::MPString, char > {
969962public:
970963 [[nodiscard]] Labeled::StateRef<MaxPlus::MPString, char > getInitialState () const override {
971- return dynamic_cast <Labeled::StateRef<MaxPlus::MPString, char >>(Labeled::FiniteStateMachine<MaxPlus::MPString, char >::getInitialState ());
964+ return dynamic_cast <Labeled::StateRef<MaxPlus::MPString, char >>(
965+ Labeled::FiniteStateMachine<MaxPlus::MPString, char >::getInitialState ());
972966 };
973967
974968 void setInitialStateLabeled (const MaxPlus::MPString &sl);
0 commit comments