42 #include <sphinxbase/priority_queue.h>
51 int (*compare)(
const void *a,
const void *b);
54 priority_queue_t* priority_queue_create(
size_t len,
int (*compare)(
const void *a,
const void *b))
59 queue->alloc_size = len;
60 queue->pointers = (
void **)
ckd_calloc(len,
sizeof(*queue->pointers));
62 queue->max_element = NULL;
63 queue->compare = compare;
74 if (queue->size == 0) {
75 E_WARN(
"Trying to poll from empty queue\n");
78 if (queue->max_element == NULL) {
79 E_ERROR(
"Trying to poll from queue and max element is undefined\n");
82 res = queue->max_element;
83 for (i = 0; i < queue->alloc_size; i++) {
84 if (queue->pointers[i] == queue->max_element) {
85 queue->pointers[i] = NULL;
89 queue->max_element = NULL;
90 for (i = 0; i < queue->alloc_size; i++) {
91 if (queue->pointers[i] == 0)
93 if (queue->max_element == NULL) {
94 queue->max_element = queue->pointers[i];
96 if (queue->compare(queue->pointers[i], queue->max_element) < 0)
97 queue->max_element = queue->pointers[i];
107 if (queue->size == queue->alloc_size) {
108 E_ERROR(
"Trying to add element into full queue\n");
111 for (i = 0; i < queue->alloc_size; i++) {
112 if (queue->pointers[i] == NULL) {
113 queue->pointers[i] = element;
118 if (queue->max_element == NULL || queue->compare(element, queue->max_element) < 0) {
119 queue->max_element = element;
129 void priority_queue_free(
priority_queue_t *queue,
void (*free_ptr)(
void *a))
133 for (i = 0; i < queue->alloc_size; i++) {
134 if (queue->pointers[i] != NULL) {
135 if (free_ptr == NULL) {
138 free_ptr(queue->pointers[i]);
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
#define E_ERROR(...)
Print error message to error log.
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
Implementation of logging routines.
#define E_WARN(...)
Print warning message to error log.