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