| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-01-08 17:58:18 +0100 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-01-08 17:58:18 +0100 | 
| commit | 24afb3e60bafd98e6a83dcb41ee6a7f7d41e76bc (patch) | |
| tree | b122cf23d4cb912f4c4418a1b9d60f475414839d /src | |
| parent | 492b9cd1ecc234ea8f3080b305103702d2ca772b (diff) | |
Continuing the 'knowledge' refactoring.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/core/knowledge.c | 398 | ||||
| -rw-r--r-- | src/core/knowledge.h | 42 | ||||
| -rw-r--r-- | src/core/knowledge_finalize.c | 121 | ||||
| -rw-r--r-- | src/core/knowledge_search.c | 106 | ||||
| -rw-r--r-- | src/core/knowledge_types.h | 11 | ||||
| -rw-r--r-- | src/core/sequence_creation.c | 32 | 
7 files changed, 224 insertions, 487 deletions
| diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 37b95cb..fe28080 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -4,6 +4,7 @@ set(     ${CMAKE_CURRENT_SOURCE_DIR}/main.c     ${CMAKE_CURRENT_SOURCE_DIR}/knowledge.c     ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_search.c +   ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_finalize.c     ${CMAKE_CURRENT_SOURCE_DIR}/assimilate.c     ${CMAKE_CURRENT_SOURCE_DIR}/sequence_creation.c     ${CMAKE_CURRENT_SOURCE_DIR}/sequence.c diff --git a/src/core/knowledge.c b/src/core/knowledge.c index 279a646..94d76cd 100644 --- a/src/core/knowledge.c +++ b/src/core/knowledge.c @@ -3,405 +3,13 @@  #include <stdint.h> /* defines SIZE_MAX */  #include "../io/error.h" -#include "../tool/sorted_list.h"  #include "knowledge.h"  /** Basic functions of the ZoO_knowledge structure ****************************/ - -/* See "knowledge.h". */ -int ZoO_knowledge_find -( -   const struct ZoO_knowledge k [const restrict static 1], -   const ZoO_char word [const restrict static 1], -   ZoO_index result [const restrict static 1] -) -{ -   ZoO_index r; - -   if -   ( -      ZoO_sorted_list_index_of -      ( -         k->words_count, -         (void const *) k->sorted_indices, -         (void const *) word, -         sizeof(ZoO_index), -         cmp_word, -         (void const *) k, -         &r -      ) -      == 0 -   ) -   { -      *result = k->sorted_indices[r]; - -      return 0; -   } - -   *result = r; - -   return -1; -} - -static void word_init (struct ZoO_knowledge_word w [const restrict static 1]) -{ -   w->word_size = 0; -   w->word = (ZoO_char *) NULL; -   w->special = ZoO_WORD_HAS_NO_EFFECT; -   w->occurrences = 1; -   w->forward_links_count  = 0; -   w->backward_links_count = 0; -   w->forward_links  = (struct ZoO_knowledge_link *) NULL; -   w->backward_links = (struct ZoO_knowledge_link *) NULL; -} - -/* - * When returning 0: - *    All punctuation symbols were added to {k}. - * When returning -1: - *    The mandatory punctuation symbols have been added to {k}, but some of the - *    additional ones did not. This does not prevent ZoO from working, but - *    will result in some punctuation symbols to be handled exactly like - *    common words. - * When returning -2: - *    The mandatory punctuation symbols have not added to {k}. ZoO will not be - *    able to work. - */ -static int add_punctuation_nodes -( -   struct ZoO_knowledge k [const static 1] -) -{ -   int error; -   char w[2]; -   ZoO_index i, id; - -   if (ZoO_knowledge_learn(k, "START OF LINE", &id) < 0) -   { -      ZoO_S_FATAL("Could not add 'START OF LINE' to knowledge."); - -      return -2; -   } - -   k->words[id].special = ZoO_WORD_STARTS_SENTENCE; -   k->words[id].occurrences = 0; - -   if (ZoO_knowledge_learn(k, "END OF LINE", &id) < 0) -   { -      ZoO_S_FATAL("Could not add 'END OF LINE' to knowledge."); - -      return -2; -   } - -   k->words[id].special = ZoO_WORD_ENDS_SENTENCE; -   k->words[id].occurrences = 0; - -   w[1] = '\0'; - -   error = 0; - -   for (i = 0; i < ZoO_knowledge_punctuation_chars_count; ++i) -   { -      w[0] = ZoO_knowledge_punctuation_chars[i]; - -      if (ZoO_knowledge_learn(k, w, &id) < 0) -      { -         ZoO_WARNING("Could not add '%s' to knowledge.", w); - -         error = -1; -      } -      else -      { -         k->words[id].special = ZoO_WORD_REMOVES_LEFT_SPACE; -         k->words[id].occurrences = 0; -      } -   } - -   return error; -} - -/* See "knowledge.h" */ -int ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1]) +void ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])  { -   k->words_count = 0;     k->words = (struct ZoO_knowledge_word *) NULL; -   k->sorted_indices = (ZoO_index *) NULL; - -   if (add_punctuation_nodes(k) < -1) -   { -      ZoO_knowledge_finalize(k); - -      return -1; -   } - -   return 0; -} - -static void finalize_links -( -   ZoO_index const count, -   struct ZoO_knowledge_link links [const restrict static count] -) -{ -   ZoO_index i; - -   for (i = 0; i < count; ++i) -   { -      free((void *) links[i].targets_occurrences); -      free((void *) links[i].targets); -   } -} - -/* - * Frees all the memory used by {w}, but not {w} itself. - * The values of {w}'s members are set to reflect the changes. - */ -static void finalize_word -( -   struct ZoO_knowledge_word w [const restrict static 1] -) -{ -   if (w->word != (ZoO_char *) NULL) -   { -      free((void *) w->word); - -      w->word = (ZoO_char *) NULL; -   } - -   if (w->forward_links != (struct ZoO_knowledge_link *) NULL) -   { -      finalize_links(w->forward_links_count, w->forward_links); - -      free((void *) w->forward_links); - -      w->forward_links = (struct ZoO_knowledge_link *) NULL; -   } - -   if (w->backward_links != (struct ZoO_knowledge_link *) NULL) -   { -      finalize_links(w->backward_links_count, w->backward_links); - -      free((void *) w->backward_links); - -      w->backward_links = (struct ZoO_knowledge_link *) NULL; -   } - -   w->forward_links_count  = 0; -   w->backward_links_count = 0; +   k->words_length = 0; +   k->words_sorted = (ZoO_index *) NULL;  } - -/* See "knowledge.h" */ -void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1]) -{ -   ZoO_index i; - -   for (i = 0; i < k->words_count; ++i) -   { -      finalize_word(k->words + i); -   } - -   k->words_count = 0; - -   if (k->words != (struct ZoO_knowledge_word *) NULL) -   { -      free((void *) k->words); - -      k->words = (struct ZoO_knowledge_word *) NULL; -   } - -   if (k->sorted_indices != (ZoO_index *) NULL) -   { -      free((void *) k->sorted_indices); - -      k->sorted_indices = (ZoO_index *) NULL; -   } -} - -/* See "knowledge.h" */ -int ZoO_knowledge_learn -( -   struct ZoO_knowledge k [const static 1], -   const ZoO_char word [const restrict static 1], -   ZoO_index result [const restrict static 1] -) -{ -   struct ZoO_knowledge_word * new_wordlist; -   ZoO_index * new_sorted_indices; -   ZoO_index temp; - -   /* prevents k [restrict] */ -   if (ZoO_knowledge_find(k, word, result) == 0) -   { -      if (k->words[*result].occurrences == ZoO_INDEX_MAX) -      { -         ZoO_WARNING -         ( -            "Maximum number of occurrences has been reached for word '" -            ZoO_CHAR_STRING_SYMBOL -            "'.", -            word -         ); - -         return -1; -      } - -      /* overflow-safe: (< k->words[*result].occurrences ZoO_INDEX_MAX) */ -      k->words[*result].occurrences += 1; - -      return 0; -   } - -   if (k->words_count == ZoO_INDEX_MAX) -   { -      ZoO_S_WARNING("Maximum number of words has been reached."); - -      return -1; -   } - -   new_wordlist = -      (struct ZoO_knowledge_word *) realloc -      ( -         (void *) k->words, -         ( -            ( -               /* overflow-safe: (< k->words_count ZoO_INDEX_MAX) */ -               (size_t) (k->words_count + 1) -            ) -            * sizeof(struct ZoO_knowledge_word) -         ) -      ); - -   if (new_wordlist == (struct ZoO_knowledge_word *) NULL) -   { -      ZoO_ERROR -      ( -         "Could not learn the word '%s': unable to realloc the word list.", -         word -      ); - -      return -1; -   } - -   k->words = new_wordlist; - -   new_sorted_indices = -      (ZoO_index *) realloc -      ( -         (void *) k->sorted_indices, -         ( -            ( -               /* overflow-safe: (< k->words_count ZoO_INDEX_MAX) */ -               (size_t) (k->words_count + 1) -            ) -            * sizeof(ZoO_index) -         ) -      ); - -   if (new_sorted_indices == (ZoO_index *) NULL) -   { -      ZoO_ERROR -      ( -         "Could not learn the word '" -         ZoO_CHAR_STRING_SYMBOL -         "': unable to realloc the index list.", -         word -      ); - -      return -1; -   } - -   k->sorted_indices = new_sorted_indices; - -   /* We can only move indices right of *result if they exist. */ -   if (*result < k->words_count) -   { -      /* TODO: check if correct. */ -      memmove -      ( -         /* -          * Safe: -          *  (-> -          *    (and -          *       (== (length k->sorted_indices) (+ k->words_count 1)) -          *       (< *result k->words_count) -          *    ) -          *    (< (+ *result 1) (length k->sorted_indices)) -          * ) -          */ -         (void *) (k->sorted_indices + *result + 1), -         /* Safe: see above */ -         (const void *) (k->sorted_indices + *result), -         ( -            ( -               /* Safe: (< *result k->words_count) */ -               (size_t) (k->words_count - *result) -            ) -            * sizeof(ZoO_index) -         ) -      ); -   } - -   temp = *result; - -   k->sorted_indices[*result] = k->words_count; - -   *result = k->words_count; - -   word_init(k->words + *result); - -   /* XXX: strlen assumed to work with ZoO_char. */ -   k->words[*result].word_size = strlen(word); - -   if (k->words[*result].word_size == SIZE_MAX) -   { -      ZoO_S_WARNING -      ( -         "Could not learn word that had a size too big to store in a '\\0' " -         "terminated string. Chances are, this is but a symptom of the real " -         "problem." -      ); - -      return -1; -   } - -   /* We also need '\0' */ -   k->words[*result].word_size += 1; - -   k->words[*result].word = -      (ZoO_char *) calloc -      ( -         k->words[*result].word_size, -         sizeof(ZoO_char) -      ); - -   if (k->words[*result].word == (ZoO_char *) NULL) -   { -      ZoO_S_ERROR -      ( -         "Could not learn word due to being unable to allocate the memory to " -         "store it." -      ); - -      k->words[*result].word_size = 0; - -      return -1; -   } - -   memcpy(k->words[*result].word, word, k->words[*result].word_size); - -   /* Safe: k->words_count < ZoO_INDEX_MAX */ -   k->words_count += 1; - -   ZoO_DEBUG -   ( -      ZoO_DEBUG_LEARNING, -      "Learned word {'%s', id: %u, rank: %u}", -      word, -      *result, -      temp -   ); - -   return 0; -} - diff --git a/src/core/knowledge.h b/src/core/knowledge.h index b4f7b7e..057e436 100644 --- a/src/core/knowledge.h +++ b/src/core/knowledge.h @@ -6,24 +6,9 @@  #include "knowledge_types.h" -/* - * Initializes all of {k}'s members to sane values. - * - * When returning 0: - *    Initial punctuation nodes (including the mandatory "START OF LINE" and - *    "END OF LINE" ones) have successfully been added to {k}. - * - * When return -1: - *    Something went wrong, leading to {k} not being safe for use. - *    {k} has been finalized. - */ -int ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1]); +void ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]); -/* - * Frees all the memory used by {k}, but not {k} itself. - * The values of {k}'s members are set to reflect the changes. - */ -void ZoO_knowledge_finalize (struct ZoO_knowledge k [const static 1]); +void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1]);  /* @@ -51,13 +36,28 @@ int ZoO_knowledge_learn_sequence     const ZoO_index sequence_length  ); -int ZoO_knowledge_get_following_sequences +int ZoO_knowledge_get_following_sequences_ref  (     const struct ZoO_knowledge k [const static 1],     const ZoO_index initial_word, -   const ZoO_index * const restrict * following_sequences [const restrict static 1], -   const ZoO_index * following_sequences_weights [const restrict static 1], -   const ZoO_index following_sequences_weights_sum [const static 1] +   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] +); + +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] +); + +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]  );  /* diff --git a/src/core/knowledge_finalize.c b/src/core/knowledge_finalize.c new file mode 100644 index 0000000..e4deda6 --- /dev/null +++ b/src/core/knowledge_finalize.c @@ -0,0 +1,121 @@ +#include <stdlib.h> +#include <string.h> +#include <stdint.h> /* defines SIZE_MAX */ + +#include "../io/error.h" + +#include "knowledge.h" + +void knowledge_sequence_collection_finalize +( +   struct ZoO_knowledge_sequence_collection c [const restrict static 1] +) +{ +   ZoO_index i; + +   if (c->sequences_ref != (ZoO_index *) NULL) +   { +      free((void *) c->sequences_ref); +      c->sequences_ref = (ZoO_index *) NULL; +   } + +   if (c->sequences_ref_sorted != (ZoO_index *) NULL) +   { +      free((void *) c->sequences_ref_sorted); +      c->sequences_ref_sorted = (ZoO_index *) NULL; +   } + +   if (c->occurrences != (ZoO_index *) NULL) +   { +      free((void *) c->occurrences); +      c->occurrences = (ZoO_index *) NULL; +   } + +   for (i = 0; i < c->sequences_ref_length; ++i) +   { +      free((void *) c->targets[i]); +      free((void *) c->targets_occurrences[i]); +   } + +   c->sequences_ref_length = 0; + +   if (c->targets != (ZoO_index **) NULL) +   { +      free((void *) c->targets); +      c->targets != (ZoO_index **) NULL; +   } + +   free((void *) c->targets_length); + +   if (c->targets_occurrences != (ZoO_index **) NULL) +   { +      free((void *) c->targets_occurrences); +      c->targets_occurrences != (ZoO_index **) NULL; +   } +} + +void knowledge_word_finalize +( +   struct ZoO_knowledge_word w [const restrict static 1] +) +{ +   w->word_size = 0; +   w->occurrences = 0; + +   if (w->word != (ZoO_char *) NULL) +   { +      free((void *) w->word); + +      w->word = (ZoO_char *) NULL; +   } + +   knowledge_sequence_collection_finalize(&(w->followed)); +   knowledge_sequence_collection_finalize(&(w->preceded)); +} + +void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1]) +{ +   ZoO_index i; + +   for (i = 0; i < k->words_length; ++i) +   { +      knowledge_word_finalize(k->words + i); +   } + +   k->words_length = 0; + +   if (k->words != (struct ZoO_knowledge_word *) NULL) +   { +      free((void *) k->words); + +      k->words = (struct ZoO_knowledge_word *) NULL; +   } + +   if (k->words_sorted != (ZoO_index *) NULL) +   { +      free((void *) k->words_sorted); + +      k->words_sorted = (ZoO_index *) NULL; +   } + +   for (i = 0; i < k->sequences_length; ++i) +   { +      free((void *) k->sequences[i]); +   } + +   k->sequences_length = 0; + +   if (k->sequences != (ZoO_index **) NULL) +   { +      free((void *) k->sequences); + +      k->sequences = (ZoO_index **) NULL; +   } + +   if (k->sequences_sorted != (ZoO_index *) NULL) +   { +      free((void *) k->sequences_sorted); + +      k->sequences_sorted = (ZoO_index *) NULL; +   } +} diff --git a/src/core/knowledge_search.c b/src/core/knowledge_search.c index af62266..d0c61ef 100644 --- a/src/core/knowledge_search.c +++ b/src/core/knowledge_search.c @@ -84,11 +84,12 @@ int ZoO_knowledge_find_preceding_words  {     /* This is a binary search */     int cmp; -   ZoO_index i, current_min, current_max; -   ZoO_index candidate_id; +   ZoO_index i, current_min, current_max, local_sequence; +   const ZoO_index * restrict candidate;     const ZoO_index markov_sequence_length = (markov_order - 1); +   const ZoO_index word = sequence[markov_sequence_length]; -   if (sequence[markov_sequence_length] >= k->words_length) +   if (word >= k->words_length)     {        ZoO_S_ERROR        ( @@ -102,30 +103,22 @@ int ZoO_knowledge_find_preceding_words        return -1;     } -   *preceding_words_weights_sum = -      k->words[sequence[markov_sequence_length]].occurrences;     if (markov_order == 1)     {        /* Special case: empty sequences. */ -      *preceding_words = -         (const ZoO_index *) k->words -         [ -            sequence[markov_sequence_length] -         ].preceded.targets; +      *preceding_words = (const ZoO_index *) k->words[word].preceded.targets;        *preceding_words_weights = -         (const ZoO_index *) k->words -         [ -            sequence[markov_sequence_length] -         ].preceded.targets_occurrences; +         (const ZoO_index *) k->words[word].preceded.targets_occurrences; + +      *preceding_words_weights_sum = k->words[word].occurrences;        return 0;     }     /* Handles the case where the list is empty ********************************/ -   current_max = -      k->words[sequence[markov_sequence_length]].preceded.sequences_length; +   current_max = k->words[word].preceded.sequences_ref_length;     if (current_max == 0)     { @@ -150,12 +143,21 @@ int ZoO_knowledge_find_preceding_words     {        i = (current_min + ((current_max - current_min) / 2)); +      local_sequence = k->words[word].preceded.sequences_ref_sorted[i]; + +      (void) ZoO_knowledge_get_sequence +      ( +         k, +         k->words[word].preceded.sequences_ref[local_sequence], +         &candidate +      ); +        cmp =           ZoO_sequence_cmp           (              sequence,              markov_sequence_length, -            k->words[sequence[markov_sequence_length]].preceded.sequences[i], +            candidate,              markov_sequence_length           ); @@ -187,17 +189,13 @@ int ZoO_knowledge_find_preceding_words        }        else        { -         *preceding_words = -            k->words -            [ -               sequence[markov_sequence_length] -            ].preceded.targets[i]; +         *preceding_words = k->words[word].preceded.targets[local_sequence];           *preceding_words_weights = -            k->words -            [ -               sequence[markov_sequence_length] -            ].preceded.targets_occurrences[i]; +            k->words[word].preceded.targets_occurrences[local_sequence]; + +         *preceding_words_weights_sum = +            k->words[word].preceded.occurrences[local_sequence];           return 0;        } @@ -217,13 +215,14 @@ int ZoO_knowledge_find_following_words  {     /* This is a binary search */     int cmp; -   ZoO_index i, current_min, current_max; -   ZoO_index candidate_id; +   ZoO_index i, current_min, current_max, local_sequence; +   const ZoO_index * restrict candidate;     const ZoO_index markov_sequence_length = (markov_order - 1); -   const ZoO_index word_of_interest = -      (sequence_length - markov_sequence_length) - 1; +   const ZoO_index sequence_offset = +      ((sequence_length - markov_sequence_length) - 1); +   const ZoO_index word = sequence[sequence_offset]; -   if (sequence[word_of_interest] >= k->words_length) +   if (word >= k->words_length)     {        ZoO_S_ERROR        ( @@ -237,29 +236,21 @@ int ZoO_knowledge_find_following_words        return -1;     } -   *following_words_weights_sum = -      k->words[sequence[word_of_interest]].occurrences; -     if (markov_order == 1)     {        /* Special case: empty sequences. */ -      *following_words = -         (const ZoO_index *) k->words -         [ -            sequence[word_of_interest] -         ].preceded.targets; +      *following_words = (const ZoO_index *) k->words[word].preceded.targets;        *following_words_weights = -         (const ZoO_index *) k->words -         [ -            sequence[word_of_interest] -         ].preceded.targets_occurrences; +         (const ZoO_index *) k->words[word].preceded.targets_occurrences; + +      *following_words_weights_sum = k->words[word].occurrences;        return 0;     }     /* Handles the case where the list is empty ********************************/ -   current_max = k->words[sequence[word_of_interest]].preceded.sequences_length; +   current_max = k->words[word].preceded.sequences_ref_length;     if (current_max == 0)     { @@ -284,12 +275,21 @@ int ZoO_knowledge_find_following_words     {        i = (current_min + ((current_max - current_min) / 2)); +      local_sequence = k->words[word].followed.sequences_ref_sorted[i]; + +      (void) ZoO_knowledge_get_sequence +      ( +         k, +         k->words[word].followed.sequences_ref[local_sequence], +         &candidate +      ); +        cmp =           ZoO_sequence_cmp           ( -            (sequence + word_of_interest), +            (sequence + sequence_offset),              markov_sequence_length, -            k->words[sequence[word_of_interest]].followed.sequences[i], +            candidate,              markov_sequence_length           ); @@ -321,17 +321,13 @@ int ZoO_knowledge_find_following_words        }        else        { -         *following_words = -            k->words -            [ -               sequence[markov_sequence_length] -            ].followed.targets[i]; +         *following_words = k->words[word].followed.targets[local_sequence];           *following_words_weights = -            k->words -            [ -               sequence[markov_sequence_length] -            ].followed.targets_occurrences[i]; +            k->words[word].followed.targets_occurrences[local_sequence]; + +         *following_words_weights_sum = +            k->words[word].followed.occurrences[local_sequence];           return 0;        } diff --git a/src/core/knowledge_types.h b/src/core/knowledge_types.h index aea11da..acd239f 100644 --- a/src/core/knowledge_types.h +++ b/src/core/knowledge_types.h @@ -6,9 +6,9 @@  struct ZoO_knowledge_sequence_collection  { -   ZoO_index ** sequences; -   ZoO_index sequences_length; -   ZoO_index * sequences_sorted; +   ZoO_index * sequences_ref; +   ZoO_index sequences_ref_length; +   ZoO_index * sequences_ref_sorted;     ZoO_index * occurrences;     ZoO_index ** targets;     ZoO_index * targets_length; @@ -17,7 +17,7 @@ struct ZoO_knowledge_sequence_collection  struct ZoO_knowledge_word  { -   ZoO_char * word; +   const ZoO_char * word;     size_t word_size;     ZoO_index occurrences;     struct ZoO_knowledge_sequence_collection followed; @@ -29,6 +29,9 @@ struct ZoO_knowledge     struct ZoO_knowledge_word * words;     ZoO_index words_length;     ZoO_index * words_sorted; +   ZoO_index ** sequences; +   ZoO_index sequences_length; +   ZoO_index * sequences_sorted;  };  #endif diff --git a/src/core/sequence_creation.c b/src/core/sequence_creation.c index b1f0f36..1133be9 100644 --- a/src/core/sequence_creation.c +++ b/src/core/sequence_creation.c @@ -579,10 +579,10 @@ static int initialize_sequence     const struct ZoO_knowledge k [const static 1]  )  { -   const ZoO_index * const restrict * following_sequences; -   const ZoO_index * following_sequences_weights; +   const ZoO_index * restrict following_sequences_ref; +   const ZoO_index * restrict chosen_sequence; +   const ZoO_index * restrict following_sequences_weights;     ZoO_index following_sequences_weights_sum; -   ZoO_index chosen_sequence;     sequence[0] = initial_word; @@ -593,11 +593,11 @@ static int initialize_sequence     if     ( -      ZoO_knowledge_get_following_sequences +      ZoO_knowledge_get_following_sequences_ref        (           k,           initial_word, -         &following_sequences, +         &following_sequences_ref,           &following_sequences_weights,           &following_sequences_weights_sum        ) < 0 @@ -611,18 +611,26 @@ static int initialize_sequence        return -1;     } -   chosen_sequence = -      weighted_random_pick -      ( -         following_sequences_weights, -         following_sequences_weights_sum -      ); +   /* following_sequences_ref contains only valid references. */ +   (void) ZoO_knowledge_get_sequence +   ( +      k, +      following_sequences_ref +      [ +         weighted_random_pick +         ( +            following_sequences_weights, +            following_sequences_weights_sum +         ) +      ], +      &chosen_sequence +   );     /* Safe if 'allocate_initial_sequence' succeeded. */     memcpy     (        (void *) (sequence + 1), -      (const void *) (following_sequences + chosen_sequence), +      (const void *) chosen_sequence,        ((((size_t) markov_order) - 1) * sizeof(ZoO_index))     ); | 


