diff options
-rwxr-xr-x | dnn/Automaton.cc | 99 | ||||
-rwxr-xr-x | dnn/Automaton.h | 81 |
2 files changed, 180 insertions, 0 deletions
diff --git a/dnn/Automaton.cc b/dnn/Automaton.cc new file mode 100755 index 0000000..558a0ba --- /dev/null +++ b/dnn/Automaton.cc @@ -0,0 +1,99 @@ +/* + * DNN - Data News Network + * + * by Stephane Chatty + * + * Copyright 1993-1995 + * Centre d'Etudes de la Navigation Aerienne (CENA) + * + * Automata. + * + * $Id$ + * $CurLog$ + */ + +#include "Automaton.h" + +DnnState :: DnnState (const char* name, int sz, DnnStateFun* in, DnnStateFun* out) +: Name (name), + InFun (in), + OutFun (out), + Links (sz) +{ +} + +DnnState :: ~DnnState () +{ + CcuHashIterOf <DnnLink> l = Links; + while (++l) + delete *l; +} + +void +DnnState :: CreateLink (DnnState* to, void* k, DnnLinkFun fn) +{ + CcuHashCellOf <DnnLink>* c = Links.Add (k); + c->SetInfo (new DnnLink (to, fn)); +} + +DnnState* +DnnState :: Next (void* k, bool peek) +{ + CcuHashCellOf <DnnLink>* c = Links.Get (k); + if (!c) + return 0; + DnnLink* l = c->GetInfo (); + DnnState* to = l->To; + if (!peek) { + if (OutFun) + (*OutFun) (this); + if (l->Fun) + l->Fun (this, to, k); + if (to->InFun) + (*to->InFun) (to); + } + return to; +} + +DnnAutomaton :: DnnAutomaton (int sz) +: Initial (0), + Size (sz) +{ +} + +DnnAutomaton :: ~DnnAutomaton () +{ + CcuListIterOf <DnnState> s = AllStates; + while (++s) + delete *s; +} + +DnnState* +DnnAutomaton :: CreateState (const char* n, DnnStateFun in, DnnStateFun out) +{ + DnnState* s = new DnnState (n, Size, in, out); + AllStates.Append (s); + return s; +} + +DnnAutomIter :: DnnAutomIter (DnnAutomaton& a) +: TheAutomaton (&a), + CurState (a.GetInitial ()) +{ +} + +DnnAutomIter :: ~DnnAutomIter () +{ +} + +bool +DnnAutomIter :: Step (void* k) +{ + if (!CurState && !(CurState = TheAutomaton->GetInitial ())) + return false; + DnnState* n = CurState->Next (k); + if (!n) + return false; + CurState = n; + return true; +} diff --git a/dnn/Automaton.h b/dnn/Automaton.h new file mode 100755 index 0000000..3cb78ba --- /dev/null +++ b/dnn/Automaton.h @@ -0,0 +1,81 @@ +/* + * DNN - Data News Network + * + * by Stephane Chatty + * + * Copyright 1993-1995 + * Centre d'Etudes de la Navigation Aerienne (CENA) + * + * Automata. + * + * $Id$ + * $CurLog$ + */ + +#ifndef DnnAutomaton_H_ +#define DnnAutomaton_H_ + +#include "cplus_bugs.h" +#include "ccu/List.h" +#include "ccu/HashTable.h" +#include "ccu/String.h" +#include "ccu/bool.h" + +class DnnState; + +typedef void (DnnStateFun) (DnnState*); +typedef void (DnnLinkFun) (DnnState*, DnnState*, void*); + +class DnnLink { +public: + DnnState* To; + DnnLinkFun* Fun; +inline DnnLink (DnnState* to, DnnLinkFun* fn) : To (to), Fun (fn) {} +inline ~DnnLink () {} +}; + + +class DnnState { +protected: + CcuString Name; + DnnStateFun* InFun; + DnnStateFun* OutFun; + CcuHashTableOf <DnnLink> Links; + +inline void In () { if (InFun) (*InFun) (this); } +inline void Out () { if (OutFun) (*OutFun) (this); } + +public: + DnnState (const char*, int sz, DnnStateFun* = 0, DnnStateFun* = 0); + ~DnnState (); + void CreateLink (DnnState*, void*, DnnLinkFun); + DnnState* Next (void*, bool = false); +}; + +class DnnAutomaton { +protected: + int Size; + DnnState* Initial; + CcuListOf <DnnState> AllStates; +public: + DnnAutomaton (int sz = 16); + ~DnnAutomaton (); +inline DnnState* GetInitial () { return Initial; } +inline void SetInitial (DnnState* s) { Initial = s; } + DnnState* CreateState (const char* = 0, DnnStateFun = 0, DnnStateFun = 0); + void CreateLink (DnnState*, DnnState*, void*, DnnLinkFun); +}; + +class DnnAutomIter { +protected: + DnnAutomaton* TheAutomaton; + DnnState* CurState; +public: + DnnAutomIter (DnnAutomaton&); + ~DnnAutomIter (); + bool Step (void*); +}; + + + +#endif /* DnnAutomaton_H_ */ |