Pt_t* ptdifference(Pt_t* a, Pt_t* b) { Pt_t* t; Ptprefix_t* ap; Ptprefix_t* bp; Ptaddr_t m; if (!(t = ptopen(a->disc))) return 0; m = 0; ap = (Ptprefix_t*)dtfirst(a->dict); bp = (Ptprefix_t*)dtfirst(b->dict); while (ap) { if (!bp || ap->max < bp->min) { if (!ptinsert(t, ap->min, ap->max)) break; ap = (Ptprefix_t*)dtnext(a->dict, ap); } else if (ap->min > bp->max) bp = (Ptprefix_t*)dtnext(b->dict, bp); else { if (ap->min < bp->min && !ptinsert(t, ap->min, bp->min - 1)) break; if (ap->max < bp->max) ap = (Ptprefix_t*)dtnext(a->dict, ap); else if (ap->max == bp->max) { ap = (Ptprefix_t*)dtnext(a->dict, ap); bp = (Ptprefix_t*)dtnext(b->dict, bp); } else { while (ap->max > bp->max) { m = bp->max + 1; if (!(bp = (Ptprefix_t*)dtnext(b->dict, bp)) || bp->min > ap->max) { if (!ptinsert(t, m, ap->max)) goto done; break; } if (bp->min > m && !ptinsert(t, m, bp->min - 1)) goto done; } ap = (Ptprefix_t*)dtnext(a->dict, ap); } } } done: return t; }
static_fn int sh_outenum(Shell_t *shp, Sfio_t *iop, Namval_t *tp) { Namval_t *mp; Dt_t *dp = NULL; struct Enum *ep; int i; if (!tp) { mp = nv_open(NV_CLASS, shp->var_tree, NV_NOADD | NV_VARNAME); if (!mp) return 0; dp = nv_dict(mp); tp = (Namval_t *)dtfirst(dp); } while (tp) { if (!tp->nvfun) continue; ep = (struct Enum *)nv_hasdisc(tp, &ENUM_disc); if (!ep) continue; sfprintf(iop, "enum %s%s=(\n", (ep->iflag ? "-i " : ""), tp->nvname); for (i = 0; i < ep->nelem; i++) sfprintf(iop, "\t%s\n", ep->values[i]); sfprintf(iop, ")\n"); if (!dp) break; tp = (Namval_t *)dtnext(dp, tp); } return 0; }
static int sh_outenum(Shell_t *shp, Sfio_t *iop, Namval_t *tp) { Namval_t *mp; Dt_t *dp=0; char nvtype[sizeof(NV_CLASS)]; struct Enum *ep; int i; if(!tp) { strcpy(nvtype,NV_CLASS); if(!(mp = nv_open(nvtype, shp->var_tree,NV_NOADD|NV_VARNAME))) return(0); dp = nv_dict(mp); tp = (Namval_t*)dtfirst(dp); } while(tp) { if(!tp->nvfun || !(ep=(struct Enum *)nv_hasdisc(tp,&ENUM_disc))) continue; sfprintf(iop,"enum %s%s=(\n",(ep->iflag?"-i ":""),tp->nvname); for(i=0; i <ep->nelem ; i++) sfprintf(iop,"\t%s\n",ep->values[i]); sfprintf(iop,")\n"); if(!dp) break; tp = (Namval_t*)dtnext(dp,tp); } return(0); }
/* sets ports that represent connections to subclusters */ static void subclustports(Agraph_t *ug) { Agraph_t *model, *clustmodel; Agnode_t *x; Agedge_t *e; vpath_t *p; Dict_t *d; double frac; /* walk all the paths */ model = GD_model(ug); d = repdict(model); for (p = dtfirst(d); p; p = dtnext(d,p)) { if ((ND_type(p->v[p->low])) == NODETYPE_CNODE) { x = p->v[p->low]; clustmodel = GD_model(ND_cluster(x)); frac = (ND_order(x) + 1) / GD_rank(clustmodel)[ND_rank(x)].n; e = p->e[p->low]; ED_tailport(e).p.x = 2 * PORTCLAMP * (frac - 0.5) + p->tailport; } if ((ND_type(p->v[p->high])) == NODETYPE_CNODE) { x = p->v[p->high]; clustmodel = GD_model(ND_cluster(x)); frac = (ND_order(x) + 1) / GD_rank(clustmodel)[ND_rank(x)].n; e = p->e[p->high-1]; ED_headport(e).p.x = 2 * PORTCLAMP * (frac - 0.5) + p->headport; } } }
static Agnode_t *extract_min(Dict_t * Q) { Agnode_t *rv; rv = dtfirst(Q); dtdelete(Q, rv); return rv; }
static void dump(Dt_t* map) { clust_t* p; fprintf (stderr, "# in map: %d\n", dtsize(map)); for (p=(clust_t*)dtfirst(map);p; p = (clust_t*)dtnext(map,p)) { fprintf (stderr, " %s\n", p->name); } }
void printNodeset(nodeset_t * ns) { nsitem_t *ip; for (ip = (nsitem_t *) dtfirst(ns); ip; ip = (nsitem_t *) dtnext(ns, ip)) { fprintf(stderr, "%s", ip->np->name); } fputs("\n", stderr); }
int ptdump(Pt_t* a, Sfio_t* sp) { Ptprefix_t* p; for (p = (Ptprefix_t*)dtfirst(a->dict); p; p = (Ptprefix_t*)dtnext(a->dict, p)) sfprintf(sp, "%0*I*x %0*I*x %-16s %-16s\n", sizeof(p->min) * 2, sizeof(p->min), p->min, sizeof(p->max) * 2, sizeof(p->max), p->max, fmtip4(p->min, -1), fmtip4(p->max, -1)); return sfsync(sp); }
Agedge_t *agfstin(Agraph_t * g, Agnode_t * n) { Agsubnode_t *sn; Agedge_t *e = NILedge; sn = agsubrep(g, n); dtrestore(g->e_seq, sn->in_seq); e = (Agedge_t *) dtfirst(g->e_seq); sn->in_seq = dtextract(g->e_seq); return e; }
Ptv_t* ptvunion(Ptv_t* a, Ptv_t* b) { Ptvprefix_t* bp; if (!(a = ptvcopy(a))) return 0; for (bp = (Ptvprefix_t*)dtfirst(b->dict); bp; bp = (Ptvprefix_t*)dtnext(b->dict, bp)) if (!ptvinsert(a, bp->min, bp->max)) break; return a; }
int ptequal(Pt_t* a, Pt_t* b) { Ptprefix_t* ap; Ptprefix_t* bp; if (a == b) return 1; ap = (Ptprefix_t*)dtfirst(a->dict); bp = (Ptprefix_t*)dtfirst(b->dict); while (ap && bp) { if (ap->min != bp->min) return 0; if (ap->max != bp->max) return 0; ap = (Ptprefix_t*)dtnext(a->dict, ap); bp = (Ptprefix_t*)dtnext(b->dict, bp); } return !ap && !bp; }
int ptvequal(Ptv_t* a, Ptv_t* b) { Ptvprefix_t* ap; Ptvprefix_t* bp; if (a == b) return 1; ap = (Ptvprefix_t*)dtfirst(a->dict); bp = (Ptvprefix_t*)dtfirst(b->dict); while (ap && bp) { if (fvcmp(a->size, ap->min, bp->min)) return 0; if (fvcmp(a->size, ap->max, bp->max)) return 0; ap = (Ptvprefix_t*)dtnext(a->dict, ap); bp = (Ptvprefix_t*)dtnext(b->dict, bp); } return !ap && !bp; }
/* return first outedge of <n> */ Agedge_t *agfstout(Agraph_t * g, Agnode_t * n) { Agsubnode_t *sn; Agedge_t *e = NILedge; sn = agsubrep(g, n); if (sn) { dtrestore(g->e_seq, sn->out_seq); e = (Agedge_t *) dtfirst(g->e_seq); sn->out_seq = dtextract(g->e_seq); } return e; }
int pzheadprint(register Pz_t* pz, register Sfio_t* op, int parts) { register Pzpart_t* pp; char t; if (pz->flags & PZ_FORCE) sfprintf(op, "# fixed record input\n"); else { sfprintf(op, "# pzip %d.%d partition\n", pz->major, pz->minor); if (pz->disc->comment) sfprintf(op, "# %s\n", pz->disc->comment); sfprintf(op, "# window %I*u\n", sizeof(pz->win), pz->win); if (pz->prefix.count) { sfprintf(op, "\nprefix=%I*u", sizeof(pz->prefix.count), pz->prefix.count); if (pz->prefix.terminator >= 0) { t = pz->prefix.terminator; sfprintf(op, "*%s\n", fmtquote(&t, "'", "'", 1, FMT_ALWAYS)); } else sfputc(op, '\n'); } if (pz->headoptions || pz->det) { sfputc(op, '\n'); if (pz->headoptions) sfputr(op, pz->headoptions, '\n'); if (pz->det) { sfwrite(op, sfstrbase(pz->det), sfstrtell(pz->det)); sfputc(op, '\n'); } } } if (parts) { pp = pz->partdict ? (Pzpart_t*)dtfirst(pz->partdict) : pz->part; while (pp) { if (pzpartprint(pz, pp, op)) return -1; if (!pz->partdict) break; pp = (Pzpart_t*)dtnext(pz->partdict, pp); } } return sferror(op) ? -1 : 0; }
Ptv_t* ptvcover(Ptv_t* a, Ptv_t* b) { Ptv_t* t; Ptvprefix_t* ap; Ptvprefix_t* bp; if (!(t = ptvopen(a->disc, a->size))) return 0; ap = (Ptvprefix_t*)dtfirst(a->dict); bp = (Ptvprefix_t*)dtfirst(b->dict); while (ap && bp) { if (fvcmp(a->size, ap->min, bp->max) > 0) bp = (Ptvprefix_t*)dtnext(b->dict, bp); else { if (fvcmp(a->size, ap->max, bp->min) >= 0 && !ptvinsert(t, ap->min, ap->max)) break; ap = (Ptvprefix_t*)dtnext(a->dict, ap); } } return t; }
Pt_t* ptcover(Pt_t* a, Pt_t* b) { Pt_t* t; Ptprefix_t* ap; Ptprefix_t* bp; if (!(t = ptopen(a->disc))) return 0; ap = (Ptprefix_t*)dtfirst(a->dict); bp = (Ptprefix_t*)dtfirst(b->dict); while (ap && bp) { if (ap->min > bp->max) bp = (Ptprefix_t*)dtnext(b->dict, bp); else { if (ap->max >= bp->min && !ptinsert(t, ap->min, ap->max)) break; ap = (Ptprefix_t*)dtnext(a->dict, ap); } } return t; }
int IMPLLflatten (void) { IMPLLnd_t *ndp; int ndi; IMPLLed_t *edp; int edi; IMPLLndn = dtsize (nddict); if (!(IMPLLnds = vmalloc (Vmheap, sizeof (IMPLLnd_t *) * IMPLLndn))) { SUwarning (0, "IMPLLflatten", "cannot allocate IMPLLnds array"); return -1; } for (ndi = 0, ndp = dtfirst (nddict); ndp; ndp = dtnext (nddict, ndp)) IMPLLnds[ndi++] = ndp; IMPLLedn = dtsize (eddict); if (!(IMPLLeds = vmalloc (Vmheap, sizeof (IMPLLed_t *) * IMPLLedn))) { SUwarning (0, "IMPLLflatten", "caenot allocate IMPLLeds array"); return -1; } for (edi = 0, edp = dtfirst (eddict); edp; edp = dtnext (eddict, edp)) IMPLLeds[edi++] = edp; return 0; }
/* firstDeglist: * Return the first node in the list (smallest degree) * Remove from list. * If the list is empty, return NULL */ Agnode_t *firstDeglist(deglist_t * list) { degitem *ip; Agnode_t *np; ip = (degitem *) dtfirst(list); if (ip) { np = ip->np; ip->np = ND_next(np); if (ip->np == NULL) dtdelete(list, ip); return np; } else return 0; }
static int count_all_crossings(nodelist_t * list, Agraph_t * subg) { nodelistitem_t *item; edgelist *openEdgeList = init_edgelist(); Agnode_t *n; Agedge_t *e; int crossings = 0; int order = 1; for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) { for (e = agfstout(subg, n); e; e = agnxtout(subg, e)) { EDGEORDER(e) = 0; } } for (item = list->first; item; item = item->next) { n = item->curr; for (e = agfstedge(subg, n); e; e = agnxtedge(subg, e, n)) { if (EDGEORDER(e) > 0) { edgelistitem *eitem; Agedge_t *ep; for (eitem = (edgelistitem *) dtfirst(openEdgeList); eitem; eitem = (edgelistitem *) dtnext(openEdgeList, eitem)) { ep = eitem->edge; if (EDGEORDER(ep) > EDGEORDER(e)) { if ((ep->head != n) && (ep->tail != n)) crossings++; } } remove_edge(openEdgeList, e); } } for (e = agfstedge(subg, n); e; e = agnxtedge(subg, e, n)) { if (EDGEORDER(e) == 0) { EDGEORDER(e) = order; add_edge(openEdgeList, e); } } order++; } free_edgelist(openEdgeList); return crossings; }
void printDeglist(deglist_t * dl) { degitem *ip; node_t *np; fprintf(stderr, " dl:\n"); for (ip = (degitem *) dtfirst(dl); ip; ip = (degitem *) dtnext(dl, ip)) { np = ip->np; if (np) fprintf(stderr, " (%d)", ip->deg); for (; np; np = ND_next(np)) { fprintf(stderr, " %s", np->name); } fprintf(stderr, "\n"); } }
static int validate_end(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc) { register State_t* state = (State_t*)expr->data; register Field_t* field; Invalid_t* ip; Cxoperand_t val; int heading; if (state->summary) { heading = 1; if (state->invalid && dtsize(state->invalid)) { heading = 0; sfprintf(expr->op, "%16s %11s %s\n", "FIELD", "COUNT", "VALUE"); for (ip = (Invalid_t*)dtfirst(state->invalid); ip; ip = (Invalid_t*)dtnext(state->invalid, ip)) { val.type = ip->variable->type; val.value = ip->value; if (!cxcast(cx, &val, NiL, cx->state->type_string, NiL, NiL)) sfprintf(expr->op, "%16s %11I*u %*.*s\n", ip->variable->name, sizeof(ip->count), ip->count, val.value.string.size, val.value.string.size, val.value.string.data); } } if (!heading) { heading = 1; sfprintf(expr->op, "\n"); } for (field = state->field; field; field = field->next) if (field->invalid || field->discarded || field->repaired) { if (heading) { heading = 0; sfprintf(expr->op, "%16s %11s %11s %11s\n", "FIELD", "INVALID", "DISCARDED", "REPAIRED"); } sfprintf(expr->op, "%16s %11I*u %11I*u %11I*u\n", field->variable->name, sizeof(field->invalid), field->invalid, sizeof(field->discarded), field->discarded, sizeof(field->repaired), field->repaired); } } vmclose(state->vm); return 0; }
int SWMIopnsave (char *sfile) { Sfio_t *sfp; opn_t *opnp; if (!(sfp = sfopen (NULL, sfile, "w"))) { sfprintf (sfstderr, "cannot open file %s\n", sfile); return -1; } for ( opnp = (opn_t *) dtfirst (opndict); opnp; opnp = (opn_t *) dtnext (opndict, opnp) ) { if (!opnp->havec || opnp->type == V_TYPE_STRING) continue; sfprintf (sfp, "%s.%s.%s|%s\n", opnp->op, opnp->pp, opnp->np, opnp->vp); } sfclose (sfp); return 0; }
static void dumpChanG (channel* cp, int v) { int k; intitem* ip; Dt_t* adj; if (cp->cnt < 2) return; fprintf (stderr, "channel %d (%f,%f)\n", v, cp->p.p1, cp->p.p2); for (k=0;k<cp->cnt;k++) { adj = cp->G->vertices[k].adj_list; if (dtsize(adj) == 0) continue; putSeg (stderr, cp->seg_list[k]); fputs (" ->\n", stderr); for (ip = (intitem*)dtfirst(adj); ip; ip = (intitem*)dtnext(adj, ip)) { fputs (" ", stderr); putSeg (stderr, cp->seg_list[ip->id]); fputs ("\n", stderr); } } }
// // Scan tree and add each name that matches the given pattern. // static_fn int scantree(Shell_t *shp, Dt_t *tree, const char *pattern, struct argnod **arghead) { Namval_t *np; struct argnod *ap; int nmatch = 0; char *cp; np = (Namval_t *)dtfirst(tree); for (; np && !nv_isnull(np); (np = (Namval_t *)dtnext(tree, np))) { cp = nv_name(np); if (strmatch(cp, pattern)) { (void)stkseek(shp->stk, ARGVAL); sfputr(shp->stk, cp, -1); ap = (struct argnod *)stkfreeze(shp->stk, 1); ap->argbegin = NULL; ap->argchn.ap = *arghead; ap->argflag = ARG_RAW | ARG_MAKE; *arghead = ap; nmatch++; } } return nmatch; }
return nq; } void push(queue * nq, void *n) { nsitem obj; obj.np = n; dtinsert(nq, &obj); } void *pop(queue * nq, int delete) { nsitem *obj; void *n; obj = dtfirst(nq); if (obj) { n = obj->np; if (delete) dtdelete(nq, 0); return n; } else return 0; } void freeQ(queue * nq) { dtclose(nq); }
void *nv_diropen(Namval_t *np,const char *name) { char *next,*last; int c,len=strlen(name); struct nvdir *save, *dp = new_of(struct nvdir,len+1); Namval_t *nq=0,fake; Namfun_t *nfp=0; if(!dp) return(0); memset((void*)dp, 0, sizeof(*dp)); dp->data = (char*)(dp+1); if(name[len-1]=='*' || name[len-1]=='@') len -= 1; name = memcpy(dp->data,name,len); dp->data[len] = 0; dp->len = len; dp->root = sh.last_root?sh.last_root:sh.var_tree; #if 1 while(1) { dp->table = sh.last_table; sh.last_table = 0; if(*(last=(char*)name)==0) break; if(!(next=nextdot(last))) break; *next = 0; np = nv_open(name, dp->root, NV_NOFAIL); *next = '.'; if(!np || !nv_istable(np)) break; dp->root = nv_dict(np); name = next+1; } #else dp->table = sh.last_table; sh.last_table = 0; last = dp->data; #endif if(*name) { fake.nvname = (char*)name; if(dp->hp = (Namval_t*)dtprev(dp->root,&fake)) { char *cp = nv_name(dp->hp); c = strlen(cp); if(memcmp(name,cp,c) || name[c]!='[') dp->hp = (Namval_t*)dtnext(dp->root,dp->hp); else { np = dp->hp; last = 0; } } else dp->hp = (Namval_t*)dtfirst(dp->root); } else dp->hp = (Namval_t*)dtfirst(dp->root); while(1) { if(!last) next = 0; else if(next= nextdot(last)) { c = *next; *next = 0; } if(!np) { if(nfp && nfp->disc && nfp->disc->createf) { np = (*nfp->disc->createf)(nq,last,0,nfp); if(*nfp->last == '[') { nv_endsubscript(np,nfp->last,NV_NOADD); if(nq = nv_opensub(np)) np = nq; } } else np = nv_search(last,dp->root,0); } if(next) *next = c; if(np==dp->hp && !next) dp->hp = (Namval_t*)dtnext(dp->root,dp->hp); if(np && ((nfp=nextdisc(np)) || nv_istable(np))) { if(!(save = new_of(struct nvdir,0))) return(0); *save = *dp; dp->prev = save; if(nv_istable(np)) dp->root = nv_dict(np); else dp->root = (Dt_t*)np; if(nfp) { dp->nextnode = nfp->disc->nextf; dp->table = np; dp->otable = sh.last_table; dp->fun = nfp; dp->hp = (*dp->nextnode)(np,(Dt_t*)0,nfp); } else dp->nextnode = 0; } else break; if(!next || next[1]==0) break; last = next+1; nq = np; np = 0; }
tmain() { Dt_t *dt; Dtstat_t stat; Obj_t *p, *o, *obj, proto; char *name; long i, k, mid, n_mid, n_obj, meth; /* construct repetitive objects */ for(i = 0; i < N_OBJ; i += R_OBJ) { for(k = 0; k < R_OBJ; ++k) { Obj[i+k].key = i; Obj[i+k].ord = k; } } for(meth = 0; meth < 4; ++meth) { switch(meth) { case 0: name = "Dtobag"; if(!(dt = dtopen(&Disc, Dtobag)) ) terror("%s: Can't open dictionary", name); break; case 1: name = "Dtbag"; if(!(dt = dtopen(&Disc, Dtbag)) ) terror("%s: Can't open dictionary", name); break; case 2: name = "Dtrhbag"; if(!(dt = dtopen(&Disc, Dtrhbag)) ) terror("%s: Can't open dictionary", name); break; case 3: name = "Dtlist"; if(!(dt = dtopen(&Disc, Dtlist)) ) terror("%s: Can't open dictionary", name); break; default: terror("Unknown storage method"); break; } tinfo("Testing method %s:", name); dtcustomize(dt, DT_SHARE, 1); /* make it more interesting */ /* add all objects into dictionary */ for(k = 0; k < R_OBJ; ++k) for(i = 0; i < N_OBJ/R_OBJ; ++i) { obj = Obj + i*R_OBJ + k; o = (meth == 3 || i%2 == 0) ? dtappend(dt,obj) : dtinsert(dt,obj); if(o != obj) terror("%s: dtappend (key=%d,ord=%d) failed", name, obj->key, obj->ord); } mid = ((N_OBJ/R_OBJ)/2) * R_OBJ; /* key for middle group */ proto.key = mid; proto.ord = -1; if(meth == 3) /* testing ATMOST/ATLEAST for Dtlist */ { /* note that dtappend() was used to keep objects in order of insertion */ if(!(o = dtatmost(dt, &proto)) ) terror("%s: dtatmost (key=%d) failed", name, mid); if(o->ord != 0) terror("%s: dtatmost (key=%d) but ord=%d > 0", name, o->key, o->ord); if(!(o = dtatleast(dt, &proto)) ) terror("%s: dtatleast (key=%d) failed", name, mid); if(o->ord != R_OBJ-1) terror("%s: dtatleast (key=%d) but ord=%d > 0", name, o->key, o->ord); n_obj = 0; /* test ordering */ for(p = NIL(Obj_t*), o = dtfirst(dt); o; p = o, o = dtnext(dt,o) ) { n_obj += 1; if(p && p->ord > o->ord) terror("%s: objects not ordered correctly p=%d > o=%d", name, p->ord, o->ord); } if(n_obj != N_OBJ) terror("%s: Bad object count %d != %d", n_obj, N_OBJ); } if(meth == 0) /* testing ordering properties of Dtobag */ { n_obj = 0; /* test atmost/next */ for(o = dtatmost(dt, &proto); o; o = dtnext(dt,o) ) { if(o->key == mid) n_obj += 1; else break; } if(n_obj != R_OBJ) terror("%s: dtatmost/dtnext count n_obj=%d != %d", name, n_obj, R_OBJ); n_obj = 0; /* test atleast/prev */ for(o = dtatleast(dt, &proto); o; o = dtprev(dt,o) ) { if(o->key == mid) n_obj += 1; else break; } if(n_obj != R_OBJ) terror("%s: dtatleast/dtprev count n_obj=%d != %d", name, n_obj, R_OBJ); n_obj = 0; /* test linear order */ for(p = NIL(Obj_t*), o = dtfirst(dt); o; p = o, o = dtnext(dt,o) ) { n_obj += 1; if(p && p->key > o->key) terror("%s: objects not ordered correctly p=%d > o=%d", name, p->key, o->key); } if(n_obj != N_OBJ) terror("%s: Bad object count %d != %d", n_obj, N_OBJ); } n_mid = n_obj = 0; /* walk forward and count objects */ for(o = dtfirst(dt); o; o = dtnext(dt,o)) { n_obj += 1; if(o->key == mid) n_mid += 1; } if(n_obj != N_OBJ) terror("%s: Walk forward n_obj=%d != %d", name, n_obj, N_OBJ); if(n_mid != R_OBJ) terror("%s: Walk forward n_mid=%d != %d", name, n_mid, R_OBJ); n_mid = n_obj = 0; /* walk backward and count objects */ for(o = dtlast(dt); o; o = dtprev(dt,o)) { n_obj += 1; if(o->key == mid) n_mid += 1; } if(n_obj != N_OBJ) terror("%s: Walk backward n_obj=%d != %d", name, n_obj, N_OBJ); if(n_mid != R_OBJ) terror("%s: Walk backward n_mid=%d != %d", name, n_mid, R_OBJ); n_mid = n_obj = 0; /* walk flattened list and count objects */ for(o = (Obj_t*)dtflatten(dt); o; o = (Obj_t*)dtlink(dt,o) ) { n_obj += 1; if(o->key == mid) n_mid += 1; } if(n_obj != N_OBJ) terror("%s: Walk flattened list n_obj=%d != %d", name, n_obj, N_OBJ); if(n_mid != R_OBJ) terror("%s: Walk flattened list n_mid=%d != %d", name, n_mid, R_OBJ); n_mid = 0; /* delete a bunch of objects */ for(i = 0; i < N_OBJ-1; i += R_OBJ) { obj = Obj + i + R_OBJ/2; /* use the one in the middle of group */ if((o = dtremove(dt, obj)) == obj ) n_mid += 1; else terror("%s: dtremove (key=%d,ord=%d) wrongly yielded (key=%d,ord=%d)", name, obj->key, obj->ord, o->key, o->ord); if((o = dtremove(dt, obj)) != NIL(Obj_t*) ) terror("%s: dtremove (key=%d,ord=%d) wrongly yielded (key=%d,ord=%d)", name, obj->key, obj->ord, o->key, o->ord); if((o = dtdelete(dt, obj)) != NIL(Obj_t*) ) n_mid += 1; else terror("%s: dtdelete matching object to (key=%d,ord=%d) failed", name, obj->key, obj->ord); }
Dssfile_t* dssfopen(Dss_t* dss, const char* path, Sfio_t* io, Dssflags_t flags, Dssformat_t* format) { Dssfile_t* file; Vmalloc_t* vm; char* s; size_t n; int i; struct stat st; Sfdisc_t top; char buf[PATH_MAX]; if (flags & DSS_FILE_WRITE) { if (io) { memset(&top, 0, sizeof(top)); if (sfdisc(io, &top)) { n = top.disc == &dss->state->compress_preferred; sfdisc(io, SF_POPDISC); if (n) { sfdisc(io, SF_POPDISC); sfdczip(io, path, dss->meth->compress ? dss->meth->compress : "gzip", dss->disc->errorf); } } } if (dss->flags & DSS_APPEND) flags |= DSS_FILE_APPEND; } if (!path || !*path || streq(path, "-")) { if (flags & DSS_FILE_WRITE) { if (io) path = "output-stream"; else { path = "/dev/stdout"; io = sfstdout; } } else if (io) path = "input-stream"; else { path = "/dev/stdin"; io = sfstdin; } flags |= DSS_FILE_KEEP; } else if (io) flags |= DSS_FILE_KEEP; else if (flags & DSS_FILE_WRITE) { if (!(io = sfopen(NiL, path, (flags & DSS_FILE_APPEND) ? "a" : "w"))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot open", path); return 0; } } else if (!(io = dssfind(path, "", DSS_VERBOSE, buf, sizeof(buf), dss->disc))) return 0; else path = (const char*)buf; if (!(vm = vmopen(Vmdcheap, Vmbest, 0))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space"); return 0; } if (!(file = vmnewof(vm, 0, Dssfile_t, 1, strlen(path) + 1))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space"); if (!(flags & DSS_FILE_KEEP)) sfclose(io); vmclose(vm); return 0; } strcpy(file->path = (char*)(file + 1), path); file->dss = dss; file->vm = vm; file->io = io; file->flags = flags; if (flags & DSS_FILE_WRITE) { if (!(file->format = format) && !(file->format = dss->format)) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "output method format must be specified"); if (!(flags & DSS_FILE_KEEP)) sfclose(io); return 0; } file->readf = noreadf; file->writef = file->format->writef; } else { if (sfsize(file->io) || !fstat(sffileno(file->io), &st) && (S_ISFIFO(st.st_mode) #ifdef S_ISSOCK || S_ISSOCK(st.st_mode) #endif )) { if (sfdczip(file->io, file->path, NiL, dss->disc->errorf) < 0) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: inflate error", file->path); dssfclose(file); return 0; } s = sfreserve(file->io, SF_UNBOUND, SF_LOCKR); n = sfvalue(file->io); if (!s) { if (n && dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot peek", file->path); dssfclose(file); return 0; } for (file->format = (Dssformat_t*)dtfirst(dss->meth->formats); file->format && !(i = (*file->format->identf)(file, s, n, dss->disc)); file->format = (Dssformat_t*)dtnext(dss->meth->formats, file->format)); sfread(file->io, s, 0); if (!file->format) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: unknown %s format", file->path, dss->meth->name); dssfclose(file); return 0; } if (i < 0) return 0; if (format && format != file->format) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s file format %s incompatible with %s", file->path, dss->meth->name, file->format->name, format->name); dssfclose(file); return 0; } if ((dss->flags & DSS_VERBOSE) && dss->disc->errorf) (*dss->disc->errorf)(dss, dss->disc, 1, "%s: %s method %s format", file->path, dss->meth->name, file->format->name); file->readf = file->format->readf; } else { file->format = format ? format : dss->format ? dss->format : (Dssformat_t*)dtfirst(dss->meth->formats); file->readf = nullreadf; } file->writef = nowritef; if (!dss->format) dss->format = file->format; } if (!file->format) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s method did not set file format", file->path, dss->meth->name); dssfclose(file); return 0; } file->record.file = file; if ((*file->format->openf)(file, dss->disc)) { dssfclose(file); return 0; } return file; }
static void write_subg(Agraph_t * g, FILE * fp, Agraph_t * par, int indent, printdict_t * state) { Agraph_t *subg, *meta; Agnode_t *n, *pn; Agedge_t *e, *pe; Dict_t *save_e, *save_n; if (indent) { tabover(fp, indent++); if (dtsearch(state->subgleft, g->meta_node)) { if (strncmp(g->name, "_anonymous", 10)) fprintf(fp, "subgraph %s {\n", agcanonical(g->name)); else fprintf(fp, "{\n"); /* no name printed for anonymous subg */ write_diffattr(fp, indent, g, par, g->univ->globattr); /* The root node and edge environment use the dictionaries, * not the proto node or edge, so the next level down must * record differences with the dictionaries. */ if (par == g->root) { pn = NULL; pe = NULL; } else { pn = par->proto->n; pe = par->proto->e; } write_diffattr(fp, indent, g->proto->n, pn, g->univ->nodeattr); write_diffattr(fp, indent, g->proto->e, pe, g->univ->edgeattr); dtdelete(state->subgleft, g->meta_node); } else { fprintf(fp, "subgraph %s;\n", agcanonical(g->name)); return; } } else write_diffattr(fp, ++indent, g, NULL, g->univ->globattr); save_n = state->n_insubg; save_e = state->e_insubg; meta = g->meta_node->graph; state->n_insubg = dtopen(&agNamedisc, Dttree); state->e_insubg = dtopen(&agOutdisc, Dttree); for (e = agfstout(meta, g->meta_node); e; e = agnxtout(meta, e)) { subg = agusergraph(e->head); write_subg(subg, fp, g, indent, state); } for (n = agfstnode(g); n; n = agnxtnode(g, n)) { if (dtsearch(state->nodesleft, n)) { agwrnode(g, fp, n, TRUE, indent); dtdelete(state->nodesleft, n); } else { if (dtsearch(state->n_insubg, n) == NULL) { agwrnode(g, fp, n, FALSE, indent); } } dtinsert(save_n, n); } dtdisc(g->outedges, &agEdgedisc, 0); /* sort by id */ for (e = (Agedge_t *) dtfirst(g->outedges); e; e = (Agedge_t *) dtnext(g->outedges, e)) { if (dtsearch(state->edgesleft, e)) { tabover(fp, indent); agwredge(g, fp, e, TRUE); dtdelete(state->edgesleft, e); } else { if (dtsearch(state->e_insubg, e) == NULL) { tabover(fp, indent); agwredge(g, fp, e, FALSE); } } dtinsert(save_e, e); } dtdisc(g->outedges, &agOutdisc, 0); /* sort by name */ dtclose(state->n_insubg); state->n_insubg = save_n; dtclose(state->e_insubg); state->e_insubg = save_e; if (indent > 1) { tabover(fp, indent - 1); fprintf(fp, "}\n"); } }
tmain() { Dt_t* dt; /* testing Dtlist */ if(!(dt = dtopen(&Disc,Dtlist)) ) terror("dtopen list"); if((long)dtinsert(dt,1L) != 1) terror("Dtlist insert 1"); if((long)dtappend(dt,2L) != 2) terror("Dtlist append 2"); if((long)dtappend(dt,3L) != 3) terror("Dtlist append 3"); if((long)dtappend(dt,1L) != 1) terror("Dtlist append 1"); if((long)dtappend(dt,2L) != 2) terror("Dtlist append 2"); if((long)dtappend(dt,3L) != 3) terror("Dtlist append 3"); if((long)dtlast(dt) != 3) terror("Dtlist dtlast"); if((long)dtprev(dt,3L) != 2) terror("Dtlist dtprev 2"); if((long)dtprev(dt,2L) != 1) terror("Dtlist dtprev 1"); if((long)dtprev(dt,1L) != 3) terror("Dtlist dtprev 3"); if((long)dtprev(dt,3L) != 2) terror("Dtlist dtprev 2"); if((long)dtprev(dt,2L) != 1) terror("Dtlist dtprev 1"); /* search to the first 3 */ if((long)dtfirst(dt) != 1) terror("Dtlist dtfirst 1"); if((long)dtsearch(dt,3L) != 3) terror("Dtlist search 3"); if((long)dtinsert(dt,4L) != 4) terror("Dtlist insert 4"); if((long)dtnext(dt,4L) != 3) terror("Dtlist next 3"); if((long)dtappend(dt,5L) != 5) terror("Dtlist append 5"); if((long)dtfirst(dt) != 1) terror("Dtlist dtfirst 1"); if((long)dtnext(dt,1L) != 2) terror("Dtlist next 2"); if((long)dtnext(dt,2L) != 4) terror("Dtlist next 4"); if((long)dtnext(dt,4L) != 3) terror("Dtlist next 3"); if((long)dtnext(dt,3L) != 5) terror("Dtlist next 5"); if((long)dtnext(dt,5L) != 1) terror("Dtlist next 1"); if((long)dtnext(dt,1L) != 2) terror("Dtlist next 2"); if((long)dtnext(dt,2L) != 3) terror("Dtlist next 3"); texit(0); }