arraylist.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #include "config.h"
  12. #include <limits.h>
  13. #ifdef STDC_HEADERS
  14. # include <stdlib.h>
  15. # include <string.h>
  16. #endif /* STDC_HEADERS */
  17. #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
  18. # include <strings.h>
  19. #endif /* HAVE_STRINGS_H */
  20. #ifndef SIZE_T_MAX
  21. #if SIZEOF_SIZE_T == SIZEOF_INT
  22. #define SIZE_T_MAX UINT_MAX
  23. #elif SIZEOF_SIZE_T == SIZEOF_LONG
  24. #define SIZE_T_MAX ULONG_MAX
  25. #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
  26. #define SIZE_T_MAX ULLONG_MAX
  27. #else
  28. #error Unable to determine size of size_t
  29. #endif
  30. #endif
  31. #include "arraylist.h"
  32. struct array_list*
  33. array_list_new(array_list_free_fn *free_fn)
  34. {
  35. struct array_list *arr;
  36. arr = (struct array_list*)calloc(1, sizeof(struct array_list));
  37. if(!arr) return NULL;
  38. arr->size = ARRAY_LIST_DEFAULT_SIZE;
  39. arr->length = 0;
  40. arr->free_fn = free_fn;
  41. if(!(arr->array = (void**)calloc(sizeof(void*), arr->size))) {
  42. free(arr);
  43. return NULL;
  44. }
  45. return arr;
  46. }
  47. extern void
  48. array_list_free(struct array_list *arr)
  49. {
  50. size_t i;
  51. for(i = 0; i < arr->length; i++)
  52. if(arr->array[i]) arr->free_fn(arr->array[i]);
  53. free(arr->array);
  54. free(arr);
  55. }
  56. void*
  57. array_list_get_idx(struct array_list *arr, size_t i)
  58. {
  59. if(i >= arr->length) return NULL;
  60. return arr->array[i];
  61. }
  62. static int array_list_expand_internal(struct array_list *arr, size_t max)
  63. {
  64. void *t;
  65. size_t new_size;
  66. if(max < arr->size) return 0;
  67. /* Avoid undefined behaviour on size_t overflow */
  68. if( arr->size >= SIZE_T_MAX / 2 )
  69. new_size = max;
  70. else
  71. {
  72. new_size = arr->size << 1;
  73. if (new_size < max)
  74. new_size = max;
  75. }
  76. if (new_size > (~((size_t)0)) / sizeof(void*)) return -1;
  77. if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
  78. arr->array = (void**)t;
  79. (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
  80. arr->size = new_size;
  81. return 0;
  82. }
  83. int
  84. array_list_put_idx(struct array_list *arr, size_t idx, void *data)
  85. {
  86. if (idx > SIZE_T_MAX - 1 ) return -1;
  87. if(array_list_expand_internal(arr, idx+1)) return -1;
  88. if(idx < arr->length && arr->array[idx])
  89. arr->free_fn(arr->array[idx]);
  90. arr->array[idx] = data;
  91. if(arr->length <= idx) arr->length = idx + 1;
  92. return 0;
  93. }
  94. int
  95. array_list_add(struct array_list *arr, void *data)
  96. {
  97. return array_list_put_idx(arr, arr->length, data);
  98. }
  99. void
  100. array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
  101. {
  102. qsort(arr->array, arr->length, sizeof(arr->array[0]), sort_fn);
  103. }
  104. void* array_list_bsearch(const void **key, struct array_list *arr,
  105. int (*sort_fn)(const void *, const void *))
  106. {
  107. return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]),
  108. sort_fn);
  109. }
  110. size_t
  111. array_list_length(struct array_list *arr)
  112. {
  113. return arr->length;
  114. }
  115. int
  116. array_list_del_idx( struct array_list *arr, size_t idx, size_t count )
  117. {
  118. size_t i, stop;
  119. stop = idx + count;
  120. if ( idx >= arr->length || stop > arr->length ) return -1;
  121. for ( i = idx; i < stop; ++i ) {
  122. if ( arr->array[i] ) arr->free_fn( arr->array[i] );
  123. }
  124. memmove( arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void*) );
  125. arr->length -= count;
  126. return 0;
  127. }