bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen/* Macros for moving inside an array implementation of binary tree where
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen [0] is the root. */
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenpriorityq_init(priorityq_cmp_callback_t *cmp_callback, unsigned int init_size)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenunsigned int priorityq_count(const struct priorityq *pq)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenstatic void heap_items_swap(struct priorityq_item **items,
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen /* swap the item indexes */
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen /* swap the item pointers */
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenstatic unsigned int
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenheap_item_bubble_up(struct priorityq *pq, unsigned int idx)
f55988edb669c6bcfdfd91f2f7f57e875168f6b2Timo Sirainen items = array_get_modifiable(&pq->items, &count);
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen while (idx > 0) {
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen if (pq->cmp_callback(items[idx], items[parent_idx]) >= 0)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen /* wrong order - swap */
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenstatic void heap_item_bubble_down(struct priorityq *pq, unsigned int idx)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen unsigned int left_idx, right_idx, min_child_idx, count;
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen items = array_get_modifiable(&pq->items, &count);
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen while ((left_idx = LEFT_CHILD_IDX(idx)) < count) {
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen pq->cmp_callback(items[left_idx], items[right_idx]) < 0)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen if (pq->cmp_callback(items[min_child_idx], items[idx]) >= 0)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen /* wrong order - swap */
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenvoid priorityq_add(struct priorityq *pq, struct priorityq_item *item)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenstatic void priorityq_remove_idx(struct priorityq *pq, unsigned int idx)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen items = array_get_modifiable(&pq->items, &count);
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainen /* move last item over the removed one and fix the heap */
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenvoid priorityq_remove(struct priorityq *pq, struct priorityq_item *item)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenstruct priorityq_item *priorityq_peek(struct priorityq *pq)
9afebd21ced1d43f638e08a1411c9a89e526231fTimo Sirainenstruct priorityq_item *priorityq_pop(struct priorityq *pq)
2a51b74d417285de5885460c659d92417f64b127Timo Sirainenstruct priorityq_item *const *priorityq_items(struct priorityq *pq)