2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2014-07-29 09:21:11 +02:00
|
|
|
* Copyright (C) 2013, 2014 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
2014-02-11 18:15:43 +01:00
|
|
|
|
2013-11-27 16:28:31 +01:00
|
|
|
/**
|
|
|
|
* @ingroup core_util
|
2010-09-22 15:10:42 +02:00
|
|
|
* @{
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2014-07-29 09:21:11 +02:00
|
|
|
* @brief A simple priority queue
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2013-06-25 15:33:40 +02:00
|
|
|
#include <inttypes.h>
|
2015-08-22 12:06:13 +02:00
|
|
|
#include <assert.h>
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
#include "priority_queue.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2020-10-22 11:32:06 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2014-07-29 09:21:11 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
void priority_queue_remove(priority_queue_t *root_, priority_queue_node_t *node)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-05-07 00:41:21 +02:00
|
|
|
/* The strict aliasing rules allow this assignment. */
|
2020-03-30 17:02:08 +02:00
|
|
|
priority_queue_node_t *root = (priority_queue_node_t *)root_;
|
2014-05-07 00:41:21 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (root->next != NULL) {
|
|
|
|
if (root->next == node) {
|
2010-09-22 15:10:42 +02:00
|
|
|
root->next = node->next;
|
|
|
|
node->next = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
root = root->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t *priority_queue_remove_head(priority_queue_t *root)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t *head = root->first;
|
2020-03-30 17:02:08 +02:00
|
|
|
|
2014-05-07 00:41:21 +02:00
|
|
|
if (head) {
|
|
|
|
root->first = head->next;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
void priority_queue_add(priority_queue_t *root, priority_queue_node_t *new_obj)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-05-07 00:41:21 +02:00
|
|
|
/* The strict aliasing rules allow this assignment. */
|
2020-03-30 17:02:08 +02:00
|
|
|
priority_queue_node_t *node = (priority_queue_node_t *)root;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (node->next != NULL) {
|
2015-08-22 12:06:13 +02:00
|
|
|
/* not trying to add the same node twice */
|
|
|
|
assert(node->next != new_obj);
|
2013-06-24 22:37:35 +02:00
|
|
|
if (node->next->priority > new_obj->priority) {
|
2010-09-22 15:10:42 +02:00
|
|
|
new_obj->next = node->next;
|
|
|
|
node->next = new_obj;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->next = new_obj;
|
|
|
|
new_obj->next = NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:20:10 +01:00
|
|
|
#if IS_ACTIVE(ENABLE_DEBUG)
|
2014-08-11 18:42:25 +02:00
|
|
|
void priority_queue_print(priority_queue_t *root)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
printf("queue:\n");
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-08-11 18:42:25 +02:00
|
|
|
for (priority_queue_node_t *node = root->first; node; node = node->next) {
|
2020-03-30 17:02:08 +02:00
|
|
|
printf("Data: %u Priority: %lu\n", node->data,
|
|
|
|
(unsigned long)node->priority);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
void priority_queue_print_node(priority_queue_node_t *node)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2020-03-30 17:02:08 +02:00
|
|
|
printf("Data: %u Priority: %lu Next: %u\n", (unsigned int)node->data,
|
|
|
|
(unsigned long)node->priority, (unsigned int)node->next);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2013-10-10 17:06:41 +02:00
|
|
|
#endif
|