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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h> /* defines SIZE_MAX */
#include "../error/error.h"
#include "knowledge.h"
/** Basic functions of the JH_knowledge structure ****************************/
/* See: "knowledge.h" */
int JH_knowledge_initialize (struct JH_knowledge k [const restrict static 1])
{
int error;
JH_index reserved_word_id;
k->words = (struct JH_knowledge_word *) NULL;
k->words_length = 0;
k->words_sorted = (JH_index *) NULL;
k->sequences = (JH_index **) NULL;
k->sequences_length = 0;
k->sequences_sorted = (JH_index *) NULL;
#ifndef JH_RUNNING_FRAMA_C
error = pthread_mutex_init(&(k->mutex), (const pthread_mutexattr_t *) NULL);
#else
k->mutex = 1;
error = 0;
#endif
if (error != 0)
{
JH_FATAL
(
stderr,
"Unable to initialize knowledge mutex: %s.",
strerror(error)
);
return -1;
}
if
(
(
JH_knowledge_learn_word
(
k,
"[SoS]",
5,
&reserved_word_id,
stderr
) < 0
)
||
(
JH_knowledge_learn_word
(
k,
"[EoS]",
5,
&reserved_word_id,
stderr
) < 0
)
)
{
JH_S_FATAL(stderr, "Unable to learn reserved words.");
return -1;
}
return 0;
}
int JH_knowledge_lock_access
(
struct JH_knowledge k [const restrict static 1],
FILE io [const restrict static 1]
)
{
int err;
#ifndef JH_RUNNING_FRAMA_C
err = pthread_mutex_lock(&(k->mutex));
#else
/*@ assert (k->mutex == 1); @*/
k->mutex = 0;
err = 0;
#endif
if (err != 0)
{
JH_ERROR
(
io,
"Unable to get exclusive access to knowledge: %s",
strerror(err)
);
return -1;
}
return 0;
}
void JH_knowledge_unlock_access
(
struct JH_knowledge k [const restrict static 1],
FILE io [const restrict static 1]
)
{
int err;
#ifndef JH_RUNNING_FRAMA_C
err = pthread_mutex_unlock(&(k->mutex));
#else
/*@ assert (k->mutex == 0); @*/
k->mutex = 1;
err = 0;
#endif
if (err != 0)
{
JH_ERROR
(
io,
"Unable to release exclusive access to knowledge: %s",
strerror(err)
);
}
}
void JH_knowledge_get_word
(
const struct JH_knowledge k [const static 1],
const JH_index word_ref,
const JH_char * word [const restrict static 1],
JH_index word_length [const restrict static 1]
)
{
*word = k->words[word_ref].word;
*word_length = k->words[word_ref].word_length;
}
int JH_knowledge_rarest_word
(
const struct JH_knowledge k [const static 1],
const JH_index sequence [const restrict static 1],
const size_t sequence_length,
JH_index word_id [const restrict static 1]
)
{
JH_index current_max_score;
size_t i;
int success;
current_max_score = JH_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;
}
|