Annotation of parser3/src/lib/cord/cordbscs.c, revision 1.3
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:
1.3 ! paf 315: CORD CORD_from_fn_gen(CORD_fn fn, void * client_data, size_t len)
! 316: {
! 317: register struct Function * result;
! 318: if (len <= 0) return(0);
! 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: {
1.3 ! paf 487: int result;
1.2 paf 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) {
1.3 ! paf 497: if (result=(*f1)(*p, client_data))
! 498: return result;
1.2 paf 499: p++;
500: }
501: return(0);
502: }
503: } else if (IS_CONCATENATION(x)) {
1.3 ! paf 504: register struct Concatenation * conc = &(((CordRep *)x) -> concatenation);
1.2 paf 505: if (i > 0) {
506: register size_t left_len = LEFT_LEN(conc);
507:
508: if (i >= left_len) {
509: return(CORD_iter5(conc -> right, i - left_len, f1, f2,
510: client_data));
511: }
512: }
1.3 ! paf 513: result=CORD_iter5(conc -> left, i, f1, f2, client_data);
! 514: if (result) return result;
1.2 paf 515: return(CORD_iter5(conc -> right, 0, f1, f2, client_data));
516: } else /* function */ {
517: register struct Function * f = &(((CordRep *)x) -> function);
518: register size_t j;
519: register size_t lim = f -> len;
520:
521: for (j = i; j < lim; j++) {
1.3 ! paf 522: if (result=(*f1)((*(f -> fn))(j, f -> client_data), client_data))
! 523: return result;
1.2 paf 524: }
525: return(0);
526: }
527: }
1.3 ! paf 528:
! 529: /* See cord.h for definition. We assume i is in range. */
! 530: int CORD_block_iter(CORD x, size_t i, CORD_block_iter_fn fb, void * client_data)
! 531: {
! 532: int result;
! 533: if (x == 0) return(0);
! 534: if (CORD_IS_STRING(x)) {
! 535: register const char* p = x+i;
! 536: const char *b=p;
! 537: char bc=*b;
! 538: int pc;
! 539:
! 540: if (bc == '\0') ABORT("2nd arg to CORD_iter5 too big");
! 541: do {
! 542: pc=*++p;
! 543: if(pc!=bc) {
! 544: if (result=fb(bc, p-b, client_data))
! 545: return result;
! 546: b=p; bc=pc;
! 547: }
! 548: } while (pc);
! 549: return(0);
! 550: } else if (IS_CONCATENATION(x)) {
! 551: register struct Concatenation * conc= &(((CordRep *)x) -> concatenation);
! 552: if (i > 0) {
! 553: register size_t left_len = LEFT_LEN(conc);
! 554:
! 555: if (i >= left_len) {
! 556: return(CORD_block_iter(conc -> right, i - left_len, fb, client_data));
! 557: }
! 558: }
! 559: result=CORD_block_iter(conc -> left, i, fb, client_data);
! 560: if (result) return result;
! 561: return(CORD_block_iter(conc -> right, 0, fb, client_data));
! 562: } else /* function */ {
! 563: register struct Function * f = &(((CordRep *)x) -> function);
! 564: register size_t lim = f -> len;
! 565:
! 566: if(f->fn == CORD_nul_func ) {
! 567: if (result=fb((char)(unsigned long)f -> client_data, f -> len-i, client_data)) return result;
! 568: } else if(f->fn == CORD_apply_access_fn) {
! 569: register struct substr_args *descr = (struct substr_args *)f->client_data;
! 570: register struct Function * fn_cord = &(descr->sa_cord->function);
! 571:
! 572: if(fn_cord->fn == CORD_nul_func ) {
! 573: if (result=fb((char)(unsigned long)fn_cord->client_data, f -> len-i, client_data))
! 574: return result;
! 575: } else
! 576: ABORT("CORD_block_iter:CORD_apply_access_fn:unknown_fn should not happen");
! 577: } else {
! 578: if(f->fn == CORD_index_access_fn)
! 579: ABORT("CORD_block_iter:CORD_index_access_fn should not happen");
! 580: ABORT("CORD_block_iter:unknown_fn should not happen");
! 581: }
! 582: }
! 583: return(0);
! 584: }
1.2 paf 585:
1.3 ! paf 586:
1.2 paf 587: #undef CORD_iter
588: int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data)
589: {
590: return(CORD_iter5(x, 0, f1, CORD_NO_FN, client_data));
591: }
592:
593: int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data)
594: {
595: if (x == 0) return(0);
596: if (CORD_IS_STRING(x)) {
597: register const char* p = x + i;
598: register char c;
599:
600: for(;;) {
601: c = *p;
602: if (c == '\0') ABORT("2nd arg to CORD_riter4 too big");
603: if ((*f1)(c, client_data)) return(1);
604: if (p == x) break;
605: p--;
606: }
607: return(0);
608: } else if (IS_CONCATENATION(x)) {
609: register struct Concatenation * conc
610: = &(((CordRep *)x) -> concatenation);
611: register CORD left_part = conc -> left;
612: register size_t left_len;
613:
614: left_len = LEFT_LEN(conc);
615: if (i >= left_len) {
616: if (CORD_riter4(conc -> right, i - left_len, f1, client_data)) {
617: return(1);
618: }
619: return(CORD_riter4(left_part, left_len - 1, f1, client_data));
620: } else {
621: return(CORD_riter4(left_part, i, f1, client_data));
622: }
623: } else /* function */ {
624: register struct Function * f = &(((CordRep *)x) -> function);
625: register size_t j;
626:
627: for (j = i; ; j--) {
628: if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
629: return(1);
630: }
631: if (j == 0) return(0);
632: }
633: }
634: }
635:
636: int CORD_riter(CORD x, CORD_iter_fn f1, void * client_data)
637: {
638: return(CORD_riter4(x, CORD_len(x) - 1, f1, client_data));
639: }
640:
641: /*
642: * The following functions are concerned with balancing cords.
643: * Strategy:
644: * Scan the cord from left to right, keeping the cord scanned so far
645: * as a forest of balanced trees of exponentialy decreasing length.
646: * When a new subtree needs to be added to the forest, we concatenate all
647: * shorter ones to the new tree in the appropriate order, and then insert
648: * the result into the forest.
649: * Crucial invariants:
650: * 1. The concatenation of the forest (in decreasing order) with the
651: * unscanned part of the rope is equal to the rope being balanced.
652: * 2. All trees in the forest are balanced.
653: * 3. forest[i] has depth at most i.
654: */
655:
656: typedef struct {
657: CORD c;
658: size_t len; /* Actual length of c */
659: } ForestElement;
660:
661: static size_t min_len [ MAX_DEPTH ];
662:
663: static int min_len_init = 0;
664:
665: int CORD_max_len;
666:
667: typedef ForestElement Forest [ MAX_DEPTH ];
668: /* forest[i].len >= fib(i+1) */
669: /* The string is the concatenation */
670: /* of the forest in order of DECREASING */
671: /* indices. */
672:
673: void CORD_init_min_len()
674: {
675: register int i;
676: register size_t last, previous, current;
677:
678: min_len[0] = previous = 1;
679: min_len[1] = last = 2;
680: for (i = 2; i < MAX_DEPTH; i++) {
681: current = last + previous;
682: if (current < last) /* overflow */ current = last;
683: min_len[i] = current;
684: previous = last;
685: last = current;
686: }
687: CORD_max_len = last - 1;
688: min_len_init = 1;
689: }
690:
691:
692: void CORD_init_forest(ForestElement * forest, size_t max_len)
693: {
694: register int i;
695:
696: for (i = 0; i < MAX_DEPTH; i++) {
697: forest[i].c = 0;
698: if (min_len[i] > max_len) return;
699: }
700: ABORT("Cord too long");
701: }
702:
703: /* Add a leaf to the appropriate level in the forest, cleaning */
704: /* out lower levels as necessary. */
705: /* Also works if x is a balanced tree of concatenations; however */
706: /* in this case an extra concatenation node may be inserted above x; */
707: /* This node should not be counted in the statement of the invariants. */
708: void CORD_add_forest(ForestElement * forest, CORD x, size_t len)
709: {
710: register int i = 0;
711: register CORD sum = CORD_EMPTY;
712: register size_t sum_len = 0;
713:
714: while (len > min_len[i + 1]) {
715: if (forest[i].c != 0) {
716: sum = CORD_cat(forest[i].c, sum);
717: sum_len += forest[i].len;
718: forest[i].c = 0;
719: }
720: i++;
721: }
722: /* Sum has depth at most 1 greter than what would be required */
723: /* for balance. */
724: sum = CORD_cat(sum, x);
725: sum_len += len;
726: /* If x was a leaf, then sum is now balanced. To see this */
727: /* consider the two cases in which forest[i-1] either is or is */
728: /* not empty. */
729: while (sum_len >= min_len[i]) {
730: if (forest[i].c != 0) {
731: sum = CORD_cat(forest[i].c, sum);
732: sum_len += forest[i].len;
733: /* This is again balanced, since sum was balanced, and has */
734: /* allowable depth that differs from i by at most 1. */
735: forest[i].c = 0;
736: }
737: i++;
738: }
739: i--;
740: forest[i].c = sum;
741: forest[i].len = sum_len;
742: }
743:
744: CORD CORD_concat_forest(ForestElement * forest, size_t expected_len)
745: {
746: register int i = 0;
747: CORD sum = 0;
748: size_t sum_len = 0;
749:
750: while (sum_len != expected_len) {
751: if (forest[i].c != 0) {
752: sum = CORD_cat(forest[i].c, sum);
753: sum_len += forest[i].len;
754: }
755: i++;
756: }
757: return(sum);
758: }
759:
760: /* Insert the frontier of x into forest. Balanced subtrees are */
761: /* treated as leaves. This potentially adds one to the depth */
762: /* of the final tree. */
763: void CORD_balance_insert(CORD x, size_t len, ForestElement * forest)
764: {
765: register int depth;
766:
767: if (CORD_IS_STRING(x)) {
768: CORD_add_forest(forest, x, len);
769: } else if (IS_CONCATENATION(x)
770: && ((depth = DEPTH(x)) >= MAX_DEPTH
771: || len < min_len[depth])) {
772: register struct Concatenation * conc
773: = &(((CordRep *)x) -> concatenation);
774: size_t left_len = LEFT_LEN(conc);
775:
776: CORD_balance_insert(conc -> left, left_len, forest);
777: CORD_balance_insert(conc -> right, len - left_len, forest);
778: } else /* function or balanced */ {
779: CORD_add_forest(forest, x, len);
780: }
781: }
782:
783:
784: CORD CORD_balance(CORD x)
785: {
786: Forest forest;
787: register size_t len;
788:
789: if (x == 0) return(0);
790: if (CORD_IS_STRING(x)) return(x);
791: if (!min_len_init) CORD_init_min_len();
792: len = LEN(x);
793: CORD_init_forest(forest, len);
794: CORD_balance_insert(x, len, forest);
795: return(CORD_concat_forest(forest, len));
796: }
797:
798:
799: /* Position primitives */
800:
801: /* Private routines to deal with the hard cases only: */
802:
803: /* P contains a prefix of the path to cur_pos. Extend it to a full */
804: /* path and set up leaf info. */
805: /* Return 0 if past the end of cord, 1 o.w. */
806: void CORD__extend_path(register CORD_pos p)
807: {
808: register struct CORD_pe * current_pe = &(p[0].path[p[0].path_len]);
809: register CORD top = current_pe -> pe_cord;
810: register size_t pos = p[0].cur_pos;
811: register size_t top_pos = current_pe -> pe_start_pos;
812: register size_t top_len = GEN_LEN(top);
813:
814: /* Fill in the rest of the path. */
815: while(!CORD_IS_STRING(top) && IS_CONCATENATION(top)) {
816: register struct Concatenation * conc =
817: &(((CordRep *)top) -> concatenation);
818: register size_t left_len;
819:
820: left_len = LEFT_LEN(conc);
821: current_pe++;
822: if (pos >= top_pos + left_len) {
823: current_pe -> pe_cord = top = conc -> right;
824: current_pe -> pe_start_pos = top_pos = top_pos + left_len;
825: top_len -= left_len;
826: } else {
827: current_pe -> pe_cord = top = conc -> left;
828: current_pe -> pe_start_pos = top_pos;
829: top_len = left_len;
830: }
831: p[0].path_len++;
832: }
833: /* Fill in leaf description for fast access. */
834: if (CORD_IS_STRING(top)) {
835: p[0].cur_leaf = top;
836: p[0].cur_start = top_pos;
837: p[0].cur_end = top_pos + top_len;
838: } else {
839: p[0].cur_end = 0;
840: }
841: if (pos >= top_pos + top_len) p[0].path_len = CORD_POS_INVALID;
842: }
843:
844: char CORD__pos_fetch(register CORD_pos p)
845: {
846: /* Leaf is a function node */
847: struct CORD_pe * pe = &((p)[0].path[(p)[0].path_len]);
848: CORD leaf = pe -> pe_cord;
849: register struct Function * f = &(((CordRep *)leaf) -> function);
850:
851: if (!IS_FUNCTION(leaf)) ABORT("CORD_pos_fetch: bad leaf");
852: return ((*(f -> fn))(p[0].cur_pos - pe -> pe_start_pos, f -> client_data));
853: }
854:
855: void CORD__next(register CORD_pos p)
856: {
857: register size_t cur_pos = p[0].cur_pos + 1;
858: register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
859: register CORD leaf = current_pe -> pe_cord;
860:
861: /* Leaf is not a string or we're at end of leaf */
862: p[0].cur_pos = cur_pos;
863: if (!CORD_IS_STRING(leaf)) {
864: /* Function leaf */
865: register struct Function * f = &(((CordRep *)leaf) -> function);
866: register size_t start_pos = current_pe -> pe_start_pos;
867: register size_t end_pos = start_pos + f -> len;
868:
869: if (cur_pos < end_pos) {
870: /* Fill cache and return. */
871: register size_t i;
872: register size_t limit = cur_pos + FUNCTION_BUF_SZ;
873: register CORD_fn fn = f -> fn;
874: register void * client_data = f -> client_data;
875:
876: if (limit > end_pos) {
877: limit = end_pos;
878: }
879: for (i = cur_pos; i < limit; i++) {
880: p[0].function_buf[i - cur_pos] =
881: (*fn)(i - start_pos, client_data);
882: }
883: p[0].cur_start = cur_pos;
884: p[0].cur_leaf = p[0].function_buf;
885: p[0].cur_end = limit;
886: return;
887: }
888: }
889: /* End of leaf */
890: /* Pop the stack until we find two concatenation nodes with the */
891: /* same start position: this implies we were in left part. */
892: {
893: while (p[0].path_len > 0
894: && current_pe[0].pe_start_pos != current_pe[-1].pe_start_pos) {
895: p[0].path_len--;
896: current_pe--;
897: }
898: if (p[0].path_len == 0) {
899: p[0].path_len = CORD_POS_INVALID;
900: return;
901: }
902: }
903: p[0].path_len--;
904: CORD__extend_path(p);
905: }
906:
907: void CORD__prev(register CORD_pos p)
908: {
909: register struct CORD_pe * pe = &(p[0].path[p[0].path_len]);
910:
911: if (p[0].cur_pos == 0) {
912: p[0].path_len = CORD_POS_INVALID;
913: return;
914: }
915: p[0].cur_pos--;
916: if (p[0].cur_pos >= pe -> pe_start_pos) return;
917:
918: /* Beginning of leaf */
919:
920: /* Pop the stack until we find two concatenation nodes with the */
921: /* different start position: this implies we were in right part. */
922: {
923: register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
924:
925: while (p[0].path_len > 0
926: && current_pe[0].pe_start_pos == current_pe[-1].pe_start_pos) {
927: p[0].path_len--;
928: current_pe--;
929: }
930: }
931: p[0].path_len--;
932: CORD__extend_path(p);
933: }
934:
935: #undef CORD_pos_fetch
936: #undef CORD_next
937: #undef CORD_prev
938: #undef CORD_pos_to_index
939: #undef CORD_pos_to_cord
940: #undef CORD_pos_valid
941:
942: char CORD_pos_fetch(register CORD_pos p)
943: {
944: if (p[0].cur_start <= p[0].cur_pos && p[0].cur_pos < p[0].cur_end) {
945: return(p[0].cur_leaf[p[0].cur_pos - p[0].cur_start]);
946: } else {
947: return(CORD__pos_fetch(p));
948: }
949: }
950:
951: void CORD_next(CORD_pos p)
952: {
953: if (p[0].cur_pos < p[0].cur_end - 1) {
954: p[0].cur_pos++;
955: } else {
956: CORD__next(p);
957: }
958: }
959:
960: void CORD_prev(CORD_pos p)
961: {
962: if (p[0].cur_end != 0 && p[0].cur_pos > p[0].cur_start) {
963: p[0].cur_pos--;
964: } else {
965: CORD__prev(p);
966: }
967: }
968:
969: size_t CORD_pos_to_index(CORD_pos p)
970: {
971: return(p[0].cur_pos);
972: }
973:
974: CORD CORD_pos_to_cord(CORD_pos p)
975: {
976: return(p[0].path[0].pe_cord);
977: }
978:
979: int CORD_pos_valid(CORD_pos p)
980: {
981: return(p[0].path_len != CORD_POS_INVALID);
982: }
983:
984: void CORD_set_pos(CORD_pos p, CORD x, size_t i)
985: {
986: if (x == CORD_EMPTY) {
987: p[0].path_len = CORD_POS_INVALID;
988: return;
989: }
990: p[0].path[0].pe_cord = x;
991: p[0].path[0].pe_start_pos = 0;
992: p[0].path_len = 0;
993: p[0].cur_pos = i;
994: CORD__extend_path(p);
995: }
E-mail: