6 * @short_description: Priority queue implemention
9 * The #GPQueue structure and its associated functions provide a sorted
10 * collection of objects. Entries can be inserted in any order and at any time,
11 * and an entry's priority can be changed after it has been inserted into the
12 * queue. Entries are supposed to be removed one at a time in order of priority
13 * with g_pqueue_pop(), but deleting entries out of order is possible.
16 * The entries <emphasis>cannot</emphasis> be iterated over in any way other
17 * than removing them one by one in order of priority, but when doing that,
18 * this structure is far more efficient than sorted lists or balanced trees,
19 * which on the other hand do not suffer from this restriction.
22 * You will want to be very careful with calls that use #GPQueueHandle.
23 * Handles immediately become invalid when an entry is removed from a #GPQueue,
24 * but the current implementation cannot detect this and will do unfortunate
25 * things to undefined memory locations if you try to use an invalid handle.
29 * Internally, #GPQueue currently uses a Fibonacci heap to store
30 * the entries. This implementation detail may change.
55 * @compare_func: the #GCompareDataFunc used to sort the new priority queue.
56 * This function is passed two elements of the queue and should return 0 if
57 * they are equal, a negative value if the first comes before the second, and
58 * a positive value if the second comes before the first.
59 * @compare_userdata: user data passed to @compare_func
61 * Creates a new #GPQueue.
63 * Returns: a new #GPQueue.
68 g_pqueue_new (GCompareDataFunc compare_func,
69 gpointer *compare_userdata)
71 g_return_val_if_fail (compare_func != NULL, NULL);
73 GPQueue *pqueue = g_slice_new (GPQueue);
75 pqueue->cmp = compare_func;
76 pqueue->cmpdata = compare_userdata;
82 * @pqueue: a #GPQueue.
84 * Returns %TRUE if the queue is empty.
86 * Returns: %TRUE if the queue is empty.
91 g_pqueue_is_empty (GPQueue *pqueue)
93 return (pqueue->root == NULL);
97 g_pqueue_node_foreach (GPQueueNode *node,
102 if (node == NULL || node == stop) return;
103 func(node->data, user_data);
104 if (stop == NULL) stop = node;
105 g_pqueue_node_foreach (node->next, stop, func, user_data);
106 g_pqueue_node_foreach (node->child, NULL, func, user_data);
111 * @pqueue: a #GQueue.
112 * @func: the function to call for each element's data
113 * @user_data: user data to pass to func
115 * Calls func for each element in the pqueue passing user_data to the function.
120 g_pqueue_foreach (GPQueue *pqueue,
124 g_pqueue_node_foreach (pqueue->root, NULL, func, user_data);
128 g_pqueue_add_ptr_cb (gpointer obj, GPtrArray *ptrs)
130 g_ptr_array_add(ptrs, obj);
133 * g_pqueue_get_array:
134 * @pqueue: a #GQueue.
136 * Construct a GPtrArray for the items in pqueue. This can be useful when
137 * updating the priorities of all the elements in pqueue.
139 * Returns: A GPtrArray containing a pointer to each item in pqueue
144 g_pqueue_get_array (GPQueue *pqueue)
146 GPtrArray *ptrs = g_ptr_array_new();
147 g_pqueue_foreach(pqueue, (GFunc)g_pqueue_add_ptr_cb, ptrs);
152 cmp (GPQueue *pqueue,
156 return pqueue->cmp (a->data, b->data, pqueue->cmpdata);
160 g_pqueue_node_cut (GPQueueNode *src)
162 src->prev->next = src->next;
163 src->next->prev = src->prev;
169 g_pqueue_node_insert_before (GPQueueNode *dest,
175 dest->prev = src->prev;
176 src->prev->next = dest;
182 g_pqueue_node_insert_after (GPQueueNode *dest,
189 src->prev->next = next;
190 next->prev = src->prev;
196 * @pqueue: a #GPQueue.
197 * @data: the object to insert into the priority queue.
199 * Inserts a new entry into a #GPQueue.
201 * The returned handle can be used in calls to g_pqueue_remove() and
202 * g_pqueue_priority_changed(). Never make such calls for entries that have
203 * already been removed from the queue. The same @data can be inserted into
204 * a #GPQueue more than once, but remember that in this case,
205 * g_pqueue_priority_changed() needs to be called for
206 * <emphasis>every</emphasis> handle for that object if its priority changes.
208 * Returns: a handle for the freshly inserted entry.
213 g_pqueue_push (GPQueue *pqueue,
218 e = g_slice_new (GPQueueNode);
227 if (pqueue->root != NULL) {
228 g_pqueue_node_insert_before (pqueue->root, e);
229 if (cmp (pqueue, e, pqueue->root) < 0)
240 * @pqueue: a #GPQueue.
242 * Returns the topmost entry's data pointer, or %NULL if the queue is empty.
244 * If you need to tell the difference between an empty queue and a queue
245 * that happens to have a %NULL pointer at the top, check if the queue is
248 * Returns: the topmost entry's data pointer, or %NULL if the queue is empty.
253 g_pqueue_peek (GPQueue *pqueue)
255 return (pqueue->root != NULL) ? pqueue->root->data : NULL;
258 static inline GPQueueNode*
259 g_pqueue_make_child (GPQueueNode *a,
262 g_pqueue_node_cut(b);
263 if (a->child != NULL) {
264 g_pqueue_node_insert_before (a->child, b);
274 static inline GPQueueNode*
275 g_pqueue_join_trees (GPQueue *pqueue,
279 if (cmp (pqueue, a, b) < 0)
280 return g_pqueue_make_child (a, b);
281 return g_pqueue_make_child (b, a);
285 g_pqueue_fix_rootlist (GPQueue* pqueue)
288 GPQueueNode **degnode;
289 GPQueueNode sentinel;
290 GPQueueNode *current;
291 GPQueueNode *minimum;
293 /* We need to iterate over the circular list we are given and do
295 * - Make sure all the elements are unmarked
296 * - Make sure to return the element in the list with smallest
298 * - Find elements of identical degree and join them into trees
299 * The last point is irrelevant for correctness, but essential
300 * for performance. If we did not do this, our data structure would
301 * degrade into an unsorted linked list.
304 degnode_size = (8 * sizeof(gpointer) + 1) * sizeof(gpointer);
305 degnode = g_slice_alloc0 (degnode_size);
307 sentinel.next = &sentinel;
308 sentinel.prev = &sentinel;
309 g_pqueue_node_insert_before (pqueue->root, &sentinel);
311 current = pqueue->root;
312 while (current != &sentinel) {
313 current->marked = FALSE;
314 current->parent = NULL;
315 gint d = current->degree;
316 if (degnode[d] == NULL) {
317 degnode[d] = current;
318 current = current->next;
320 if (degnode[d] != current) {
321 current = g_pqueue_join_trees (pqueue, degnode[d], current);
324 current = current->next;
329 current = sentinel.next;
331 while (current != &sentinel) {
332 if (cmp (pqueue, current, minimum) < 0)
334 current = current->next;
336 pqueue->root = minimum;
338 g_pqueue_node_cut (&sentinel);
340 g_slice_free1 (degnode_size, degnode);
344 g_pqueue_remove_root (GPQueue *pqueue,
347 /* This removes a node at the root _level_ of the structure, which can be,
348 * but does not have to be, the actual pqueue->root node. That is why
349 * we require an explicit pointer to the node to be removed instead of just
350 * removing pqueue->root implictly.
354 * If root has any children, pull them up to root level.
355 * At this time, we only deal with their next/prev pointers,
356 * further changes are made later in g_pqueue_fix_rootlist().
359 g_pqueue_node_insert_after (root, root->child);
365 * Cut root out of the list.
367 if (root->next != root) {
368 pqueue->root = root->next;
369 g_pqueue_node_cut (root);
371 * Clean up the remaining list.
373 g_pqueue_fix_rootlist (pqueue);
378 g_slice_free (GPQueueNode, root);
383 * @pqueue: a #GPQueue.
385 * Removes the topmost entry from a #GPQueue and returns its data pointer.
386 * Calling this on an empty #GPQueue is not an error, but removes nothing
389 * If you need to tell the difference between an empty queue and a queue
390 * that happens to have a %NULL pointer at the top, check if the queue is
393 * Returns: the topmost entry's data pointer, or %NULL if the queue was empty.
398 g_pqueue_pop (GPQueue *pqueue)
402 if (pqueue->root == NULL) return NULL;
403 data = pqueue->root->data;
404 g_pqueue_remove_root (pqueue, pqueue->root);
409 g_pqueue_make_root (GPQueue *pqueue,
412 /* This moves a node up to the root _level_ of the structure.
413 * It does not always become the actual root element (pqueue->root).
418 parent = entry->parent;
419 entry->parent = NULL;
420 entry->marked = FALSE;
421 if (parent != NULL) {
422 if (entry->next != entry) {
423 if (parent->child == entry) parent->child = entry->next;
424 g_pqueue_node_cut (entry);
427 parent->child = NULL;
430 g_pqueue_node_insert_before (pqueue->root, entry);
433 if (cmp (pqueue, entry, pqueue->root) < 0)
434 pqueue->root = entry;
438 g_pqueue_cut_tree (GPQueue *pqueue,
441 /* This function moves an entry up to the root level of the structure.
442 * It extends g_pqueue_make_root() in that the entry's parent, grandparent
443 * etc. may also be moved to the root level if they are "marked". This is
444 * not essential for correctness, it just maintains the so-called "potential"
445 * of the structure, which is necessary for the amortized runtime analysis.
448 GPQueueNode *current;
452 while ((current != NULL) && (current->parent != NULL)) {
453 parent = current->parent;
454 g_pqueue_make_root (pqueue, entry);
455 if (parent->marked) {
458 parent->marked = TRUE;
462 if (cmp (pqueue, entry, pqueue->root) < 0)
463 pqueue->root = entry;
468 * @pqueue: a #GPQueue.
469 * @entry: a #GPQueueHandle for an entry in @pqueue.
471 * Removes one entry from a #GPQueue.
473 * Make sure that @entry refers to an entry that is actually part of
474 * @pqueue at the time, otherwise the behavior of this function is
475 * undefined (expect crashes).
480 g_pqueue_remove (GPQueue* pqueue,
483 g_pqueue_cut_tree (pqueue, entry);
484 g_pqueue_remove_root (pqueue, entry);
488 * g_pqueue_priority_changed:
489 * @pqueue: a #GPQueue.
490 * @entry: a #GPQueueHandle for an entry in @pqueue.
492 * Notifies the #GPQueue that the priority of one entry has changed.
493 * The internal representation is updated accordingly.
495 * Make sure that @entry refers to an entry that is actually part of
496 * @pqueue at the time, otherwise the behavior of this function is
497 * undefined (expect crashes).
499 * Do not attempt to change the priorities of several entries at once.
500 * Every time a single object is changed, the #GPQueue needs to be updated
501 * by calling g_pqueue_priority_changed() for that object.
506 g_pqueue_priority_changed (GPQueue* pqueue,
509 g_pqueue_cut_tree (pqueue, entry);
512 g_pqueue_node_insert_after (entry, entry->child);
517 g_pqueue_fix_rootlist (pqueue);
521 * g_pqueue_priority_decreased:
522 * @pqueue: a #GPQueue.
523 * @entry: a #GPQueueHandle for an entry in @pqueue.
525 * Notifies the #GPQueue that the priority of one entry has
526 * <emphasis>decreased</emphasis>.
528 * This is a special case of g_pqueue_priority_changed(). If you are absolutely
529 * sure that the new priority of @entry is lower than it was before, you
530 * may call this function instead of g_pqueue_priority_changed().
534 * In the current implementation, an expensive step in
535 * g_pqueue_priority_changed() can be skipped if the new priority is known
536 * to be lower, leading to an amortized running time of O(1) instead of
537 * O(log n). Of course, if the priority is not actually lower, behavior
545 g_pqueue_priority_decreased (GPQueue* pqueue,
548 g_pqueue_cut_tree (pqueue, entry);
552 g_pqueue_node_free_all (GPQueueNode *node)
554 if (node == NULL) return;
555 g_pqueue_node_free_all (node->child);
556 node->prev->next = NULL;
557 g_pqueue_node_free_all (node->next);
558 g_slice_free (GPQueueNode, node);
563 * @pqueue: a #GPQueue.
565 * Removes all entries from a @pqueue.
570 g_pqueue_clear (GPQueue* pqueue)
572 g_pqueue_node_free_all (pqueue->root);
578 * @pqueue: a #GPQueue.
580 * Deallocates the memory used by @pqueue itself, but not any memory pointed
581 * to by the data pointers of its entries.
586 g_pqueue_free (GPQueue* pqueue)
588 g_pqueue_clear (pqueue);
589 g_slice_free (GPQueue, pqueue);