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 "list.h"
26 #include "err.h"
27 #include "attr.h"
28
nn_list_init(struct nn_list * self)29 void nn_list_init (struct nn_list *self)
30 {
31 self->first = NULL;
32 self->last = NULL;
33 }
34
nn_list_term(struct nn_list * self)35 void nn_list_term (struct nn_list *self)
36 {
37 nn_assert (self->first == NULL);
38 nn_assert (self->last == NULL);
39 }
40
nn_list_empty(struct nn_list * self)41 int nn_list_empty (struct nn_list *self)
42 {
43 return self->first ? 0 : 1;
44 }
45
nn_list_begin(struct nn_list * self)46 struct nn_list_item *nn_list_begin (struct nn_list *self)
47 {
48 return self->first;
49 }
50
nn_list_end(NN_UNUSED struct nn_list * self)51 struct nn_list_item *nn_list_end (NN_UNUSED struct nn_list *self)
52 {
53 return NULL;
54 }
55
nn_list_prev(struct nn_list * self,struct nn_list_item * it)56 struct nn_list_item *nn_list_prev (struct nn_list *self,
57 struct nn_list_item *it)
58 {
59 if (!it)
60 return self->last;
61 nn_assert (it->prev != NN_LIST_NOTINLIST);
62 return it->prev;
63 }
64
nn_list_next(NN_UNUSED struct nn_list * self,struct nn_list_item * it)65 struct nn_list_item *nn_list_next (NN_UNUSED struct nn_list *self,
66 struct nn_list_item *it)
67 {
68 nn_assert (it->next != NN_LIST_NOTINLIST);
69 return it->next;
70 }
71
nn_list_insert(struct nn_list * self,struct nn_list_item * item,struct nn_list_item * it)72 void nn_list_insert (struct nn_list *self, struct nn_list_item *item,
73 struct nn_list_item *it)
74 {
75 nn_assert (!nn_list_item_isinlist (item));
76
77 item->prev = it ? it->prev : self->last;
78 item->next = it;
79 if (item->prev)
80 item->prev->next = item;
81 if (item->next)
82 item->next->prev = item;
83 if (!self->first || self->first == it)
84 self->first = item;
85 if (!it)
86 self->last = item;
87 }
88
nn_list_erase(struct nn_list * self,struct nn_list_item * item)89 struct nn_list_item *nn_list_erase (struct nn_list *self,
90 struct nn_list_item *item)
91 {
92 struct nn_list_item *next;
93
94 nn_assert (nn_list_item_isinlist (item));
95
96 if (item->prev)
97 item->prev->next = item->next;
98 else
99 self->first = item->next;
100 if (item->next)
101 item->next->prev = item->prev;
102 else
103 self->last = item->prev;
104
105 next = item->next;
106
107 item->prev = NN_LIST_NOTINLIST;
108 item->next = NN_LIST_NOTINLIST;
109
110 return next;
111 }
112
nn_list_item_init(struct nn_list_item * self)113 void nn_list_item_init (struct nn_list_item *self)
114 {
115 self->prev = NN_LIST_NOTINLIST;
116 self->next = NN_LIST_NOTINLIST;
117 }
118
nn_list_item_term(struct nn_list_item * self)119 void nn_list_item_term (struct nn_list_item *self)
120 {
121 nn_assert (!nn_list_item_isinlist (self));
122 }
123
nn_list_item_isinlist(struct nn_list_item * self)124 int nn_list_item_isinlist (struct nn_list_item *self)
125 {
126 return self->prev == NN_LIST_NOTINLIST ? 0 : 1;
127 }
128
129