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