vc_vector.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // https://github.com/skogorev/vc_vector
  2. #ifndef VCVECTOR_H
  3. #define VCVECTOR_H
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. typedef void (vc_vector_deleter)(void *);
  7. typedef struct vc_vector {
  8. size_t count;
  9. size_t element_size;
  10. size_t reserved_size;
  11. char* data;
  12. vc_vector_deleter* deleter;
  13. } vc_vector;
  14. // typedef struct vc_vector vc_vector;
  15. // ----------------------------------------------------------------------------
  16. // Control
  17. // ----------------------------------------------------------------------------
  18. // Constructs an empty vector with an reserver size for count_elements.
  19. vc_vector* vc_vector_create(size_t count_elements, size_t size_of_element, vc_vector_deleter* deleter);
  20. // Constructs a copy of an existing vector.
  21. vc_vector* vc_vector_create_copy(const vc_vector* vector);
  22. // Releases the vector.
  23. void vc_vector_release(vc_vector* vector);
  24. // Compares vector content
  25. bool vc_vector_is_equals(vc_vector* vector1, vc_vector* vector2);
  26. // Returns constant value of the vector growth factor.
  27. float vc_vector_get_growth_factor();
  28. // Returns constant value of the vector default count of elements.
  29. size_t vc_vector_get_default_count_of_elements();
  30. // Returns constant value of the vector struct size.
  31. size_t vc_vector_struct_size();
  32. // ----------------------------------------------------------------------------
  33. // Element access
  34. // ----------------------------------------------------------------------------
  35. // Returns the item at index position in the vector.
  36. void* vc_vector_at(vc_vector* vector, size_t index);
  37. // Returns the first item in the vector.
  38. void* vc_vector_front(vc_vector* vector);
  39. // Returns the last item in the vector.
  40. void* vc_vector_back(vc_vector* vector);
  41. // Returns a pointer to the data stored in the vector. The pointer can be used to access and modify the items in the vector.
  42. void* vc_vector_data(vc_vector* vector);
  43. // ----------------------------------------------------------------------------
  44. // Iterators
  45. // ----------------------------------------------------------------------------
  46. // Returns a pointer to the first item in the vector.
  47. void* vc_vector_begin(vc_vector* vector);
  48. // Returns a pointer to the imaginary item after the last item in the vector.
  49. void* vc_vector_end(vc_vector* vector);
  50. // Returns a pointer to the next element of vector after 'i'.
  51. void* vc_vector_next(vc_vector* vector, void* i);
  52. // ----------------------------------------------------------------------------
  53. // Capacity
  54. // ----------------------------------------------------------------------------
  55. // Returns true if the vector is empty; otherwise returns false.
  56. bool vc_vector_empty(vc_vector* vector);
  57. // Returns the number of elements in the vector.
  58. size_t vc_vector_count(const vc_vector* vector);
  59. // Returns the size (in bytes) of occurrences of value in the vector.
  60. size_t vc_vector_size(const vc_vector* vector);
  61. // Returns the maximum number of elements that the vector can hold.
  62. size_t vc_vector_max_count(const vc_vector* vector);
  63. // Returns the maximum size (in bytes) that the vector can hold.
  64. size_t vc_vector_max_size(const vc_vector* vector);
  65. // Resizes the container so that it contains n elements.
  66. bool vc_vector_reserve_count(vc_vector* vector, size_t new_count);
  67. // Resizes the container so that it contains new_size / element_size elements.
  68. bool vc_vector_reserve_size(vc_vector* vector, size_t new_size);
  69. // ----------------------------------------------------------------------------
  70. // Modifiers
  71. // ----------------------------------------------------------------------------
  72. // Removes all elements from the vector (without reallocation).
  73. void vc_vector_clear(vc_vector* vector);
  74. // The container is extended by inserting a new element at position.
  75. bool vc_vector_insert(vc_vector* vector, size_t index, const void* value);
  76. // Removes from the vector a single element by 'index'
  77. bool vc_vector_erase(vc_vector* vector, size_t index);
  78. // Removes from the vector a range of elements '[first_index, last_index)'.
  79. bool vc_vector_erase_range(vc_vector* vector, size_t first_index, size_t last_index);
  80. // Inserts multiple values at the end of the vector.
  81. bool vc_vector_append(vc_vector* vector, const void* values, size_t count);
  82. // Inserts value at the end of the vector.
  83. bool vc_vector_push_back(vc_vector* vector, const void* value);
  84. // Removes the last item in the vector.
  85. bool vc_vector_pop_back(vc_vector* vector);
  86. // Replace value by index in the vector.
  87. bool vc_vector_replace(vc_vector* vector, size_t index, const void* value);
  88. // Replace multiple values by index in the vector.
  89. bool vc_vector_replace_multiple(vc_vector* vector, size_t index, const void* values, size_t count);
  90. #endif // VCVECTOR_H