version 1.5.2.1, 2012/03/29 09:37:19
|
version 1.5.2.2, 2012/03/29 12:23:00
|
Line 584 _cmp_arr_key_desc(const void *a, const void *b)
|
Line 584 _cmp_arr_key_desc(const void *a, const void *b)
|
} |
} |
|
|
/* |
/* |
* io_sortVars() - Sorting array with variables | * io_sortVars() Sorting array with variables |
* |
* |
* @vars = Variable array |
* @vars = Variable array |
* @order = Sort order. If =0 ascend or !=0 descend |
* @order = Sort order. If =0 ascend or !=0 descend |
Line 603 io_sortVars(array_t * __restrict vars, int order, int
|
Line 603 io_sortVars(array_t * __restrict vars, int order, int
|
qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_desc); |
qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_desc); |
else |
else |
qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_asc); |
qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_asc); |
|
} |
|
|
|
/* |
|
* io_findKeyVars() Find variable by key from array |
|
* |
|
* @vars = Variables |
|
* @key = Search key |
|
* return: NULL error or not found, !=NULL valid element |
|
*/ |
|
ait_val_t * |
|
io_findKeyVars(array_t * __restrict vars, u_short key) |
|
{ |
|
array_t *tmp; |
|
ait_val_t **v = NULL; |
|
register int i; |
|
const u_char *p; |
|
|
|
if (!vars) |
|
return NULL; |
|
|
|
if (io_arrayCopy(&tmp, vars) == -1) |
|
return NULL; |
|
else |
|
qsort(tmp->arr_data, tmp->arr_num, sizeof(void*), _cmp_arr_key_asc); |
|
|
|
/* binary search */ |
|
for (p = (const u_char*) tmp->arr_data, i = io_arraySize(tmp); i; i >>= 1) { |
|
v = (ait_val_t**) (p + (i >> 1) * sizeof(void*)); |
|
if (!(key - AIT_KEY(*v))) { /* found! */ |
|
io_arrayDestroy(&tmp); |
|
return *v; |
|
} |
|
if ((key - AIT_KEY(*v)) > 0) { /* move right key > current */ |
|
p = (const u_char*) v + sizeof(void*); |
|
i--; |
|
} /* else move left */ |
|
} |
|
|
|
io_arrayDestroy(&tmp); |
|
return NULL; |
} |
} |