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 2.8 kB

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