cctools
priority_queue.h
Go to the documentation of this file.
1/*
2Copyright (C) 2024 The University of Notre Dame
3This software is distributed under the GNU General Public License.
4See the file COPYING for details.
5*/
6
7#ifndef PRIORITY_QUEUE_H
8#define PRIORITY_QUEUE_H
9
90
91
97struct priority_queue *priority_queue_create(int init_capacity);
98
103int priority_queue_size(struct priority_queue *pq);
104
112int priority_queue_push(struct priority_queue *pq, void *data, double priority);
113
118void *priority_queue_pop(struct priority_queue *pq);
119
125void *priority_queue_peek_top(struct priority_queue *pq);
126
133void *priority_queue_peek_at(struct priority_queue *pq, int index);
134
140double priority_queue_get_priority_at(struct priority_queue *pq, int index);
141
146double priority_queue_get_top_priority(struct priority_queue *pq);
147
154int priority_queue_update_priority(struct priority_queue *pq, void *data, double new_priority);
155
161int priority_queue_find_idx(struct priority_queue *pq, void *data);
162
169int priority_queue_static_next(struct priority_queue *pq);
170
175void priority_queue_base_reset(struct priority_queue *pq);
176
181int priority_queue_base_next(struct priority_queue *pq);
182
190void priority_queue_rotate_reset(struct priority_queue *pq);
191
196int priority_queue_rotate_next(struct priority_queue *pq);
197
203int priority_queue_remove(struct priority_queue *pq, int idx);
204
208void priority_queue_delete(struct priority_queue *pq);
209
239
240/* Iterate from begining to the end every time starts. */
241#define PRIORITY_QUEUE_BASE_ITERATE( pq, idx, data, iter_count, iter_depth ) \
242 iter_count = 0; \
243 priority_queue_base_reset(pq); \
244 while ((iter_count < iter_depth) && ((idx = priority_queue_base_next(pq)) >= 0) && (data = priority_queue_peek_at(pq, idx)) && (iter_count += 1))
245
246/* Iterate from last position, never reset. */
247#define PRIORITY_QUEUE_STATIC_ITERATE( pq, idx, data, iter_count, iter_depth ) \
248 iter_count = 0; \
249 while ((iter_count < iter_depth) && ((idx = priority_queue_static_next(pq)) >= 0) && (data = priority_queue_peek_at(pq, idx)) && (iter_count += 1))
250
251/* Iterate from last position, reset to the begining when needed. */
252#define PRIORITY_QUEUE_ROTATE_ITERATE( pq, idx, data, iter_count, iter_depth ) \
253 iter_count = 0; \
254 while ((iter_count < iter_depth) && ((idx = priority_queue_rotate_next(pq)) >= 0) && (data = priority_queue_peek_at(pq, idx)) && (iter_count += 1))
255
256#endif
int priority_queue_size(struct priority_queue *pq)
Count the elements in a priority queue.
void * priority_queue_pop(struct priority_queue *pq)
Pop the element with the highest priority from a priority queue.
double priority_queue_get_priority_at(struct priority_queue *pq, int index)
Get the priority of an element at a specified index.
int priority_queue_push(struct priority_queue *pq, void *data, double priority)
Push an element into a priority queue.
int priority_queue_update_priority(struct priority_queue *pq, void *data, double new_priority)
Update the priority of an element in a priority queue.
int priority_queue_base_next(struct priority_queue *pq)
Advance the base_cursor to the next element and return the index.
int priority_queue_remove(struct priority_queue *pq, int idx)
Remove the element with the specified index from a priority queue.
int priority_queue_find_idx(struct priority_queue *pq, void *data)
Find the index of an element in a priority queue.
int priority_queue_static_next(struct priority_queue *pq)
Advance the static_cursor to the next element and return the index.
void priority_queue_rotate_reset(struct priority_queue *pq)
Reset the rotate_cursor to 0.
void priority_queue_base_reset(struct priority_queue *pq)
Reset the base_cursor to 0.
void priority_queue_delete(struct priority_queue *pq)
Delete a priority queue.
int priority_queue_rotate_next(struct priority_queue *pq)
Advance the rotate_cursor to the next element and return the index.
void * priority_queue_peek_top(struct priority_queue *pq)
Get the element with the highest priority from a priority queue.
void * priority_queue_peek_at(struct priority_queue *pq, int index)
Get an element from a priority queue by a specified index.
struct priority_queue * priority_queue_create(int init_capacity)
Create a new priority queue.
double priority_queue_get_top_priority(struct priority_queue *pq)
Get the priority of the top element in a priority queue.