priority_queue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/priority_queue.h>
  6. #include <string.h>
  7. #define PARENT_OF(index) (((index)&1) ? (index) >> 1 : (index) > 1 ? ((index)-2) >> 1 : 0)
  8. #define LEFT_OF(index) (((index) << 1) + 1)
  9. #define RIGHT_OF(index) (((index) << 1) + 2)
  10. static void s_swap(struct aws_priority_queue *queue, size_t a, size_t b) {
  11. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  12. AWS_PRECONDITION(a < queue->container.length);
  13. AWS_PRECONDITION(b < queue->container.length);
  14. AWS_PRECONDITION(aws_priority_queue_backpointer_index_valid(queue, a));
  15. AWS_PRECONDITION(aws_priority_queue_backpointer_index_valid(queue, b));
  16. aws_array_list_swap(&queue->container, a, b);
  17. /* Invariant: If the backpointer array is initialized, we have enough room for all elements */
  18. if (!AWS_IS_ZEROED(queue->backpointers)) {
  19. AWS_ASSERT(queue->backpointers.length > a);
  20. AWS_ASSERT(queue->backpointers.length > b);
  21. struct aws_priority_queue_node **bp_a = &((struct aws_priority_queue_node **)queue->backpointers.data)[a];
  22. struct aws_priority_queue_node **bp_b = &((struct aws_priority_queue_node **)queue->backpointers.data)[b];
  23. struct aws_priority_queue_node *tmp = *bp_a;
  24. *bp_a = *bp_b;
  25. *bp_b = tmp;
  26. if (*bp_a) {
  27. (*bp_a)->current_index = a;
  28. }
  29. if (*bp_b) {
  30. (*bp_b)->current_index = b;
  31. }
  32. }
  33. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  34. AWS_POSTCONDITION(aws_priority_queue_backpointer_index_valid(queue, a));
  35. AWS_POSTCONDITION(aws_priority_queue_backpointer_index_valid(queue, b));
  36. }
  37. /* Precondition: with the exception of the given root element, the container must be
  38. * in heap order */
  39. static bool s_sift_down(struct aws_priority_queue *queue, size_t root) {
  40. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  41. AWS_PRECONDITION(root < queue->container.length);
  42. bool did_move = false;
  43. size_t len = aws_array_list_length(&queue->container);
  44. while (LEFT_OF(root) < len) {
  45. size_t left = LEFT_OF(root);
  46. size_t right = RIGHT_OF(root);
  47. size_t first = root;
  48. void *first_item = NULL, *other_item = NULL;
  49. aws_array_list_get_at_ptr(&queue->container, &first_item, root);
  50. aws_array_list_get_at_ptr(&queue->container, &other_item, left);
  51. if (queue->pred(first_item, other_item) > 0) {
  52. first = left;
  53. first_item = other_item;
  54. }
  55. if (right < len) {
  56. aws_array_list_get_at_ptr(&queue->container, &other_item, right);
  57. /* choose the larger/smaller of the two in case of a max/min heap
  58. * respectively */
  59. if (queue->pred(first_item, other_item) > 0) {
  60. first = right;
  61. first_item = other_item;
  62. }
  63. }
  64. if (first != root) {
  65. s_swap(queue, first, root);
  66. did_move = true;
  67. root = first;
  68. } else {
  69. break;
  70. }
  71. }
  72. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  73. return did_move;
  74. }
  75. /* Precondition: Elements prior to the specified index must be in heap order. */
  76. static bool s_sift_up(struct aws_priority_queue *queue, size_t index) {
  77. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  78. AWS_PRECONDITION(index < queue->container.length);
  79. bool did_move = false;
  80. void *parent_item = NULL, *child_item = NULL;
  81. size_t parent = PARENT_OF(index);
  82. while (index) {
  83. /*
  84. * These get_ats are guaranteed to be successful; if they are not, we have
  85. * serious state corruption, so just abort.
  86. */
  87. if (aws_array_list_get_at_ptr(&queue->container, &parent_item, parent) ||
  88. aws_array_list_get_at_ptr(&queue->container, &child_item, index)) {
  89. abort();
  90. }
  91. if (queue->pred(parent_item, child_item) > 0) {
  92. s_swap(queue, index, parent);
  93. did_move = true;
  94. index = parent;
  95. parent = PARENT_OF(index);
  96. } else {
  97. break;
  98. }
  99. }
  100. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  101. return did_move;
  102. }
  103. /*
  104. * Precondition: With the exception of the given index, the heap condition holds for all elements.
  105. * In particular, the parent of the current index is a predecessor of all children of the current index.
  106. */
  107. static void s_sift_either(struct aws_priority_queue *queue, size_t index) {
  108. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  109. AWS_PRECONDITION(index < queue->container.length);
  110. if (!index || !s_sift_up(queue, index)) {
  111. s_sift_down(queue, index);
  112. }
  113. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  114. }
  115. int aws_priority_queue_init_dynamic(
  116. struct aws_priority_queue *queue,
  117. struct aws_allocator *alloc,
  118. size_t default_size,
  119. size_t item_size,
  120. aws_priority_queue_compare_fn *pred) {
  121. AWS_FATAL_PRECONDITION(queue != NULL);
  122. AWS_FATAL_PRECONDITION(alloc != NULL);
  123. AWS_FATAL_PRECONDITION(item_size > 0);
  124. queue->pred = pred;
  125. AWS_ZERO_STRUCT(queue->backpointers);
  126. int ret = aws_array_list_init_dynamic(&queue->container, alloc, default_size, item_size);
  127. if (ret == AWS_OP_SUCCESS) {
  128. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  129. } else {
  130. AWS_POSTCONDITION(AWS_IS_ZEROED(queue->container));
  131. AWS_POSTCONDITION(AWS_IS_ZEROED(queue->backpointers));
  132. }
  133. return ret;
  134. }
  135. void aws_priority_queue_init_static(
  136. struct aws_priority_queue *queue,
  137. void *heap,
  138. size_t item_count,
  139. size_t item_size,
  140. aws_priority_queue_compare_fn *pred) {
  141. AWS_FATAL_PRECONDITION(queue != NULL);
  142. AWS_FATAL_PRECONDITION(heap != NULL);
  143. AWS_FATAL_PRECONDITION(item_count > 0);
  144. AWS_FATAL_PRECONDITION(item_size > 0);
  145. queue->pred = pred;
  146. AWS_ZERO_STRUCT(queue->backpointers);
  147. aws_array_list_init_static(&queue->container, heap, item_count, item_size);
  148. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  149. }
  150. bool aws_priority_queue_backpointer_index_valid(const struct aws_priority_queue *const queue, size_t index) {
  151. if (AWS_IS_ZEROED(queue->backpointers)) {
  152. return true;
  153. }
  154. if (index < queue->backpointers.length) {
  155. struct aws_priority_queue_node *node = ((struct aws_priority_queue_node **)queue->backpointers.data)[index];
  156. return (node == NULL) || AWS_MEM_IS_WRITABLE(node, sizeof(struct aws_priority_queue_node));
  157. }
  158. return false;
  159. }
  160. bool aws_priority_queue_backpointers_valid_deep(const struct aws_priority_queue *const queue) {
  161. if (!queue) {
  162. return false;
  163. }
  164. for (size_t i = 0; i < queue->backpointers.length; i++) {
  165. if (!aws_priority_queue_backpointer_index_valid(queue, i)) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. bool aws_priority_queue_backpointers_valid(const struct aws_priority_queue *const queue) {
  172. if (!queue) {
  173. return false;
  174. }
  175. /* Internal container validity */
  176. bool backpointer_list_is_valid =
  177. ((aws_array_list_is_valid(&queue->backpointers) && (queue->backpointers.current_size != 0) &&
  178. (queue->backpointers.data != NULL)));
  179. /* Backpointer struct should either be zero or should be
  180. * initialized to be at most as long as the container, and having
  181. * as elements potentially null pointers to
  182. * aws_priority_queue_nodes */
  183. bool backpointer_list_item_size = queue->backpointers.item_size == sizeof(struct aws_priority_queue_node *);
  184. bool lists_equal_lengths = queue->backpointers.length == queue->container.length;
  185. bool backpointers_non_zero_current_size = queue->backpointers.current_size > 0;
  186. /* This check must be guarded, as it is not efficient, neither
  187. * when running tests nor CBMC */
  188. #if (AWS_DEEP_CHECKS == 1)
  189. bool backpointers_valid_deep = aws_priority_queue_backpointers_valid_deep(queue);
  190. #else
  191. bool backpointers_valid_deep = true;
  192. #endif
  193. bool backpointers_zero =
  194. (queue->backpointers.current_size == 0 && queue->backpointers.length == 0 && queue->backpointers.data == NULL);
  195. bool backpointer_struct_is_valid =
  196. backpointers_zero || (backpointer_list_item_size && lists_equal_lengths && backpointers_non_zero_current_size &&
  197. backpointers_valid_deep);
  198. return ((backpointer_list_is_valid && backpointer_struct_is_valid) || AWS_IS_ZEROED(queue->backpointers));
  199. }
  200. bool aws_priority_queue_is_valid(const struct aws_priority_queue *const queue) {
  201. /* Pointer validity checks */
  202. if (!queue) {
  203. return false;
  204. }
  205. bool pred_is_valid = (queue->pred != NULL);
  206. bool container_is_valid = aws_array_list_is_valid(&queue->container);
  207. bool backpointers_valid = aws_priority_queue_backpointers_valid(queue);
  208. return pred_is_valid && container_is_valid && backpointers_valid;
  209. }
  210. void aws_priority_queue_clean_up(struct aws_priority_queue *queue) {
  211. aws_array_list_clean_up(&queue->container);
  212. if (!AWS_IS_ZEROED(queue->backpointers)) {
  213. aws_array_list_clean_up(&queue->backpointers);
  214. }
  215. }
  216. int aws_priority_queue_push(struct aws_priority_queue *queue, void *item) {
  217. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  218. AWS_PRECONDITION(item && AWS_MEM_IS_READABLE(item, queue->container.item_size));
  219. int rval = aws_priority_queue_push_ref(queue, item, NULL);
  220. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  221. return rval;
  222. }
  223. int aws_priority_queue_push_ref(
  224. struct aws_priority_queue *queue,
  225. void *item,
  226. struct aws_priority_queue_node *backpointer) {
  227. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  228. AWS_PRECONDITION(item && AWS_MEM_IS_READABLE(item, queue->container.item_size));
  229. int err = aws_array_list_push_back(&queue->container, item);
  230. if (err) {
  231. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  232. return err;
  233. }
  234. size_t index = aws_array_list_length(&queue->container) - 1;
  235. if (backpointer && !queue->backpointers.alloc) {
  236. if (!queue->container.alloc) {
  237. aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
  238. goto backpointer_update_failed;
  239. }
  240. if (aws_array_list_init_dynamic(
  241. &queue->backpointers, queue->container.alloc, index + 1, sizeof(struct aws_priority_queue_node *))) {
  242. goto backpointer_update_failed;
  243. }
  244. /* When we initialize the backpointers array we need to zero out all existing entries */
  245. memset(queue->backpointers.data, 0, queue->backpointers.current_size);
  246. }
  247. /*
  248. * Once we have any backpointers, we want to make sure we always have room in the backpointers array
  249. * for all elements; otherwise, sift_down gets complicated if it runs out of memory when sifting an
  250. * element with a backpointer down in the array.
  251. */
  252. if (!AWS_IS_ZEROED(queue->backpointers)) {
  253. if (aws_array_list_set_at(&queue->backpointers, &backpointer, index)) {
  254. goto backpointer_update_failed;
  255. }
  256. }
  257. if (backpointer) {
  258. backpointer->current_index = index;
  259. }
  260. s_sift_up(queue, aws_array_list_length(&queue->container) - 1);
  261. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  262. return AWS_OP_SUCCESS;
  263. backpointer_update_failed:
  264. /* Failed to initialize or grow the backpointer array, back out the node addition */
  265. aws_array_list_pop_back(&queue->container);
  266. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  267. return AWS_OP_ERR;
  268. }
  269. static int s_remove_node(struct aws_priority_queue *queue, void *item, size_t item_index) {
  270. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  271. AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size));
  272. if (aws_array_list_get_at(&queue->container, item, item_index)) {
  273. /* shouldn't happen, but if it does we've already raised an error... */
  274. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  275. return AWS_OP_ERR;
  276. }
  277. size_t swap_with = aws_array_list_length(&queue->container) - 1;
  278. struct aws_priority_queue_node *backpointer = NULL;
  279. if (item_index != swap_with) {
  280. s_swap(queue, item_index, swap_with);
  281. }
  282. aws_array_list_pop_back(&queue->container);
  283. if (!AWS_IS_ZEROED(queue->backpointers)) {
  284. aws_array_list_get_at(&queue->backpointers, &backpointer, swap_with);
  285. if (backpointer) {
  286. backpointer->current_index = SIZE_MAX;
  287. }
  288. aws_array_list_pop_back(&queue->backpointers);
  289. }
  290. if (item_index != swap_with) {
  291. s_sift_either(queue, item_index);
  292. }
  293. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  294. return AWS_OP_SUCCESS;
  295. }
  296. int aws_priority_queue_remove(
  297. struct aws_priority_queue *queue,
  298. void *item,
  299. const struct aws_priority_queue_node *node) {
  300. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  301. AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size));
  302. AWS_PRECONDITION(node && AWS_MEM_IS_READABLE(node, sizeof(struct aws_priority_queue_node)));
  303. AWS_ERROR_PRECONDITION(
  304. node->current_index < aws_array_list_length(&queue->container), AWS_ERROR_PRIORITY_QUEUE_BAD_NODE);
  305. AWS_ERROR_PRECONDITION(queue->backpointers.data, AWS_ERROR_PRIORITY_QUEUE_BAD_NODE);
  306. int rval = s_remove_node(queue, item, node->current_index);
  307. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  308. return rval;
  309. }
  310. int aws_priority_queue_pop(struct aws_priority_queue *queue, void *item) {
  311. AWS_PRECONDITION(aws_priority_queue_is_valid(queue));
  312. AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size));
  313. AWS_ERROR_PRECONDITION(aws_array_list_length(&queue->container) != 0, AWS_ERROR_PRIORITY_QUEUE_EMPTY);
  314. int rval = s_remove_node(queue, item, 0);
  315. AWS_POSTCONDITION(aws_priority_queue_is_valid(queue));
  316. return rval;
  317. }
  318. int aws_priority_queue_top(const struct aws_priority_queue *queue, void **item) {
  319. AWS_ERROR_PRECONDITION(aws_array_list_length(&queue->container) != 0, AWS_ERROR_PRIORITY_QUEUE_EMPTY);
  320. return aws_array_list_get_at_ptr(&queue->container, item, 0);
  321. }
  322. size_t aws_priority_queue_size(const struct aws_priority_queue *queue) {
  323. return aws_array_list_length(&queue->container);
  324. }
  325. size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue) {
  326. return aws_array_list_capacity(&queue->container);
  327. }