void frame_setson_ext(framestruc *fr, framestruc *parent, framestruc *where) { framestruc *pold, **rr; if (!fr) return; pold = FRPARENT(fr); if (pold) { /* removing from the son-list of its old parent */ if (!FRSONS(pold)) rr = NULL; else for (rr=FRSONS(pold); *rr && *rr!=fr; rr++) ; if (rr?(*rr==fr):0) { alist_delete(rr); FRNUMSONS(pold)--; } else {PROGERROR("Probably broken list of sons (%p) in the parent %p.",fr,pold);} } FRPARENT(fr) = parent; if (parent) { /* the new parent */ if (!where) { FRSONS_XD(parent) = alist_append(FRSONS(parent),fr); } else { if (!FRSONS(parent)) rr = NULL; else for (rr=FRSONS(parent); *rr && *rr!=where; rr++) ; if (rr?(*rr!=where):1) {PROGERROR("Nonexistent \"where\" position given.");} FRSONS_XD(parent) = alist_insert(FRSONS(parent),rr,fr); } FRNUMSONS(parent)++; } }
void alist_delete_by_offset(Alist *lp, Aliste *offp) { Aliste idx; ASSERT(lp != NULL); idx = (*offp - ALIST_OFF_DATA) / lp->al_size; alist_delete(lp, &idx); *offp -= lp->al_size; }
static historyp make_variable_history(void) { alist al; historyp hp; hpair *pair; al = htable_list(var_table); hp = new_history(FALSE); for (pair = alist_first(al); pair != NULL; pair = alist_next(al)) alist_append(hp->completions, zstrdup(pair->key)); alist_delete(al); return hp; }
void stress_test_alist(int amt) { alist al; int i; gendata x, y; al = alist_create(); assert(alist_empty(al)); printf("Creating an association list with %d items...\n", amt); for (i = 0; i < amt; ++i) { x.num = y.num = i; al = alist_insert_uniq(al, x, y, num_eq); assert(!alist_empty(al)); alist_lookup(al, x, num_eq, &y); assert(x.num == y.num); } y.ptr = NULL; printf("Walking an association list of %d items...\n", amt); alist_walk(al, walker, y); printf("Deleting %d items from the association list...\n", amt); for (i = 0; i < amt; ++i) { /* Inserting an item that's already there should fail */ x.num = i; y.num = i; /* Value does not matter */ assert(alist_insert_uniq(al, x, y, num_eq) == NULL); al = alist_delete(al, x, num_eq, NULL, NULL); } assert(alist_empty(al)); alist_destroy(al, NULL, NULL); }
int main(void) { alist al; char *t1 = "def", *t2 = "abc", *t3 = "xyz"; char *s; al = alist_new(); assert(alist_count(al) == 0); assert(alist_current(al) == NULL); assert(alist_current_idx(al) == -1); alist_append(al, t1); assert(alist_count(al) == 1); assert(alist_current(al) == t1); assert(alist_current_idx(al) == 0); alist_append(al, t2); assert(alist_count(al) == 2); assert(alist_current(al) == t2); assert(alist_current_idx(al) == 1); s = alist_first(al); assert(s == t1); assert(alist_current(al) == t1); assert(alist_current_idx(al) == 0); s = alist_next(al); assert(s == t2); assert(alist_current(al) == t2); assert(alist_current_idx(al) == 1); s = alist_next(al); assert(s == NULL); assert(alist_current(al) == NULL); assert(alist_current_idx(al) == -1); alist_prepend(al, t3); assert(alist_count(al) == 3); assert(alist_current(al) == t3); assert(alist_current_idx(al) == 0); printf("elements:\n"); for (s = alist_first(al); s != NULL; s = alist_next(al)) printf("element %d: %s\n", alist_current_idx(al), s); alist_sort(al, sorter); printf("sorted elements:\n"); for (s = alist_first(al); s != NULL; s = alist_next(al)) printf("element %d: %s\n", alist_current_idx(al), s) ; assert(alist_at(al, 0) == t2); assert(alist_at(al, 1) == t1); assert(alist_at(al, 2) == t3); alist_clear(al); assert(alist_count(al) == 0); assert(alist_current(al) == NULL); assert(alist_current_idx(al) == -1); alist_insert(al, 5, t1); assert(alist_count(al) == 1); assert(alist_current(al) == t1); assert(alist_current_idx(al) == 0); alist_insert(al, 0, t2); assert(alist_count(al) == 2); assert(alist_current(al) == t2); assert(alist_current_idx(al) == 0); alist_insert(al, 1, t3); assert(alist_count(al) == 3); assert(alist_at(al, 0) == t2); assert(alist_at(al, 1) == t3); assert(alist_at(al, 2) == t1); assert(alist_current(al) == t3); assert(alist_current_idx(al) == 1); alist_delete(al); printf("alist test successful.\n"); return 0; }