reference-libobs-util-darray.rst 6.7 KB

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