vec.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #ifndef ELL_VEC_H
  3. #define ELL_VEC_H
  4. #include <stdlib.h>
  5. struct ell_vec_core {
  6. size_t capacity;
  7. size_t size;
  8. };
  9. #define ELL_VEC_RAW(name, type) \
  10. struct name { \
  11. struct ell_vec_core core; \
  12. type; \
  13. }
  14. #define ELL_VEC_DECLARE(name, type) ELL_VEC_RAW(name, type* data)
  15. #define ELL_VEC_DECLARE_FP(name, ret, ...) \
  16. ELL_VEC_RAW(name, ret (**data)(__VA_ARGS__))
  17. #define ELL_VEC_DECLARE_TDEF(name, type) \
  18. typedef ELL_VEC_DECLARE(name, type) name
  19. #define ELL_VEC_RAW_TDEF(name, type) typedef ELL_VEC_RAW(name, type) name
  20. /* Initialization and Modifying */
  21. #define ELL_VEC_INIT(vec) ell__vec_init(&(vec)->core, (void**)&(vec)->data)
  22. #define ELL_VEC_INIT_RESERVE(vec, i) \
  23. do { \
  24. ell__vec_init(&(vec)->core, (void**)&(vec)->data); \
  25. ELL_VEC_RESERVE((vec), (i)); \
  26. } while (0)
  27. #define ELL_VEC_INIT_SIZE(vec, i) \
  28. do { \
  29. ell__vec_init(&(vec)->core, (void**)&(vec)->data); \
  30. ELL_VEC_RESIZE((vec), (i)); \
  31. } while (0)
  32. #define ELL_VEC_INIT_FILL(vec, s, i) \
  33. do { \
  34. ELL_VEC_INIT_SIZE((vec), (s)); \
  35. ELL_VEC_FILL((vec), (i)); \
  36. } while (0)
  37. #define ELL_VEC_PUSH(vec, value) \
  38. do { \
  39. ell__vec_ensure_capacity(&(vec)->core, (void**)&(vec)->data, \
  40. sizeof(*((vec)->data))); \
  41. (vec)->data[(vec)->core.size++] = (value); \
  42. } while (0)
  43. #define ELL_VEC_POP(vec) ((vec)->core.size--)
  44. #define ELL_VEC_RESET(vec) ell__vec_reset(&(vec)->core, (void**)&(vec)->data)
  45. #define ELL_VEC_RESERVE(vec, i) \
  46. ell__vec_reserve(&(vec)->core, (void**)&(vec)->data, \
  47. sizeof(*((vec)->data)), (i));
  48. #define ELL_VEC_RESIZE(vec, i) \
  49. ell__vec_resize(&(vec)->core, (void**)&(vec)->data, \
  50. sizeof(*((vec)->data)), (i));
  51. #define ELL_VEC_AT(vec, i) ((vec)->data[(i)])
  52. #define ELL_VEC_FRONT(vec) ELL_VEC_AT((vec), 0)
  53. #define ELL_VEC_BACK(vec) ELL_VEC_AT((vec), (vec)->core.size - 1)
  54. #define ELL_VEC_PTR(vec, i) ((vec)->data + (i))
  55. #define ELL_VEC_FILL(vec, i) \
  56. do { \
  57. if ((vec)->core.size == 0) { \
  58. break; \
  59. } \
  60. ((vec))->data[0] = (i); \
  61. ell__vec_fill_from_first(&(vec)->core, (void*)((vec)->data), \
  62. sizeof(*((vec)->data))); \
  63. } while (0)
  64. /* Queries */
  65. // XXX: build wrapper functions such that size and capacity cannot
  66. // be written through these macros.
  67. #define ELL_VEC_SIZE(vec) ((vec)->core.size)
  68. #define ELL_VEC_CAPACITY(vec) ((vec)->core.capacity)
  69. /* Implementation */
  70. void ell__vec_init(struct ell_vec_core* core, void** data);
  71. void ell__vec_ensure_capacity(struct ell_vec_core* core, void** data,
  72. size_t element_size);
  73. void ell__vec_reset(struct ell_vec_core* core, void** data);
  74. void ell__vec_reserve(struct ell_vec_core* core, void** data,
  75. size_t element_size, size_t elements);
  76. void ell__vec_resize(struct ell_vec_core* core, void** data,
  77. size_t element_size, size_t elements);
  78. void ell__vec_fill_from_first(struct ell_vec_core* core, void* data,
  79. size_t element_size);
  80. /* Convenience types */
  81. ELL_VEC_DECLARE(ell_vec_uint, unsigned int);
  82. ELL_VEC_DECLARE(ell_vec_int, int);
  83. ELL_VEC_DECLARE(ell_vec_ulong, unsigned long);
  84. ELL_VEC_DECLARE(ell_vec_long, long);
  85. ELL_VEC_DECLARE(ell_vec_float, float);
  86. ELL_VEC_DECLARE(ell_vec_double, double);
  87. #endif