Debian Codegraph @

external/bdwgc/cord/cordbscs.c

raw
1 /*
2 * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17 #ifndef CORD_BUILD
18 # define CORD_BUILD
19 #endif
20
21 # include "gc.h"
22 # include "cord.h"
23 # include <stdlib.h>
24 # include <stdio.h>
25 # include <string.h>
26
27 /* An implementation of the cord primitives. These are the only */
28 /* Functions that understand the representation. We perform only */
29 /* minimal checks on arguments to these functions. Out of bounds */
30 /* arguments to the iteration functions may result in client functions */
31 /* invoked on garbage data. In most cases, client functions should be */
32 /* programmed defensively enough that this does not result in memory */
33 /* smashes. */
34
35 typedef void (* oom_fn)(void);
36
37 oom_fn CORD_oom_fn = (oom_fn) 0;
38
39 # define OUT_OF_MEMORY { if (CORD_oom_fn != (oom_fn) 0) (*CORD_oom_fn)(); \
40 ABORT("Out of memory"); }
41 # define ABORT(msg) { fprintf(stderr, "%s\n", msg); abort(); }
42
43 typedef unsigned long word;
44
45 struct Concatenation {
46 char null;
47 char header;
48 char depth; /* concatenation nesting depth. */
49 unsigned char left_len;
50 /* Length of left child if it is sufficiently */
51 /* short; 0 otherwise. */
52 # define MAX_LEFT_LEN 255
53 word len;
54 CORD left; /* length(left) > 0 */
55 CORD right; /* length(right) > 0 */
56 };
57
58 struct Function {
59 char null;
60 char header;
61 char depth; /* always 0 */
62 char left_len; /* always 0 */
63 word len;
64 CORD_fn fn;
65 void * client_data;
66 };
67
68 struct Generic {
69 char null;
70 char header;
71 char depth;
72 char left_len;
73 word len;
74 };
75
76 typedef union {
77 struct Concatenation concatenation;
78 struct Function function;
79 struct Generic generic;
80 char string[1];
81 } CordRep;
82
83 # define CONCAT_HDR 1
84
85 # define FN_HDR 4
86 # define SUBSTR_HDR 6
87 /* Substring nodes are a special case of function nodes. */
88 /* The client_data field is known to point to a substr_args */
89 /* structure, and the function is either CORD_apply_access_fn */
90 /* or CORD_index_access_fn. */
91
92 /* The following may be applied only to function and concatenation nodes: */
93 #define IS_CONCATENATION(s) (((CordRep *)s)->generic.header == CONCAT_HDR)
94
95 #define IS_FUNCTION(s) ((((CordRep *)s)->generic.header & FN_HDR) != 0)
96
97 #define IS_SUBSTR(s) (((CordRep *)s)->generic.header == SUBSTR_HDR)
98
99 #define LEN(s) (((CordRep *)s) -> generic.len)
100 #define DEPTH(s) (((CordRep *)s) -> generic.depth)
101 #define GEN_LEN(s) (CORD_IS_STRING(s) ? strlen(s) : LEN(s))
102
103 #define LEFT_LEN(c) ((c) -> left_len != 0? \
104 (c) -> left_len \
105 : (CORD_IS_STRING((c) -> left) ? \
106 (c) -> len - GEN_LEN((c) -> right) \
107 : LEN((c) -> left)))
108
109 #define SHORT_LIMIT (sizeof(CordRep) - 1)
110 /* Cords shorter than this are C strings */
111
112
113 /* Dump the internal representation of x to stdout, with initial */
114 /* indentation level n. */
115 void CORD_dump_inner(CORD x, unsigned n)
116 {
117 size_t i;
118
119 for (i = 0; i < (size_t)n; i++) {
120 fputs(" ", stdout);
121 }
122 if (x == 0) {
123 fputs("NIL\n", stdout);
124 } else if (CORD_IS_STRING(x)) {
125 for (i = 0; i <= SHORT_LIMIT; i++) {
126 if (x[i] == '\0') break;
127 putchar(x[i]);
128 }
129 if (x[i] != '\0') fputs("...", stdout);
130 putchar('\n');
131 } else if (IS_CONCATENATION(x)) {
132 struct Concatenation * conc = &(((CordRep *)x) -> concatenation);
133
134 printf("Concatenation: %p (len: %d, depth: %d)\n",
135 (void *)x, (int)(conc -> len), (int)(conc -> depth));
136 CORD_dump_inner(conc -> left, n+1);
137 CORD_dump_inner(conc -> right, n+1);
138 } else /* function */ {
139 struct Function * func = &(((CordRep *)x) -> function);
140
141 if (IS_SUBSTR(x)) printf("(Substring) ");
142 printf("Function: %p (len: %d): ", (void *)x, (int)(func -> len));
143 for (i = 0; i < 20 && i < func -> len; i++) {
144 putchar((*(func -> fn))(i, func -> client_data));
145 }
146 if (i < func -> len) fputs("...", stdout);
147 putchar('\n');
148 }
149 }
150
151 /* Dump the internal representation of x to stdout */
152 void CORD_dump(CORD x)
153 {
154 CORD_dump_inner(x, 0);
155 fflush(stdout);
156 }
157
158 CORD CORD_cat_char_star(CORD x, const char * y, size_t leny)
159 {
160 size_t result_len;
161 size_t lenx;
162 int depth;
163
164 if (x == CORD_EMPTY) return(y);
165 if (leny == 0) return(x);
166 if (CORD_IS_STRING(x)) {
167 lenx = strlen(x);
168 result_len = lenx + leny;
169 if (result_len <= SHORT_LIMIT) {
170 char * result = (char *)GC_MALLOC_ATOMIC(result_len + 1);
171
172 if (result == 0) OUT_OF_MEMORY;
173 memcpy(result, x, lenx);
174 memcpy(result + lenx, y, leny);
175 result[result_len] = '\0';
176 return((CORD) result);
177 } else {
178 depth = 1;
179 }
180 } else {
181 CORD right;
182 CORD left;
183 char * new_right;
184
185 lenx = LEN(x);
186
187 if (leny <= SHORT_LIMIT/2
188 && IS_CONCATENATION(x)
189 && CORD_IS_STRING(right = ((CordRep *)x) -> concatenation.right)) {
190 size_t right_len;
191
192 /* Merge y into right part of x. */
193 if (!CORD_IS_STRING(left = ((CordRep *)x) -> concatenation.left)) {
194 right_len = lenx - LEN(left);
195 } else if (((CordRep *)x) -> concatenation.left_len != 0) {
196 right_len = lenx - ((CordRep *)x) -> concatenation.left_len;
197 } else {
198 right_len = strlen(right);
199 }
200 result_len = right_len + leny; /* length of new_right */
201 if (result_len <= SHORT_LIMIT) {
202 new_right = (char *)GC_MALLOC_ATOMIC(result_len + 1);
203 if (new_right == 0) OUT_OF_MEMORY;
204 memcpy(new_right, right, right_len);
205 memcpy(new_right + right_len, y, leny);
206 new_right[result_len] = '\0';
207 y = new_right;
208 leny = result_len;
209 x = left;
210 lenx -= right_len;
211 /* Now fall through to concatenate the two pieces: */
212 }
213 if (CORD_IS_STRING(x)) {
214 depth = 1;
215 } else {
216 depth = DEPTH(x) + 1;
217 }
218 } else {
219 depth = DEPTH(x) + 1;
220 }
221 result_len = lenx + leny;
222 }
223 {
224 /* The general case; lenx, result_len is known: */
225 struct Concatenation * result = GC_NEW(struct Concatenation);
226
227 if (NULL == result) OUT_OF_MEMORY;
228 result->header = CONCAT_HDR;
229 result->depth = (char)depth;
230 if (lenx <= MAX_LEFT_LEN)
231 result->left_len = (unsigned char)lenx;
232 result->len = (word)result_len;
233 result->left = x;
234 result->right = y;
235 if (depth >= MAX_DEPTH) {
236 return(CORD_balance((CORD)result));
237 } else {
238 return((CORD) result);
239 }
240 }
241 }
242
243
244 CORD CORD_cat(CORD x, CORD y)
245 {
246 size_t result_len;
247 int depth;
248 size_t lenx;
249
250 if (x == CORD_EMPTY) return(y);
251 if (y == CORD_EMPTY) return(x);
252 if (CORD_IS_STRING(y)) {
253 return(CORD_cat_char_star(x, y, strlen(y)));
254 } else if (CORD_IS_STRING(x)) {
255 lenx = strlen(x);
256 depth = DEPTH(y) + 1;
257 } else {
258 int depthy = DEPTH(y);
259
260 lenx = LEN(x);
261 depth = DEPTH(x) + 1;
262 if (depthy >= depth) depth = depthy + 1;
263 }
264 result_len = lenx + LEN(y);
265 {
266 struct Concatenation * result = GC_NEW(struct Concatenation);
267
268 if (NULL == result) OUT_OF_MEMORY;
269 result->header = CONCAT_HDR;
270 result->depth = (char)depth;
271 if (lenx <= MAX_LEFT_LEN)
272 result->left_len = (unsigned char)lenx;
273 result->len = (word)result_len;
274 result->left = x;
275 result->right = y;
276 if (depth >= MAX_DEPTH) {
277 return(CORD_balance((CORD)result));
278 } else {
279 return((CORD) result);
280 }
281 }
282 }
283
284
285 static CordRep *CORD_from_fn_inner(CORD_fn fn, void * client_data, size_t len)
286 {
287 if (len == 0) return(0);
288 if (len <= SHORT_LIMIT) {
289 char * result;
290 size_t i;
291 char buf[SHORT_LIMIT+1];
292
293 for (i = 0; i < len; i++) {
294 char c = (*fn)(i, client_data);
295
296 if (c == '\0') goto gen_case;
297 buf[i] = c;
298 }
299
300 result = (char *)GC_MALLOC_ATOMIC(len + 1);
301 if (result == 0) OUT_OF_MEMORY;
302 memcpy(result, buf, len);
303 result[len] = '\0';
304 return (CordRep *)result;
305 }
306 gen_case:
307 {
308 struct Function * result = GC_NEW(struct Function);
309
310 if (NULL == result) OUT_OF_MEMORY;
311 result->header = FN_HDR;
312 /* depth is already 0 */
313 result->len = (word)len;
314 result->fn = fn;
315 result->client_data = client_data;
316 return (CordRep *)result;
317 }
318 }
319
320 CORD CORD_from_fn(CORD_fn fn, void * client_data, size_t len)
321 {
322 return (/* const */ CORD) CORD_from_fn_inner(fn, client_data, len);
323 }
324
325 size_t CORD_len(CORD x)
326 {
327 if (x == 0) {
328 return(0);
329 } else {
330 return(GEN_LEN(x));
331 }
332 }
333
334 struct substr_args {
335 CordRep * sa_cord;
336 size_t sa_index;
337 };
338
339 char CORD_index_access_fn(size_t i, void * client_data)
340 {
341 struct substr_args *descr = (struct substr_args *)client_data;
342
343 return(((char *)(descr->sa_cord))[i + descr->sa_index]);
344 }
345
346 char CORD_apply_access_fn(size_t i, void * client_data)
347 {
348 struct substr_args *descr = (struct substr_args *)client_data;
349 struct Function * fn_cord = &(descr->sa_cord->function);
350
351 return((*(fn_cord->fn))(i + descr->sa_index, fn_cord->client_data));
352 }
353
354 /* A version of CORD_substr that simply returns a function node, thus */
355 /* postponing its work. The fourth argument is a function that may */
356 /* be used for efficient access to the ith character. */
357 /* Assumes i >= 0 and i + n < length(x). */
358 CORD CORD_substr_closure(CORD x, size_t i, size_t n, CORD_fn f)
359 {
360 struct substr_args * sa = GC_NEW(struct substr_args);
361 CordRep * result;
362
363 if (sa == 0) OUT_OF_MEMORY;
364 sa->sa_cord = (CordRep *)x;
365 sa->sa_index = i;
366 result = CORD_from_fn_inner(f, (void *)sa, n);
367 if ((CORD)result != CORD_EMPTY && 0 == result -> function.null)
368 result -> function.header = SUBSTR_HDR;
369 return (CORD)result;
370 }
371
372 # define SUBSTR_LIMIT (10 * SHORT_LIMIT)
373 /* Substrings of function nodes and flat strings shorter than */
374 /* this are flat strings. Othewise we use a functional */
375 /* representation, which is significantly slower to access. */
376
377 /* A version of CORD_substr that assumes i >= 0, n > 0, and i + n < length(x).*/
378 CORD CORD_substr_checked(CORD x, size_t i, size_t n)
379 {
380 if (CORD_IS_STRING(x)) {
381 if (n > SUBSTR_LIMIT) {
382 return(CORD_substr_closure(x, i, n, CORD_index_access_fn));
383 } else {
384 char * result = (char *)GC_MALLOC_ATOMIC(n + 1);
385
386 if (result == 0) OUT_OF_MEMORY;
387 strncpy(result, x+i, n);
388 result[n] = '\0';
389 return(result);
390 }
391 } else if (IS_CONCATENATION(x)) {
392 struct Concatenation * conc = &(((CordRep *)x) -> concatenation);
393 size_t left_len = LEFT_LEN(conc);
394 size_t right_len = conc -> len - left_len;
395
396 if (i >= left_len) {
397 if (n == right_len) return(conc -> right);
398 return(CORD_substr_checked(conc -> right, i - left_len, n));
399 } else if (i+n <= left_len) {
400 if (n == left_len) return(conc -> left);
401 return(CORD_substr_checked(conc -> left, i, n));
402 } else {
403 /* Need at least one character from each side. */
404 CORD left_part;
405 CORD right_part;
406 size_t left_part_len = left_len - i;
407
408 if (i == 0) {
409 left_part = conc -> left;
410 } else {
411 left_part = CORD_substr_checked(conc -> left, i, left_part_len);
412 }
413 if (i + n == right_len + left_len) {
414 right_part = conc -> right;
415 } else {
416 right_part = CORD_substr_checked(conc -> right, 0,
417 n - left_part_len);
418 }
419 return(CORD_cat(left_part, right_part));
420 }
421 } else /* function */ {
422 if (n > SUBSTR_LIMIT) {
423 if (IS_SUBSTR(x)) {
424 /* Avoid nesting substring nodes. */
425 struct Function * f = &(((CordRep *)x) -> function);
426 struct substr_args *descr =
427 (struct substr_args *)(f -> client_data);
428
429 return(CORD_substr_closure((CORD)descr->sa_cord,
430 i + descr->sa_index,
431 n, f -> fn));
432 } else {
433 return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
434 }
435 } else {
436 char * result;
437 struct Function * f = &(((CordRep *)x) -> function);
438 char buf[SUBSTR_LIMIT+1];
439 char * p = buf;
440 size_t j;
441 size_t lim = i + n;
442
443 for (j = i; j < lim; j++) {
444 char c = (*(f -> fn))(j, f -> client_data);
445
446 if (c == '\0') {
447 return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
448 }
449 *p++ = c;
450 }
451 result = (char *)GC_MALLOC_ATOMIC(n + 1);
452 if (result == 0) OUT_OF_MEMORY;
453 memcpy(result, buf, n);
454 result[n] = '\0';
455 return(result);
456 }
457 }
458 }
459
460 CORD CORD_substr(CORD x, size_t i, size_t n)
461 {
462 size_t len = CORD_len(x);
463
464 if (i >= len || n == 0) return(0);
465 if (i + n > len) n = len - i;
466 return(CORD_substr_checked(x, i, n));
467 }
468
469 /* See cord.h for definition. We assume i is in range. */
470 int CORD_iter5(CORD x, size_t i, CORD_iter_fn f1,
471 CORD_batched_iter_fn f2, void * client_data)
472 {
473 if (x == 0) return(0);
474 if (CORD_IS_STRING(x)) {
475 const char *p = x+i;
476
477 if (*p == '\0') ABORT("2nd arg to CORD_iter5 too big");
478 if (f2 != CORD_NO_FN) {
479 return((*f2)(p, client_data));
480 } else {
481 while (*p) {
482 if ((*f1)(*p, client_data)) return(1);
483 p++;
484 }
485 return(0);
486 }
487 } else if (IS_CONCATENATION(x)) {
488 struct Concatenation * conc = &(((CordRep *)x) -> concatenation);
489
490 if (i > 0) {
491 size_t left_len = LEFT_LEN(conc);
492
493 if (i >= left_len) {
494 return(CORD_iter5(conc -> right, i - left_len, f1, f2,
495 client_data));
496 }
497 }
498 if (CORD_iter5(conc -> left, i, f1, f2, client_data)) {
499 return(1);
500 }
501 return(CORD_iter5(conc -> right, 0, f1, f2, client_data));
502 } else /* function */ {
503 struct Function * f = &(((CordRep *)x) -> function);
504 size_t j;
505 size_t lim = f -> len;
506
507 for (j = i; j < lim; j++) {
508 if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
509 return(1);
510 }
511 }
512 return(0);
513 }
514 }
515
516 #undef CORD_iter
517 int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data)
518 {
519 return(CORD_iter5(x, 0, f1, CORD_NO_FN, client_data));
520 }
521
522 int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data)
523 {
524 if (x == 0) return(0);
525 if (CORD_IS_STRING(x)) {
526 const char *p = x + i;
527
528 for(;;) {
529 char c = *p;
530
531 if (c == '\0') ABORT("2nd arg to CORD_riter4 too big");
532 if ((*f1)(c, client_data)) return(1);
533 if (p == x) break;
534 p--;
535 }
536 return(0);
537 } else if (IS_CONCATENATION(x)) {
538 struct Concatenation * conc = &(((CordRep *)x) -> concatenation);
539 CORD left_part = conc -> left;
540 size_t left_len = LEFT_LEN(conc);
541
542 if (i >= left_len) {
543 if (CORD_riter4(conc -> right, i - left_len, f1, client_data)) {
544 return(1);
545 }
546 return(CORD_riter4(left_part, left_len - 1, f1, client_data));
547 } else {
548 return(CORD_riter4(left_part, i, f1, client_data));
549 }
550 } else /* function */ {
551 struct Function * f = &(((CordRep *)x) -> function);
552 size_t j;
553
554 for (j = i; ; j--) {
555 if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
556 return(1);
557 }
558 if (j == 0) return(0);
559 }
560 }
561 }
562
563 int CORD_riter(CORD x, CORD_iter_fn f1, void * client_data)
564 {
565 size_t len = CORD_len(x);
566 if (len == 0) return(0);
567 return(CORD_riter4(x, len - 1, f1, client_data));
568 }
569
570 /*
571 * The following functions are concerned with balancing cords.
572 * Strategy:
573 * Scan the cord from left to right, keeping the cord scanned so far
574 * as a forest of balanced trees of exponentially decreasing length.
575 * When a new subtree needs to be added to the forest, we concatenate all
576 * shorter ones to the new tree in the appropriate order, and then insert
577 * the result into the forest.
578 * Crucial invariants:
579 * 1. The concatenation of the forest (in decreasing order) with the
580 * unscanned part of the rope is equal to the rope being balanced.
581 * 2. All trees in the forest are balanced.
582 * 3. forest[i] has depth at most i.
583 */
584
585 typedef struct {
586 CORD c;
587 size_t len; /* Actual length of c */
588 } ForestElement;
589
590 static size_t min_len [ MAX_DEPTH ];
591
592 static int min_len_init = 0;
593
594 int CORD_max_len;
595
596 typedef ForestElement Forest [ MAX_DEPTH ];
597 /* forest[i].len >= fib(i+1) */
598 /* The string is the concatenation */
599 /* of the forest in order of DECREASING */
600 /* indices. */
601
602 void CORD_init_min_len(void)
603 {
604 int i;
605 size_t last, previous;
606
607 min_len[0] = previous = 1;
608 min_len[1] = last = 2;
609 for (i = 2; i < MAX_DEPTH; i++) {
610 size_t current = last + previous;
611
612 if (current < last) /* overflow */ current = last;
613 min_len[i] = current;
614 previous = last;
615 last = current;
616 }
617 CORD_max_len = (int)last - 1;
618 min_len_init = 1;
619 }
620
621
622 void CORD_init_forest(ForestElement * forest, size_t max_len)
623 {
624 int i;
625
626 for (i = 0; i < MAX_DEPTH; i++) {
627 forest[i].c = 0;
628 if (min_len[i] > max_len) return;
629 }
630 ABORT("Cord too long");
631 }
632
633 /* Add a leaf to the appropriate level in the forest, cleaning */
634 /* out lower levels as necessary. */
635 /* Also works if x is a balanced tree of concatenations; however */
636 /* in this case an extra concatenation node may be inserted above x; */
637 /* This node should not be counted in the statement of the invariants. */
638 void CORD_add_forest(ForestElement * forest, CORD x, size_t len)
639 {
640 int i = 0;
641 CORD sum = CORD_EMPTY;
642 size_t sum_len = 0;
643
644 while (len > min_len[i + 1]) {
645 if (forest[i].c != 0) {
646 sum = CORD_cat(forest[i].c, sum);
647 sum_len += forest[i].len;
648 forest[i].c = 0;
649 }
650 i++;
651 }
652 /* Sum has depth at most 1 greter than what would be required */
653 /* for balance. */
654 sum = CORD_cat(sum, x);
655 sum_len += len;
656 /* If x was a leaf, then sum is now balanced. To see this */
657 /* consider the two cases in which forest[i-1] either is or is */
658 /* not empty. */
659 while (sum_len >= min_len[i]) {
660 if (forest[i].c != 0) {
661 sum = CORD_cat(forest[i].c, sum);
662 sum_len += forest[i].len;
663 /* This is again balanced, since sum was balanced, and has */
664 /* allowable depth that differs from i by at most 1. */
665 forest[i].c = 0;
666 }
667 i++;
668 }
669 i--;
670 forest[i].c = sum;
671 forest[i].len = sum_len;
672 }
673
674 CORD CORD_concat_forest(ForestElement * forest, size_t expected_len)
675 {
676 int i = 0;
677 CORD sum = 0;
678 size_t sum_len = 0;
679
680 while (sum_len != expected_len) {
681 if (forest[i].c != 0) {
682 sum = CORD_cat(forest[i].c, sum);
683 sum_len += forest[i].len;
684 }
685 i++;
686 }
687 return(sum);
688 }
689
690 /* Insert the frontier of x into forest. Balanced subtrees are */
691 /* treated as leaves. This potentially adds one to the depth */
692 /* of the final tree. */
693 void CORD_balance_insert(CORD x, size_t len, ForestElement * forest)
694 {
695 int depth;
696
697 if (CORD_IS_STRING(x)) {
698 CORD_add_forest(forest, x, len);
699 } else if (IS_CONCATENATION(x)
700 && ((depth = DEPTH(x)) >= MAX_DEPTH
701 || len < min_len[depth])) {
702 struct Concatenation * conc = &(((CordRep *)x) -> concatenation);
703 size_t left_len = LEFT_LEN(conc);
704
705 CORD_balance_insert(conc -> left, left_len, forest);
706 CORD_balance_insert(conc -> right, len - left_len, forest);
707 } else /* function or balanced */ {
708 CORD_add_forest(forest, x, len);
709 }
710 }
711
712
713 CORD CORD_balance(CORD x)
714 {
715 Forest forest;
716 size_t len;
717
718 if (x == 0) return(0);
719 if (CORD_IS_STRING(x)) return(x);
720 if (!min_len_init) CORD_init_min_len();
721 len = LEN(x);
722 CORD_init_forest(forest, len);
723 CORD_balance_insert(x, len, forest);
724 return(CORD_concat_forest(forest, len));
725 }
726
727
728 /* Position primitives */
729
730 /* Private routines to deal with the hard cases only: */
731
732 /* P contains a prefix of the path to cur_pos. Extend it to a full */
733 /* path and set up leaf info. */
734 /* Return 0 if past the end of cord, 1 o.w. */
735 void CORD__extend_path(CORD_pos p)
736 {
737 struct CORD_pe * current_pe = &(p[0].path[p[0].path_len]);
738 CORD top = current_pe -> pe_cord;
739 size_t pos = p[0].cur_pos;
740 size_t top_pos = current_pe -> pe_start_pos;
741 size_t top_len = GEN_LEN(top);
742
743 /* Fill in the rest of the path. */
744 while(!CORD_IS_STRING(top) && IS_CONCATENATION(top)) {
745 struct Concatenation * conc = &(((CordRep *)top) -> concatenation);
746 size_t left_len;
747
748 left_len = LEFT_LEN(conc);
749 current_pe++;
750 if (pos >= top_pos + left_len) {
751 current_pe -> pe_cord = top = conc -> right;
752 current_pe -> pe_start_pos = top_pos = top_pos + left_len;
753 top_len -= left_len;
754 } else {
755 current_pe -> pe_cord = top = conc -> left;
756 current_pe -> pe_start_pos = top_pos;
757 top_len = left_len;
758 }
759 p[0].path_len++;
760 }
761 /* Fill in leaf description for fast access. */
762 if (CORD_IS_STRING(top)) {
763 p[0].cur_leaf = top;
764 p[0].cur_start = top_pos;
765 p[0].cur_end = top_pos + top_len;
766 } else {
767 p[0].cur_end = 0;
768 }
769 if (pos >= top_pos + top_len) p[0].path_len = CORD_POS_INVALID;
770 }
771
772 char CORD__pos_fetch(CORD_pos p)
773 {
774 /* Leaf is a function node */
775 struct CORD_pe * pe = &((p)[0].path[(p)[0].path_len]);
776 CORD leaf = pe -> pe_cord;
777 struct Function * f = &(((CordRep *)leaf) -> function);
778
779 if (!IS_FUNCTION(leaf)) ABORT("CORD_pos_fetch: bad leaf");
780 return ((*(f -> fn))(p[0].cur_pos - pe -> pe_start_pos, f -> client_data));
781 }
782
783 void CORD__next(CORD_pos p)
784 {
785 size_t cur_pos = p[0].cur_pos + 1;
786 struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
787 CORD leaf = current_pe -> pe_cord;
788
789 /* Leaf is not a string or we're at end of leaf */
790 p[0].cur_pos = cur_pos;
791 if (!CORD_IS_STRING(leaf)) {
792 /* Function leaf */
793 struct Function * f = &(((CordRep *)leaf) -> function);
794 size_t start_pos = current_pe -> pe_start_pos;
795 size_t end_pos = start_pos + f -> len;
796
797 if (cur_pos < end_pos) {
798 /* Fill cache and return. */
799 size_t i;
800 size_t limit = cur_pos + FUNCTION_BUF_SZ;
801 CORD_fn fn = f -> fn;
802 void * client_data = f -> client_data;
803
804 if (limit > end_pos) {
805 limit = end_pos;
806 }
807 for (i = cur_pos; i < limit; i++) {
808 p[0].function_buf[i - cur_pos] =
809 (*fn)(i - start_pos, client_data);
810 }
811 p[0].cur_start = cur_pos;
812 p[0].cur_leaf = p[0].function_buf;
813 p[0].cur_end = limit;
814 return;
815 }
816 }
817 /* End of leaf */
818 /* Pop the stack until we find two concatenation nodes with the */
819 /* same start position: this implies we were in left part. */
820 {
821 while (p[0].path_len > 0
822 && current_pe[0].pe_start_pos != current_pe[-1].pe_start_pos) {
823 p[0].path_len--;
824 current_pe--;
825 }
826 if (p[0].path_len == 0) {
827 p[0].path_len = CORD_POS_INVALID;
828 return;
829 }
830 }
831 p[0].path_len--;
832 CORD__extend_path(p);
833 }
834
835 void CORD__prev(CORD_pos p)
836 {
837 struct CORD_pe * pe = &(p[0].path[p[0].path_len]);
838
839 if (p[0].cur_pos == 0) {
840 p[0].path_len = CORD_POS_INVALID;
841 return;
842 }
843 p[0].cur_pos--;
844 if (p[0].cur_pos >= pe -> pe_start_pos) return;
845
846 /* Beginning of leaf */
847
848 /* Pop the stack until we find two concatenation nodes with the */
849 /* different start position: this implies we were in right part. */
850 {
851 struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
852
853 while (p[0].path_len > 0
854 && current_pe[0].pe_start_pos == current_pe[-1].pe_start_pos) {
855 p[0].path_len--;
856 current_pe--;
857 }
858 }
859 p[0].path_len--;
860 CORD__extend_path(p);
861 }
862
863 #undef CORD_pos_fetch
864 #undef CORD_next
865 #undef CORD_prev
866 #undef CORD_pos_to_index
867 #undef CORD_pos_to_cord
868 #undef CORD_pos_valid
869
870 char CORD_pos_fetch(CORD_pos p)
871 {
872 if (p[0].cur_end != 0) {
873 return(p[0].cur_leaf[p[0].cur_pos - p[0].cur_start]);
874 } else {
875 return(CORD__pos_fetch(p));
876 }
877 }
878
879 void CORD_next(CORD_pos p)
880 {
881 if (p[0].cur_pos + 1 < p[0].cur_end) {
882 p[0].cur_pos++;
883 } else {
884 CORD__next(p);
885 }
886 }
887
888 void CORD_prev(CORD_pos p)
889 {
890 if (p[0].cur_end != 0 && p[0].cur_pos > p[0].cur_start) {
891 p[0].cur_pos--;
892 } else {
893 CORD__prev(p);
894 }
895 }
896
897 size_t CORD_pos_to_index(CORD_pos p)
898 {
899 return(p[0].cur_pos);
900 }
901
902 CORD CORD_pos_to_cord(CORD_pos p)
903 {
904 return(p[0].path[0].pe_cord);
905 }
906
907 int CORD_pos_valid(CORD_pos p)
908 {
909 return(p[0].path_len != CORD_POS_INVALID);
910 }
911
912 void CORD_set_pos(CORD_pos p, CORD x, size_t i)
913 {
914 if (x == CORD_EMPTY) {
915 p[0].path_len = CORD_POS_INVALID;
916 return;
917 }
918 p[0].path[0].pe_cord = x;
919 p[0].path[0].pe_start_pos = 0;
920 p[0].path_len = 0;
921 p[0].cur_pos = i;
922 CORD__extend_path(p);
923 }