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