diff options
Diffstat (limited to 'utils/Automaton.cc')
-rw-r--r-- | utils/Automaton.cc | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/utils/Automaton.cc b/utils/Automaton.cc new file mode 100644 index 0000000..9643bdb --- /dev/null +++ b/utils/Automaton.cc @@ -0,0 +1,181 @@ +/* + * CENA C++ Utilities + * + * by Stephane Chatty + * + * Copyright 1993-1995 + * Centre d'Etudes de la Navigation Aerienne (CENA) + * + * Automata. + * + * $Id$ + * $CurLog$ + */ + +#include "Automaton.h" + +CcuBaseLink :: CcuBaseLink (CcuBaseState* s) +: To (s) +{ +} + +CcuBaseLink :: ~CcuBaseLink () +{ +} + +CcuBaseState :: CcuBaseState (const char* name, CcuBaseAutomaton& a) +: Name (name), + Links (a.Size, a.Hash, a.Copy, a.Delete, a.Compare), + TheAutomaton (&a) +{ +} + +CcuBaseState :: ~CcuBaseState () +{ + CcuHashIterOf <CcuBaseLink> l = Links; + while (++l) + delete *l; +} + +#if 0 +void +CcuBaseState :: CreateLink (CcuBaseState* to, CcuKey* k) +{ + CcuHashCellOf <CcuBaseLink>* c = Links.Add (k); + c->SetInfo (new CcuBaseLink (to)); +} +#endif + +#if 0 +void +CcuBaseState :: Add (CcuKey* key, CcuBaseLink* l) +{ + CcuHashCellOf <CcuLink>* c = Links.Add (key); + c->SetInfo (l); +} +#endif + +CcuBaseState* +CcuBaseState :: Next (CcuToken* tk, bool peek) +{ + CcuKey* k = TheAutomaton->GetKey ? (TheAutomaton->GetKey) (tk) : tk; + CcuHashCellOf <CcuBaseLink>* c = Links.Get (k); + if (!c) + return 0; + CcuBaseLink* l = c->GetInfo (); + CcuBaseState* to = l->GetDestination (); + + if (!peek) { + Out (); + l->Fire (this, to, tk); + to->In (); + } + return to; +} + +CcuBaseAutomaton :: CcuBaseAutomaton (AKEY_F gk, HASH_F hash, HCP_F cp, HDEL_F del, HCMP_F cmp, int sz) +: Size (sz), + Hash (hash), + Copy (cp), + Delete (del), + Compare (cmp), + GetKey (gk), + Initial (0), + AllStates () +{ +} + +CcuBaseAutomaton :: ~CcuBaseAutomaton () +{ + CcuListIterOf <CcuBaseState> s = AllStates; + while (++s) + delete *s; +} + +#if 0 +CcuBaseState* +CcuBaseAutomaton :: CreateState (const char* n); +{ + CcuBaseState* s = new CcuBaseState (n, *this); + AllStates.Append (s); + return s; +} +#endif + + +CcuLink :: CcuLink (CcuState* s, CcuLinkFun fn) +: CcuBaseLink (s), + Fun (fn) +{ +} + +CcuLink :: ~CcuLink () +{ +} + +void +CcuLink :: Fire (CcuBaseState* from, CcuBaseState* to, CcuToken* data) +{ + if (Fun) + (*Fun) ((CcuState*) from, (CcuState*) to, data); +} + +CcuState :: CcuState (const char* name, CcuAutomaton& a, CcuStateFun* in, CcuStateFun* out) +: CcuBaseState (name, a), + InFun (in), + OutFun (out) +{ +} + +CcuState :: ~CcuState () +{ +} + +void +CcuState :: In () +{ + if (InFun) (*InFun) (this); +} + +void +CcuState :: Out () +{ + if (OutFun) (*OutFun) (this); +} + +void +CcuState :: CreateLink (CcuState* to, CcuKey* k, CcuLinkFun fn) +{ + CcuHashCellOf <CcuBaseLink>* c = Links.Add (k); + c->SetInfo (new CcuLink (to, fn)); +} + +CcuState* +CcuAutomaton :: CreateState (const char* n, CcuStateFun in, CcuStateFun out) +{ + CcuState* s = new CcuState (n, *this, in, out); + AllStates.Append (s); + return s; +} + +CcuAutomIter :: CcuAutomIter (CcuBaseAutomaton& a) +: TheAutomaton (&a), + CurState (a.GetInitial ()) +{ +} + +CcuAutomIter :: ~CcuAutomIter () +{ +} + +bool +CcuAutomIter :: Step (CcuToken* tk) +{ + if (!CurState && !(CurState = TheAutomaton->GetInitial ())) + return false; + CcuBaseState* n = CurState->Next (tk); + if (!n) + return false; + CurState = n; + return true; +} |