| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src/knowledge')
| -rw-r--r-- | src/knowledge/knowledge.c | 19 | ||||
| -rw-r--r-- | src/knowledge/knowledge.h | 44 | ||||
| -rw-r--r-- | src/knowledge/knowledge_learn_markov_sequence.c | 398 | ||||
| -rw-r--r-- | src/knowledge/knowledge_learn_sequence.c | 318 | ||||
| -rw-r--r-- | src/knowledge/knowledge_learn_word.c | 34 | ||||
| -rw-r--r-- | src/knowledge/knowledge_search.c | 17 | 
6 files changed, 527 insertions, 303 deletions
diff --git a/src/knowledge/knowledge.c b/src/knowledge/knowledge.c index a72969e..95683b9 100644 --- a/src/knowledge/knowledge.c +++ b/src/knowledge/knowledge.c @@ -2,7 +2,7 @@  #include <string.h>  #include <stdint.h> /* defines SIZE_MAX */ -#include "../cli/cli.h" +#include "../pipe/pipe.h"  #include "knowledge.h" @@ -19,3 +19,20 @@ void ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])     k->sequences_length = 0;     k->sequences_sorted = (ZoO_index *) NULL;  } + +int ZoO_knowledge_lock_access +( +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1] +) +{ +   return 0; +} + +void ZoO_knowledge_unlock_access +( +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1] +) +{ +} diff --git a/src/knowledge/knowledge.h b/src/knowledge/knowledge.h index 51d94c4..872bb94 100644 --- a/src/knowledge/knowledge.h +++ b/src/knowledge/knowledge.h @@ -4,9 +4,26 @@  #include "../core/char_types.h"  #include "../core/index_types.h" +#include "../pipe/pipe_types.h" +  #include "knowledge_types.h" -void ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]); +int ZoO_knowledge_lock_access +( +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1] +); + +void ZoO_knowledge_unlock_access +( +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1] +); + +void ZoO_knowledge_initialize +( +   struct ZoO_knowledge k [const restrict static 1] +);  void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1]); @@ -25,7 +42,8 @@ int ZoO_knowledge_learn_word     struct ZoO_knowledge k [const static 1],     const ZoO_char word [const restrict static 1],     const ZoO_index word_length, -   ZoO_index result [const restrict static 1] +   ZoO_index result [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  );  int ZoO_knowledge_learn_sequence @@ -33,7 +51,8 @@ int ZoO_knowledge_learn_sequence     struct ZoO_knowledge k [const restrict static 1],     const ZoO_index sequence [const restrict static 1],     const ZoO_index sequence_length, -   const ZoO_index markov_order +   const ZoO_index markov_order, +   const struct ZoO_pipe io [const restrict static 1]  );  int ZoO_knowledge_learn_markov_sequence @@ -41,7 +60,9 @@ int ZoO_knowledge_learn_markov_sequence     struct ZoO_knowledge k [const restrict static 1],     const ZoO_index sequence [const restrict static 1],     const ZoO_index sequence_length, -   const ZoO_index markov_order +   const ZoO_index markov_order, +   ZoO_index sequence_id [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  );  int ZoO_knowledge_get_following_sequences_ref @@ -50,14 +71,16 @@ int ZoO_knowledge_get_following_sequences_ref     const ZoO_index initial_word,     const ZoO_index * restrict following_sequences_ref [const restrict static 1],     const ZoO_index * restrict following_sequences_weights [const restrict static 1], -   ZoO_index following_sequences_weights_sum [const static 1] +   ZoO_index following_sequences_weights_sum [const static 1], +   const struct ZoO_pipe io [const restrict static 1]  );  int ZoO_knowledge_get_sequence  (     const struct ZoO_knowledge k [const static 1],     const ZoO_index sequences_ref, -   const ZoO_index * restrict sequence [const restrict static 1] +   const ZoO_index * restrict sequence [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  );  int ZoO_knowledge_get_word @@ -65,7 +88,8 @@ int ZoO_knowledge_get_word     const struct ZoO_knowledge k [const static 1],     const ZoO_index word_ref,     const ZoO_char * word [const restrict static 1], -   size_t word_size [const restrict static 1] +   size_t word_size [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  );  /* @@ -93,7 +117,8 @@ int ZoO_knowledge_find_preceding_words     const ZoO_index markov_order,     const ZoO_index * restrict preceding_words [const restrict static 1],     const ZoO_index * restrict preceding_words_weights [const restrict static 1], -   ZoO_index preceding_words_weights_sum [const restrict static 1] +   ZoO_index preceding_words_weights_sum [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  );  int ZoO_knowledge_find_following_words @@ -104,7 +129,8 @@ int ZoO_knowledge_find_following_words     const ZoO_index markov_order,     const ZoO_index * restrict following_words [const restrict static 1],     const ZoO_index * restrict following_words_weights [const restrict static 1], -   ZoO_index following_words_weights_sum [const restrict static 1] +   ZoO_index following_words_weights_sum [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  );  #endif diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c new file mode 100644 index 0000000..ec71254 --- /dev/null +++ b/src/knowledge/knowledge_learn_markov_sequence.c @@ -0,0 +1,398 @@ +#include <stdlib.h> +#include <string.h> +#include <stdint.h> /* defines SIZE_MAX */ + +#include "../core/sequence.h" + +#include "../pipe/pipe.h" + +#include "knowledge.h" + +/******************************************************************************/ +/** INITIALIZE ****************************************************************/ +/******************************************************************************/ +static void set_nth_sequence +( +   struct ZoO_knowledge k [const restrict static 1], +   const ZoO_index sorted_sequence_id, +   const ZoO_index sequence_id +) +{ +   /* Safe: (> k->sequences_length 1) */ +   if (sorted_sequence_id < (k->sequences_length - 1)) +   { +      memmove +      ( +         /* Safe: (=< (+ sorted_sequence_id 1) k->sequences_length) */ +         (void *) (k->sequences_sorted + (sorted_sequence_id + 1)), +         (const void *) (k->sequences_sorted + sorted_sequence_id), +         ((k->sequences_length - 1) - sorted_sequence_id) +      ); +   } + +   k->sequences_sorted[sorted_sequence_id] = sequence_id; +} + +/******************************************************************************/ +/** ALLOCATING MEMORY *********************************************************/ +/******************************************************************************/ +static int reallocate_sequences_list +( +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1] +) +{ +   ZoO_index ** new_sequences; + +   if ((SIZE_MAX / sizeof(ZoO_index *)) > (size_t) k->sequences_length) +   { +      ZoO_S_ERROR +      ( +         io, +         "Unable to store the size of the sequences list, as it would overflow" +         "size_t variables." +      ); + +      return -1; +   } + +   new_sequences = +      (ZoO_index **) realloc +      ( +         (void *) k->sequences, +         (((size_t) k->sequences_length) * sizeof(ZoO_index *)) +      ); + +   if (new_sequences == (ZoO_index **) NULL) +   { +      ZoO_S_ERROR +      ( +         io, +         "Unable to allocate the memory required for the new sequence list." +      ); + +      return -1; +   } + +   k->sequences = new_sequences; + +   return 0; +} + +static int reallocate_sequences_sorted_list +( +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1] +) +{ +   ZoO_index * new_sequences_sorted; + +   if ((SIZE_MAX / sizeof(ZoO_index)) > (size_t) k->sequences_length) +   { +      ZoO_S_ERROR +      ( +         io, +         "Unable to store the size of the sorted sequences list, as it would" +         " overflow size_t variables." +      ); + +      return -1; +   } + +   new_sequences_sorted = +      (ZoO_index *) realloc +      ( +         (void *) k->sequences_sorted, +         ((size_t) k->sequences_length) * sizeof(ZoO_index) +      ); + +   if (new_sequences_sorted == (ZoO_index *) NULL) +   { +      ZoO_S_ERROR +      ( +         io, +         "Unable to allocate the memory required for the new sorted sequences" +         " list." +      ); + +      return -1; +   } + +   k->sequences_sorted = new_sequences_sorted; + +   return 0; +} + +/* Pre: (=< ZoO_INDEX_MAX SIZE_MAX) */ +/*@ +   requires +   ( +      (base_length == destination_length) +      || +      ( +         (base_length < destination_length) +         && +         ( +           (base[0] == ZoO_START_OF_SEQUENCE) +           (base[base_length - 1] == ZoO_END_OF_SEQUENCE) +         ) +      ) +   ); +@*/ +static ZoO_index * copy_sequence +( +   const ZoO_index base [const restrict static 1], +   const ZoO_index base_length, +   const ZoO_index destination_length, +   const struct ZoO_pipe io [const restrict static 1] +) +{ +   ZoO_index i, diff; +   ZoO_index * result; + +   result = +      (ZoO_index *) calloc +      ( +         (size_t) (destination_length), +         sizeof(ZoO_index) +      ); + +   if (result == (ZoO_index *) NULL) +   { +      ZoO_S_ERROR +      ( +         io, +         "Unable to allocate the memory required to store a new sequence." +      ); + +      return (ZoO_index *) NULL; +   } + +   if (base_length == destination_length) +   { +      memcpy +      ( +         (void *) result, +         (const void *) base, +         (((size_t) base_length) * sizeof(ZoO_index)) +      ); +   } +   else if (base[0] == ZoO_START_OF_SEQUENCE_ID) +   { +      diff = (destination_length - base_length); + +      memcpy +      ( +         (void *) (result + diff), +         (const void *) base, +         (((size_t) base_length) * sizeof(ZoO_index)) +      ); + +      for (i = 0; i < diff; ++i) +      { +         result[i] = ZoO_START_OF_SEQUENCE_ID; +      } +   } +   else if (base[(base_length - 1)] == ZoO_END_OF_SEQUENCE_ID) +   { +      memcpy +      ( +         (void *) result, +         (const void *) base, +         (((size_t) base_length) * sizeof(ZoO_index)) +      ); + +      for (i = base_length; i < destination_length; ++i) +      { +         result[i] = ZoO_END_OF_SEQUENCE_ID; +      } +   } + +   return result; +} + +/******************************************************************************/ +/** ADD A NEW SEQUENCE ********************************************************/ +/******************************************************************************/ + +static int add_sequence +( +   struct ZoO_knowledge k [const restrict static 1], +   const ZoO_index sequence [const restrict static 1], +   const ZoO_index sequence_length, +   const ZoO_index markov_order, /* Pre (> markov_order 1) */ +   const ZoO_index sequence_id, +   const ZoO_index sorted_sequence_id, +   const struct ZoO_pipe io [const restrict static 1] +) +{ +   ZoO_index * stored_sequence; + +   if (k->sequences_length == ZoO_INDEX_MAX) +   { +      ZoO_S_ERROR +      ( +         io, +         "Unable to add sequence: the variable that stores the number of known " +         "sequences would overflow." +      ); + +      return -1; +   } + +   stored_sequence = copy_sequence(sequence, sequence_length, markov_order, io); + +   if (stored_sequence == (ZoO_index *) NULL) +   { +      return -1; +   } + +   k->sequences_length += 1; + +   if (reallocate_sequences_list(k, io) < 0) +   { +      k->sequences_length -= 1; + +      free((void *) stored_sequence); + +      return -1; +   } + +   k->sequences[sequence_id] = stored_sequence; + +   if (reallocate_sequences_sorted_list(k, io) < 0) +   { +      k->sequences_length -= 1; + +      free((void *) stored_sequence); + +      return -1; +   } + +   set_nth_sequence(k, sorted_sequence_id, sequence_id); + +   return 0; +} + +/******************************************************************************/ +/** SEARCH EXISTING SEQUENCES *************************************************/ +/******************************************************************************/ + +static int find_sequence +( +   const struct ZoO_knowledge k [const static 1], +   const ZoO_index sequence [const restrict static 1], +   const ZoO_index sequence_length, +   const ZoO_index markov_order, /* Pre: (> 1) */ +   ZoO_index sequence_id [const restrict static 1] +) +{ +   /* This is a binary search */ +   int cmp; +   ZoO_index i, current_min, current_max; +   const ZoO_index markov_sequence_length = (markov_order - 1); + +   /* Handles the case where the list is empty ********************************/ +   current_max = k->sequences_length; + +   if (current_max == 0) +   { +      *sequence_id = 0; + +      return -1; +   } +   /***************************************************************************/ + +   current_min = 0; +   current_max -= 1; + +   for (;;) +   { +      i = (current_min + ((current_max - current_min) / 2)); + +      cmp = +         ZoO_sequence_cmp +         ( +            k->sequences[k->sequences_sorted[i]], +            markov_sequence_length, +            sequence, +            sequence_length +         ); + +      if (cmp > 0) +      { +         current_min = (i + 1); + +         if (current_min > current_max) +         { +            *sequence_id = current_min; + +            return -1; +         } +      } +      else if (cmp < 0) +      { +         if ((current_min > current_max) || (i == 0)) +         { +            *sequence_id = i; + +            return -1; +         } + +         current_max = (i - 1); +      } +      else +      { +         *sequence_id = k->sequences_sorted[i]; + +         return 0; +      } +   } +} + +/******************************************************************************/ +/** EXPORTED ******************************************************************/ +/******************************************************************************/ + +int ZoO_knowledge_learn_markov_sequence +( +   struct ZoO_knowledge k [const restrict static 1], +   const ZoO_index sequence [const restrict static 1], +   const ZoO_index sequence_length, +   const ZoO_index markov_order, /* Pre (> markov_order 1) */ +   ZoO_index sequence_id [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1] +) +{ +   ZoO_index sorted_id; + +   if +   ( +      find_sequence +      ( +         k, +         sequence, +         sequence_length, +         markov_order, +         sequence_id +      ) == 0 +   ) +   { +      return 0; +   } + +   sorted_id = *sequence_id; +   *sequence_id = k->sequences_length; + +   return +      add_sequence +      ( +         k, +         sequence, +         sequence_length, +         markov_order, +         *sequence_id, +         sorted_id, +         io +      ); +} diff --git a/src/knowledge/knowledge_learn_sequence.c b/src/knowledge/knowledge_learn_sequence.c index 23a5ca7..7696fbc 100644 --- a/src/knowledge/knowledge_learn_sequence.c +++ b/src/knowledge/knowledge_learn_sequence.c @@ -4,321 +4,87 @@  #include "../core/sequence.h" -#include "../cli/cli.h" +#include "../pipe/pipe.h"  #include "knowledge.h"  /******************************************************************************/ -/** INITIALIZE ****************************************************************/ +/** LEARN FOLLOWING SEQUENCE **************************************************/  /******************************************************************************/ -static void set_nth_sequence -( -   struct ZoO_knowledge k [const restrict static 1], -   const ZoO_index sorted_sequence_id, -   const ZoO_index sequence_id -) -{ -   /* Safe: (> k->sequences_length 1) */ -   if (sorted_sequence_id < (k->sequences_length - 1)) -   { -      memmove -      ( -         /* Safe: (=< (+ sorted_sequence_id 1) k->sequences_length) */ -         (void *) (k->sequences_sorted + (sorted_sequence_id + 1)), -         (const void *) (k->sequences_sorted + sorted_sequence_id), -         ((k->sequences_length - 1) - sorted_sequence_id) -      ); -   } - -   k->sequences_sorted[sorted_sequence_id] = sequence_id; -} - -/******************************************************************************/ -/** ALLOCATING MEMORY *********************************************************/ -/******************************************************************************/ -static int reallocate_sequences_list -( -   struct ZoO_knowledge k [const restrict static 1] -) -{ -   ZoO_index ** new_sequences; - -   if ((SIZE_MAX / sizeof(ZoO_index *)) > (size_t) k->sequences_length) -   { -      ZoO_S_ERROR -      ( -         "Unable to store the size of the sequences list, as it would overflow" -         "size_t variables." -      ); - -      return -1; -   } - -   new_sequences = -      (ZoO_index **) realloc -      ( -         (void *) k->sequences, -         (((size_t) k->sequences_length) * sizeof(ZoO_index *)) -      ); - -   if (new_sequences == (ZoO_index **) NULL) -   { -      ZoO_S_ERROR -      ( -         "Unable to allocate the memory required for the new sequence list." -      ); - -      return -1; -   } - -   k->sequences = new_sequences; - -   return 0; -} - -static int reallocate_sequences_sorted_list -( -   struct ZoO_knowledge k [const restrict static 1] -) -{ -   ZoO_index * new_sequences_sorted; - -   if ((SIZE_MAX / sizeof(ZoO_index)) > (size_t) k->sequences_length) -   { -      ZoO_S_ERROR -      ( -         "Unable to store the size of the sorted sequences list, as it would" -         " overflow size_t variables." -      ); - -      return -1; -   } - -   new_sequences_sorted = -      (ZoO_index *) realloc -      ( -         (void *) k->sequences_sorted, -         ((size_t) k->sequences_length) * sizeof(ZoO_index) -      ); - -   if (new_sequences_sorted == (ZoO_index *) NULL) -   { -      ZoO_S_ERROR -      ( -         "Unable to allocate the memory required for the new sorted sequences" -         " list." -      ); - -      return -1; -   } - -   k->sequences_sorted = new_sequences_sorted; - -   return 0; -} - -/* Pre: (=< ZoO_INDEX_MAX SIZE_MAX) */ -static ZoO_index * copy_sequence -( -   const ZoO_index base [const restrict static 1], -   const ZoO_index base_length, -   const ZoO_index markov_order -) -{ -   ZoO_index * result; - -   result = (ZoO_index *) calloc((size_t) base_length, sizeof(ZoO_index)); - -   if (result == (ZoO_index *) NULL) -   { -      ZoO_S_ERROR -      ( -         "Unable to allocate the memory required to store a new sequence." -      ); - -      return (ZoO_index *) NULL; -   } - -   memcpy -   ( -      (void *) result, -      (const void *) base, -      (((size_t) base_length) * sizeof(ZoO_index)) -   ); - -   return result; -} - -static int add_sequence +static int add_following_sequence  (     struct ZoO_knowledge k [const restrict static 1],     const ZoO_index sequence [const restrict static 1], +   const ZoO_index index,     const ZoO_index sequence_length, -   const ZoO_index markov_order, /* Pre (> markov_order 1) */ -   const ZoO_index sequence_id, -   const ZoO_index sorted_sequence_id -) -{ -   ZoO_index * stored_sequence; - -   if (k->sequences_length == ZoO_INDEX_MAX) -   { -      ZoO_S_ERROR -      ( -         "Unable to add sequence: the variable that stores the number of known " -         "sequences would overflow." -      ); - -      return -1; -   } - -   stored_sequence = copy_sequence(sequence, sequence_length, markov_order); - -   if (stored_sequence == (ZoO_index *) NULL) -   { -      return -1; -   } - -   k->sequences_length += 1; - -   if (reallocate_sequences_list(k) < 0) -   { -      k->sequences_length -= 1; - -      return -1; -   } - -   k->sequences[sequence_id] = stored_sequence; - -   if (reallocate_sequences_sorted_list(k) < 0) -   { -      k->sequences_length -= 1; - -      return -1; -   } - -   set_nth_sequence(k, sorted_sequence_id, sequence_id); - -   return -1; -} +   const ZoO_index markov_order, +   const struct ZoO_pipe io [const restrict static 1] +);  /******************************************************************************/ -/** SEARCH ********************************************************************/ +/** LEARN PRECEDING SEQUENCE **************************************************/  /******************************************************************************/ - -static int find_sequence +static int add_preceding_sequence  ( -   const struct ZoO_knowledge k [const static 1], +   struct ZoO_knowledge k [const restrict static 1],     const ZoO_index sequence [const restrict static 1], +   const ZoO_index index,     const ZoO_index sequence_length, -   const ZoO_index markov_order, /* Pre: (> 1) */ -   ZoO_index sequence_id [const restrict static 1] +   const ZoO_index markov_order, +   const struct ZoO_pipe io [const restrict static 1]  )  { -   /* This is a binary search */ -   int cmp; -   ZoO_index i, current_min, current_max; -   const ZoO_index markov_sequence_length = (markov_order - 1); - -   /* Handles the case where the list is empty ********************************/ -   current_max = k->sequences_length; - -   if (current_max == 0) -   { -      *sequence_id = 0; - -      return -1; -   } -   /***************************************************************************/ - -   current_min = 0; -   current_max -= 1; - -   for (;;) -   { -      i = (current_min + ((current_max - current_min) / 2)); - -      cmp = -         ZoO_sequence_cmp -         ( -            k->sequences[k->sequences_sorted[i]], -            markov_sequence_length, -            sequence, -            sequence_length -         ); - -      if (cmp > 0) -      { -         current_min = (i + 1); - -         if (current_min > current_max) -         { -            *sequence_id = current_min; - -            return -1; -         } -      } -      else if (cmp < 0) -      { -         if ((current_min > current_max) || (i == 0)) -         { -            *sequence_id = i; - -            return -1; -         } - -         current_max = (i - 1); -      } -      else -      { -         *sequence_id = k->sequences_sorted[i]; - -         return 0; -      } -   }  }  /******************************************************************************/  /** EXPORTED ******************************************************************/  /******************************************************************************/ -int ZoO_knowledge_learn_markov_sequence +int ZoO_knowledge_learn_sequence  (     struct ZoO_knowledge k [const restrict static 1],     const ZoO_index sequence [const restrict static 1],     const ZoO_index sequence_length, -   const ZoO_index markov_order, /* Pre (> markov_order 1) */ -   ZoO_index sequence_id [const restrict static 1] +   const ZoO_index markov_order, +   const struct ZoO_pipe io [const restrict static 1]  )  { -   ZoO_index sorted_id; +   ZoO_index * buffer; +   ZoO_index i; +   const ZoO_index buffer_length = (markov_order - 1); -   if -   ( -      find_sequence +   /* TODO */ + +   for (i = 0; i < sequence_length; ++i) +   { +      k->words[sequence[i]].occurrences += 1; + +      add_preceding_sequence        (           k,           sequence, +         i,           sequence_length, -         markov_order, -         sequence_id -      ) == 0 -   ) -   { -      return 0; -   } - -   sorted_id = *sequence_id; -   *sequence_id = k->sequences_length; +         buffer_length, +         io +      ); -   return -      add_sequence +      add_following_sequence        (           k,           sequence, +         i,           sequence_length,           markov_order, -         *sequence_id, -         sorted_id +         io        ); + +      /* +       * TODO: in case of failure, undo part of the word done so far: instead +       * of unlearning, just remove the occurrence count of sequences and +       * words so that {k} remains coherent. +       */ +   } + +   return 0;  } diff --git a/src/knowledge/knowledge_learn_word.c b/src/knowledge/knowledge_learn_word.c index f55ac5b..e6979dc 100644 --- a/src/knowledge/knowledge_learn_word.c +++ b/src/knowledge/knowledge_learn_word.c @@ -2,7 +2,7 @@  #include <string.h>  #include <stdint.h> /* defines SIZE_MAX */ -#include "../cli/cli.h" +#include "../pipe/pipe.h"  #include "knowledge.h" @@ -43,7 +43,8 @@ static void initialize_word  static ZoO_char * copy_word  (     const ZoO_char original [const restrict static 1], -   const ZoO_index original_length +   const ZoO_index original_length, +   const struct ZoO_pipe io [const restrict static 1]  )  {     ZoO_char * result; @@ -58,7 +59,7 @@ static ZoO_char * copy_word     if (result == (ZoO_char *) NULL)     { -      ZoO_S_ERROR("Unable to allocate memory to store new word."); +      ZoO_S_ERROR(io, "Unable to allocate memory to store new word.");        return (ZoO_char *) NULL;     } @@ -77,7 +78,8 @@ static ZoO_char * copy_word  static int reallocate_words_list  ( -   struct ZoO_knowledge k [const restrict static 1] +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  )  {     struct ZoO_knowledge_word * new_words; @@ -89,6 +91,7 @@ static int reallocate_words_list     {        ZoO_S_ERROR        ( +         io,           "Unable to store the size of the words list, as it would overflow"           "size_t variables."        ); @@ -107,6 +110,7 @@ static int reallocate_words_list     {        ZoO_S_ERROR        ( +         io,           "Unable to allocate the memory required for the new words list."        ); @@ -120,7 +124,8 @@ static int reallocate_words_list  static int reallocate_words_sorted_list  ( -   struct ZoO_knowledge k [const restrict static 1] +   struct ZoO_knowledge k [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  )  {     ZoO_index * new_words_sorted; @@ -153,6 +158,7 @@ static int reallocate_words_sorted_list     {        ZoO_S_ERROR        ( +         io,           "Unable to allocate the memory required for the new sorted words list."        ); @@ -192,7 +198,8 @@ static int add_word     const ZoO_char word [const restrict static 1],     const ZoO_index word_length,     const ZoO_index word_id, -   const ZoO_index sorted_word_id +   const ZoO_index sorted_word_id, +   const struct ZoO_pipe io [const restrict static 1]  )  {     ZoO_char * stored_word; @@ -201,6 +208,7 @@ static int add_word     {        ZoO_S_ERROR        ( +         io,           "Unable to add word: the variable that stores the number of known "           "words would overflow."        ); @@ -208,7 +216,7 @@ static int add_word        return -1;     } -   stored_word = copy_word(word, word_length); +   stored_word = copy_word(word, word_length, io);     if (stored_word == (ZoO_char *) NULL)     { @@ -217,7 +225,7 @@ static int add_word     k->words_length += 1; -   if (reallocate_words_list(k) < 0) +   if (reallocate_words_list(k, io) < 0)     {        k->words_length -= 1; @@ -229,7 +237,7 @@ static int add_word     k->words[word_id].word = stored_word;     k->words[word_id].word_size = ((word_length + 1) * sizeof(ZoO_char)); -   if (reallocate_words_sorted_list(k) < 0) +   if (reallocate_words_sorted_list(k, io) < 0)     {        k->words_length -= 1; @@ -250,7 +258,8 @@ int ZoO_knowledge_learn_word     struct ZoO_knowledge k [const restrict static 1],     const ZoO_char word [const restrict static 1],     const ZoO_index word_length, -   ZoO_index word_id [const restrict static 1] +   ZoO_index word_id [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  )  {     ZoO_index sorted_id; @@ -262,7 +271,8 @@ int ZoO_knowledge_learn_word           k,           word,           (word_length * sizeof(ZoO_char)), -         word_id +         word_id, +         io        ) == 0     )     { @@ -272,5 +282,5 @@ int ZoO_knowledge_learn_word     sorted_id = *word_id;     *word_id = k->words_length; -   return add_word(k, word, word_length, *word_id, sorted_id); +   return add_word(k, word, word_length, *word_id, sorted_id, io);  } diff --git a/src/knowledge/knowledge_search.c b/src/knowledge/knowledge_search.c index a48585b..23aab71 100644 --- a/src/knowledge/knowledge_search.c +++ b/src/knowledge/knowledge_search.c @@ -4,7 +4,7 @@  #include "../core/index.h"  #include "../core/sequence.h" -#include "../cli/cli.h" +#include "../pipe/pipe.h"  #include "knowledge.h" @@ -80,7 +80,8 @@ int ZoO_knowledge_find_preceding_words     const ZoO_index markov_order, /* Pre: (> 0) */     const ZoO_index * restrict preceding_words [const restrict static 1],     const ZoO_index * restrict preceding_words_weights [const restrict static 1], -   ZoO_index preceding_words_weights_sum [const restrict static 1] +   ZoO_index preceding_words_weights_sum [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  )  {     /* This is a binary search */ @@ -94,6 +95,7 @@ int ZoO_knowledge_find_preceding_words     {        ZoO_S_ERROR        ( +         io,           "Attempting to find the preceding words of an unknown word."        ); @@ -129,6 +131,7 @@ int ZoO_knowledge_find_preceding_words        ZoO_S_ERROR        ( +         io,           "Attempting to find the preceding words of a sequence that never had "           "any."        ); @@ -150,7 +153,8 @@ int ZoO_knowledge_find_preceding_words        (           k,           k->words[word].preceded.sequences_ref[local_sequence], -         &candidate +         &candidate, +         io        );        cmp = @@ -211,7 +215,8 @@ int ZoO_knowledge_find_following_words     const ZoO_index markov_order,     const ZoO_index * restrict following_words [const restrict static 1],     const ZoO_index * restrict following_words_weights [const restrict static 1], -   ZoO_index following_words_weights_sum [const restrict static 1] +   ZoO_index following_words_weights_sum [const restrict static 1], +   const struct ZoO_pipe io [const restrict static 1]  )  {     /* This is a binary search */ @@ -261,6 +266,7 @@ int ZoO_knowledge_find_following_words        ZoO_S_WARNING        ( +         io,           "Attempting to find the following words of a sequence that never had "           "any."        ); @@ -282,7 +288,8 @@ int ZoO_knowledge_find_following_words        (           k,           k->words[word].followed.sequences_ref[local_sequence], -         &candidate +         &candidate, +         io        );        cmp =  | 


