summaryrefslogtreecommitdiff
path: root/dnn/Automaton.cc
diff options
context:
space:
mode:
Diffstat (limited to 'dnn/Automaton.cc')
-rwxr-xr-xdnn/Automaton.cc99
1 files changed, 99 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;
+}