From bf991aa993b50aca12f5e42306ca335e1fa2ac29 Mon Sep 17 00:00:00 2001 From: ccremers Date: Sat, 24 Jul 2004 19:00:43 +0000 Subject: [PATCH] - Buggy: Broken intermediate version. --- src/temmap.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/temmap.h | 31 +++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 src/temmap.c create mode 100644 src/temmap.h diff --git a/src/temmap.c b/src/temmap.c new file mode 100644 index 0000000..73031b1 --- /dev/null +++ b/src/temmap.c @@ -0,0 +1,123 @@ +#include +#include +#include "termmap.h" +#include "debug.h" +#include "memory.h" + +//! Open termmaps code. +void +termmapsInit (void) +{ + return; +} + +//! Close termmaps code. +void +termmapsDone (void) +{ + return; +} + +//! Allocate memory for a termmap node. +/** + *@return A pointer to uninitialised memory of the size of a termmap node. + */ +Termmap +makeTermmap (void) +{ + /* inline candidate */ + return (Termmap) memAlloc (sizeof (struct termmap)); +} + +//! Get function result +/** + *@return Yields f(x), or -1 when it is not present. + */ +int +termmapGet (Termmap f, const Term x) +{ + while (f != NULL) + { + if (isTermEqual (x, f->term)) + return f->result; + f = f->next; + } + return -1; +} + +//! Add a value to a function. +/** + *@return Adds f(x)=y to an existing function f. If f is NULL, a function is created. If x is already in the domain, the value is replaced. + */ +Termmap +termmapSet (const Termmap f, const Term x, const int y) +{ + Termmap fscan; + + //! Determine whether term already occurs + fscan = f; + while (fscan != NULL) + { + if (isTermEqual (x, fscan->term)) + { + //! Is the result correct already? + if (fscan->result != y) + fscan->result = y; + return f; + } + fscan = fscan->next; + } + //! Not occurred yet, make new node + fscan = makeTermmap (); + fscan->term = x; + fscan->result = y; + fscan->next = f; + return fscan; +} + +//! Duplicate a function +Termmap +termmapDuplicate (const Termmap f) +{ + if (f != NULL) + { + Termmap g; + + g = makeTermmap (); + g->term = f->term; + g->result = f->result; + g->next = termmapDuplicate (f->next); + return g; + } + else + { + return NULL; + } +} + +//! Delete a function +void +termmapDelete (const Termmap f) +{ + if (f != NULL) + { + termmapDelete (f->next); + memFree (f, sizeof (struct termmap)); + } +} + +//! Print a function +void termmapPrint (Termmap f) +{ + if (f != NULL) + { + printf ("\""); + termPrint (f->term); + printf ("\" -> %i", f->result); + if (f->next != NULL) + { + printf (", "); + termmapPrint (f->next); + } + } +} diff --git a/src/temmap.h b/src/temmap.h new file mode 100644 index 0000000..3506b8b --- /dev/null +++ b/src/temmap.h @@ -0,0 +1,31 @@ +#ifndef TERMMAPS +#define TERMMAPS + +#include "term.h" + +//! The function container for the term to integer function type. +/** + *\sa term + */ +struct termmap +{ + //! The term element for this node. + Term term; + //! Next node pointer or NULL for the last element of the function. + struct termmap *next; + //! Function result + int result; +}; + +//! Shorthand for termmap pointers. +typedef struct termmap *Termmap; + +void termmapsInit (void); +void termmapsDone (void); +int termmapGet (Termmap f, const Term x); +Termmap termmapSet (const Termmap f, const Term x, const int y); +Termmap termmapDuplicate (const Termmap f); +void termmapDelete (const Termmap f); +void termmapPrint (Termmap f); + +#endif