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.3 kB

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