5 * SECTION:priority_queues
6 * @short_description: a collection of data entries with associated priority
7 * values that returns entries one by one in order of priority
10 * The #GPQueue structure and its associated functions provide a sorted
11 * collection of objects. Entries can be inserted in any order and at any time,
12 * and an entry's priority can be changed after it has been inserted into the
13 * queue. Entries are supposed to be removed one at a time in order of priority
14 * with g_pqueue_pop(), but deleting entries out of order is possible.
17 * The entries <emphasis>cannot</emphasis> be iterated over in any way other
18 * than removing them one by one in order of priority, but when doing that,
19 * this structure is far more efficient than sorted lists or balanced trees,
20 * which on the other hand do not suffer from this restriction.
23 * You will want to be very careful with calls that use #GPQueueHandle.
24 * Handles immediately become invalid when an entry is removed from a #GPQueue,
25 * but the current implementation cannot detect this and will do unfortunate
26 * things to undefined memory locations if you try to use an invalid handle.
30 * Internally, #GPQueue currently uses a Fibonacci heap to store
31 * the entries. This implementation detail may change.
56 * @compare_func: the #GCompareDataFunc used to sort the new priority queue.
57 * This function is passed two elements of the queue and should return 0 if
58 * they are equal, a negative value if the first comes before the second, and
59 * a positive value if the second comes before the first.
60 * @compare_userdata: user data passed to @compare_func
62 * Creates a new #GPQueue.
64 * Returns: a new #GPQueue.
69 g_pqueue_new (GCompareDataFunc compare_func,
70 gpointer *compare_userdata)
72 g_return_val_if_fail (compare_func != NULL, NULL);
74 GPQueue *pqueue = g_slice_new (GPQueue);
76 pqueue->cmp = compare_func;
77 pqueue->cmpdata = compare_userdata;
83 * @pqueue: a #GPQueue.
85 * Returns %TRUE if the queue is empty.
87 * Returns: %TRUE if the queue is empty.
92 g_pqueue_is_empty (GPQueue *pqueue)
94 return (pqueue->root == NULL);
98 g_pqueue_node_foreach (GPQueueNode *node,
103 if (node == NULL || node == stop) return;
104 func(node->data, user_data);
105 if (stop == NULL) stop = node;
106 g_pqueue_node_foreach (node->next, stop, func, user_data);
107 g_pqueue_node_foreach (node->child, NULL, func, user_data);
112 * @pqueue: a #GQueue.
113 * @func: the function to call for each element's data
114 * @user_data: user data to pass to func
116 * Calls func for each element in the pqueue passing user_data to the function.
121 g_pqueue_foreach (GPQueue *pqueue,
125 g_pqueue_node_foreach (pqueue->root, NULL, func, user_data);
129 cmp (GPQueue *pqueue,
133 return pqueue->cmp (a->data, b->data, pqueue->cmpdata);
137 g_pqueue_node_cut (GPQueueNode *src)
139 src->prev->next = src->next;
140 src->next->prev = src->prev;
146 g_pqueue_node_insert_before (GPQueueNode *dest,
152 dest->prev = src->prev;
153 src->prev->next = dest;
159 g_pqueue_node_insert_after (GPQueueNode *dest,
166 src->prev->next = next;
167 next->prev = src->prev;
173 * @pqueue: a #GPQueue.
174 * @data: the object to insert into the priority queue.
176 * Inserts a new entry into a #GPQueue.
178 * The returned handle can be used in calls to g_pqueue_remove() and
179 * g_pqueue_priority_changed(). Never make such calls for entries that have
180 * already been removed from the queue. The same @data can be inserted into
181 * a #GPQueue more than once, but remember that in this case,
182 * g_pqueue_priority_changed() needs to be called for
183 * <emphasis>every</emphasis> handle for that object if its priority changes.
185 * Returns: a handle for the freshly inserted entry.
190 g_pqueue_push (GPQueue *pqueue,
195 e = g_slice_new (GPQueueNode);
204 if (pqueue->root != NULL) {
205 g_pqueue_node_insert_before (pqueue->root, e);
206 if (cmp (pqueue, e, pqueue->root) < 0)
217 * @pqueue: a #GPQueue.
219 * Returns the topmost entry's data pointer, or %NULL if the queue is empty.
221 * If you need to tell the difference between an empty queue and a queue
222 * that happens to have a %NULL pointer at the top, check if the queue is
225 * Returns: the topmost entry's data pointer, or %NULL if the queue is empty.
230 g_pqueue_peek (GPQueue *pqueue)
232 return (pqueue->root != NULL) ? pqueue->root->data : NULL;
235 static inline GPQueueNode*
236 g_pqueue_make_child (GPQueueNode *a,
239 g_pqueue_node_cut(b);
240 if (a->child != NULL) {
241 g_pqueue_node_insert_before (a->child, b);
251 static inline GPQueueNode*
252 g_pqueue_join_trees (GPQueue *pqueue,
256 if (cmp (pqueue, a, b) < 0)
257 return g_pqueue_make_child (a, b);
258 return g_pqueue_make_child (b, a);
262 g_pqueue_fix_rootlist (GPQueue* pqueue)
265 GPQueueNode **degnode;
266 GPQueueNode sentinel;
267 GPQueueNode *current;
268 GPQueueNode *minimum;
270 /* We need to iterate over the circular list we are given and do
272 * - Make sure all the elements are unmarked
273 * - Make sure to return the element in the list with smallest
275 * - Find elements of identical degree and join them into trees
276 * The last point is irrelevant for correctness, but essential
277 * for performance. If we did not do this, our data structure would
278 * degrade into an unsorted linked list.
281 degnode_size = (8 * sizeof(gpointer) + 1) * sizeof(gpointer);
282 degnode = g_slice_alloc0 (degnode_size);
284 sentinel.next = &sentinel;
285 sentinel.prev = &sentinel;
286 g_pqueue_node_insert_before (pqueue->root, &sentinel);
288 current = pqueue->root;
289 while (current != &sentinel) {
290 current->marked = FALSE;
291 current->parent = NULL;
292 gint d = current->degree;
293 if (degnode[d] == NULL) {
294 degnode[d] = current;
295 current = current->next;
297 if (degnode[d] != current) {
298 current = g_pqueue_join_trees (pqueue, degnode[d], current);
301 current = current->next;
306 current = sentinel.next;
308 while (current != &sentinel) {
309 if (cmp (pqueue, current, minimum) < 0)
311 current = current->next;
313 pqueue->root = minimum;
315 g_pqueue_node_cut (&sentinel);
317 g_slice_free1 (degnode_size, degnode);
321 g_pqueue_remove_root (GPQueue *pqueue,
324 /* This removes a node at the root _level_ of the structure, which can be,
325 * but does not have to be, the actual pqueue->root node. That is why
326 * we require an explicit pointer to the node to be removed instead of just
327 * removing pqueue->root implictly.
331 * If root has any children, pull them up to root level.
332 * At this time, we only deal with their next/prev pointers,
333 * further changes are made later in g_pqueue_fix_rootlist().
336 g_pqueue_node_insert_after (root, root->child);
342 * Cut root out of the list.
344 if (root->next != root) {
345 pqueue->root = root->next;
346 g_pqueue_node_cut (root);
348 * Clean up the remaining list.
350 g_pqueue_fix_rootlist (pqueue);
355 g_slice_free (GPQueueNode, root);
360 * @pqueue: a #GPQueue.
362 * Removes the topmost entry from a #GPQueue and returns its data pointer.
363 * Calling this on an empty #GPQueue is not an error, but removes nothing
366 * If you need to tell the difference between an empty queue and a queue
367 * that happens to have a %NULL pointer at the top, check if the queue is
370 * Returns: the topmost entry's data pointer, or %NULL if the queue was empty.
375 g_pqueue_pop (GPQueue *pqueue)
379 if (pqueue->root == NULL) return NULL;
380 data = pqueue->root->data;
381 g_pqueue_remove_root (pqueue, pqueue->root);
386 g_pqueue_make_root (GPQueue *pqueue,
389 /* This moves a node up to the root _level_ of the structure.
390 * It does not always become the actual root element (pqueue->root).
395 parent = entry->parent;
396 entry->parent = NULL;
397 entry->marked = FALSE;
398 if (parent != NULL) {
399 if (entry->next != entry) {
400 if (parent->child == entry) parent->child = entry->next;
401 g_pqueue_node_cut (entry);
404 parent->child = NULL;
407 g_pqueue_node_insert_before (pqueue->root, entry);
410 if (cmp (pqueue, entry, pqueue->root) < 0)
411 pqueue->root = entry;
415 g_pqueue_cut_tree (GPQueue *pqueue,
418 /* This function moves an entry up to the root level of the structure.
419 * It extends g_pqueue_make_root() in that the entry's parent, grandparent
420 * etc. may also be moved to the root level if they are "marked". This is
421 * not essential for correctness, it just maintains the so-called "potential"
422 * of the structure, which is necessary for the amortized runtime analysis.
425 GPQueueNode *current;
429 while ((current != NULL) && (current->parent != NULL)) {
430 parent = current->parent;
431 g_pqueue_make_root (pqueue, entry);
432 if (parent->marked) {
435 parent->marked = TRUE;
439 if (cmp (pqueue, entry, pqueue->root) < 0)
440 pqueue->root = entry;
445 * @pqueue: a #GPQueue.
446 * @entry: a #GPQueueHandle for an entry in @pqueue.
448 * Removes one entry from a #GPQueue.
450 * Make sure that @entry refers to an entry that is actually part of
451 * @pqueue at the time, otherwise the behavior of this function is
452 * undefined (expect crashes).
457 g_pqueue_remove (GPQueue* pqueue,
460 g_pqueue_cut_tree (pqueue, entry);
461 g_pqueue_remove_root (pqueue, entry);
465 * g_pqueue_priority_changed:
466 * @pqueue: a #GPQueue.
467 * @entry: a #GPQueueHandle for an entry in @pqueue.
469 * Notifies the #GPQueue that the priority of one entry has changed.
470 * The internal representation is updated accordingly.
472 * Make sure that @entry refers to an entry that is actually part of
473 * @pqueue at the time, otherwise the behavior of this function is
474 * undefined (expect crashes).
476 * Do not attempt to change the priorities of several entries at once.
477 * Every time a single object is changed, the #GPQueue needs to be updated
478 * by calling g_pqueue_priority_changed() for that object.
483 g_pqueue_priority_changed (GPQueue* pqueue,
486 g_pqueue_cut_tree (pqueue, entry);
489 g_pqueue_node_insert_after (entry, entry->child);
494 g_pqueue_fix_rootlist (pqueue);
498 * g_pqueue_priority_decreased:
499 * @pqueue: a #GPQueue.
500 * @entry: a #GPQueueHandle for an entry in @pqueue.
502 * Notifies the #GPQueue that the priority of one entry has
503 * <emphasis>decreased</emphasis>.
505 * This is a special case of g_pqueue_priority_changed(). If you are absolutely
506 * sure that the new priority of @entry is lower than it was before, you
507 * may call this function instead of g_pqueue_priority_changed().
511 * In the current implementation, an expensive step in
512 * g_pqueue_priority_changed() can be skipped if the new priority is known
513 * to be lower, leading to an amortized running time of O(1) instead of
514 * O(log n). Of course, if the priority is not actually lower, behavior
522 g_pqueue_priority_decreased (GPQueue* pqueue,
525 g_pqueue_cut_tree (pqueue, entry);
529 g_pqueue_node_free_all (GPQueueNode *node)
531 if (node == NULL) return;
532 g_pqueue_node_free_all (node->child);
533 node->prev->next = NULL;
534 g_pqueue_node_free_all (node->next);
535 g_slice_free (GPQueueNode, node);
540 * @pqueue: a #GPQueue.
542 * Removes all entries from a @pqueue.
547 g_pqueue_clear (GPQueue* pqueue)
549 g_pqueue_node_free_all (pqueue->root);
555 * @pqueue: a #GPQueue.
557 * Deallocates the memory used by @pqueue itself, but not any memory pointed
558 * to by the data pointers of its entries.
563 g_pqueue_free (GPQueue* pqueue)
565 g_pqueue_clear (pqueue);
566 g_slice_free (GPQueue, pqueue);