ArrayList.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <http://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. void* ArrayList_new(struct Allocator* alloc, int initialCapacity);
  21. int ArrayList_add(void* list, void* val);
  22. void* ArrayList_get(void* list, int number);
  23. int ArrayList_put(void* list, int number, void* val);
  24. void* ArrayList_shift(void* list);
  25. #endif // Used multiple times...
  26. #ifndef ArrayList_NOCREATE
  27. #ifndef ArrayList_TYPE
  28. #error ArrayList_TYPE must be specified
  29. #endif
  30. #ifndef ArrayList_NAME
  31. #error ArrayList_NAME must be specified
  32. #endif
  33. #ifndef ArrayList_INITIAL_CAPACITY
  34. #define ArrayList_INITIAL_CAPACITY 8
  35. #endif
  36. #define ArrayList_FUNCTION(name) \
  37. ArrayList_GLUE(ArrayList_GLUE(ArrayList, ArrayList_NAME), name)
  38. #define ArrayList_STRUCT ArrayList_GLUE(ArrayList, ArrayList_NAME)
  39. #define ArrayList_GLUE(x,y) ArrayList_GLUE2(x,y)
  40. #define ArrayList_GLUE2(x,y) x ## _ ## y
  41. struct ArrayList_STRUCT {
  42. int length;
  43. };
  44. static inline struct ArrayList_STRUCT* ArrayList_FUNCTION(new)(struct Allocator* alloc)
  45. {
  46. return (struct ArrayList_STRUCT*) ArrayList_new(alloc, ArrayList_INITIAL_CAPACITY);
  47. }
  48. static inline ArrayList_TYPE* ArrayList_FUNCTION(get)(struct ArrayList_STRUCT* list, int number)
  49. {
  50. return (ArrayList_TYPE*) ArrayList_get((void*) list, number);
  51. }
  52. static inline int ArrayList_FUNCTION(put)(struct ArrayList_STRUCT* list,
  53. int number,
  54. ArrayList_TYPE* val)
  55. {
  56. return ArrayList_put((void*) list, number, val);
  57. }
  58. static inline int ArrayList_FUNCTION(add)(struct ArrayList_STRUCT* list, void* val)
  59. {
  60. return ArrayList_put((void*) list, list->length, val);
  61. }
  62. static inline ArrayList_TYPE* ArrayList_FUNCTION(shift)(struct ArrayList_STRUCT* list)
  63. {
  64. return (ArrayList_TYPE*) ArrayList_shift((void*) list);
  65. }
  66. #undef ArrayList_TYPE
  67. #undef ArrayList_NAME
  68. #undef ArrayList_INITIAL_CAPACITY
  69. #undef ArrayList_FUNCTION
  70. #undef ArrayList_STRUCT
  71. #undef ArrayList_GLUE
  72. #undef ArrayList_GLUE2
  73. #endif // not defined ArrayList_NOCREATE