vector.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif
  5. typedef struct c11_array{
  6. void* data;
  7. int count;
  8. int elem_size;
  9. } c11_array;
  10. void c11_array__ctor(c11_array* self, int elem_size, int count);
  11. void c11_array__dtor(c11_array* self);
  12. c11_array c11_array__copy(const c11_array* self);
  13. void* c11_array__at(c11_array* self, int index);
  14. typedef struct c11_vector{
  15. void* data;
  16. int count;
  17. int capacity;
  18. int elem_size;
  19. } c11_vector;
  20. void c11_vector__ctor(c11_vector* self, int elem_size);
  21. void c11_vector__dtor(c11_vector* self);
  22. c11_vector c11_vector__copy(const c11_vector* self);
  23. void* c11_vector__at(c11_vector* self, int index);
  24. void c11_vector__reserve(c11_vector* self, int capacity);
  25. #define c11__getitem(T, self, index) ((T*)(self)->data)[index]
  26. #define c11__setitem(T, self, index, value) ((T*)(self)->data)[index] = value;
  27. #define c11_vector__push(T, self, elem) \
  28. do{ \
  29. if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity*2); \
  30. ((T*)(self)->data)[(self)->count] = (elem); \
  31. (self)->count++; \
  32. }while(0)
  33. #define c11_vector__pop(T, self) \
  34. do{ \
  35. (self)->count--; \
  36. }while(0)
  37. #define c11_vector__extend(T, self, p, size) \
  38. do{ \
  39. c11_vector__reserve((self), (self)->count + (size)); \
  40. memcpy((T*)(self)->data + (self)->count, (p), (size) * sizeof(T)); \
  41. (self)->count += (size); \
  42. }while(0)
  43. #ifdef __cplusplus
  44. }
  45. #endif