1 |
|
---|
2 | /* singularly linked-list */
|
---|
3 |
|
---|
4 | #include <stdlib.h>
|
---|
5 |
|
---|
6 | #include "globals.h"
|
---|
7 | #include "module-datastruct-llist.h"
|
---|
8 |
|
---|
9 | static void _destroy(LLIST *l)
|
---|
10 | {
|
---|
11 | pthread_mutex_unlock(&l->lock);
|
---|
12 | pthread_mutex_destroy(&l->lock);
|
---|
13 |
|
---|
14 | NULLFREE(l);
|
---|
15 | }
|
---|
16 |
|
---|
17 | LLIST *ll_create()
|
---|
18 | {
|
---|
19 | LLIST *l = calloc(1, sizeof(LLIST));
|
---|
20 |
|
---|
21 | pthread_mutex_init(&l->lock, NULL);
|
---|
22 |
|
---|
23 | return l;
|
---|
24 | }
|
---|
25 |
|
---|
26 | void ll_destroy(LLIST *l)
|
---|
27 | {
|
---|
28 | ll_clear(l);
|
---|
29 |
|
---|
30 | _destroy(l);
|
---|
31 | }
|
---|
32 |
|
---|
33 | void ll_destroy_data(LLIST *l)
|
---|
34 | {
|
---|
35 | ll_clear_data(l);
|
---|
36 |
|
---|
37 | _destroy(l);
|
---|
38 | }
|
---|
39 |
|
---|
40 | void ll_clear(LLIST *l)
|
---|
41 | {
|
---|
42 | void *obj;
|
---|
43 |
|
---|
44 | LL_ITER *it = ll_iter_create(l);
|
---|
45 | while ((obj = ll_iter_next(it)))
|
---|
46 | ll_iter_remove(it);
|
---|
47 | ll_iter_release(it);
|
---|
48 | }
|
---|
49 |
|
---|
50 | void ll_clear_data(LLIST *l)
|
---|
51 | {
|
---|
52 | void *obj;
|
---|
53 |
|
---|
54 | LL_ITER *it = ll_iter_create(l);
|
---|
55 | while ((obj = ll_iter_next(it)))
|
---|
56 | ll_iter_remove_data(it);
|
---|
57 | ll_iter_release(it);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void ll_append(LLIST *l, void *obj)
|
---|
61 | {
|
---|
62 | if (obj) {
|
---|
63 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
64 | LL_NODE *n = l->initial;
|
---|
65 |
|
---|
66 | new->obj = obj;
|
---|
67 |
|
---|
68 | if (n) {
|
---|
69 | while (n->nxt) n = n->nxt;
|
---|
70 | n->nxt = new;
|
---|
71 | new->prv = n;
|
---|
72 | } else
|
---|
73 | l->initial = new;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | LL_ITER *ll_iter_create(LLIST *l)
|
---|
78 | {
|
---|
79 | LL_ITER *it = malloc(sizeof(LL_ITER));
|
---|
80 |
|
---|
81 | it->l = l;
|
---|
82 | if (l)
|
---|
83 | it->cur = l->initial;
|
---|
84 | else
|
---|
85 | it->cur = NULL;
|
---|
86 |
|
---|
87 | pthread_mutex_lock(&l->lock);
|
---|
88 |
|
---|
89 | return it;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void ll_iter_release(LL_ITER *it)
|
---|
93 | {
|
---|
94 | pthread_mutex_unlock(&it->l->lock);
|
---|
95 |
|
---|
96 | NULLFREE(it);
|
---|
97 | }
|
---|
98 |
|
---|
99 | void *ll_iter_next(LL_ITER *it)
|
---|
100 | {
|
---|
101 | if (it) {
|
---|
102 | if (it->cur) {
|
---|
103 | void *obj = it->cur->obj;
|
---|
104 |
|
---|
105 | it->cur = it->cur->nxt;
|
---|
106 |
|
---|
107 | return obj;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return NULL;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void *ll_iter_peek(LL_ITER *it, int offset)
|
---|
115 | {
|
---|
116 | LL_NODE *n = it->cur;
|
---|
117 | int i;
|
---|
118 |
|
---|
119 | for (i = 0; i < offset; i++)
|
---|
120 | if (n)
|
---|
121 | n = n->nxt;
|
---|
122 | else
|
---|
123 | return NULL;
|
---|
124 |
|
---|
125 | if (!n)
|
---|
126 | return NULL;
|
---|
127 |
|
---|
128 | return n->obj;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void ll_iter_reset(LL_ITER *it)
|
---|
132 | {
|
---|
133 | it->cur = it->l->initial;
|
---|
134 | }
|
---|
135 |
|
---|
136 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
137 | {
|
---|
138 | if(obj) {
|
---|
139 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
140 | n->obj = obj;
|
---|
141 | n->nxt = it->cur;
|
---|
142 | n->prv = it->cur->prv;
|
---|
143 |
|
---|
144 | it->cur->prv->nxt = n;
|
---|
145 | it->cur->prv = n;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | void *ll_iter_remove(LL_ITER *it)
|
---|
150 | {
|
---|
151 | if (it) {
|
---|
152 | LL_NODE *n;
|
---|
153 |
|
---|
154 | // if is last node, handle differently
|
---|
155 | if (!it->cur && it->l->initial) {
|
---|
156 | n = it->l->initial;
|
---|
157 | while (n->nxt) n = n->nxt;
|
---|
158 | } else
|
---|
159 | n = it->cur->prv;
|
---|
160 |
|
---|
161 | if (n) {
|
---|
162 | void *obj = n->obj;
|
---|
163 | if (n->nxt) n->nxt->prv = n->prv;
|
---|
164 | if (n->prv) n->prv->nxt = n->nxt;
|
---|
165 | else it->l->initial = n->nxt;
|
---|
166 |
|
---|
167 | NULLFREE(n);
|
---|
168 | return obj;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | return NULL;
|
---|
173 | }
|
---|
174 |
|
---|
175 | void ll_iter_remove_data(LL_ITER *it)
|
---|
176 | {
|
---|
177 | void *obj = ll_iter_remove(it);
|
---|
178 | NULLFREE(obj);
|
---|
179 | }
|
---|
180 |
|
---|
181 | int ll_count(LLIST *l)
|
---|
182 | {
|
---|
183 | void *obj;
|
---|
184 | int c = 0;
|
---|
185 |
|
---|
186 | LL_ITER *it = ll_iter_create(l);;
|
---|
187 | while ((obj = ll_iter_next(it))) c++;
|
---|
188 | ll_iter_release(it);
|
---|
189 |
|
---|
190 | return c;
|
---|
191 | }
|
---|
192 |
|
---|
193 | void ll_insert_at(LLIST *l, void *obj, int pos)
|
---|
194 | {
|
---|
195 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
196 | LL_NODE *n = l->initial;
|
---|
197 | int i;
|
---|
198 |
|
---|
199 | for (i = 0; i < pos; i++)
|
---|
200 | if (n)
|
---|
201 | n = n->nxt;
|
---|
202 | else
|
---|
203 | break;
|
---|
204 |
|
---|
205 | new->obj = obj;
|
---|
206 | new->nxt = n;
|
---|
207 |
|
---|
208 | if (n && n->prv) n->prv->nxt = new;
|
---|
209 | else l->initial = new;
|
---|
210 |
|
---|
211 | if (n) n->prv = new;
|
---|
212 | }
|
---|
213 |
|
---|