/* * CENA C++ Utilities * * by Stephane Chatty * * Copyright 1993-1995 * Centre d'Etudes de la Navigation Aerienne (CENA) * * Automata. * * $Id$ * $CurLog$ */ #include "Automaton.h" IvlBaseLink :: IvlBaseLink (IvlBaseState* s) : To (s) { } IvlBaseLink :: ~IvlBaseLink () { } IvlBaseState :: IvlBaseState (const char* name, IvlBaseAutomaton& a) : Name (name), Links (a.Size, a.Hash, a.Copy, a.Delete, a.Compare), TheAutomaton (&a) { } IvlBaseState :: ~IvlBaseState () { IvlHashIterOf l = Links; while (++l) delete *l; } #if 0 void IvlBaseState :: CreateLink (IvlBaseState* to, IvlKey* k) { IvlHashCellOf * c = Links.Add (k); c->SetInfo (new IvlBaseLink (to)); } #endif #if 0 void IvlBaseState :: Add (IvlKey* key, IvlBaseLink* l) { IvlHashCellOf * c = Links.Add (key); c->SetInfo (l); } #endif IvlBaseState* IvlBaseState :: Next (IvlToken* tk, bool peek) { IvlKey* k = TheAutomaton->GetKey ? (TheAutomaton->GetKey) (tk) : tk; IvlHashCellOf * c = Links.Get (k); if (!c) return 0; IvlBaseLink* l = c->GetInfo (); IvlBaseState* to = l->GetDestination (); if (!peek) { Out (); l->Fire (this, to, tk); to->In (); } return to; } IvlBaseAutomaton :: IvlBaseAutomaton (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 () { } IvlBaseAutomaton :: ~IvlBaseAutomaton () { IvlListIterOf s = AllStates; while (++s) delete *s; } #if 0 IvlBaseState* IvlBaseAutomaton :: CreateState (const char* n); { IvlBaseState* s = new IvlBaseState (n, *this); AllStates.Append (s); return s; } #endif IvlLink :: IvlLink (IvlState* s, IvlLinkFun fn) : IvlBaseLink (s), Fun (fn) { } IvlLink :: ~IvlLink () { } void IvlLink :: Fire (IvlBaseState* from, IvlBaseState* to, IvlToken* data) { if (Fun) (*Fun) ((IvlState*) from, (IvlState*) to, data); } IvlState :: IvlState (const char* name, IvlAutomaton& a, IvlStateFun* in, IvlStateFun* out) : IvlBaseState (name, a), InFun (in), OutFun (out) { } IvlState :: ~IvlState () { } void IvlState :: In () { if (InFun) (*InFun) (this); } void IvlState :: Out () { if (OutFun) (*OutFun) (this); } void IvlState :: CreateLink (IvlState* to, IvlKey* k, IvlLinkFun fn) { IvlHashCellOf * c = Links.Add (k); c->SetInfo (new IvlLink (to, fn)); } IvlState* IvlAutomaton :: CreateState (const char* n, IvlStateFun in, IvlStateFun out) { IvlState* s = new IvlState (n, *this, in, out); AllStates.Append (s); return s; } IvlAutomIter :: IvlAutomIter (IvlBaseAutomaton& a) : TheAutomaton (&a), CurState (a.GetInitial ()) { } IvlAutomIter :: ~IvlAutomIter () { } bool IvlAutomIter :: Step (IvlToken* tk) { if (!CurState && !(CurState = TheAutomaton->GetInitial ())) return false; IvlBaseState* n = CurState->Next (tk); if (!n) return false; CurState = n; return true; }