summaryrefslogtreecommitdiff
blob: 0f4043e5b10799d41674100e94f5430f954a4811 (plain)
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
#include <stdlib.h>
#include <string.h>

#include "../io/error.h"
#include "../tool/sorted_list.h"

#include "knowledge.h"

static int cmp_seq_link
(
   const void * const a,
   const void * const b,
   const void * const other
)
{
   ZoO_index j;
   ZoO_index * sequence;
   struct ZoO_knowledge_link * link;

   sequence = (ZoO_index *) a;
   link = (struct ZoO_knowledge_link *) b;

   for (j = 0; j < ZoO_SEQUENCE_SIZE; ++j)
   {
      if (sequence[j] < link->sequence[j])
      {
         return -1;
      }
      else if (sequence[j] > link->sequence[j])
      {
         return 1;
      }
   }

   return 0;
}

int ZoO_knowledge_find_link
(
   ZoO_index const links_count,
   struct ZoO_knowledge_link links [const],
   ZoO_index const sequence [const restrict static ZoO_SEQUENCE_SIZE],
   ZoO_index result [const restrict static 1]
)
{
   return
      ZoO_sorted_list_index_of
      (
         links_count,
         (void const *) links,
         (void const *) sequence,
         sizeof(struct ZoO_knowledge_link),
         cmp_seq_link,
         (void const *) NULL,
         result
      );
}

int ZoO_knowledge_get_link
(
   ZoO_index links_count [const],
   struct ZoO_knowledge_link * links [const],
   ZoO_index const sequence [const restrict static ZoO_SEQUENCE_SIZE],
   ZoO_index result [const restrict static 1]
)
{
   struct ZoO_knowledge_link * new_p;

   if
   (
      ZoO_sorted_list_index_of
      (
         *links_count,
         (void const *) *links,
         (void const *) sequence,
         sizeof(struct ZoO_knowledge_link),
         cmp_seq_link,
         (void const *) NULL,
         result
      ) == 0
   )
   {
      return 0;
   }

   *links_count += 1;

   new_p =
      (struct ZoO_knowledge_link *) realloc
      (
         (void *) *links,
         (sizeof(struct ZoO_knowledge_link) * (*links_count))
      );

   if (new_p == (struct ZoO_knowledge_link *) NULL)
   {
      *links_count -= 1;

      return -1;
   }

   if (*result < (*links_count - 1))
   {
      memmove(
         (void *) (new_p + *result + 1),
         (const void *) (new_p + *result),
         (sizeof(struct ZoO_knowledge_link) * (*links_count - 1 - *result))
      );
   }

   *links = new_p;

   new_p += *result;

   memcpy
   (
      (void *) new_p->sequence,
      (void const *) sequence,
      /* can be zero */
      (sizeof(ZoO_index) * ZoO_SEQUENCE_SIZE)
   );

   new_p->occurrences = 0;
   new_p->targets_count = 0;
   new_p->targets_occurrences = (ZoO_index *) NULL;
   new_p->targets = (ZoO_index *) NULL;

   return 0;
}