You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

arraylist.c 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #if SIZEOF_SIZE_T == SIZEOF_INT
  21. #define SIZE_T_MAX UINT_MAX
  22. #elif SIZEOF_SIZE_T == SIZEOF_LONG
  23. #define SIZE_T_MAX ULONG_MAX
  24. #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
  25. #define SIZE_T_MAX ULLONG_MAX
  26. #else
  27. #error Unable to determine size of size_t
  28. #endif
  29. #include "arraylist.h"
  30. struct array_list*
  31. array_list_new(array_list_free_fn *free_fn)
  32. {
  33. struct array_list *arr;
  34. arr = (struct array_list*)calloc(1, sizeof(struct array_list));
  35. if(!arr) return NULL;
  36. arr->size = ARRAY_LIST_DEFAULT_SIZE;
  37. arr->length = 0;
  38. arr->free_fn = free_fn;
  39. if(!(arr->array = (void**)calloc(sizeof(void*), arr->size))) {
  40. free(arr);
  41. return NULL;
  42. }
  43. return arr;
  44. }
  45. extern void
  46. array_list_free(struct array_list *arr)
  47. {
  48. size_t i;
  49. for(i = 0; i < arr->length; i++)
  50. if(arr->array[i]) arr->free_fn(arr->array[i]);
  51. free(arr->array);
  52. free(arr);
  53. }
  54. void*
  55. array_list_get_idx(struct array_list *arr, size_t i)
  56. {
  57. if(i >= arr->length) return NULL;
  58. return arr->array[i];
  59. }
  60. static int array_list_expand_internal(struct array_list *arr, size_t max)
  61. {
  62. void *t;
  63. size_t new_size;
  64. if(max < arr->size) return 0;
  65. /* Avoid undefined behaviour on size_t overflow */
  66. if( arr->size >= SIZE_T_MAX / 2 )
  67. new_size = max;
  68. else
  69. {
  70. new_size = arr->size << 1;
  71. if (new_size < max)
  72. new_size = max;
  73. }
  74. if (new_size > (~((size_t)0)) / sizeof(void*)) return -1;
  75. if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
  76. arr->array = (void**)t;
  77. (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
  78. arr->size = new_size;
  79. return 0;
  80. }
  81. int
  82. array_list_put_idx(struct array_list *arr, size_t idx, void *data)
  83. {
  84. if (idx > SIZE_T_MAX - 1 ) return -1;
  85. if(array_list_expand_internal(arr, idx+1)) return -1;
  86. if(arr->array[idx]) arr->free_fn(arr->array[idx]);
  87. arr->array[idx] = data;
  88. if(arr->length <= idx) arr->length = idx + 1;
  89. return 0;
  90. }
  91. int
  92. array_list_add(struct array_list *arr, void *data)
  93. {
  94. return array_list_put_idx(arr, arr->length, data);
  95. }
  96. void
  97. array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
  98. {
  99. qsort(arr->array, arr->length, sizeof(arr->array[0]), sort_fn);
  100. }
  101. void* array_list_bsearch(const void **key, struct array_list *arr,
  102. int (*sort_fn)(const void *, const void *))
  103. {
  104. return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]),
  105. sort_fn);
  106. }
  107. size_t
  108. array_list_length(struct array_list *arr)
  109. {
  110. return arr->length;
  111. }
  112. int
  113. array_list_del_idx( struct array_list *arr, size_t idx, size_t count )
  114. {
  115. size_t i, stop;
  116. stop = idx + count;
  117. if ( idx >= arr->length || stop > arr->length ) return -1;
  118. for ( i = idx; i < stop; ++i ) {
  119. if ( arr->array[i] ) arr->free_fn( arr->array[i] );
  120. }
  121. memmove( arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void*) );
  122. arr->length -= count;
  123. return 0;
  124. }