/* is range-in-range? */ @define isrinr(r, rs) { @local n, b, e, i, x, xb, xe; n = length(rs); b = rangebeg(r); e = b+rangelen(r); for(i = 0; i < n; i++){ x = rs[i]; xb = rangebeg(x); if(xb > b) return 0; xe = xb+rangelen(x); if(b >= xb && b <= xe){ if(e <= xe) return 1; else b = xe; } } return 0; } @define time(fn) { @local s, e; s = gettimeofday(); fn(); e = gettimeofday(); printf("%d seconds %d useconds\n", (e-s)/1000000, (e-s)%1000000); return e-s; } @define dumptab(fmt, t) { foreach(@lambda(k, v){ printf(fmt, k, v); }, t); } @define dumpstats() { @local s; s = map(@lambda(k, v) { cons(k, v); }, statistics()); sort(s, @lambda(p1, p2){ @local s1, s2; s1 = cid2str(car(p1)); s2 = cid2str(car(p2)); return strcmp(s1, s2); }); foreach(@lambda(p){ printf("\t%20s\t%20u\n", car(p), cdr(p)); }, s); } @define join(ss) { @local buf, p, m; m = 0; foreach(@lambda(s) { m += length(s); }, ss); buf = mkstr(m); p = (char*)buf; foreach(@lambda(s) { putbytes(p, s); p += length(s); }, ss); return buf; } @define fread(fd, sz) { @local ss, buf; ss = []; while(sz > 0){ buf = read(fd, sz); if(buf == nil) if(length(ss) == 0) return nil; else return join(ss); append(ss, buf); sz -= length(buf); } return join(ss); } @define max(arg ...) { @local x; if(length(arg) == 0) error("wrong number of arguments to max"); @defloc r(m, ns) { @local x; if(length(ns) == 0) return m; x = pop(ns); if(x > m) r(x, ns); else r(m, ns); } x = pop(arg); r(x, arg); } @define min(arg ...) { @local x; if(length(arg) == 0) error("wrong number of arguments to min"); @defloc r(m, ns) { @local x; if(length(ns) == 0) return m; x = pop(ns); if(x < m) r(x, ns); else r(m, ns); } x = pop(arg); r(x, arg); }