src/array_list.c

changeset 985
68754c7de906
parent 968
b5814aac3a76
equal deleted inserted replaced
984:e8f354a25ac8 985:68754c7de906
35 35
36 static void *cx_array_default_realloc( 36 static void *cx_array_default_realloc(
37 void *array, 37 void *array,
38 size_t capacity, 38 size_t capacity,
39 size_t elem_size, 39 size_t elem_size,
40 __attribute__((__unused__)) CxArrayReallocator *alloc 40 cx_attr_unused CxArrayReallocator *alloc
41 ) { 41 ) {
42 return realloc(array, capacity * elem_size); 42 return realloc(array, capacity * elem_size);
43 } 43 }
44 44
45 CxArrayReallocator cx_array_default_reallocator_impl = { 45 CxArrayReallocator cx_array_default_reallocator_impl = {
52 52
53 static void *cx_array_advanced_realloc( 53 static void *cx_array_advanced_realloc(
54 void *array, 54 void *array,
55 size_t capacity, 55 size_t capacity,
56 size_t elem_size, 56 size_t elem_size,
57 __attribute__((__unused__)) CxArrayReallocator *alloc 57 cx_attr_unused CxArrayReallocator *alloc
58 ) { 58 ) {
59 // retrieve the pointer to the actual allocator 59 // retrieve the pointer to the actual allocator
60 const CxAllocator *al = alloc->ptr1; 60 const CxAllocator *al = alloc->ptr1;
61 61
62 // check if the array is still located on the stack 62 // check if the array is still located on the stack
322 } 322 }
323 } 323 }
324 324
325 // report the largest upper bound 325 // report the largest upper bound
326 return result < 0 ? (pivot_index - 1) : pivot_index; 326 return result < 0 ? (pivot_index - 1) : pivot_index;
327 }
328
329 size_t cx_array_binary_search(
330 const void *arr,
331 size_t size,
332 size_t elem_size,
333 const void *elem,
334 cx_compare_func cmp_func
335 ) {
336 size_t index = cx_array_binary_search_inf(
337 arr, size, elem_size, elem, cmp_func
338 );
339 if (index < size &&
340 cmp_func(((const char *) arr) + index * elem_size, elem) == 0) {
341 return index;
342 } else {
343 return size;
344 }
345 }
346
347 size_t cx_array_binary_search_sup(
348 const void *arr,
349 size_t size,
350 size_t elem_size,
351 const void *elem,
352 cx_compare_func cmp_func
353 ) {
354 size_t inf = cx_array_binary_search_inf(
355 arr, size, elem_size, elem, cmp_func
356 );
357 if (inf == size) {
358 // no infimum means, first element is supremum
359 return 0;
360 } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) {
361 return inf;
362 } else {
363 return inf + 1;
364 }
327 } 365 }
328 366
329 #ifndef CX_ARRAY_SWAP_SBO_SIZE 367 #ifndef CX_ARRAY_SWAP_SBO_SIZE
330 #define CX_ARRAY_SWAP_SBO_SIZE 128 368 #define CX_ARRAY_SWAP_SBO_SIZE 128
331 #endif 369 #endif

mercurial