1 /*
2     Copyright (c) 2012 Martin Sustrik  All rights reserved.
3 
4     Permission is hereby granted, free of charge, to any person obtaining a copy
5     of this software and associated documentation files (the "Software"),
6     to deal in the Software without restriction, including without limitation
7     the rights to use, copy, modify, merge, publish, distribute, sublicense,
8     and/or sell copies of the Software, and to permit persons to whom
9     the Software is furnished to do so, subject to the following conditions:
10 
11     The above copyright notice and this permission notice shall be included
12     in all copies or substantial portions of the Software.
13 
14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20     IN THE SOFTWARE.
21 */
22 
23 #include <stddef.h>
24 
25 #include "queue.h"
26 #include "err.h"
27 
nn_queue_init(struct nn_queue * self)28 void nn_queue_init (struct nn_queue *self)
29 {
30     self->head = NULL;
31     self->tail = NULL;
32 }
33 
nn_queue_term(struct nn_queue * self)34 void nn_queue_term (struct nn_queue *self)
35 {
36     self->head = NULL;
37     self->tail = NULL;
38 }
39 
nn_queue_empty(struct nn_queue * self)40 int nn_queue_empty (struct nn_queue *self)
41 {
42     return self->head ? 0 : 1;
43 }
44 
nn_queue_push(struct nn_queue * self,struct nn_queue_item * item)45 void nn_queue_push (struct nn_queue *self, struct nn_queue_item *item)
46 {
47     nn_assert (item->next == NN_QUEUE_NOTINQUEUE);
48 
49     item->next = NULL;
50     if (!self->head)
51         self->head = item;
52     if (self->tail)
53         self->tail->next = item;
54     self->tail = item;
55 }
56 
nn_queue_remove(struct nn_queue * self,struct nn_queue_item * item)57 void nn_queue_remove (struct nn_queue *self, struct nn_queue_item *item)
58 {
59     struct nn_queue_item *it;
60     struct nn_queue_item *prev;
61 
62     if (item->next == NN_QUEUE_NOTINQUEUE)
63         return;
64 
65     prev = NULL;
66     for (it = self->head; it != NULL; it = it->next) {
67         if (it == item) {
68             if (self->head == it)
69                 self->head = it->next;
70             if (self->tail == it)
71                 self->tail = prev;
72             if (prev)
73                 prev->next = it->next;
74             item->next = NN_QUEUE_NOTINQUEUE;
75             return;
76         }
77         prev = it;
78     }
79 }
80 
nn_queue_pop(struct nn_queue * self)81 struct nn_queue_item *nn_queue_pop (struct nn_queue *self)
82 {
83     struct nn_queue_item *result;
84 
85     if (!self->head)
86         return NULL;
87     result = self->head;
88     self->head = result->next;
89     if (!self->head)
90         self->tail = NULL;
91     result->next = NN_QUEUE_NOTINQUEUE;
92     return result;
93 }
94 
nn_queue_item_init(struct nn_queue_item * self)95 void nn_queue_item_init (struct nn_queue_item *self)
96 {
97     self->next = NN_QUEUE_NOTINQUEUE;
98 }
99 
nn_queue_item_term(struct nn_queue_item * self)100 void nn_queue_item_term (struct nn_queue_item *self)
101 {
102     nn_assert (self->next == NN_QUEUE_NOTINQUEUE);
103 }
104 
nn_queue_item_isinqueue(struct nn_queue_item * self)105 int nn_queue_item_isinqueue (struct nn_queue_item *self)
106 {
107     return self->next == NN_QUEUE_NOTINQUEUE ? 0 : 1;
108 }
109 
110