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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * $Id: arraylist.c,v 1.3 2005/06/14 22:41:51 mclark Exp $
  3. *
  4. * Copyright Metaparadigm Pte. Ltd. 2004.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public (LGPL)
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details: http://www.gnu.org/
  16. *
  17. */
  18. #include "config.h"
  19. #if STDC_HEADERS
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif /* STDC_HEADERS */
  23. #if HAVE_STRINGS_H
  24. # include <strings.h>
  25. #endif /* HAVE_STRINGS_H */
  26. #include "bits.h"
  27. #include "arraylist.h"
  28. struct array_list*
  29. array_list_new(array_list_free_fn *free_fn)
  30. {
  31. struct array_list *this;
  32. if(!(this = calloc(1, sizeof(struct array_list)))) return NULL;
  33. this->size = ARRAY_LIST_DEFAULT_SIZE;
  34. this->length = 0;
  35. this->free_fn = free_fn;
  36. if(!(this->array = calloc(sizeof(void*), this->size))) {
  37. free(this);
  38. return NULL;
  39. }
  40. return this;
  41. }
  42. extern void
  43. array_list_free(struct array_list *this)
  44. {
  45. int i;
  46. for(i = 0; i < this->length; i++)
  47. if(this->array[i]) this->free_fn(this->array[i]);
  48. free(this->array);
  49. free(this);
  50. }
  51. void*
  52. array_list_get_idx(struct array_list *this, int i)
  53. {
  54. if(i >= this->length) return NULL;
  55. return this->array[i];
  56. }
  57. static int array_list_expand_internal(struct array_list *this, int max)
  58. {
  59. void *t;
  60. int new_size;
  61. if(max < this->size) return 0;
  62. new_size = max(this->size << 1, max);
  63. if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1;
  64. this->array = t;
  65. (void)memset(this->array + this->size, 0, (new_size-this->size)*sizeof(void*));
  66. this->size = new_size;
  67. return 0;
  68. }
  69. int
  70. array_list_put_idx(struct array_list *this, int idx, void *data)
  71. {
  72. if(array_list_expand_internal(this, idx)) return -1;
  73. if(this->array[idx]) this->free_fn(this->array[idx]);
  74. this->array[idx] = data;
  75. if(this->length <= idx) this->length = idx + 1;
  76. return 0;
  77. }
  78. int
  79. array_list_add(struct array_list *this, void *data)
  80. {
  81. return array_list_put_idx(this, this->length, data);
  82. }
  83. int
  84. array_list_length(struct array_list *this)
  85. {
  86. return this->length;
  87. }

No Description

Contributors (1)