/* * li_api_insert - insert a new element after given index position * return: NO_ERROR if successful, error code otherwise * indexer(in): VALUE_INDEXER * index(in): index of element * va(in): pointer to VALUE_AREA * dval(in): pointer to API_VALUE * NOTE * To insert the element to the top front of position the indexer, index -1 is used. */ static int li_api_insert (VALUE_INDEXER * indexer, int index, VALUE_AREA * va, API_VALUE * dval) { LIST_INDEXER *li = (LIST_INDEXER *) indexer; LIST_INDEXER_ELEM *e; dlisth *h; assert (li != NULL); assert (index >= -1 && index < li->nelems); e = API_MALLOC (sizeof (*e)); if (e == NULL) return ER_INTERFACE_NO_MORE_MEMORY; dlisth_init (&e->header); e->va = va; e->value = dval; if (index == -1) h = &li->elems; else h = (dlisth *) li_getf (li, index); assert (h != NULL); dlisth_insert_after ((dlisth *) e, h); li->nelems++; /* ajust cache */ if (index == -1 && li->cache_idx == -1) { li->cache_idx = 0; li->cache_elem = e; } else if (index < li->cache_idx) { li->cache_idx++; } return NO_ERROR; }
/* * li_api_delete - delete existing element at the given position * return: NO_ERROR if successful, error code otherwise * indexer(in): VALUE_INDEXER * index(in): index of the element * rva(out): pointer to VALUE_AREA * dbval(out): pointer to API_VALUE */ static int li_api_delete (VALUE_INDEXER * indexer, int index, VALUE_AREA ** rva, API_VALUE ** dbval) { LIST_INDEXER *li = (LIST_INDEXER *) indexer; LIST_INDEXER_ELEM *e; assert (li != NULL); assert (li->nelems > 0); assert (index >= 0 && index < li->nelems); assert (rva != NULL); assert (dbval != NULL); assert (li->cache_idx >= 0 && li->cache_elem != NULL); e = li_getf (li, index); assert (e != NULL); *rva = e->va; *dbval = e->value; li->nelems--; /* ajust cache */ if (li->nelems > 0) { if (index < li->cache_idx) { li->cache_idx--; } else if (index == li->cache_idx) { if (index > 0) { li->cache_idx = index - 1; li->cache_elem = (LIST_INDEXER_ELEM *) (((dlisth *) e)->prev); } else { li->cache_elem = (LIST_INDEXER_ELEM *) (((dlisth *) e)->next); } } } else { li->cache_idx = -1; li->cache_elem = NULL; } dlisth_delete ((dlisth *) e); API_FREE (e); return NO_ERROR; }
/* * li_api_set - set VALUE_AREA and API_VALUE to given index * return: NO_ERROR if successful, error code otherwise * indexer(in): VALUE_INDEXER * index(int): index of the item * va(in): pointer to VALUE_AREA * val(in): pointer to API_VALUE */ static int li_api_set (VALUE_INDEXER * indexer, int index, VALUE_AREA * va, API_VALUE * dval) { LIST_INDEXER *li = (LIST_INDEXER *) indexer; LIST_INDEXER_ELEM *e; assert (li != NULL); assert (index >= 0 && index < li->nelems); e = li_getf (li, index); assert (e != NULL); e->va = va; e->value = dval; return NO_ERROR; }
/* * li_api_get - get element * return: NO_ERROR if successful, error code otherwise * indexer(in): VALUE_INDEXER * index(in): index of the item * rva(out): VALUE_AREA * rv(out): API_VALUE */ static int li_api_get (VALUE_INDEXER * indexer, int index, VALUE_AREA ** rva, API_VALUE ** rv) { LIST_INDEXER *li = (LIST_INDEXER *) indexer; LIST_INDEXER_ELEM *e; assert (li != NULL); assert (index >= 0 && index < li->nelems); assert (rva != NULL); assert (rv != NULL); e = li_getf (li, index); assert (e != NULL); *rva = e->va; *rv = e->value; return NO_ERROR; }