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