Annotation of parser3/src/lib/cord/cordbscs.c, revision 1.2.4.2
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.2 ! paf 315: CORD CORD_from_fn_gen(CORD_fn fn, void * client_data, size_t len)
1.2.4.1 paf 316: {
317: register struct Function * result;
1.2.4.2 ! paf 318: if (len <= 0) return(0);
1.2.4.1 paf 319:
320: result = GC_NEW(struct Function);
321: if (result == 0) OUT_OF_MEMORY;
322: result->header = FN_HDR;
323: /* depth is already 0 */
324: result->len = len;
325: result->fn = fn;
326: result->client_data = client_data;
327: return((CORD) result);
328: }
329:
1.2 paf 330: size_t CORD_len(CORD x)
331: {
332: if (x == 0) {
333: return(0);
334: } else {
335: return(GEN_LEN(x));
336: }
337: }
338:
339: struct substr_args {
340: CordRep * sa_cord;
341: size_t sa_index;
342: };
343:
344: char CORD_index_access_fn(size_t i, void * client_data)
345: {
346: register struct substr_args *descr = (struct substr_args *)client_data;
347:
348: return(((char *)(descr->sa_cord))[i + descr->sa_index]);
349: }
350:
351: char CORD_apply_access_fn(size_t i, void * client_data)
352: {
353: register struct substr_args *descr = (struct substr_args *)client_data;
354: register struct Function * fn_cord = &(descr->sa_cord->function);
355:
356: return((*(fn_cord->fn))(i + descr->sa_index, fn_cord->client_data));
357: }
358:
359: /* A version of CORD_substr that simply returns a function node, thus */
360: /* postponing its work. The fourth argument is a function that may */
361: /* be used for efficient access to the ith character. */
362: /* Assumes i >= 0 and i + n < length(x). */
363: CORD CORD_substr_closure(CORD x, size_t i, size_t n, CORD_fn f)
364: {
365: register struct substr_args * sa = GC_NEW(struct substr_args);
366: CORD result;
367:
368: if (sa == 0) OUT_OF_MEMORY;
369: sa->sa_cord = (CordRep *)x;
370: sa->sa_index = i;
371: result = CORD_from_fn(f, (void *)sa, n);
372: ((CordRep *)result) -> function.header = SUBSTR_HDR;
373: return (result);
374: }
375:
376: # define SUBSTR_LIMIT (10 * SHORT_LIMIT)
377: /* Substrings of function nodes and flat strings shorter than */
378: /* this are flat strings. Othewise we use a functional */
379: /* representation, which is significantly slower to access. */
380:
381: /* A version of CORD_substr that assumes i >= 0, n > 0, and i + n < length(x).*/
382: CORD CORD_substr_checked(CORD x, size_t i, size_t n)
383: {
384: if (CORD_IS_STRING(x)) {
385: if (n > SUBSTR_LIMIT) {
386: return(CORD_substr_closure(x, i, n, CORD_index_access_fn));
387: } else {
388: register char * result = GC_MALLOC_ATOMIC(n+1);
389:
390: if (result == 0) OUT_OF_MEMORY;
391: strncpy(result, x+i, n);
392: result[n] = '\0';
393: return(result);
394: }
395: } else if (IS_CONCATENATION(x)) {
396: register struct Concatenation * conc
397: = &(((CordRep *)x) -> concatenation);
398: register size_t left_len;
399: register size_t right_len;
400:
401: left_len = LEFT_LEN(conc);
402: right_len = conc -> len - left_len;
403: if (i >= left_len) {
404: if (n == right_len) return(conc -> right);
405: return(CORD_substr_checked(conc -> right, i - left_len, n));
406: } else if (i+n <= left_len) {
407: if (n == left_len) return(conc -> left);
408: return(CORD_substr_checked(conc -> left, i, n));
409: } else {
410: /* Need at least one character from each side. */
411: register CORD left_part;
412: register CORD right_part;
413: register size_t left_part_len = left_len - i;
414:
415: if (i == 0) {
416: left_part = conc -> left;
417: } else {
418: left_part = CORD_substr_checked(conc -> left, i, left_part_len);
419: }
420: if (i + n == right_len + left_len) {
421: right_part = conc -> right;
422: } else {
423: right_part = CORD_substr_checked(conc -> right, 0,
424: n - left_part_len);
425: }
426: return(CORD_cat(left_part, right_part));
427: }
428: } else /* function */ {
429: if (n > SUBSTR_LIMIT) {
430: if (IS_SUBSTR(x)) {
431: /* Avoid nesting substring nodes. */
432: register struct Function * f = &(((CordRep *)x) -> function);
433: register struct substr_args *descr =
434: (struct substr_args *)(f -> client_data);
435:
436: return(CORD_substr_closure((CORD)descr->sa_cord,
437: i + descr->sa_index,
438: n, f -> fn));
439: } else {
440: return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
441: }
442: } else {
443: char * result;
444: register struct Function * f = &(((CordRep *)x) -> function);
445: char buf[SUBSTR_LIMIT+1];
446: register char * p = buf;
447: register char c;
448: register int j;
449: register int lim = i + n;
450:
451: for (j = i; j < lim; j++) {
452: c = (*(f -> fn))(j, f -> client_data);
453: if (c == '\0') {
454: return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
455: }
456: *p++ = c;
457: }
458: *p = '\0';
459: result = GC_MALLOC_ATOMIC(n+1);
460: if (result == 0) OUT_OF_MEMORY;
461: strcpy(result, buf);
462: return(result);
463: }
464: }
465: }
466:
467: CORD CORD_substr(CORD x, size_t i, size_t n)
468: {
469: register size_t len = CORD_len(x);
470:
471: if (i >= len || n <= 0) return(0);
472: /* n < 0 is impossible in a correct C implementation, but */
473: /* quite possible under SunOS 4.X. */
474: if (i + n > len) n = len - i;
475: # ifndef __STDC__
476: if (i < 0) ABORT("CORD_substr: second arg. negative");
477: /* Possible only if both client and C implementation are buggy. */
478: /* But empirically this happens frequently. */
479: # endif
480: return(CORD_substr_checked(x, i, n));
481: }
482:
483: /* See cord.h for definition. We assume i is in range. */
484: int CORD_iter5(CORD x, size_t i, CORD_iter_fn f1,
485: CORD_batched_iter_fn f2, void * client_data)
486: {
487: if (x == 0) return(0);
488: if (CORD_IS_STRING(x)) {
489: register const char* p = x+i;
490:
491: if (*p == '\0') ABORT("2nd arg to CORD_iter5 too big");
492: if (f2 != CORD_NO_FN) {
493: return((*f2)(p, client_data));
494: } else {
495: while (*p) {
496: if ((*f1)(*p, client_data)) return(1);
497: p++;
498: }
499: return(0);
500: }
501: } else if (IS_CONCATENATION(x)) {
502: register struct Concatenation * conc
503: = &(((CordRep *)x) -> concatenation);
504:
505:
506: if (i > 0) {
507: register size_t left_len = LEFT_LEN(conc);
508:
509: if (i >= left_len) {
510: return(CORD_iter5(conc -> right, i - left_len, f1, f2,
511: client_data));
512: }
513: }
514: if (CORD_iter5(conc -> left, i, f1, f2, client_data)) {
515: return(1);
516: }
517: return(CORD_iter5(conc -> right, 0, f1, f2, client_data));
518: } else /* function */ {
519: register struct Function * f = &(((CordRep *)x) -> function);
520: register size_t j;
521: register size_t lim = f -> len;
522:
523: for (j = i; j < lim; j++) {
524: if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
525: return(1);
526: }
527: }
528: return(0);
1.2.4.1 paf 529: }
530: }
531:
1.2.4.2 ! paf 532: #define LEFT_BLOCK_LEN(c) ((c) -> left_len != 0? \
! 533: (c) -> left_len \
! 534: : LEN((c) -> left))
1.2.4.1 paf 535: /* See cord.h for definition. We assume i is in range. */
536: int CORD_block_iter(CORD x, size_t i, CORD_block_iter_fn f1, void * client_data)
537: {
538: if (x == 0) return(0);
1.2.4.2 ! paf 539: //assert(!CORD_IS_STRING(x));
1.2.4.1 paf 540:
541: if (IS_CONCATENATION(x)) {
542: register struct Concatenation * conc
543: = &(((CordRep *)x) -> concatenation);
544: register int result;
545:
546: if (i > 0) {
547: register size_t left_len = LEFT_BLOCK_LEN(conc);
548:
549: if (i >= left_len)
550: return CORD_block_iter(conc -> right, i - left_len, f1,
551: client_data);
552: }
553: if (result=CORD_block_iter(conc -> left, i, f1, client_data))
554: return result;
555:
556: return(CORD_block_iter(conc -> right, 0, f1, client_data));
557: } else /* function =block */ {
558: register struct Function * f = &(((CordRep *)x) -> function);
559:
560: return f1((char)(unsigned long)f -> client_data, f -> len-i, client_data);
1.2 paf 561: }
562: }
563:
564: #undef CORD_iter
565: int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data)
566: {
567: return(CORD_iter5(x, 0, f1, CORD_NO_FN, client_data));
568: }
569:
570: int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data)
571: {
572: if (x == 0) return(0);
573: if (CORD_IS_STRING(x)) {
574: register const char* p = x + i;
575: register char c;
576:
577: for(;;) {
578: c = *p;
579: if (c == '\0') ABORT("2nd arg to CORD_riter4 too big");
580: if ((*f1)(c, client_data)) return(1);
581: if (p == x) break;
582: p--;
583: }
584: return(0);
585: } else if (IS_CONCATENATION(x)) {
586: register struct Concatenation * conc
587: = &(((CordRep *)x) -> concatenation);
588: register CORD left_part = conc -> left;
589: register size_t left_len;
590:
591: left_len = LEFT_LEN(conc);
592: if (i >= left_len) {
593: if (CORD_riter4(conc -> right, i - left_len, f1, client_data)) {
594: return(1);
595: }
596: return(CORD_riter4(left_part, left_len - 1, f1, client_data));
597: } else {
598: return(CORD_riter4(left_part, i, f1, client_data));
599: }
600: } else /* function */ {
601: register struct Function * f = &(((CordRep *)x) -> function);
602: register size_t j;
603:
604: for (j = i; ; j--) {
605: if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
606: return(1);
607: }
608: if (j == 0) return(0);
609: }
610: }
611: }
612:
613: int CORD_riter(CORD x, CORD_iter_fn f1, void * client_data)
614: {
615: return(CORD_riter4(x, CORD_len(x) - 1, f1, client_data));
616: }
617:
618: /*
619: * The following functions are concerned with balancing cords.
620: * Strategy:
621: * Scan the cord from left to right, keeping the cord scanned so far
622: * as a forest of balanced trees of exponentialy decreasing length.
623: * When a new subtree needs to be added to the forest, we concatenate all
624: * shorter ones to the new tree in the appropriate order, and then insert
625: * the result into the forest.
626: * Crucial invariants:
627: * 1. The concatenation of the forest (in decreasing order) with the
628: * unscanned part of the rope is equal to the rope being balanced.
629: * 2. All trees in the forest are balanced.
630: * 3. forest[i] has depth at most i.
631: */
632:
633: typedef struct {
634: CORD c;
635: size_t len; /* Actual length of c */
636: } ForestElement;
637:
638: static size_t min_len [ MAX_DEPTH ];
639:
640: static int min_len_init = 0;
641:
642: int CORD_max_len;
643:
644: typedef ForestElement Forest [ MAX_DEPTH ];
645: /* forest[i].len >= fib(i+1) */
646: /* The string is the concatenation */
647: /* of the forest in order of DECREASING */
648: /* indices. */
649:
650: void CORD_init_min_len()
651: {
652: register int i;
653: register size_t last, previous, current;
654:
655: min_len[0] = previous = 1;
656: min_len[1] = last = 2;
657: for (i = 2; i < MAX_DEPTH; i++) {
658: current = last + previous;
659: if (current < last) /* overflow */ current = last;
660: min_len[i] = current;
661: previous = last;
662: last = current;
663: }
664: CORD_max_len = last - 1;
665: min_len_init = 1;
666: }
667:
668:
669: void CORD_init_forest(ForestElement * forest, size_t max_len)
670: {
671: register int i;
672:
673: for (i = 0; i < MAX_DEPTH; i++) {
674: forest[i].c = 0;
675: if (min_len[i] > max_len) return;
676: }
677: ABORT("Cord too long");
678: }
679:
680: /* Add a leaf to the appropriate level in the forest, cleaning */
681: /* out lower levels as necessary. */
682: /* Also works if x is a balanced tree of concatenations; however */
683: /* in this case an extra concatenation node may be inserted above x; */
684: /* This node should not be counted in the statement of the invariants. */
685: void CORD_add_forest(ForestElement * forest, CORD x, size_t len)
686: {
687: register int i = 0;
688: register CORD sum = CORD_EMPTY;
689: register size_t sum_len = 0;
690:
691: while (len > min_len[i + 1]) {
692: if (forest[i].c != 0) {
693: sum = CORD_cat(forest[i].c, sum);
694: sum_len += forest[i].len;
695: forest[i].c = 0;
696: }
697: i++;
698: }
699: /* Sum has depth at most 1 greter than what would be required */
700: /* for balance. */
701: sum = CORD_cat(sum, x);
702: sum_len += len;
703: /* If x was a leaf, then sum is now balanced. To see this */
704: /* consider the two cases in which forest[i-1] either is or is */
705: /* not empty. */
706: while (sum_len >= min_len[i]) {
707: if (forest[i].c != 0) {
708: sum = CORD_cat(forest[i].c, sum);
709: sum_len += forest[i].len;
710: /* This is again balanced, since sum was balanced, and has */
711: /* allowable depth that differs from i by at most 1. */
712: forest[i].c = 0;
713: }
714: i++;
715: }
716: i--;
717: forest[i].c = sum;
718: forest[i].len = sum_len;
719: }
720:
721: CORD CORD_concat_forest(ForestElement * forest, size_t expected_len)
722: {
723: register int i = 0;
724: CORD sum = 0;
725: size_t sum_len = 0;
726:
727: while (sum_len != expected_len) {
728: if (forest[i].c != 0) {
729: sum = CORD_cat(forest[i].c, sum);
730: sum_len += forest[i].len;
731: }
732: i++;
733: }
734: return(sum);
735: }
736:
737: /* Insert the frontier of x into forest. Balanced subtrees are */
738: /* treated as leaves. This potentially adds one to the depth */
739: /* of the final tree. */
740: void CORD_balance_insert(CORD x, size_t len, ForestElement * forest)
741: {
742: register int depth;
743:
744: if (CORD_IS_STRING(x)) {
745: CORD_add_forest(forest, x, len);
746: } else if (IS_CONCATENATION(x)
747: && ((depth = DEPTH(x)) >= MAX_DEPTH
748: || len < min_len[depth])) {
749: register struct Concatenation * conc
750: = &(((CordRep *)x) -> concatenation);
751: size_t left_len = LEFT_LEN(conc);
752:
753: CORD_balance_insert(conc -> left, left_len, forest);
754: CORD_balance_insert(conc -> right, len - left_len, forest);
755: } else /* function or balanced */ {
756: CORD_add_forest(forest, x, len);
757: }
758: }
759:
760:
761: CORD CORD_balance(CORD x)
762: {
763: Forest forest;
764: register size_t len;
765:
766: if (x == 0) return(0);
767: if (CORD_IS_STRING(x)) return(x);
768: if (!min_len_init) CORD_init_min_len();
769: len = LEN(x);
770: CORD_init_forest(forest, len);
771: CORD_balance_insert(x, len, forest);
772: return(CORD_concat_forest(forest, len));
773: }
774:
775:
776: /* Position primitives */
777:
778: /* Private routines to deal with the hard cases only: */
779:
780: /* P contains a prefix of the path to cur_pos. Extend it to a full */
781: /* path and set up leaf info. */
782: /* Return 0 if past the end of cord, 1 o.w. */
783: void CORD__extend_path(register CORD_pos p)
784: {
785: register struct CORD_pe * current_pe = &(p[0].path[p[0].path_len]);
786: register CORD top = current_pe -> pe_cord;
787: register size_t pos = p[0].cur_pos;
788: register size_t top_pos = current_pe -> pe_start_pos;
789: register size_t top_len = GEN_LEN(top);
790:
791: /* Fill in the rest of the path. */
792: while(!CORD_IS_STRING(top) && IS_CONCATENATION(top)) {
793: register struct Concatenation * conc =
794: &(((CordRep *)top) -> concatenation);
795: register size_t left_len;
796:
797: left_len = LEFT_LEN(conc);
798: current_pe++;
799: if (pos >= top_pos + left_len) {
800: current_pe -> pe_cord = top = conc -> right;
801: current_pe -> pe_start_pos = top_pos = top_pos + left_len;
802: top_len -= left_len;
803: } else {
804: current_pe -> pe_cord = top = conc -> left;
805: current_pe -> pe_start_pos = top_pos;
806: top_len = left_len;
807: }
808: p[0].path_len++;
809: }
810: /* Fill in leaf description for fast access. */
811: if (CORD_IS_STRING(top)) {
812: p[0].cur_leaf = top;
813: p[0].cur_start = top_pos;
814: p[0].cur_end = top_pos + top_len;
815: } else {
816: p[0].cur_end = 0;
817: }
818: if (pos >= top_pos + top_len) p[0].path_len = CORD_POS_INVALID;
819: }
820:
821: char CORD__pos_fetch(register CORD_pos p)
822: {
823: /* Leaf is a function node */
824: struct CORD_pe * pe = &((p)[0].path[(p)[0].path_len]);
825: CORD leaf = pe -> pe_cord;
826: register struct Function * f = &(((CordRep *)leaf) -> function);
827:
828: if (!IS_FUNCTION(leaf)) ABORT("CORD_pos_fetch: bad leaf");
829: return ((*(f -> fn))(p[0].cur_pos - pe -> pe_start_pos, f -> client_data));
830: }
831:
832: void CORD__next(register CORD_pos p)
833: {
834: register size_t cur_pos = p[0].cur_pos + 1;
835: register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
836: register CORD leaf = current_pe -> pe_cord;
837:
838: /* Leaf is not a string or we're at end of leaf */
839: p[0].cur_pos = cur_pos;
840: if (!CORD_IS_STRING(leaf)) {
841: /* Function leaf */
842: register struct Function * f = &(((CordRep *)leaf) -> function);
843: register size_t start_pos = current_pe -> pe_start_pos;
844: register size_t end_pos = start_pos + f -> len;
845:
846: if (cur_pos < end_pos) {
847: /* Fill cache and return. */
848: register size_t i;
849: register size_t limit = cur_pos + FUNCTION_BUF_SZ;
850: register CORD_fn fn = f -> fn;
851: register void * client_data = f -> client_data;
852:
853: if (limit > end_pos) {
854: limit = end_pos;
855: }
856: for (i = cur_pos; i < limit; i++) {
857: p[0].function_buf[i - cur_pos] =
858: (*fn)(i - start_pos, client_data);
859: }
860: p[0].cur_start = cur_pos;
861: p[0].cur_leaf = p[0].function_buf;
862: p[0].cur_end = limit;
863: return;
864: }
865: }
866: /* End of leaf */
867: /* Pop the stack until we find two concatenation nodes with the */
868: /* same start position: this implies we were in left part. */
869: {
870: while (p[0].path_len > 0
871: && current_pe[0].pe_start_pos != current_pe[-1].pe_start_pos) {
872: p[0].path_len--;
873: current_pe--;
874: }
875: if (p[0].path_len == 0) {
876: p[0].path_len = CORD_POS_INVALID;
877: return;
878: }
879: }
880: p[0].path_len--;
881: CORD__extend_path(p);
882: }
883:
884: void CORD__prev(register CORD_pos p)
885: {
886: register struct CORD_pe * pe = &(p[0].path[p[0].path_len]);
887:
888: if (p[0].cur_pos == 0) {
889: p[0].path_len = CORD_POS_INVALID;
890: return;
891: }
892: p[0].cur_pos--;
893: if (p[0].cur_pos >= pe -> pe_start_pos) return;
894:
895: /* Beginning of leaf */
896:
897: /* Pop the stack until we find two concatenation nodes with the */
898: /* different start position: this implies we were in right part. */
899: {
900: register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
901:
902: while (p[0].path_len > 0
903: && current_pe[0].pe_start_pos == current_pe[-1].pe_start_pos) {
904: p[0].path_len--;
905: current_pe--;
906: }
907: }
908: p[0].path_len--;
909: CORD__extend_path(p);
910: }
911:
912: #undef CORD_pos_fetch
913: #undef CORD_next
914: #undef CORD_prev
915: #undef CORD_pos_to_index
916: #undef CORD_pos_to_cord
917: #undef CORD_pos_valid
918:
919: char CORD_pos_fetch(register CORD_pos p)
920: {
921: if (p[0].cur_start <= p[0].cur_pos && p[0].cur_pos < p[0].cur_end) {
922: return(p[0].cur_leaf[p[0].cur_pos - p[0].cur_start]);
923: } else {
924: return(CORD__pos_fetch(p));
925: }
926: }
927:
928: void CORD_next(CORD_pos p)
929: {
930: if (p[0].cur_pos < p[0].cur_end - 1) {
931: p[0].cur_pos++;
932: } else {
933: CORD__next(p);
934: }
935: }
936:
937: void CORD_prev(CORD_pos p)
938: {
939: if (p[0].cur_end != 0 && p[0].cur_pos > p[0].cur_start) {
940: p[0].cur_pos--;
941: } else {
942: CORD__prev(p);
943: }
944: }
945:
946: size_t CORD_pos_to_index(CORD_pos p)
947: {
948: return(p[0].cur_pos);
949: }
950:
951: CORD CORD_pos_to_cord(CORD_pos p)
952: {
953: return(p[0].path[0].pe_cord);
954: }
955:
956: int CORD_pos_valid(CORD_pos p)
957: {
958: return(p[0].path_len != CORD_POS_INVALID);
959: }
960:
961: void CORD_set_pos(CORD_pos p, CORD x, size_t i)
962: {
963: if (x == CORD_EMPTY) {
964: p[0].path_len = CORD_POS_INVALID;
965: return;
966: }
967: p[0].path[0].pe_cord = x;
968: p[0].path[0].pe_start_pos = 0;
969: p[0].path_len = 0;
970: p[0].cur_pos = i;
971: CORD__extend_path(p);
972: }
E-mail: