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