reference-libobs-util-darray.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. Dynamic Arrays
  2. ==============
  3. Dynamically resizing arrays (a C equivalent to std::vector).
  4. .. code:: cpp
  5. #include <util/darray.h>
  6. .. struct:: darray
  7. The base dynamic array structure.
  8. .. macro:: DARRAY(type)
  9. Macro for a dynamic array based upon an actual type. Use this with
  10. da_* macros.
  11. .. member:: void *darray.array
  12. The array pointer.
  13. .. member:: size_t darray.num
  14. The number of items within the array.
  15. .. member:: size_t darray.capacity
  16. The capacity of the array.
  17. Dynamic Array Macros
  18. --------------------
  19. These macro functions are used with variables created with the
  20. **DARRAY** type macro. When using these functions, do not use the
  21. dynamic array value with a reference (&) operator. For example:
  22. .. code:: cpp
  23. /* creates an array of integers: 0..9 */
  24. DARRAY(int) array_of_integers;
  25. da_init(array_of_integers);
  26. for (size_t i = 0; i < 10; i++)
  27. da_push_back(array_of_integers, &i);
  28. [...]
  29. /* free when complete */
  30. da_free(array_of_integers);
  31. To pass dynamic arrays to functions as parameters, create a typedef with the
  32. :c:macro:`DARRAY` macro and type so you can pass the dynamic array with a
  33. reference (&) operator. An alternative is to define a struct that will contain
  34. the dynamic array, and that struct will be passed to the functions, instead of
  35. the dynamic array directly. An example with the typedef method:
  36. .. code:: cpp
  37. typedef DARRAY(int) int_array_t;
  38. void generate_integers(int_array_t *integers, int start, int end)
  39. {
  40. for (int i = start; i < end; i++)
  41. da_push_back(*integers, &i);
  42. }
  43. [...]
  44. int_array_t array_of_integers;
  45. da_init(array_of_integers);
  46. generate_integers(&array_of_integers, 0, 10);
  47. /* free when complete */
  48. da_free(array_of_integers);
  49. **IMPORTANT NOTE:** While it is also possible to accept the internal
  50. :c:struct:`darray` struct as a function parameter (via the ``da`` member
  51. variable of dynamic arrays) and redefine a variable with :c:macro:`DARRAY`
  52. inside the function, doing so is not safe and not recommended. One potential
  53. issue with it is having a type declaration in the function that is different
  54. than the type of the actual dynamic array that will be passed to the function,
  55. which will cause memory access issues that will not be caught by the compiler.
  56. As mentioned above, the recommended way is to create a typedef or a container
  57. struct, which will be safer in usage.
  58. .. function:: void da_init(da)
  59. Initializes a dynamic array.
  60. :param da: The dynamic array
  61. ---------------------
  62. .. function:: void da_free(da)
  63. Frees a dynamic array.
  64. :param da: The dynamic array
  65. ---------------------
  66. .. function:: size_t da_alloc_size(v)
  67. Gets a size of allocated array in bytes.
  68. :param da: The dynamic array
  69. :return: The allocated size of the dynamic array.
  70. ---------------------
  71. .. function:: void *da_end(da)
  72. Gets a pointer to the last value.
  73. :param da: The dynamic array
  74. :return: The last value of a dynamic array, or *NULL* if empty.
  75. ---------------------
  76. .. function:: void da_reserve(da, size_t capacity)
  77. Reserves a specific amount of buffer space for the dynamic array.
  78. :param da: The dynamic array
  79. :param capacity: New capacity of the dynamic array
  80. ---------------------
  81. .. function:: void da_resize(da, size_t new_size)
  82. Resizes the dynamic array with zeroed values.
  83. :param da: The dynamic array
  84. :param size: New size of the dynamic array
  85. ---------------------
  86. .. function:: void da_copy(da_dst, da_src)
  87. Makes a copy of a dynamic array.
  88. :param da_dst: The dynamic array to copy to
  89. :param da_src: The dynamic array to copy from
  90. ---------------------
  91. .. function:: void da_copy_array(da, const void *src_array, size_t size)
  92. Makes a copy of an array pointer.
  93. :param da: The dynamic array
  94. :param src_array: The array pointer to make a copy from
  95. :param size: New size of the dynamic array
  96. ---------------------
  97. .. function:: void da_move(da_dst, da_src)
  98. Moves one dynamic array variable to another without allocating new
  99. data. *da_dst* is freed before moving, *da_dst* is set to *da_src*,
  100. then *da_src* is then zeroed.
  101. :param da_dst: Destination variable
  102. :param da_src: Source variable
  103. ---------------------
  104. .. function:: size_t da_find(da, const void *item_data, size_t starting_idx)
  105. Finds a value based upon its data. If the value cannot be found, the
  106. return value will be DARRAY_INVALID (-1).
  107. :param da: The dynamic array
  108. :param item_data: The item data to find
  109. :param starting_idx: The index to start from or 0 to search the
  110. entire array
  111. ---------------------
  112. .. function:: size_t da_push_back(da, const void *data)
  113. Pushes data to the back of the array.
  114. :param da: The dynamic array
  115. :param data: Pointer to the new data to push
  116. :return: Index of the new value
  117. ---------------------
  118. .. function:: void *da_push_back_new(da)
  119. Pushes a zeroed value to the back of the array, and returns a pointer
  120. to it.
  121. :param da: The dynamic array
  122. :return: Pointer to the new value
  123. ---------------------
  124. .. function:: size_t da_push_back_array(da, const void *src_array, size_t item_count)
  125. Pushes an array of values to the back of the array.
  126. :param da: The dynamic array
  127. :param src_array: Pointer of the array of values
  128. :param item_count: Number of items to push back
  129. :return: Index of the first new value
  130. ---------------------
  131. .. function:: void da_insert(da, size_t idx, const void *data)
  132. Inserts a value at a given index.
  133. :param da: The dynamic array:
  134. :param idx: Index where the new item will be inserted
  135. :param data: Pointer to the item data to insert
  136. ---------------------
  137. .. function:: void *da_insert_new(da, size_t idx)
  138. Inserts a new zeroed value at a specific index, and returns a pointer
  139. to it.
  140. :param da: The dynamic array
  141. :param idx: Index to insert at
  142. :return: Pointer to the new value
  143. ---------------------
  144. .. function:: void da_insert_array(dst, size_t idx, src, size_t n)
  145. Inserts one or more items at a given index.
  146. :param dst: The dynamic array:
  147. :param idx: Index where the new item will be inserted
  148. :param src: Pointer to the first item to insert
  149. :param n: Number of items to insert
  150. ---------------------
  151. .. function:: void da_insert_da(da_dst, size_t idx, da_src)
  152. Inserts a dynamic array in to another dynamic array at a specific
  153. index.
  154. :param da_dst: Destination dynamic array being inserted in to
  155. :param idx: Index to insert the data at
  156. :param da_src: The dynamic array to insert
  157. ---------------------
  158. .. function:: void da_erase(da, size_t idx)
  159. Erases an item at a specific index.
  160. :param da: The dynamic array
  161. :param idx: The index of the value to remove
  162. ---------------------
  163. .. function:: void da_erase_item(da, const void *item_data)
  164. Erases an item that matches the value specified
  165. :param da: The dynamic array
  166. :param item_data: Pointer to the data to remove
  167. ---------------------
  168. .. function:: void da_erase_range(da, size_t start_idx, size_t end_idx)
  169. Erases a range of values, including the element at ``start_idx``, but
  170. not the one at ``end_idx``.
  171. :param da: The dynamic array
  172. :param start_idx: The starting index
  173. :param end_idx: The ending index
  174. ---------------------
  175. .. function:: void da_pop_back(da)
  176. Removes one item from the end of a dynamic array.
  177. :param da: The dynamic array
  178. ---------------------
  179. .. function:: void da_join(da_dst, da_src)
  180. Pushes *da_src* to the end of *da_dst* and frees *da_src*.
  181. :param da_dst: The destination dynamic array
  182. :param da_src: The source dynamic array
  183. ---------------------
  184. .. function:: void da_split(da_dst1, da_dst2, da_src, size_t split_idx)
  185. Creates two dynamic arrays by splitting another dynamic array at a
  186. specific index. If the destination arrays are not freed, they will
  187. be freed before getting their new values. The array being split will
  188. not be freed.
  189. :param da_dst1: Dynamic array that will get the lower half
  190. :param da_dst2: Dynamic array that will get the upper half
  191. :param da_src: Dynamic array to split
  192. :param split_idx: Index to split *da_src* at
  193. ---------------------
  194. .. function:: void da_move_item(da, size_t src_idx, size_t dst_idx)
  195. Moves an item from one index to another, moving data between if
  196. necessary.
  197. :param da: The dynamic array
  198. :param src_idx: The index of the item to move
  199. :param dst_idx: The new index of where the item will be moved to
  200. ---------------------
  201. .. function:: void da_swap(da, size_t idx1, size_t idx2)
  202. Swaps two values at the given indices.
  203. :param da: The dynamic array
  204. :param idx1: Index of the first item to swap
  205. :param idx2: Index of the second item to swap