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.h 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * $Id: arraylist.h,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. /**
  12. * @file
  13. * @brief Internal methods for working with json_type_array objects.
  14. * Although this is exposed by the json_object_get_array() method,
  15. * it is not recommended for direct use.
  16. */
  17. #ifndef _json_c_arraylist_h_
  18. #define _json_c_arraylist_h_
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include <stddef.h>
  23. #define ARRAY_LIST_DEFAULT_SIZE 32
  24. typedef void(array_list_free_fn)(void *data);
  25. struct array_list
  26. {
  27. void **array;
  28. size_t length;
  29. size_t size;
  30. array_list_free_fn *free_fn;
  31. };
  32. typedef struct array_list array_list;
  33. /**
  34. * Allocate an array_list of the default size (32).
  35. * @deprecated Use array_list_new2() instead.
  36. */
  37. extern struct array_list *array_list_new(array_list_free_fn *free_fn);
  38. /**
  39. * Allocate an array_list of the desired size.
  40. *
  41. * If possible, the size should be chosen to closely match
  42. * the actual number of elements expected to be used.
  43. * If the exact size is unknown, there are tradeoffs to be made:
  44. * - too small - the array_list code will need to call realloc() more
  45. * often (which might incur an additional memory copy).
  46. * - too large - will waste memory, but that can be mitigated
  47. * by calling array_list_shrink() once the final size is known.
  48. *
  49. * @see array_list_shrink
  50. */
  51. extern struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size);
  52. extern void array_list_free(struct array_list *al);
  53. extern void *array_list_get_idx(struct array_list *al, size_t i);
  54. extern int array_list_insert_idx(struct array_list *al, size_t i, void *data);
  55. extern int array_list_put_idx(struct array_list *al, size_t i, void *data);
  56. extern int array_list_add(struct array_list *al, void *data);
  57. extern size_t array_list_length(struct array_list *al);
  58. extern void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *));
  59. extern void *array_list_bsearch(const void **key, struct array_list *arr,
  60. int (*compar)(const void *, const void *));
  61. extern int array_list_del_idx(struct array_list *arr, size_t idx, size_t count);
  62. /**
  63. * Shrink the array list to just enough to fit the number of elements in it,
  64. * plus empty_slots.
  65. */
  66. extern int array_list_shrink(struct array_list *arr, size_t empty_slots);
  67. #ifdef __cplusplus
  68. }
  69. #endif
  70. #endif