1
0

reference-libobs-util-darray.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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:: void 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. ---------------------
  90. .. function:: void *da_push_back_new(da)
  91. Pushes a zeroed value to the back of the array, and returns a pointer
  92. to it.
  93. :param da: The dynamic array
  94. :return: Pointer to the new value
  95. ---------------------
  96. .. function:: void da_push_back_array(da, const void *src_array, size_t item_count)
  97. Pushes an array of values to the back of the array.
  98. :param da: The dynamic array
  99. :param src_array: Pointer of the array of values
  100. :param item_count: Number of items to push back
  101. ---------------------
  102. .. function:: void da_insert(da, size_t idx, const void *data)
  103. Inserts a value at a given index.
  104. :param da: The dynamic array:
  105. :param idx: Index where the new item will be inserted
  106. :param data: Pointer to the item data to insert
  107. ---------------------
  108. .. function:: void *da_insert_new(da, size_t idx)
  109. Inserts a new zeroed value at a specific index, and returns a pointer
  110. to it.
  111. :param da: The dynamic array
  112. :param idx: Index to insert at
  113. :return: Pointer to the new value
  114. ---------------------
  115. .. function:: void da_insert_array(dst, size_t idx, src, size_t n)
  116. Inserts one or more items at a given index.
  117. :param dst: The dynamic array:
  118. :param idx: Index where the new item will be inserted
  119. :param src: Pointer to the first item to insert
  120. :param n: Number of items to insert
  121. ---------------------
  122. .. function:: void da_insert_da(da_dst, size_t idx, da_src)
  123. Inserts a dynamic array in to another dynamic array at a specific
  124. index.
  125. :param da_dst: Destination dynamic array being inserted in to
  126. :param idx: Index to insert the data at
  127. :param da_src: The dynamic array to insert
  128. ---------------------
  129. .. function:: void da_erase(da, size_t idx)
  130. Erases an item at a specific index.
  131. :param da: The dynamic array
  132. :param idx: The index of the value to remove
  133. ---------------------
  134. .. function:: void da_erase_item(da, const void *item_data)
  135. Erases an item that matches the value specified
  136. :param da: The dynamic array
  137. :param item_data: Pointer to the data to remove
  138. ---------------------
  139. .. function:: void da_erase_range(da, size_t start_idx, size_t end_idx)
  140. Erases a range of values, including the element at ``start_idx``, but
  141. not the one at ``end_idx``.
  142. :param da: The dynamic array
  143. :param start_idx: The starting index
  144. :param end_idx: The ending index
  145. ---------------------
  146. .. function:: void da_pop_back(da)
  147. Removes one item from the end of a dynamic array.
  148. :param da: The dynamic array
  149. ---------------------
  150. .. function:: void da_join(da_dst, da_src)
  151. Pushes *da_src* to the end of *da_dst* and frees *da_src*.
  152. :param da_dst: The destination dynamic array
  153. :param da_src: The source dynamic array
  154. ---------------------
  155. .. function:: void da_split(da_dst1, da_dst2, da_src, size_t split_idx)
  156. Creates two dynamic arrays by splitting another dynamic array at a
  157. specific index. If the destination arrays are not freed, they will
  158. be freed before getting their new values. The array being split will
  159. not be freed.
  160. :param da_dst1: Dynamic array that will get the lower half
  161. :param da_dst2: Dynamic array that will get the upper half
  162. :param da_src: Dynamic array to split
  163. :param split_idx: Index to split *da_src* at
  164. ---------------------
  165. .. function:: void da_move_item(da, size_t src_idx, size_t dst_idx)
  166. Moves an item from one index to another, moving data between if
  167. necessary.
  168. :param da: The dynamic array
  169. :param src_idx: The index of the item to move
  170. :param dst_idx: The new index of where the item will be moved to
  171. ---------------------
  172. .. function:: void da_swap(da, size_t idx1, size_t idx2)
  173. Swaps two values at the given indices.
  174. :param da: The dynamic array
  175. :param idx1: Index of the first item to swap
  176. :param idx2: Index of the second item to swap