Hash :
3d8d7728
Author :
Thomas de Grivel
Date :
2023-03-23T17:19:58
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
/* c3
* Copyright 2022,2023 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 <assert.h>
#include <stdlib.h>
#include "compare.h"
#include "fact.h"
#include "facts_spec.h"
#include "facts_spec_cursor.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 = calloc(count * 4 + 1, sizeof(s_tag *));
p_facts_spec 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_sort (p_facts_spec spec)
{
s_tag **a;
s_tag **b;
uw count;
uw i = 0;
uw j;
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;
if (compare_fact_unbound_var_count((s_fact *) a,
(s_fact *) b) > 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;
}