/* * inserts a thing after the thing in index * NOTE: index must represent a valid index */ int insert_thing_after( resizable_array *ra, void *thing, int index) { int rc; int next; /* check if the array must be resized */ if ((rc = check_and_resize(ra)) != PBSE_NONE) { return(-1); } /* insert this element */ ra->slots[ra->next_slot].item = thing; /* save the insertion point */ rc = ra->next_slot; /* move pointers around */ ra->slots[rc].prev = index; next = ra->slots[index].next; ra->slots[rc].next = next; ra->slots[index].next = rc; if (next != 0) { ra->slots[next].prev = rc; } /* update the last index if needed */ if (ra->last == index) ra->last = rc; /* increase the count */ ra->num++; update_next_slot(ra); return(rc); } /* END insert_thing_after() */
int insert_thing( resizable_array *ra, void *thing) { int rc; /* check if the array must be resized */ if ((rc = check_and_resize(ra)) != PBSE_NONE) { return(-1); } ra->slots[ra->next_slot].item = thing; /* save the insertion point */ rc = ra->next_slot; /* handle the backwards pointer, next pointer is left at zero */ ra->slots[rc].prev = ra->last; /* make sure the empty slot points to the next occupied slot */ if (ra->last == ALWAYS_EMPTY_INDEX) { ra->slots[ALWAYS_EMPTY_INDEX].next = rc; } /* update the last index */ ra->slots[ra->last].next = rc; ra->last = rc; /* update the new item's next index */ ra->slots[rc].next = ALWAYS_EMPTY_INDEX; /* increase the count */ ra->num++; update_next_slot(ra); return(rc); } /* END insert_thing() */
int insert_thing_before( resizable_array *ra, void *thing, int index) { int rc; int prev; /* check if the array must be resized */ if ((rc = check_and_resize(ra)) != PBSE_NONE) { return(-1); } /* insert this element */ ra->slots[ra->next_slot].item = thing; /* save the insertion point */ rc = ra->next_slot; /* move pointers around */ prev = ra->slots[index].prev; ra->slots[rc].next = index; ra->slots[rc].prev = prev; ra->slots[index].prev = rc; ra->slots[prev].next = rc; /* increase the count */ ra->num++; update_next_slot(ra); return(rc); } /* END insert_thing_before() */