From 959c8d2c8be5eaf64db0feebc2069f3e87d7bca6 Mon Sep 17 00:00:00 2001 From: ccremers Date: Thu, 26 Aug 2004 12:36:01 +0000 Subject: [PATCH] - Added termlist_to_tuple function. --- src/termlist.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/termlist.h | 1 + 2 files changed, 52 insertions(+) diff --git a/src/termlist.c b/src/termlist.c index ae4f4e9..7aa04d9 100644 --- a/src/termlist.c +++ b/src/termlist.c @@ -790,3 +790,54 @@ termlist_iterate (Termlist tl, int (*func) ()) } return 1; } + +//! Create a tuple term from a termlist +Term termlist_to_tuple (Termlist tl) +{ + int width; + + width = termlistLength (tl); + if (width > 1) + { + // 2 parts + // Make two termlists for each side. + Term tresult; + Termlist tl1, tl2; + int split, i; + + /** + * This can be done much more efficiently by cutting + * the list temporarily, and reconnecting it afterwards. + */ + tl1 = NULL; + tl2 = NULL; + split = width / 2; + i = 0; + while (tl != NULL) + { + if (i < split) + tl1 = termlistAdd (tl1, tl->term); + else + tl2 = termlistAdd (tl2, tl->term); + tl = tl->next; + i++; + } + tresult = makeTermTuple (termlist_to_tuple (tl1), termlist_to_tuple (tl2)); + termlistDelete (tl1); + termlistDelete (tl2); + return tresult; + } + else + { + if (tl == NULL) + { + // W00t! Wtf? + error ("termlist_to_tuple called (internally?) with NULL"); + } + else + { + // Single node, simple + return termDuplicate(tl->term); + } + } +} diff --git a/src/termlist.h b/src/termlist.h index a20e5c5..dc917a4 100644 --- a/src/termlist.h +++ b/src/termlist.h @@ -55,5 +55,6 @@ Term termFunction (Termlist fromlist, Termlist tolist, Term tx); Termlist termlistForward (Termlist tl); int termlistOrder (Termlist tl1, Termlist tl2); int termlist_iterate (Termlist tl, int (*func) ()); +Term termlist_to_tuple (Termlist tl); #endif