YAP 7.1.0
tab.rational.h
1/************************************************************************
2** **
3** The YapTab/YapOr/OPTYap systems **
4** **
5** YapTab extends the Yap Prolog engine to support sequential tabling **
6** YapOr extends the Yap Prolog engine to support or-parallelism **
7** OPTYap extends the Yap Prolog engine to support or-parallel tabling **
8** **
9** **
10** Yap Prolog was developed at University of Porto, Portugal **
11** **
12************************************************************************/
13
14#define RationalMark 7 //0m0...111
15#define IsRationalTerm(TERM) ((int) TERM == 7)
16
17typedef struct term_array {
18 void* *terms;
19 void* *nodes;
20 size_t length;
21 size_t capacity;
23
24void term_array_init(term_array *array, int capacity);
25void term_array_free(term_array *array);
26void term_array_push(term_array *array, void* t, void* n);
27void* term_array_member(term_array array, void* t);
28
29void term_array_init(term_array *array, int capacity) {
30 array->length = 0;
31 array->terms = malloc(capacity * sizeof(void*));
32 if (array->terms != NULL) {
33 array->capacity = capacity;
34 } else
35 Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "Out of memory."); // Handle out-of-memory
36 array->capacity = capacity;
37 array->nodes = malloc(capacity * sizeof(void*));
38 if (array->nodes == NULL)
39 Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "Out of memory."); // Handle out-of-memory
40}
41
42void term_array_free(term_array *array) {
43 free(array->terms);
44 free(array->nodes);
45 array->terms = NULL;
46 array->nodes = NULL;
47 array->length = 0;
48 array->capacity = 0;
49}
50
51void term_array_push(term_array *array, void* t, void* n) {
52 if (array->length == array->capacity) {
53 int new_capacity = array->capacity * 2;
54 void *new_terms = realloc(array->terms, new_capacity * sizeof(void*));
55 if (new_terms != NULL) {
56 array->terms = new_terms;
57 } else
58 Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "Out of memory."); // Handle out-of-memory
59 void *new_nodes = realloc(array->nodes, new_capacity * sizeof(void *));
60 if (new_nodes != NULL) {
61 array->nodes = new_nodes;
62 } else
63 Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "Out of memory."); // Handle out-of-memory
64 array->capacity = new_capacity;
65 }
66 array->terms[array->length] = t;
67 array->nodes[array->length] = n;
68 array->length++;
69}
70
71void* term_array_member(term_array array, void* t) {
72 int i;
73 for (i = 0; i < array.length; i++)
74 if (array.terms[i] == t) return array.nodes[i];
75 return NULL;
76}
77