Hash :
ed2cf4e6
Author :
Thomas de Grivel
Date :
2024-08-21T18:57:05
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "alloc.h"
#include "assert.h"
#include "compare.h"
#include "fact.h"
#include "facts_spec.h"
#include "facts_spec_cursor.h"
#include "list.h"
#include "tag.h"
uw facts_spec_count_facts (p_facts_spec spec)
{
s_facts_spec_cursor cursor;
uw count = 0;
s_fact fact;
facts_spec_cursor_init(&cursor, spec);
while (facts_spec_cursor_next(&cursor, &fact))
count++;
return count;
}
p_facts_spec facts_spec_new_expand (p_facts_spec spec)
{
uw count;
assert(spec);
count = facts_spec_count_facts(spec);
if (count > 0) {
s_facts_spec_cursor cursor;
s_fact fact;
p_facts_spec new;
p_facts_spec n;
new = alloc((count * 4 + 1) * sizeof(s_tag *));
if (! new)
return NULL;
n = new;
facts_spec_cursor_init(&cursor, spec);
while (facts_spec_cursor_next(&cursor, &fact)) {
*n++ = (s_tag *) fact.subject;
*n++ = (s_tag *) fact.predicate;
*n++ = (s_tag *) fact.object;
*n++ = NULL;
}
*n = NULL;
return new;
}
return NULL;
}
p_facts_spec facts_spec_new_list (s_list *spec)
{
uw c;
uw count = 1;
p_facts_spec new;
p_facts_spec n;
s_list *s;
s_list *t;
assert(spec);
if (! spec)
return NULL;
s = spec;
while (s) {
if (s->tag.type != TAG_LIST) {
err_puts("facts_spec_new_list: invalid spec: not a List of List");
assert(! "facts_spec_new_list: invalid spec: not a List of List");
return NULL;
}
t = s->tag.data.list;
if ((c = list_length(t)) < 3) {
err_puts("facts_spec_new_list: invalid spec: list length < 3");
assert(! "facts_spec_new_list: invalid spec: list length < 3");
return NULL;
}
if ((c - 1) % 2) {
err_puts("facts_spec_new_list: invalid spec: list length"
" != 3 + 2 * n");
assert(! "facts_spec_new_list: invalid spec: list length"
" != 3 + 2 * n");
return NULL;
}
count += c + 1;
s = list_next(s);
}
new = alloc(count * sizeof(s_tag *));
if (! new)
return NULL;
n = new;
s = spec;
while (s) {
t = s->tag.data.list;
while (t) {
*n++ = &t->tag;
t = list_next(t);
}
*n++ = NULL;
s = list_next(s);
}
*n = NULL;
return new;
}
p_facts_spec facts_spec_sort (p_facts_spec spec)
{
s_tag **a;
s_tag **b;
uw count;
uw i = 0;
uw j;
s8 r;
assert(spec);
count = facts_spec_count_facts(spec);
if (count > 0)
while (i < count - 1) {
j = 0;
while (j < count - i - 1) {
a = spec + j * 4;
b = spec + (j + 1) * 4;
r = compare_fact_unbound_var_count((s_fact *) a,
(s_fact *) b);
if (r == COMPARE_ERROR)
return NULL;
if (r > 0) {
s_tag *swap[3];
swap[0] = a[0];
swap[1] = a[1];
swap[2] = a[2];
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
b[0] = swap[0];
b[1] = swap[1];
b[2] = swap[2];
}
j++;
}
i++;
}
return spec;
}