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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifdef STDC_HEADERS
  13. # include <stdlib.h>
  14. # include <string.h>
  15. #endif /* STDC_HEADERS */
  16. #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
  17. # include <strings.h>
  18. #endif /* HAVE_STRINGS_H */
  19. #include "arraylist.h"
  20. struct array_list*
  21. array_list_new(array_list_free_fn *free_fn)
  22. {
  23. struct array_list *arr;
  24. arr = (struct array_list*)calloc(1, sizeof(struct array_list));
  25. if(!arr) return NULL;
  26. arr->size = ARRAY_LIST_DEFAULT_SIZE;
  27. arr->length = 0;
  28. arr->free_fn = free_fn;
  29. if(!(arr->array = (void**)calloc(sizeof(void*), arr->size))) {
  30. free(arr);
  31. return NULL;
  32. }
  33. return arr;
  34. }
  35. extern void
  36. array_list_free(struct array_list *arr)
  37. {
  38. int i;
  39. for(i = 0; i < arr->length; i++)
  40. if(arr->array[i]) arr->free_fn(arr->array[i]);
  41. free(arr->array);
  42. free(arr);
  43. }
  44. void*
  45. array_list_get_idx(struct array_list *arr, int i)
  46. {
  47. if(i >= arr->length) return NULL;
  48. return arr->array[i];
  49. }
  50. static int array_list_expand_internal(struct array_list *arr, int max)
  51. {
  52. void *t;
  53. int new_size;
  54. if(max < arr->size) return 0;
  55. new_size = arr->size << 1;
  56. if (new_size < max)
  57. new_size = max;
  58. if(!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
  59. arr->array = (void**)t;
  60. (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
  61. arr->size = new_size;
  62. return 0;
  63. }
  64. int
  65. array_list_put_idx(struct array_list *arr, int idx, void *data)
  66. {
  67. if(array_list_expand_internal(arr, idx+1)) return -1;
  68. if(arr->array[idx]) arr->free_fn(arr->array[idx]);
  69. arr->array[idx] = data;
  70. if(arr->length <= idx) arr->length = idx + 1;
  71. return 0;
  72. }
  73. int
  74. array_list_add(struct array_list *arr, void *data)
  75. {
  76. return array_list_put_idx(arr, arr->length, data);
  77. }
  78. void
  79. array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
  80. {
  81. qsort(arr->array, arr->length, sizeof(arr->array[0]),
  82. (int (*)(const void *, const void *))sort_fn);
  83. }
  84. int
  85. array_list_length(struct array_list *arr)
  86. {
  87. return arr->length;
  88. }