2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2013-06-18 17:21:38 +02:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
2013-06-18 17:21:38 +02:00
|
|
|
* Public License. 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
|
|
|
*
|
|
|
|
* @file queue.c
|
|
|
|
* @brief A simple queue implementation
|
|
|
|
*
|
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>
|
2013-07-24 00:44:28 +02:00
|
|
|
#include <stdio.h>
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
#include "queue.h"
|
|
|
|
|
2014-05-07 00:41:21 +02:00
|
|
|
void queue_remove(queue_t *root_, 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. */
|
|
|
|
queue_node_t *root = (queue_node_t *) root_;
|
|
|
|
|
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-05-07 00:41:21 +02:00
|
|
|
queue_node_t *queue_remove_head(queue_t *root)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-05-07 00:41:21 +02:00
|
|
|
queue_node_t *head = root->first;
|
|
|
|
if (head) {
|
|
|
|
root->first = head->next;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:21 +02:00
|
|
|
void queue_priority_add(queue_t *root, 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. */
|
|
|
|
queue_node_t *node = (queue_node_t *) root;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (node->next != NULL) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-10 17:06:41 +02:00
|
|
|
#if ENABLE_DEBUG
|
2014-05-07 00:41:21 +02:00
|
|
|
void queue_print(queue_t *node)
|
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-05-07 00:41:21 +02:00
|
|
|
for (queue_node_t *node = node->first; node; node = node->next) {
|
2014-05-07 00:06:18 +02:00
|
|
|
printf("Data: %u Priority: %lu\n", node->data, (unsigned long) node->priority);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void queue_print_node(queue_node_t *node)
|
|
|
|
{
|
2014-05-07 00:06:18 +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
|