reference-libobs-util-darray.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. .. :c: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. .. function:: void da_init(da)
  32. Initializes a dynamic array.
  33. :param da: The dynamic array
  34. ---------------------
  35. .. function:: void da_free(da)
  36. Frees a dynamic array.
  37. :param da: The dynamic array
  38. ---------------------
  39. .. function:: size_t da_alloc_size(v)
  40. Gets a size of allocated array in bytes.
  41. :param da: The dynamic array
  42. :return: The allocated size of the dynamic array.
  43. ---------------------
  44. .. function:: void *da_end(da)
  45. Gets a pointer to the last value.
  46. :param da: The dynamic array
  47. :return: The last value of a dynamic array, or *NULL* if empty.
  48. ---------------------
  49. .. function:: void da_reserve(da, size_t capacity)
  50. Reserves a specific amount of buffer space for the dynamic array.
  51. :param da: The dynamic array
  52. :param capacity: New capacity of the dynamic array
  53. ---------------------
  54. .. function:: void da_resize(da, size_t new_size)
  55. Resizes the dynamic array with zeroed values.
  56. :param da: The dynamic array
  57. :param size: New size of the dynamic array
  58. ---------------------
  59. .. function:: void da_copy(da_dst, da_src)
  60. Makes a copy of a dynamic array.
  61. :param da_dst: The dynamic array to copy to
  62. :param da_src: The dynamic array to copy from
  63. ---------------------
  64. .. function:: void da_copy_array(da, const void *src_array, size_t size)
  65. Makes a copy of an array pointer.
  66. :param da: The dynamic array
  67. :param src_array: The array pointer to make a copy from
  68. :param size: New size of the dynamic array
  69. ---------------------
  70. .. function:: void da_move(da_dst, da_src)
  71. Moves one dynamic array variable to another without allocating new
  72. data. *da_dst* is freed before moving, *da_dst* is set to *da_src*,
  73. then *da_src* is then zeroed.
  74. :param da_dst: Destination variable
  75. :param da_src: Source variable
  76. ---------------------
  77. .. function:: size_t da_find(da, const void *item_data, size_t starting_idx)
  78. Finds a value based upon its data. If the value cannot be found, the
  79. return value will be DARRAY_INVALID (-1).
  80. :param da: The dynamic array
  81. :param item_data: The item data to find
  82. :param starting_idx: The index to start from or 0 to search the
  83. entire array
  84. ---------------------
  85. .. function:: size_t da_push_back(da, const void *data)
  86. Pushes data to the back of the array.
  87. :param da: The dynamic array
  88. :param data: Pointer to the new data to push
  89. :return: Index of the new value
  90. ---------------------
  91. .. function:: void *da_push_back_new(da)
  92. Pushes a zeroed value to the back of the array, and returns a pointer
  93. to it.
  94. :param da: The dynamic array
  95. :return: Pointer to the new value
  96. ---------------------
  97. .. function:: size_t da_push_back_array(da, const void *src_array, size_t item_count)
  98. Pushes an array of values to the back of the array.
  99. :param da: The dynamic array
  100. :param src_array: Pointer of the array of values
  101. :param item_count: Number of items to push back
  102. :return: Index of the first new value
  103. ---------------------
  104. .. function:: void da_insert(da, size_t idx, const void *data)
  105. Inserts a value at a given index.
  106. :param da: The dynamic array:
  107. :param idx: Index where the new item will be inserted
  108. :param data: Pointer to the item data to insert
  109. ---------------------
  110. .. function:: void *da_insert_new(da, size_t idx)
  111. Inserts a new zeroed value at a specific index, and returns a pointer
  112. to it.
  113. :param da: The dynamic array
  114. :param idx: Index to insert at
  115. :return: Pointer to the new value
  116. ---------------------
  117. .. function:: void da_insert_array(dst, size_t idx, src, size_t n)
  118. Inserts one or more items at a given index.
  119. :param dst: The dynamic array:
  120. :param idx: Index where the new item will be inserted
  121. :param src: Pointer to the first item to insert
  122. :param n: Number of items to insert
  123. ---------------------
  124. .. function:: void da_insert_da(da_dst, size_t idx, da_src)
  125. Inserts a dynamic array in to another dynamic array at a specific
  126. index.
  127. :param da_dst: Destination dynamic array being inserted in to
  128. :param idx: Index to insert the data at
  129. :param da_src: The dynamic array to insert
  130. ---------------------
  131. .. function:: void da_erase(da, size_t idx)
  132. Erases an item at a specific index.
  133. :param da: The dynamic array
  134. :param idx: The index of the value to remove
  135. ---------------------
  136. .. function:: void da_erase_item(da, const void *item_data)
  137. Erases an item that matches the value specified
  138. :param da: The dynamic array
  139. :param item_data: Pointer to the data to remove
  140. ---------------------
  141. .. function:: void da_erase_range(da, size_t start_idx, size_t end_idx)
  142. Erases a range of values, including the element at ``start_idx``, but
  143. not the one at ``end_idx``.
  144. :param da: The dynamic array
  145. :param start_idx: The starting index
  146. :param end_idx: The ending index
  147. ---------------------
  148. .. function:: void da_pop_back(da)
  149. Removes one item from the end of a dynamic array.
  150. :param da: The dynamic array
  151. ---------------------
  152. .. function:: void da_join(da_dst, da_src)
  153. Pushes *da_src* to the end of *da_dst* and frees *da_src*.
  154. :param da_dst: The destination dynamic array
  155. :param da_src: The source dynamic array
  156. ---------------------
  157. .. function:: void da_split(da_dst1, da_dst2, da_src, size_t split_idx)
  158. Creates two dynamic arrays by splitting another dynamic array at a
  159. specific index. If the destination arrays are not freed, they will
  160. be freed before getting their new values. The array being split will
  161. not be freed.
  162. :param da_dst1: Dynamic array that will get the lower half
  163. :param da_dst2: Dynamic array that will get the upper half
  164. :param da_src: Dynamic array to split
  165. :param split_idx: Index to split *da_src* at
  166. ---------------------
  167. .. function:: void da_move_item(da, size_t src_idx, size_t dst_idx)
  168. Moves an item from one index to another, moving data between if
  169. necessary.
  170. :param da: The dynamic array
  171. :param src_idx: The index of the item to move
  172. :param dst_idx: The new index of where the item will be moved to
  173. ---------------------
  174. .. function:: void da_swap(da, size_t idx1, size_t idx2)
  175. Swaps two values at the given indices.
  176. :param da: The dynamic array
  177. :param idx1: Index of the first item to swap
  178. :param idx2: Index of the second item to swap