1
0

ArrayList.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef ArrayList_H
  16. #define ArrayList_H
  17. #include "memory/Allocator.h"
  18. #include "util/Linker.h"
  19. Linker_require("util/ArrayList.c")
  20. /*
  21. * This struct is never defined anywhere.
  22. * It's used in place to void* so that casts must be explicit to reduce likelihood of error.
  23. */
  24. struct ArrayList;
  25. void* ArrayList_new(struct Allocator* alloc, int initialCapacity);
  26. int ArrayList_add(struct ArrayList* list, void* val);
  27. void* ArrayList_get(struct ArrayList* list, int number);
  28. int ArrayList_put(struct ArrayList* list, int number, void* val);
  29. void* ArrayList_remove(struct ArrayList* list, int num);
  30. void ArrayList_sort(struct ArrayList* list, int (* compare)(const void* a, const void* b));
  31. void* ArrayList_clone(struct ArrayList* vlist, struct Allocator* alloc);
  32. #endif // Used multiple times...
  33. #ifndef ArrayList_NOCREATE
  34. #ifndef ArrayList_TYPE
  35. #error ArrayList_TYPE must be specified
  36. #endif
  37. #ifndef ArrayList_NAME
  38. #ifndef ArrayList_FULLNAME
  39. #error ArrayList_NAME must be specified
  40. #endif
  41. #else
  42. #define ArrayList_FULLNAME ArrayList_GLUE(ArrayList, ArrayList_NAME)
  43. #endif
  44. #ifndef ArrayList_INITIAL_CAPACITY
  45. #define ArrayList_INITIAL_CAPACITY 8
  46. #endif
  47. #define ArrayList_FUNCTION(name) \
  48. ArrayList_GLUE(ArrayList_FULLNAME, name)
  49. #define ArrayList_GLUE(x,y) ArrayList_GLUE2(x,y)
  50. #define ArrayList_GLUE2(x,y) x ## _ ## y
  51. struct ArrayList_FULLNAME {
  52. int length;
  53. };
  54. static inline struct ArrayList_FULLNAME* ArrayList_FUNCTION(new)(struct Allocator* alloc)
  55. {
  56. return (struct ArrayList_FULLNAME*) ArrayList_new(alloc, ArrayList_INITIAL_CAPACITY);
  57. }
  58. static inline ArrayList_TYPE* ArrayList_FUNCTION(get)(struct ArrayList_FULLNAME* list, int number)
  59. {
  60. return (ArrayList_TYPE*) ArrayList_get((struct ArrayList*) list, number);
  61. }
  62. static inline int ArrayList_FUNCTION(put)(struct ArrayList_FULLNAME* list,
  63. int number,
  64. ArrayList_TYPE* val)
  65. {
  66. return ArrayList_put((struct ArrayList*) list, number, val);
  67. }
  68. static inline int ArrayList_FUNCTION(add)(struct ArrayList_FULLNAME* list, void* val)
  69. {
  70. return ArrayList_put((struct ArrayList*) list, list->length, val);
  71. }
  72. static inline ArrayList_TYPE* ArrayList_FUNCTION(shift)(struct ArrayList_FULLNAME* list)
  73. {
  74. return (ArrayList_TYPE*) ArrayList_remove((struct ArrayList*) list, 0);
  75. }
  76. static inline ArrayList_TYPE* ArrayList_FUNCTION(pop)(struct ArrayList_FULLNAME* list)
  77. {
  78. return (ArrayList_TYPE*) ArrayList_remove((struct ArrayList*) list, list->length - 1);
  79. }
  80. static inline ArrayList_TYPE* ArrayList_FUNCTION(remove)(struct ArrayList_FULLNAME* list, int num)
  81. {
  82. return (ArrayList_TYPE*) ArrayList_remove((struct ArrayList*) list, num);
  83. }
  84. #ifdef ArrayList_COMPARE
  85. static inline int ArrayList_FUNCTION(sort_compare)(const void* a, const void* b)
  86. {
  87. return ArrayList_COMPARE(((ArrayList_TYPE**)a)[0], ((ArrayList_TYPE**)b)[0]);
  88. }
  89. static inline void ArrayList_FUNCTION(sort)(struct ArrayList_FULLNAME* list)
  90. {
  91. ArrayList_sort((struct ArrayList*) list, ArrayList_FUNCTION(sort_compare));
  92. }
  93. #endif
  94. /** Cloning the list does not clone the elements, just the pointers to them. */
  95. static inline void* ArrayList_FUNCTION(clone)(struct ArrayList_FULLNAME* l, struct Allocator* alloc)
  96. {
  97. return ArrayList_clone((struct ArrayList*) l, alloc);
  98. }
  99. #undef ArrayList_TYPE
  100. #undef ArrayList_NAME
  101. #undef ArrayList_FULLNAME
  102. #undef ArrayList_INITIAL_CAPACITY
  103. #undef ArrayList_COMPARE
  104. #undef ArrayList_FUNCTION
  105. #undef ArrayList_FULLNAME
  106. #undef ArrayList_GLUE
  107. #undef ArrayList_GLUE2
  108. #endif // not defined ArrayList_NOCREATE