summaryrefslogtreecommitdiff
blob: dc67159ec7ab5419014c581612ce73530191188d (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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#ifndef _JH_CORE_SEQUENCE_H_
#define _JH_CORE_SEQUENCE_H_

/* Defines SIZE_MAX */
#include <stdint.h>

#include "../core/char_types.h"
#include "../core/index_types.h"

#include "../error/error.h"

#include "../knowledge/knowledge_types.h"

#include "sequence_types.h"

/*@
   requires \valid(sequence);
   requires (\block_length(sequence) >= 1);
   requires \valid(sequence_capacity);
   requires (\block_length(sequence) >= 1);
   requires \valid(io);

   requires (((*sequence_capacity) * sizeof(JH_index)) <= SIZE_MAX);
   requires ((sequence_required_capacity * sizeof(JH_index)) <= SIZE_MAX);

   requires
      \separated
      (
         (sequence+ (0 .. \block_length(sequence))),
         ((*sequence)+ (0 .. \block_length(*sequence))),
         (sequence_capacity+ (0 ..\block_length(sequence_capacity))),
         (io+ (0 ..\block_length(io)))
      );

   ensures
      \separated
      (
         (sequence+ (0 .. \block_length(sequence))),
         ((*sequence)+ (0 .. \block_length(*sequence))),
         (sequence_capacity+ (0 ..\block_length(sequence_capacity))),
         (io+ (0 ..\block_length(io)))
      );

   ensures (((*sequence_capacity) * sizeof(JH_index)) <= SIZE_MAX);
   ensures ((sequence_required_capacity * sizeof(JH_index)) <= SIZE_MAX);
   ensures \valid(sequence);
   ensures \valid(*sequence);
   ensures \valid(sequence_capacity);
   ensures \valid(io);

   assigns (*sequence);
   assigns (*sequence_capacity);

   ensures ((\result == 1) || (\result == 0) || (\result == -1));

   ensures
      (
         (\result == 1) ==>
            ((*sequence_capacity) == sequence_required_capacity)
      );

   ensures
      (
         (\result == 1) ==>
            ((*sequence_capacity) > \old(*sequence_capacity))
      );

   ensures ((\result == -1) ==> ((*sequence) == \old(*sequence)));

   ensures
      (
         (\result == -1) ==>
            ((*sequence_capacity) == \old(*sequence_capacity))
      );

   ensures ((\result == 0) ==> ((*sequence) == \old(*sequence)));

   ensures
      (
         (\result == 0) ==>
            ((*sequence_capacity) == \old(*sequence_capacity))
      );
@*/
int JH_sequence_ensure_capacity
(
   JH_index * sequence [const restrict static 1],
   size_t sequence_capacity [const restrict static 1],
   const size_t sequence_required_capacity,
   FILE io [const restrict static 1]
);

int JH_sequence_from_undercase_string
(
   const JH_char string [const restrict],
   const size_t string_length,
   struct JH_knowledge k [const restrict static 1],
   JH_index * sequence [const restrict static 1],
   size_t sequence_capacity [const restrict static 1],
   size_t sequence_length [const restrict static 1],
   FILE io [const restrict static 1]
);

/*
 * Creates a sequence containing {initial_word}. The remaining elements of
 * sequence are added according to what is known to {k} as being possible.
 * The resulting sequence starts by JH_START_OF_SEQUENCE_ID, and ends by
 * JH_END_OF_SEQUENCE_ID. The sequence is allocated by the function. If an
 * error occur, it is unallocated and set to NULL ({sequence_size} is set
 * accordingly).
 * Return:
 *    0 on success.
 *    -1 iff the allocating failed.
 *    -2 iff the sequence initialization failed.
 *    -3 iff an error occured when trying to add elements to the right of the
 *       sequence.
 *    -4 iff an error occured when trying to add elements to the left of the
 *       sequence.
 *    -5 iff the resulting sequence would have been empty.
 * Pre:
 *    (> {markov_order} 1)
 *    (knows {k} {initial_word})
 *    (initialized {k})
 */
int JH_sequence_create_from
(
   const JH_index initial_word,
   size_t credits [const restrict],
   struct JH_knowledge k [const restrict static 1],
   const JH_index markov_order,
   JH_index * sequence [const restrict static 1],
   size_t sequence_capacity [const restrict static 1],
   size_t sequence_length [const restrict static 1],
   FILE io [const restrict static 1]
);

/*@
   requires \valid(sequence);
   requires \valid(*sequence);
   requires \valid(sequence_capacity);
   requires \valid(sequence_length);
   requires \valid(io);
   requires (((*sequence_length) * sizeof(JH_index)) <= SIZE_MAX);
   requires (((*sequence_capacity) * sizeof(JH_index)) <= SIZE_MAX);
   requires
      \separated
      (
         (sequence+ (0 .. \block_length(sequence))),
         ((*sequence)+ (0 .. \block_length(*sequence))),
         (sequence_capacity+ (0 ..\block_length(sequence_capacity))),
         (sequence_length+ (0 ..\block_length(sequence_length))),
         (io+ (0 ..\block_length(io)))
      );

   assigns (*sequence_length);
   assigns (*sequence[0]);
   assigns (*sequence_capacity);

   ensures \valid(sequence);
   ensures \valid(*sequence);
   ensures \valid(sequence_capacity);
   ensures \valid(sequence_length);
   ensures \valid(io);
   ensures (((*sequence_length) * sizeof(JH_index)) <= SIZE_MAX);
   ensures (((*sequence_capacity) * sizeof(JH_index)) <= SIZE_MAX);
   ensures
      \separated
      (
         (sequence+ (0 .. \block_length(sequence))),
         ((*sequence)+ (0 .. \block_length(*sequence))),
         (sequence_capacity+ (0 ..\block_length(sequence_capacity))),
         (sequence_length+ (0 ..\block_length(sequence_length))),
         (io+ (0 ..\block_length(io)))
      );

   ensures ((\result == 0) || (\result == -1));

   ensures
   (
      (\result == 0) ==>
         (*sequence_length == (\old(*sequence_length) + 1))
   );

   ensures
      (
         (\result == 0) ==>
            ((*sequence_capacity) >= \old(*sequence_capacity))
      );

   ensures ((\result == 0) ==> (*sequence_length > \old(*sequence_length)));

   ensures ((\result == -1) ==> ((*sequence_length) == \old(*sequence_length)));
   ensures ((\result == -1) ==> (((*sequence)[0]) == \old((*sequence)[0])));
   ensures
      (
         (\result == -1) ==>
            ((*sequence_capacity) == \old(*sequence_capacity))
      );

   ensures ((\result == -1) ==> ((*sequence_length) == \old(*sequence_length)));
   ensures
      (
         (\result == -1) ==>
            ((*sequence_capacity) == \old(*sequence_capacity))
      );

@*/
int JH_sequence_append_left
(
   const JH_index word_id,
   JH_index * sequence [const restrict static 1],
   size_t sequence_capacity [const restrict static 1],
   size_t sequence_length [const restrict static 1],
   FILE io [const restrict static 1]
);

/*@
   requires \valid(sequence);
   requires \valid(*sequence);
   requires \valid(sequence_capacity);
   requires \valid(sequence_length);
   requires \valid(io);
   requires (((*sequence_length) * sizeof(JH_index)) <= SIZE_MAX);
   requires (((*sequence_capacity) * sizeof(JH_index)) <= SIZE_MAX);
   requires
      \separated
      (
         (sequence+ (0 .. \block_length(sequence))),
         ((*sequence)+ (0 .. \block_length(*sequence))),
         (sequence_capacity+ (0 ..\block_length(sequence_capacity))),
         (sequence_length+ (0 ..\block_length(sequence_length))),
         (io+ (0 ..\block_length(io)))
      );

   assigns (*sequence_length);
   assigns ((*sequence)[0]);
   assigns (*sequence_capacity);

   ensures \valid(sequence);
   ensures \valid(*sequence);
   ensures \valid(sequence_capacity);
   ensures \valid(sequence_length);
   ensures \valid(io);
   ensures (((*sequence_length) * sizeof(JH_index)) <= SIZE_MAX);
   ensures (((*sequence_capacity) * sizeof(JH_index)) <= SIZE_MAX);
   ensures
      \separated
      (
         (sequence+ (0 .. \block_length(sequence))),
         ((*sequence)+ (0 .. \block_length(*sequence))),
         (sequence_capacity+ (0 ..\block_length(sequence_capacity))),
         (sequence_length+ (0 ..\block_length(sequence_length))),
         (io+ (0 ..\block_length(io)))
      );

   ensures ((\result == 0) || (\result == -1));

   ensures
   (
      (\result == 0) ==>
         (*sequence_length == (\old(*sequence_length) + 1))
   );

   ensures
      (
         (\result == 0) ==>
            ((*sequence_capacity) >= \old(*sequence_capacity))
      );

   ensures ((\result == 0) ==> (*sequence_length > \old(*sequence_length)));

   ensures ((\result == -1) ==> ((*sequence_length) == \old(*sequence_length)));
   ensures ((\result == -1) ==> (((*sequence)[0]) == \old((*sequence)[0])));
   ensures
      (
         (\result == -1) ==>
            ((*sequence_capacity) == \old(*sequence_capacity))
      );

   ensures ((\result == -1) ==> ((*sequence_length) == \old(*sequence_length)));
   ensures
      (
         (\result == -1) ==>
            ((*sequence_capacity) == \old(*sequence_capacity))
      );

@*/
int JH_sequence_append_right
(
   JH_index * sequence [const restrict static 1],
   const JH_index word_id,
   size_t sequence_capacity [const restrict static 1],
   size_t sequence_length [const restrict static 1],
   FILE io [const restrict static 1]
);

/*
 * Compares two sequences.
 * JH_END_OF_SEQUENCE marks the ending of a sequence, regardless of indicated
 * sequence length, meaning that [10][JH_END_OF_SEQUENCE][9] and
 * [10][JH_END_OF_SEQUENCE][8] are considered equal. Sequences do not have to
 * contain JH_END_OF_SEQUENCE. [10][JH_END_OF_SEQUENCE] and [10] are
 * considered different, [10][JH_END_OF_SEQUENCE]
 * and [10][JH_END_OF_SEQUENCE][JH_END_OF_SEQUENCE] are considered equal.
 * Same logic is applyied for JH_START_OF_SEQUENCE:
 * [START_OF_SEQUENCE][10] is not [10], but
 * [START_OF_SEQUENCE][START_OF_SEQUENCE][10] and [START_OF_SEQUENCE][10] are
 * the same.
 * Return:
 *    1 iff {sequence_a} should be considered being more than {sequence_b}
 *    0 iff {sequence_a} should be considered being equal to {sequence_b}
 *    -1 iff {sequence_a} should be considered being less than {sequence_b}
 */
int JH_sequence_cmp
(
   const JH_index sequence_a [const],
   const JH_index sequence_b [const],
   const JH_index length
);

int JH_sequence_to_undercase_string
(
   const JH_index sequence [const restrict static 1],
   const size_t sequence_length,
   struct JH_knowledge k [const restrict static 1],
   JH_char * destination [const restrict static 1],
   size_t destination_capacity [const restrict static 1],
   size_t destination_length [const restrict static 1],
   FILE io [const restrict static 1]
);

#endif