LibCDS
priority_queue.h
Go to the documentation of this file.
1 /**
2  * @file priority_queue.h The priority queue implemented with binary heap.
3  */
4 
5 #ifndef _PRIORITY_QUEUE_H_
6 #define _PRIORITY_QUEUE_H_
7 
8 #include "../util.h"
9 
10 /** PriorityQueueData is the data type for the container private information. */
11 typedef struct _PriorityQueueData PriorityQueueData;
12 
13 /** The implementation for priority queue. */
14 typedef struct _PriorityQueue {
15  /** The container private information */
17 
18  /** Push an item onto the queue.
19  @see PriorityQueuePush */
20  int32_t (*push) (struct _PriorityQueue*, Item);
21 
22  /** Retrieve item from top of the queue.
23  @see PriorityQueueTop */
24  int32_t (*top) (struct _PriorityQueue*, Item*);
25 
26  /** Delete item from top of the queue.
27  @see PriorityQueuePop */
28  int32_t (*pop) (struct _PriorityQueue*);
29 
30  /** Return the number of stored items.
31  @see PriorityQueueSize */
32  int32_t (*size) (struct _PriorityQueue*);
33 
34  /** Set the custom item comparison method.
35  @see PriorityQueueSetCompare */
36  int32_t (*set_compare) (struct _PriorityQueue*, int32_t (*) (Item, Item));
37 
38  /** Set the custom item resource clean method.
39  @see PriorityQueueSetDestroy */
40  int32_t (*set_destroy) (struct _PriorityQueue*, void (*) (Item));
42 
43 
44 /*===========================================================================*
45  * Definition for the exported member operations *
46  *===========================================================================*/
47 /**
48  * @brief The constructor for PriorityQueue.
49  *
50  * @param ppObj The double pointer to the to be constructed queue
51  *
52  * @retval SUCC
53  * @retval ERR_NOMEM Insufficient memory for queue construction
54  */
55 int32_t PriorityQueueInit(PriorityQueue **ppObj);
56 
57 /**
58  * @brief The destructor for PriorityQueue.
59  *
60  * If the custom resource clean method is set, it also runs the clean method
61  * for all the items.
62  *
63  * @param ppObj The double pointer to the to be destructed queue
64  */
66 
67 /**
68  * @brief Push an item onto the queue.
69  *
70  * This function pushes an item onto the queue with the corresponding queue size
71  * extension.
72  *
73  * @param self The pointer to PriorityQueue structure
74  * @param item The designated item
75  *
76  * @retval SUCC
77  * @retval ERR_NOINIT Uninitialized container
78  * @retval ERR_NOMEM Insufficient memory for queue extension
79  */
80 int32_t PriorityQueuePush(PriorityQueue *self, Item item);
81 
82 /**
83  * @brief Delete item from top of the queue.
84  *
85  * This function deletes item from top of the queue. If the custom resource clean
86  * method is set, it also runs the clean method for the deleted item.
87  *
88  * @param self The pointer to PriorityQueue structure
89  *
90  * @retval SUCC
91  * @retval ERR_NOINIT Uninitialized container
92  * @retval ERR_IDX Empty queue
93  */
94 int32_t PriorityQueuePop(PriorityQueue *self);
95 
96 /**
97  * @brief Retrieve item from top of the queue.
98  *
99  * This function retrieves item from top of the queue. If the queue is not empty,
100  * the item is returned by the second parameter. Otherwise, the error code is
101  * returned and the second parameter is updated with NULL.
102  *
103  * @param self The pointer to PriorityQueue structure
104  * @param pItem The pointer to the returned item
105  *
106  * @retval SUCC
107  * @retval ERR_NOINIT Uninitialized container
108  * @retval ERR_IDX Empty queue
109  * @retval ERR_GET Invalid parameter to store returned item
110  */
111 int32_t PriorityQueueTop(PriorityQueue *self, Item *pItem);
112 
113 /**
114  * @brief Return the number of stored items.
115  *
116  * @param self The pointer to PriorityQueue structure
117  *
118  * @return The number of items
119  * @retval ERR_NOINIT Uninitialized container
120  */
121 int32_t PriorityQueueSize(PriorityQueue *self);
122 
123 /**
124  * @brief Set the custom item comparison method.
125  *
126  * @param self The pointer to PriorityQueue structure
127  * @param pFunc The function pointer to the custom method
128  *
129  * @retval SUCC
130  * @retval ERR_NOINIT Uninitialized container
131  */
132 int32_t PriorityQueueSetCompare(PriorityQueue *self, int32_t (*pFunc) (Item, Item));
133 
134 /**
135  * @brief Set the custom item resource clean method.
136  *
137  * @param self The pointer to PriorityQueue structure
138  * @param pFunc The function pointer to the custom method
139  *
140  * @retval SUCC
141  * @retval ERR_NOINIT Uninitialized container
142  */
143 int32_t PriorityQueueSetDestroy(PriorityQueue *self, void (*pFunc) (Item));
144 
145 #endif
int32_t PriorityQueueSetDestroy(PriorityQueue *self, void(*pFunc)(Item))
Set the custom item resource clean method.
int32_t PriorityQueueSize(PriorityQueue *self)
Return the number of stored items.
struct _PriorityQueueData PriorityQueueData
PriorityQueueData is the data type for the container private information.
int32_t PriorityQueuePush(PriorityQueue *self, Item item)
Push an item onto the queue.
int32_t PriorityQueuePop(PriorityQueue *self)
Delete item from top of the queue.
The implementation for priority queue.
int32_t PriorityQueueInit(PriorityQueue **ppObj)
The constructor for PriorityQueue.
void PriorityQueueDeinit(PriorityQueue **ppObj)
The destructor for PriorityQueue.
int32_t PriorityQueueTop(PriorityQueue *self, Item *pItem)
Retrieve item from top of the queue.
PriorityQueueData * pData
The container private information.
int32_t PriorityQueueSetCompare(PriorityQueue *self, int32_t(*pFunc)(Item, Item))
Set the custom item comparison method.