summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-02-09 20:03:33 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-02-09 20:03:33 +0100
commit7af295b2ec22f06b24079bf895ac97079f64b6d7 (patch)
tree84a554fc2c169956e3ee975152332c39f6c3615a /src/knowledge
parent9ca43c73ba29d6b42cd771f1567074418c883c3e (diff)
It's starting to "properly" reply...
The ACSL coverage is far behind though.
Diffstat (limited to 'src/knowledge')
-rw-r--r--src/knowledge/knowledge.c103
-rw-r--r--src/knowledge/knowledge.h30
-rw-r--r--src/knowledge/knowledge_finalize.c1
-rw-r--r--src/knowledge/knowledge_get_random_sequence.c5
-rw-r--r--src/knowledge/knowledge_get_random_target.c5
-rw-r--r--src/knowledge/knowledge_learn_markov_sequence.c87
-rw-r--r--src/knowledge/knowledge_learn_sequence.c78
-rw-r--r--src/knowledge/knowledge_learn_word.c39
-rw-r--r--src/knowledge/knowledge_search.c35
-rw-r--r--src/knowledge/knowledge_swt_tws_modifications.c15
10 files changed, 304 insertions, 94 deletions
diff --git a/src/knowledge/knowledge.c b/src/knowledge/knowledge.c
index 5ce6917..e7d1cd9 100644
--- a/src/knowledge/knowledge.c
+++ b/src/knowledge/knowledge.c
@@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdint.h> /* defines SIZE_MAX */
-#include "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -13,6 +13,7 @@
int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1])
{
int error;
+ ZoO_index reserved_word_id;
k->words = (struct ZoO_knowledge_word *) NULL;
k->words_length = 0;
@@ -36,25 +37,83 @@ int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1])
return -1;
}
+ if
+ (
+ (
+ ZoO_knowledge_learn_word
+ (
+ k,
+ "[SoS]",
+ 5,
+ &reserved_word_id,
+ stderr
+ ) < 0
+ )
+ ||
+ (
+ ZoO_knowledge_learn_word
+ (
+ k,
+ "[EoS]",
+ 5,
+ &reserved_word_id,
+ stderr
+ ) < 0
+ )
+ )
+ {
+ ZoO_S_FATAL(stderr, "Unable to learn reserved words.");
+
+ return -1;
+ }
+
return 0;
}
int ZoO_knowledge_lock_access
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
- return pthread_mutex_lock(&(k->mutex));
+ int err;
+
+ err = pthread_mutex_lock(&(k->mutex));
+
+ if (err != 0)
+ {
+ ZoO_ERROR
+ (
+ io,
+ "Unable to get exclusive access to knowledge: %s",
+ strerror(err)
+ );
+
+ return -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]
+ FILE io [const restrict static 1]
)
{
- pthread_mutex_unlock(&(k->mutex));
+ int err;
+
+ err = pthread_mutex_unlock(&(k->mutex));
+
+ if (err != 0)
+ {
+ ZoO_ERROR
+ (
+ io,
+ "Unable to release exclusive access to knowledge: %s",
+ strerror(err)
+ );
+ }
}
void ZoO_knowledge_get_word
@@ -68,3 +127,37 @@ void ZoO_knowledge_get_word
*word = k->words[word_ref].word;
*word_length = k->words[word_ref].word_length;
}
+
+int ZoO_knowledge_rarest_word
+(
+ const struct ZoO_knowledge k [const static 1],
+ const ZoO_index sequence [const restrict static 1],
+ const size_t sequence_length,
+ ZoO_index word_id [const restrict static 1]
+)
+{
+ ZoO_index current_max_score;
+ size_t i;
+ int success;
+
+ current_max_score = ZoO_INDEX_MAX;
+
+ success = -1;
+
+ for (i = 0; i < sequence_length; ++i)
+ {
+ if
+ (
+ (k->words[sequence[i]].occurrences <= current_max_score)
+ /* Otherwise we might just have learned it, or it must not be used. */
+ && (k->words[sequence[i]].occurrences > 1)
+ )
+ {
+ current_max_score = k->words[sequence[i]].occurrences;
+ *word_id = sequence[i];
+ success = 0;
+ }
+ }
+
+ return success;
+}
diff --git a/src/knowledge/knowledge.h b/src/knowledge/knowledge.h
index e97d84e..876dbe4 100644
--- a/src/knowledge/knowledge.h
+++ b/src/knowledge/knowledge.h
@@ -4,20 +4,20 @@
#include "../core/char_types.h"
#include "../core/index_types.h"
-#include "../pipe/pipe_types.h"
+#include "../error/error.h"
#include "knowledge_types.h"
int ZoO_knowledge_lock_access
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE 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]
+ FILE io [const restrict static 1]
);
int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]);
@@ -40,7 +40,7 @@ int ZoO_knowledge_learn_word
const ZoO_char word [const restrict static 1],
const size_t word_length,
ZoO_index result [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
);
int ZoO_knowledge_learn_sequence
@@ -49,7 +49,7 @@ int ZoO_knowledge_learn_sequence
const ZoO_index sequence [const restrict static 1],
const size_t sequence_length,
const ZoO_index markov_order,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
);
int ZoO_knowledge_learn_markov_sequence
@@ -58,7 +58,7 @@ int ZoO_knowledge_learn_markov_sequence
const ZoO_index sequence [const restrict static 1],
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]
+ FILE io [const restrict static 1]
);
void ZoO_knowledge_get_word
@@ -95,6 +95,14 @@ int ZoO_knowledge_find_sequence
ZoO_index sequence_id [const restrict static 1]
);
+int ZoO_knowledge_rarest_word
+(
+ const struct ZoO_knowledge k [const static 1],
+ const ZoO_index sequence [const restrict static 1],
+ const size_t sequence_length,
+ ZoO_index word_id [const restrict static 1]
+);
+
int ZoO_knowledge_find_markov_sequence
(
const ZoO_index sequence_id,
@@ -131,7 +139,7 @@ int ZoO_knowledge_copy_random_swt_sequence
ZoO_index sequence [const restrict static 1],
const ZoO_index word_id,
const ZoO_index markov_order,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
);
int ZoO_knowledge_strengthen_swt
@@ -140,7 +148,7 @@ int ZoO_knowledge_strengthen_swt
const ZoO_index sequence_id,
const ZoO_index word_id,
const ZoO_index target_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
);
int ZoO_knowledge_strengthen_tws
@@ -149,7 +157,7 @@ int ZoO_knowledge_strengthen_tws
const ZoO_index target_id,
const ZoO_index word_id,
const ZoO_index sequence_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
);
/*
@@ -162,7 +170,7 @@ int ZoO_knowledge_weaken_swt
const ZoO_index sequence_id,
const ZoO_index word_id,
const ZoO_index target_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
);
int ZoO_knowledge_weaken_tws
@@ -171,7 +179,7 @@ int ZoO_knowledge_weaken_tws
const ZoO_index target_id,
const ZoO_index word_id,
const ZoO_index sequence_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
);
*/
#endif
diff --git a/src/knowledge/knowledge_finalize.c b/src/knowledge/knowledge_finalize.c
index 6c249f0..37548d4 100644
--- a/src/knowledge/knowledge_finalize.c
+++ b/src/knowledge/knowledge_finalize.c
@@ -2,7 +2,6 @@
#include "knowledge.h"
-
static void knowledge_sequence_data_finalize
(
struct ZoO_knowledge_sequence_data sd [const restrict static 1]
diff --git a/src/knowledge/knowledge_get_random_sequence.c b/src/knowledge/knowledge_get_random_sequence.c
index 9055d31..92800a4 100644
--- a/src/knowledge/knowledge_get_random_sequence.c
+++ b/src/knowledge/knowledge_get_random_sequence.c
@@ -2,9 +2,10 @@
#include "../core/char.h"
#include "../core/index.h"
+
#include "../sequence/sequence.h"
-#include "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -52,7 +53,7 @@ int ZoO_knowledge_copy_random_swt_sequence
ZoO_index sequence [const restrict static 1],
const ZoO_index word_id,
const ZoO_index markov_order,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index sequence_id;
diff --git a/src/knowledge/knowledge_get_random_target.c b/src/knowledge/knowledge_get_random_target.c
index d9bf8a9..99fbc9b 100644
--- a/src/knowledge/knowledge_get_random_target.c
+++ b/src/knowledge/knowledge_get_random_target.c
@@ -2,9 +2,10 @@
#include "../core/char.h"
#include "../core/index.h"
-#include "../sequence/sequence.h"
-#include "../pipe/pipe.h"
+#include "../error/error.h"
+
+#include "../sequence/sequence.h"
#include "knowledge.h"
diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c
index 35cc123..54357c0 100644
--- a/src/knowledge/knowledge_learn_markov_sequence.c
+++ b/src/knowledge/knowledge_learn_markov_sequence.c
@@ -4,7 +4,7 @@
#include "../sequence/sequence.h"
-#include "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -40,12 +40,12 @@ static void set_nth_sequence
static int reallocate_sequences_list
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index ** new_sequences;
- if ((SIZE_MAX / sizeof(ZoO_index *)) > (size_t) k->sequences_length)
+ if ((SIZE_MAX / sizeof(ZoO_index *)) < (size_t) k->sequences_length)
{
ZoO_S_ERROR
(
@@ -83,12 +83,12 @@ static int reallocate_sequences_list
static int reallocate_sequences_sorted_list
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index * new_sequences_sorted;
- if ((SIZE_MAX / sizeof(ZoO_index)) > (size_t) k->sequences_length)
+ if ((SIZE_MAX / sizeof(ZoO_index)) < (size_t) k->sequences_length)
{
ZoO_S_ERROR
(
@@ -104,7 +104,7 @@ static int reallocate_sequences_sorted_list
(ZoO_index *) realloc
(
(void *) k->sequences_sorted,
- ((size_t) k->sequences_length) * sizeof(ZoO_index)
+ (((size_t) k->sequences_length) * sizeof(ZoO_index))
);
if (new_sequences_sorted == (ZoO_index *) NULL)
@@ -144,7 +144,7 @@ static ZoO_index * copy_sequence
(
const ZoO_index base [const restrict static 1],
const ZoO_index destination_length,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index i, diff;
@@ -189,7 +189,7 @@ static int add_sequence
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]
+ FILE io [const restrict static 1]
)
{
ZoO_index * stored_sequence;
@@ -213,6 +213,30 @@ static int add_sequence
return -1;
}
+ if (ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE)
+ {
+ ZoO_index i;
+
+ ZoO_S_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "Learning new sequence."
+ );
+
+ for (i = 0; i < (markov_order - 1); ++i)
+ {
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "markov_sequence[%u]: %u",
+ i,
+ stored_sequence[i]
+ );
+ }
+ }
+
k->sequences_length += 1;
if (reallocate_sequences_list(k, io) < 0)
@@ -271,16 +295,15 @@ int ZoO_knowledge_find_sequence
current_min = 0;
current_max -= 1;
- for (;;)
+ while (current_min <= current_max)
{
i = (current_min + ((current_max - current_min) / 2));
cmp =
ZoO_sequence_cmp
(
- k->sequences[k->sequences_sorted[i]],
- markov_sequence_length,
sequence,
+ k->sequences[k->sequences_sorted[i]],
markov_sequence_length
);
@@ -297,9 +320,9 @@ int ZoO_knowledge_find_sequence
}
else if (cmp < 0)
{
- if ((current_min > current_max) || (i == 0))
+ if ((current_min >= current_max) || (i == 0))
{
- *sequence_id = i;
+ *sequence_id = current_min;
return -1;
}
@@ -313,6 +336,10 @@ int ZoO_knowledge_find_sequence
return 0;
}
}
+
+ *sequence_id = current_min;
+
+ return -1;
}
/******************************************************************************/
@@ -325,11 +352,35 @@ int ZoO_knowledge_learn_markov_sequence
const ZoO_index sequence [const restrict static 1],
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]
+ FILE io [const restrict static 1]
)
{
ZoO_index sorted_id;
+ if (ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE)
+ {
+ ZoO_index i;
+
+ ZoO_S_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "Studying markov sequence..."
+ );
+
+ for (i = 0; i < (markov_order - 1); ++i)
+ {
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "markov_sequence[%u]: %u",
+ i,
+ sequence[i]
+ );
+ }
+ }
+
if
(
ZoO_knowledge_find_sequence
@@ -341,6 +392,14 @@ int ZoO_knowledge_learn_markov_sequence
) == 0
)
{
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "Markov sequence is known. ID: %u",
+ *sequence_id
+ );
+
return 0;
}
diff --git a/src/knowledge/knowledge_learn_sequence.c b/src/knowledge/knowledge_learn_sequence.c
index 6a666bd..b127bfb 100644
--- a/src/knowledge/knowledge_learn_sequence.c
+++ b/src/knowledge/knowledge_learn_sequence.c
@@ -4,7 +4,7 @@
#include "../sequence/sequence.h"
-#include "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -26,8 +26,6 @@ static void parse_swt_sequence
for (j = 0; j < buffer_length; ++j)
{
- index_offset = (buffer_length - j);
-
if (index >= index_offset)
{
buffer[j] = sequence[index - index_offset];
@@ -36,6 +34,8 @@ static void parse_swt_sequence
{
buffer[j] = ZoO_START_OF_SEQUENCE_ID;
}
+
+ --index_offset;
}
}
@@ -48,7 +48,7 @@ static int add_swt_sequence
const ZoO_index markov_order,
ZoO_index buffer [const restrict static 1],
const ZoO_index buffer_length,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index sequence_id;
@@ -64,7 +64,7 @@ static int add_swt_sequence
(buffer_length + 1),
&sequence_id,
io
- )
+ ) < 0
)
{
return -1;
@@ -110,15 +110,14 @@ static void parse_tws_sequence
{
size_t j;
size_t index_offset;
- const size_t remaining_items = (sequence_length - index);
for (j = 0; j < buffer_length; ++j)
{
- index_offset = (j + 1);
+ index_offset = (j + 1) + index;
- if (remaining_items > index_offset)
+ if (sequence_length > index_offset)
{
- buffer[j] = sequence[index + index_offset];
+ buffer[j] = sequence[index_offset];
}
else
{
@@ -136,7 +135,7 @@ static int add_tws_sequence
const ZoO_index markov_order,
ZoO_index buffer [const restrict static 1],
const ZoO_index buffer_length,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index sequence_id;
@@ -152,7 +151,7 @@ static int add_tws_sequence
(buffer_length + 1),
&sequence_id,
io
- )
+ ) < 0
)
{
return -1;
@@ -193,7 +192,7 @@ int ZoO_knowledge_learn_sequence
const ZoO_index sequence [const restrict static 1],
const size_t sequence_length,
const ZoO_index markov_order,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index * buffer;
@@ -220,30 +219,43 @@ int ZoO_knowledge_learn_sequence
for (i = 0; i < sequence_length; ++i)
{
- /* TODO: handle failure. */
- add_tws_sequence
+ if
(
- k,
- sequence,
- i,
- sequence_length,
- markov_order,
- buffer,
- buffer_length,
- io
- );
+ add_swt_sequence
+ (
+ k,
+ sequence,
+ i,
+ sequence_length,
+ markov_order,
+ buffer,
+ buffer_length,
+ io
+ ) < 0
+ )
+ {
+ return -1;
+ }
- add_swt_sequence
+ /* TODO: handle failure. */
+ if
(
- k,
- sequence,
- i,
- sequence_length,
- markov_order,
- buffer,
- buffer_length,
- io
- );
+ add_tws_sequence
+ (
+ k,
+ sequence,
+ i,
+ sequence_length,
+ markov_order,
+ buffer,
+ buffer_length,
+ io
+ ) < 0
+ )
+ {
+ return -1;
+ }
+
k->words[sequence[i]].occurrences += 1;
}
diff --git a/src/knowledge/knowledge_learn_word.c b/src/knowledge/knowledge_learn_word.c
index 8430bc0..605af8e 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 "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -40,7 +40,7 @@ static ZoO_char * copy_word
(
const ZoO_char original [const restrict static 1],
const ZoO_index original_length,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_char * result;
@@ -67,13 +67,13 @@ static ZoO_char * copy_word
(((size_t) original_length) * sizeof(ZoO_char))
);
- return 0;
+ return result;
}
static int reallocate_words_list
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
struct ZoO_knowledge_word * new_words;
@@ -81,7 +81,7 @@ static int reallocate_words_list
if
(
(SIZE_MAX / sizeof(struct ZoO_knowledge_word))
- > (size_t) k->words_length
+ < (size_t) k->words_length
)
{
ZoO_S_ERROR
@@ -120,7 +120,7 @@ static int reallocate_words_list
static int reallocate_words_sorted_list
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index * new_words_sorted;
@@ -130,7 +130,7 @@ static int reallocate_words_sorted_list
* whose size is bigger than a ZoO_index.
* */
/*
- if ((SIZE_MAX / sizeof(ZoO_index)) > k->words_length)
+ if ((SIZE_MAX / sizeof(ZoO_index)) < k->words_length)
{
ZoO_S_ERROR
(
@@ -197,7 +197,7 @@ static int add_word
const ZoO_index word_length,
const ZoO_index word_id,
const ZoO_index sorted_word_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_char * stored_word;
@@ -244,7 +244,7 @@ static int add_word
set_nth_word(k, sorted_word_id, word_id);
- return -1;
+ return 0;
}
/******************************************************************************/
@@ -257,7 +257,7 @@ int ZoO_knowledge_learn_word
const ZoO_char word [const restrict static 1],
const size_t word_length,
ZoO_index word_id [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index sorted_id;
@@ -280,11 +280,30 @@ int ZoO_knowledge_learn_word
) == 0
)
{
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_WORD,
+ "Word of size %u is already known (id: %u).",
+ (ZoO_index) word_length,
+ *word_id
+ );
+
return 0;
}
sorted_id = *word_id;
*word_id = k->words_length;
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_WORD,
+ "Learning new word of size %u (id: %u, sorted_id: %u).",
+ (ZoO_index) word_length,
+ *word_id,
+ sorted_id
+ );
+
return add_word(k, word, (ZoO_index) word_length, *word_id, sorted_id, io);
}
diff --git a/src/knowledge/knowledge_search.c b/src/knowledge/knowledge_search.c
index cc1934e..1163f61 100644
--- a/src/knowledge/knowledge_search.c
+++ b/src/knowledge/knowledge_search.c
@@ -4,7 +4,7 @@
#include "../core/index.h"
#include "../sequence/sequence.h"
-#include "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -36,11 +36,18 @@ int ZoO_knowledge_find_word_id
current_min = 0;
current_max -= 1;
- for (;;)
+ while (current_min <= current_max)
{
i = (current_min + ((current_max - current_min) / 2));
- cmp = ZoO_word_cmp(word, word_length, k->words[k->words_sorted[i]].word);
+ cmp =
+ ZoO_word_cmp
+ (
+ word,
+ word_length,
+ k->words[k->words_sorted[i]].word,
+ k->words[k->words_sorted[i]].word_length
+ );
if (cmp > 0)
{
@@ -55,7 +62,7 @@ int ZoO_knowledge_find_word_id
}
else if (cmp < 0)
{
- if ((current_min > current_max) || (i == 0))
+ if ((current_min >= current_max) || (i == 0))
{
*result = current_min;
@@ -71,6 +78,10 @@ int ZoO_knowledge_find_word_id
return 0;
}
}
+
+ *result = current_min;
+
+ return -1;
}
int ZoO_knowledge_find_markov_sequence
@@ -94,7 +105,7 @@ int ZoO_knowledge_find_markov_sequence
current_min = 0;
current_max = (sc->sequences_ref_length - 1);
- for (;;)
+ while (current_min <= current_max)
{
i = (current_min + ((current_max - current_min) / 2));
@@ -118,7 +129,7 @@ int ZoO_knowledge_find_markov_sequence
}
else if (cmp < 0)
{
- if ((current_min > current_max) || (i == 0))
+ if ((current_min >= current_max) || (i == 0))
{
*result = current_min;
@@ -134,6 +145,10 @@ int ZoO_knowledge_find_markov_sequence
return 0;
}
}
+
+ *result = current_min;
+
+ return -1;
}
int ZoO_knowledge_find_sequence_target
@@ -157,7 +172,7 @@ int ZoO_knowledge_find_sequence_target
current_min = 0;
current_max = (sd->targets_length - 1);
- for (;;)
+ while (current_min <= current_max)
{
i = (current_min + ((current_max - current_min) / 2));
@@ -176,7 +191,7 @@ int ZoO_knowledge_find_sequence_target
}
else if (cmp < 0)
{
- if ((current_min > current_max) || (i == 0))
+ if ((current_min >= current_max) || (i == 0))
{
*result = current_min;
@@ -192,4 +207,8 @@ int ZoO_knowledge_find_sequence_target
return 0;
}
}
+
+ *result = current_min;
+
+ return -1;
}
diff --git a/src/knowledge/knowledge_swt_tws_modifications.c b/src/knowledge/knowledge_swt_tws_modifications.c
index 40e2e3b..1a8048d 100644
--- a/src/knowledge/knowledge_swt_tws_modifications.c
+++ b/src/knowledge/knowledge_swt_tws_modifications.c
@@ -2,7 +2,7 @@
#include "../core/index.h"
-#include "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -12,7 +12,7 @@ static int add_target
const ZoO_index target_id,
const ZoO_index s_index,
const ZoO_index t_index,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
struct ZoO_knowledge_target * new_p;
@@ -68,7 +68,7 @@ static int add_sequence
struct ZoO_knowledge_sequence_collection sc [const restrict static 1],
const ZoO_index sequence_id,
ZoO_index s_index [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
struct ZoO_knowledge_sequence_data * new_p;
@@ -149,7 +149,7 @@ static int add_sequence
sc->sequences_ref[*s_index].targets = (struct ZoO_knowledge_target *) NULL;
sc->sequences_ref[*s_index].targets_length = 0;
- return -1;
+ return 0;
}
int ZoO_knowledge_strengthen_swt
@@ -158,7 +158,7 @@ int ZoO_knowledge_strengthen_swt
const ZoO_index sequence_id,
const ZoO_index word_id,
const ZoO_index target_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index s_index, t_index;
@@ -248,7 +248,7 @@ int ZoO_knowledge_strengthen_tws
const ZoO_index target_id,
const ZoO_index word_id,
const ZoO_index sequence_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index s_index, t_index;
@@ -278,7 +278,6 @@ int ZoO_knowledge_strengthen_tws
}
}
-
if
(
ZoO_knowledge_find_sequence_target
@@ -330,5 +329,5 @@ int ZoO_knowledge_strengthen_tws
k->words[word_id].tws.sequences_ref[s_index].occurrences += 1;
k->words[word_id].tws.sequences_ref[s_index].targets[t_index].occurrences += 1;
- return -1;
+ return 0;
}