NBR_API int nbr_array_get_index(ARRAY ad, void *p) { ASSERT(ad); array_t *a = ad; element_t *e; if (a->option & NBR_PRIM_EXPANDABLE) { return NBR_EINVAL; } e = array_get_top_address(p); return array_get_index(a, e); }
data_unset *array_get_element(array *a, const char *key) { int ndx; if (-1 != (ndx = array_get_index(a, key, strlen(key) + 1, NULL))) { /* found, leave here */ return a->data[ndx]; } return NULL; }
/* replace or insert data, return the old one with the same key */ data_unset *array_replace(array *a, data_unset *du) { int ndx; if (-1 == (ndx = array_get_index(a, du->key->ptr, du->key->used, NULL))) { array_insert_unique(a, du); return NULL; } else { data_unset *old = a->data[ndx]; a->data[ndx] = du; return old; } }
NBR_INLINE BOOL array_check_address(const array_t *a, const element_t *e) { int idx = array_get_index(a, e); return idx >= 0 ? (idx < a->max) : FALSE; }
int array_insert_unique(array *a, data_unset *str) { int ndx = -1; int pos = 0; size_t j; /* generate unique index if neccesary */ if (str->key->used == 0 || str->is_index_key) { buffer_copy_long(str->key, a->unique_ndx++); str->is_index_key = 1; } /* try to find the string */ if (-1 != (ndx = array_get_index(a, str->key->ptr, str->key->used, &pos))) { /* found, leave here */ if (a->data[ndx]->type == str->type) { str->insert_dup(a->data[ndx], str); } else { fprintf(stderr, "a\n"); } return 0; } /* insert */ if (a->used+1 > INT_MAX) { /* we can't handle more then INT_MAX entries: see array_get_index() */ return -1; } if (a->size == 0) { a->size = 16; a->data = malloc(sizeof(*a->data) * a->size); a->sorted = malloc(sizeof(*a->sorted) * a->size); assert(a->data); assert(a->sorted); for (j = a->used; j < a->size; j++) a->data[j] = NULL; } else if (a->size == a->used) { a->size += 16; a->data = realloc(a->data, sizeof(*a->data) * a->size); a->sorted = realloc(a->sorted, sizeof(*a->sorted) * a->size); assert(a->data); assert(a->sorted); for (j = a->used; j < a->size; j++) a->data[j] = NULL; } ndx = (int) a->used; a->data[a->used++] = str; if (pos != ndx && ((pos < 0) || buffer_caseless_compare(str->key->ptr, str->key->used, a->data[a->sorted[pos]]->key->ptr, a->data[a->sorted[pos]]->key->used) > 0)) { pos++; } /* move everything on step to the right */ if (pos != ndx) { memmove(a->sorted + (pos + 1), a->sorted + (pos), (ndx - pos) * sizeof(*a->sorted)); } /* insert */ a->sorted[pos] = ndx; if (a->next_power_of_2 == (size_t)ndx) a->next_power_of_2 <<= 1; return 0; }
void* array_get_front(array* arr) { return array_get_index(arr, 0); }
void* array_location_get(array_location* loc) { assert(loc != NULL); assert(array_location_valid(loc)); return array_get_index(loc->index); }