2 * @file PriorityQueue.h
5 #ifndef CGDS_PRIORITY_QUEUE_H
6 #define CGDS_PRIORITY_QUEUE_H
11 #include "cgds/Heap.h"
12 #include "cgds/safe_alloc.h"
15 * @brief Priority queue data structure (wrapper around Heap).
17 typedef struct PriorityQueue
{
18 Heap
* heap
; ///< Internal heap.
22 * @brief Return an allocated and initialized Queue.
24 PriorityQueue
* _priorityqueue_new(
25 size_t dataSize
, ///< Size in bytes of a priority queue element.
26 OrderType pType
, ///< Type of priority queue: max or min first (MAX_T or MIN_T).
27 UInt arity
///< Arity of the wrapped heap: any integer >=2.
31 * @brief Return an allocated and initialized Queue.
32 * @param type Type of a priority queue item (int, char*, ...).
33 * @param pType type of priority queue: max or min first (MAX_T or MIN_T).
34 * @param arity Arity of the wrapped heap: any integer >=2.
36 * Usage: PriorityQueue* priorityqueue_new(\
37 * <Type> type, OrderType pType, UInt arity)
39 #define priorityqueue_new(type, pType, arity) \
40 _priorityqueue_new(sizeof(type), pType, arity)
43 * @brief Copy constructor (shallow copy, ok for basic types).
45 PriorityQueue
* priorityqueue_copy(
46 PriorityQueue
* priorityQueue
///< "this" pointer.
50 * @brief Check if the priority queue is empty.
52 bool priorityqueue_empty(
53 PriorityQueue
* priorityQueue
///< "this" pointer.
57 * @brief Return the size of current priority queue.
59 UInt
priorityqueue_size(
60 PriorityQueue
* priorityQueue
///< "this" pointer.
64 * @brief Add an (item,priority) inside the priority queue.
65 * @param priorityQueue "this" pointer.
66 * @param item Item to be added.
67 * @param priority Priority of the added item.
69 * Usage: void priorityqueue_insert(\
70 * PriorityQueue* priorityQueue, void item, Real priority)
72 #define priorityqueue_insert(priorityQueue, item, priority) \
73 heap_insert(priorityQueue->heap, item, priority)
76 * @brief Change the priority of an item in the queue.
77 * @param priorityQueue "this" pointer.
78 * @param item Item to be modified.
79 * @param newPriority New priority of the modified item.
80 * @note If several similar items are present, only the first is affected.
82 * Usage: void priorityqueue_set_priority(\
83 * PriorityQueue* priorityQueue, void item, Real newPriority)
85 #define priorityqueue_set(priorityQueue, item, newPriority) \
86 heap_modify(priorityQueue->heap, item, newPriority)
89 * @brief Remove an item in the queue.
90 * @param priorityQueue "this" pointer.
91 * @param item Item to be removed.
92 * @note If several similar items are present, only the first is deleted.
94 * Usage: void priorityqueue_remove(PriorityQueue* priorityQueue, void item)
96 #define priorityqueue_remove(priorityQueue, item) \
97 heap_remove(priorityQueue->heap, item)
100 * @brief Return what is at the beginning of the queue.
101 * @return An ItemValue* 'iv' with iv->item = data, and iv->value its priority.
103 ItemValue
* priorityqueue_peek_raw(
104 PriorityQueue
* priorityQueue
///< "this" pointer.
108 * @brief Peek the item at the beginning of the queue.
109 * @param priorityQueue "this" pointer.
110 * @param item Item to be assigned.
112 * Usage: void priorityqueue_peek(PriorityQueue* priorityQueue, void item)
114 #define priorityqueue_peek(priorityQueue, item) \
115 heap_top(priorityQueue->heap, item)
118 * @brief Remove the top element in the queue.
120 void priorityqueue_pop(
121 PriorityQueue
* priorityQueue
///< "this" pointer.
125 * @brief Clear the entire queue.
127 void priorityqueue_clear(
128 PriorityQueue
* priorityQueue
///< "this" pointer.
132 * @brief Destroy the queue: clear it and free 'priorityQueue' memory.
134 void priorityqueue_destroy(
135 PriorityQueue
* priorityQueue
///< "this" pointer.