~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/tsearch2/snowball/utilities.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <string.h>
 
5
 
 
6
#include "header.h"
 
7
 
 
8
#define unless(C) if(!(C))
 
9
 
 
10
#define CREATE_SIZE 1
 
11
 
 
12
symbol *
 
13
create_s(void)
 
14
{
 
15
        symbol     *p = (symbol *) (HEAD + (char *) malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol)));
 
16
 
 
17
        if (p == (symbol *) (HEAD))
 
18
                return NULL;
 
19
        CAPACITY(p) = CREATE_SIZE;
 
20
        SET_SIZE(p, CREATE_SIZE);
 
21
        return p;
 
22
}
 
23
 
 
24
void
 
25
lose_s(symbol * p)
 
26
{
 
27
        free((char *) p - HEAD);
 
28
}
 
29
 
 
30
int
 
31
in_grouping(struct SN_env * z, unsigned char *s, int min, int max)
 
32
{
 
33
        if (z->c >= z->l)
 
34
                return 0;
 
35
        {
 
36
                int                     ch = z->p[z->c];
 
37
 
 
38
                if
 
39
                        (ch > max || (ch -= min) < 0 ||
 
40
                         (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
 
41
                        return 0;
 
42
        }
 
43
        z->c++;
 
44
        return 1;
 
45
}
 
46
 
 
47
int
 
48
in_grouping_b(struct SN_env * z, unsigned char *s, int min, int max)
 
49
{
 
50
        if (z->c <= z->lb)
 
51
                return 0;
 
52
        {
 
53
                int                     ch = z->p[z->c - 1];
 
54
 
 
55
                if
 
56
                        (ch > max || (ch -= min) < 0 ||
 
57
                         (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
 
58
                        return 0;
 
59
        }
 
60
        z->c--;
 
61
        return 1;
 
62
}
 
63
 
 
64
int
 
65
out_grouping(struct SN_env * z, unsigned char *s, int min, int max)
 
66
{
 
67
        if (z->c >= z->l)
 
68
                return 0;
 
69
        {
 
70
                int                     ch = z->p[z->c];
 
71
 
 
72
                unless
 
73
                        (ch > max || (ch -= min) < 0 ||
 
74
                         (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0;
 
75
        }
 
76
        z->c++;
 
77
        return 1;
 
78
}
 
79
 
 
80
int
 
81
out_grouping_b(struct SN_env * z, unsigned char *s, int min, int max)
 
82
{
 
83
        if (z->c <= z->lb)
 
84
                return 0;
 
85
        {
 
86
                int                     ch = z->p[z->c - 1];
 
87
 
 
88
                unless
 
89
                        (ch > max || (ch -= min) < 0 ||
 
90
                         (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0;
 
91
        }
 
92
        z->c--;
 
93
        return 1;
 
94
}
 
95
 
 
96
 
 
97
int
 
98
in_range(struct SN_env * z, int min, int max)
 
99
{
 
100
        if (z->c >= z->l)
 
101
                return 0;
 
102
        {
 
103
                int                     ch = z->p[z->c];
 
104
 
 
105
                if
 
106
                        (ch > max || ch < min)
 
107
                        return 0;
 
108
        }
 
109
        z->c++;
 
110
        return 1;
 
111
}
 
112
 
 
113
int
 
114
in_range_b(struct SN_env * z, int min, int max)
 
115
{
 
116
        if (z->c <= z->lb)
 
117
                return 0;
 
118
        {
 
119
                int                     ch = z->p[z->c - 1];
 
120
 
 
121
                if
 
122
                        (ch > max || ch < min)
 
123
                        return 0;
 
124
        }
 
125
        z->c--;
 
126
        return 1;
 
127
}
 
128
 
 
129
int
 
130
out_range(struct SN_env * z, int min, int max)
 
131
{
 
132
        if (z->c >= z->l)
 
133
                return 0;
 
134
        {
 
135
                int                     ch = z->p[z->c];
 
136
 
 
137
                unless
 
138
                        (ch > max || ch < min) return 0;
 
139
        }
 
140
        z->c++;
 
141
        return 1;
 
142
}
 
143
 
 
144
int
 
145
out_range_b(struct SN_env * z, int min, int max)
 
146
{
 
147
        if (z->c <= z->lb)
 
148
                return 0;
 
149
        {
 
150
                int                     ch = z->p[z->c - 1];
 
151
 
 
152
                unless
 
153
                        (ch > max || ch < min) return 0;
 
154
        }
 
155
        z->c--;
 
156
        return 1;
 
157
}
 
158
 
 
159
int
 
160
eq_s(struct SN_env * z, int s_size, symbol * s)
 
161
{
 
162
        if (z->l - z->c < s_size ||
 
163
                memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0)
 
164
                return 0;
 
165
        z->c += s_size;
 
166
        return 1;
 
167
}
 
168
 
 
169
int
 
170
eq_s_b(struct SN_env * z, int s_size, symbol * s)
 
171
{
 
172
        if (z->c - z->lb < s_size ||
 
173
                memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0)
 
174
                return 0;
 
175
        z->c -= s_size;
 
176
        return 1;
 
177
}
 
178
 
 
179
int
 
180
eq_v(struct SN_env * z, symbol * p)
 
181
{
 
182
        return eq_s(z, SIZE(p), p);
 
183
}
 
184
 
 
185
int
 
186
eq_v_b(struct SN_env * z, symbol * p)
 
187
{
 
188
        return eq_s_b(z, SIZE(p), p);
 
189
}
 
190
 
 
191
int
 
192
find_among(struct SN_env * z, struct among * v, int v_size)
 
193
{
 
194
        int                     i = 0;
 
195
        int                     j = v_size;
 
196
 
 
197
        int                     c = z->c;
 
198
        int                     l = z->l;
 
199
        symbol     *q = z->p + c;
 
200
 
 
201
        struct among *w;
 
202
 
 
203
        int                     common_i = 0;
 
204
        int                     common_j = 0;
 
205
 
 
206
        int                     first_key_inspected = 0;
 
207
 
 
208
        while (1)
 
209
        {
 
210
                int                     k = i + ((j - i) >> 1);
 
211
                int                     diff = 0;
 
212
                int                     common = common_i < common_j ? common_i : common_j; /* smaller */
 
213
 
 
214
                w = v + k;
 
215
                {
 
216
                        int                     i;
 
217
 
 
218
                        for (i = common; i < w->s_size; i++)
 
219
                        {
 
220
                                if (c + common == l)
 
221
                                {
 
222
                                        diff = -1;
 
223
                                        break;
 
224
                                }
 
225
                                diff = q[common] - w->s[i];
 
226
                                if (diff != 0)
 
227
                                        break;
 
228
                                common++;
 
229
                        }
 
230
                }
 
231
                if (diff < 0)
 
232
                {
 
233
                        j = k;
 
234
                        common_j = common;
 
235
                }
 
236
                else
 
237
                {
 
238
                        i = k;
 
239
                        common_i = common;
 
240
                }
 
241
                if (j - i <= 1)
 
242
                {
 
243
                        if (i > 0)
 
244
                                break;                  /* v->s has been inspected */
 
245
                        if (j == i)
 
246
                                break;                  /* only one item in v */
 
247
 
 
248
                        /*
 
249
                         * - but now we need to go round once more to get v->s
 
250
                         * inspected. This looks messy, but is actually the optimal
 
251
                         * approach.
 
252
                         */
 
253
 
 
254
                        if (first_key_inspected)
 
255
                                break;
 
256
                        first_key_inspected = 1;
 
257
                }
 
258
        }
 
259
        while (1)
 
260
        {
 
261
                w = v + i;
 
262
                if (common_i >= w->s_size)
 
263
                {
 
264
                        z->c = c + w->s_size;
 
265
                        if (w->function == 0)
 
266
                                return w->result;
 
267
                        {
 
268
                                int                     res = w->function(z);
 
269
 
 
270
                                z->c = c + w->s_size;
 
271
                                if (res)
 
272
                                        return w->result;
 
273
                        }
 
274
                }
 
275
                i = w->substring_i;
 
276
                if (i < 0)
 
277
                        return 0;
 
278
        }
 
279
}
 
280
 
 
281
/* find_among_b is for backwards processing. Same comments apply */
 
282
 
 
283
int
 
284
find_among_b(struct SN_env * z, struct among * v, int v_size)
 
285
{
 
286
        int                     i = 0;
 
287
        int                     j = v_size;
 
288
 
 
289
        int                     c = z->c;
 
290
        int                     lb = z->lb;
 
291
        symbol     *q = z->p + c - 1;
 
292
 
 
293
        struct among *w;
 
294
 
 
295
        int                     common_i = 0;
 
296
        int                     common_j = 0;
 
297
 
 
298
        int                     first_key_inspected = 0;
 
299
 
 
300
        while (1)
 
301
        {
 
302
                int                     k = i + ((j - i) >> 1);
 
303
                int                     diff = 0;
 
304
                int                     common = common_i < common_j ? common_i : common_j;
 
305
 
 
306
                w = v + k;
 
307
                {
 
308
                        int                     i;
 
309
 
 
310
                        for (i = w->s_size - 1 - common; i >= 0; i--)
 
311
                        {
 
312
                                if (c - common == lb)
 
313
                                {
 
314
                                        diff = -1;
 
315
                                        break;
 
316
                                }
 
317
                                diff = q[-common] - w->s[i];
 
318
                                if (diff != 0)
 
319
                                        break;
 
320
                                common++;
 
321
                        }
 
322
                }
 
323
                if (diff < 0)
 
324
                {
 
325
                        j = k;
 
326
                        common_j = common;
 
327
                }
 
328
                else
 
329
                {
 
330
                        i = k;
 
331
                        common_i = common;
 
332
                }
 
333
                if (j - i <= 1)
 
334
                {
 
335
                        if (i > 0)
 
336
                                break;
 
337
                        if (j == i)
 
338
                                break;
 
339
                        if (first_key_inspected)
 
340
                                break;
 
341
                        first_key_inspected = 1;
 
342
                }
 
343
        }
 
344
        while (1)
 
345
        {
 
346
                w = v + i;
 
347
                if (common_i >= w->s_size)
 
348
                {
 
349
                        z->c = c - w->s_size;
 
350
                        if (w->function == 0)
 
351
                                return w->result;
 
352
                        {
 
353
                                int                     res = w->function(z);
 
354
 
 
355
                                z->c = c - w->s_size;
 
356
                                if (res)
 
357
                                        return w->result;
 
358
                        }
 
359
                }
 
360
                i = w->substring_i;
 
361
                if (i < 0)
 
362
                        return 0;
 
363
        }
 
364
}
 
365
 
 
366
 
 
367
symbol *
 
368
increase_size(symbol * p, int n)
 
369
{
 
370
        int                     new_size = n + 20;
 
371
        symbol     *q = (symbol *) (HEAD + (char *) malloc(HEAD + (new_size + 1) * sizeof(symbol)));
 
372
 
 
373
        CAPACITY(q) = new_size;
 
374
        memmove(q, p, CAPACITY(p) * sizeof(symbol));
 
375
        lose_s(p);
 
376
        return q;
 
377
}
 
378
 
 
379
/* to replace symbols between c_bra and c_ket in z->p by the
 
380
   s_size symbols at s
 
381
*/
 
382
 
 
383
int
 
384
replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s)
 
385
{
 
386
        int                     adjustment = s_size - (c_ket - c_bra);
 
387
        int                     len = SIZE(z->p);
 
388
 
 
389
        if (adjustment != 0)
 
390
        {
 
391
                if (adjustment + len > CAPACITY(z->p))
 
392
                        z->p = increase_size(z->p, adjustment + len);
 
393
                memmove(z->p + c_ket + adjustment, z->p + c_ket, (len - c_ket) * sizeof(symbol));
 
394
                SET_SIZE(z->p, adjustment + len);
 
395
                z->l += adjustment;
 
396
                if (z->c >= c_ket)
 
397
                        z->c += adjustment;
 
398
                else if (z->c > c_bra)
 
399
                        z->c = c_bra;
 
400
        }
 
401
        unless(s_size == 0) memmove(z->p + c_bra, s, s_size * sizeof(symbol));
 
402
        return adjustment;
 
403
}
 
404
 
 
405
static void
 
406
slice_check(struct SN_env * z)
 
407
{
 
408
        if (!(0 <= z->bra &&
 
409
                  z->bra <= z->ket &&
 
410
                  z->ket <= z->l &&
 
411
                  z->l <= SIZE(z->p)))  /* this line could be removed */
 
412
        {
 
413
                fprintf(stderr, "faulty slice operation:\n");
 
414
                debug(z, -1, 0);
 
415
                exit(1);
 
416
        }
 
417
}
 
418
 
 
419
void
 
420
slice_from_s(struct SN_env * z, int s_size, symbol * s)
 
421
{
 
422
        slice_check(z);
 
423
        replace_s(z, z->bra, z->ket, s_size, s);
 
424
}
 
425
 
 
426
void
 
427
slice_from_v(struct SN_env * z, symbol * p)
 
428
{
 
429
        slice_from_s(z, SIZE(p), p);
 
430
}
 
431
 
 
432
void
 
433
slice_del(struct SN_env * z)
 
434
{
 
435
        slice_from_s(z, 0, NULL);
 
436
}
 
437
 
 
438
void
 
439
insert_s(struct SN_env * z, int bra, int ket, int s_size, symbol * s)
 
440
{
 
441
        int                     adjustment = replace_s(z, bra, ket, s_size, s);
 
442
 
 
443
        if (bra <= z->bra)
 
444
                z->bra += adjustment;
 
445
        if (bra <= z->ket)
 
446
                z->ket += adjustment;
 
447
}
 
448
 
 
449
void
 
450
insert_v(struct SN_env * z, int bra, int ket, symbol * p)
 
451
{
 
452
        int                     adjustment = replace_s(z, bra, ket, SIZE(p), p);
 
453
 
 
454
        if (bra <= z->bra)
 
455
                z->bra += adjustment;
 
456
        if (bra <= z->ket)
 
457
                z->ket += adjustment;
 
458
}
 
459
 
 
460
symbol *
 
461
slice_to(struct SN_env * z, symbol * p)
 
462
{
 
463
        slice_check(z);
 
464
        {
 
465
                int                     len = z->ket - z->bra;
 
466
 
 
467
                if (CAPACITY(p) < len)
 
468
                        p = increase_size(p, len);
 
469
                memmove(p, z->p + z->bra, len * sizeof(symbol));
 
470
                SET_SIZE(p, len);
 
471
        }
 
472
        return p;
 
473
}
 
474
 
 
475
symbol *
 
476
assign_to(struct SN_env * z, symbol * p)
 
477
{
 
478
        int                     len = z->l;
 
479
 
 
480
        if (CAPACITY(p) < len)
 
481
                p = increase_size(p, len);
 
482
        memmove(p, z->p, len * sizeof(symbol));
 
483
        SET_SIZE(p, len);
 
484
        return p;
 
485
}
 
486
 
 
487
void
 
488
debug(struct SN_env * z, int number, int line_count)
 
489
{
 
490
        int                     i;
 
491
        int                     limit = SIZE(z->p);
 
492
 
 
493
        /* if (number >= 0) printf("%3d (line %4d): '", number, line_count); */
 
494
        if (number >= 0)
 
495
                printf("%3d (line %4d): [%d]'", number, line_count, limit);
 
496
        for (i = 0; i <= limit; i++)
 
497
        {
 
498
                if (z->lb == i)
 
499
                        printf("{");
 
500
                if (z->bra == i)
 
501
                        printf("[");
 
502
                if (z->c == i)
 
503
                        printf("|");
 
504
                if (z->ket == i)
 
505
                        printf("]");
 
506
                if (z->l == i)
 
507
                        printf("}");
 
508
                if (i < limit)
 
509
                {
 
510
                        int                     ch = z->p[i];
 
511
 
 
512
                        if (ch == 0)
 
513
                                ch = '#';
 
514
                        printf("%c", ch);
 
515
                }
 
516
        }
 
517
        printf("'\n");
 
518
}