From 4076d24022f667080015561d1104f0b8f17649ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Mon, 26 Jan 2015 07:36:54 +0000 Subject: [PATCH] editors prefer h --- OPTYap/tab.rational.h | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 OPTYap/tab.rational.h diff --git a/OPTYap/tab.rational.h b/OPTYap/tab.rational.h new file mode 100644 index 000000000..907137865 --- /dev/null +++ b/OPTYap/tab.rational.h @@ -0,0 +1,77 @@ +/************************************************************************ +** ** +** The YapTab/YapOr/OPTYap systems ** +** ** +** YapTab extends the Yap Prolog engine to support sequential tabling ** +** YapOr extends the Yap Prolog engine to support or-parallelism ** +** OPTYap extends the Yap Prolog engine to support or-parallel tabling ** +** ** +** ** +** Yap Prolog was developed at University of Porto, Portugal ** +** ** +************************************************************************/ + +#define RationalMark 7 //0m0...111 +#define IsRationalTerm(TERM) ((int) TERM == 7) + +typedef struct term_array { + void* *terms; + void* *nodes; + size_t length; + size_t capacity; +} term_array; + +void term_array_init(term_array *array, int capacity); +void term_array_free(term_array *array); +void term_array_push(term_array *array, void* t, void* n); +void* term_array_member(term_array array, void* t); + +void term_array_init(term_array *array, int capacity) { + array->length = 0; + array->terms = malloc(capacity * sizeof(void*)); + if (array->terms != NULL) { + array->capacity = capacity; + } else + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory + array->capacity = capacity; + array->nodes = malloc(capacity * sizeof(void*)); + if (array->nodes == NULL) + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory +} + +void term_array_free(term_array *array) { + free(array->terms); + free(array->nodes); + array->terms = NULL; + array->nodes = NULL; + array->length = 0; + array->capacity = 0; +} + +void term_array_push(term_array *array, void* t, void* n) { + if (array->length == array->capacity) { + int new_capacity = array->capacity * 2; + void *new_terms = realloc(array->terms, new_capacity * sizeof(void*)); + if (new_terms != NULL) { + array->terms = new_terms; + } else + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory + void *new_nodes = realloc(array->nodes, new_capacity * sizeof(void *)); + if (new_nodes != NULL) { + array->nodes = new_nodes; + } else + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory + array->capacity = new_capacity; + } + array->terms[array->length] = t; + array->nodes[array->length] = n; + array->length++; +} + +void* term_array_member(term_array array, void* t) { + int i; + for (i = 0; i < array.length; i++) + if (array.terms[i] == t) return array.nodes[i]; + return NULL; +} +