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 | pthread_mutex_lock(&l->lock);
|
---|
85 | }
|
---|
86 | else
|
---|
87 | it->cur = NULL;
|
---|
88 |
|
---|
89 |
|
---|
90 | return it;
|
---|
91 | }
|
---|
92 |
|
---|
93 | void ll_iter_release(LL_ITER *it)
|
---|
94 | {
|
---|
95 | if(it->l)
|
---|
96 | pthread_mutex_unlock(&it->l->lock);
|
---|
97 |
|
---|
98 | NULLFREE(it);
|
---|
99 | }
|
---|
100 |
|
---|
101 | void *ll_iter_next(LL_ITER *it)
|
---|
102 | {
|
---|
103 | if (it) {
|
---|
104 | if (it->cur) {
|
---|
105 | void *obj = it->cur->obj;
|
---|
106 |
|
---|
107 | it->cur = it->cur->nxt;
|
---|
108 |
|
---|
109 | return obj;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void *ll_iter_peek(LL_ITER *it, int offset)
|
---|
117 | {
|
---|
118 | LL_NODE *n = it->cur;
|
---|
119 | int i;
|
---|
120 |
|
---|
121 | for (i = 0; i < offset; i++)
|
---|
122 | if (n)
|
---|
123 | n = n->nxt;
|
---|
124 | else
|
---|
125 | return NULL;
|
---|
126 |
|
---|
127 | if (!n)
|
---|
128 | return NULL;
|
---|
129 |
|
---|
130 | return n->obj;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void ll_iter_reset(LL_ITER *it)
|
---|
134 | {
|
---|
135 | it->cur = it->l->initial;
|
---|
136 | }
|
---|
137 |
|
---|
138 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
139 | {
|
---|
140 | if(obj) {
|
---|
141 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
142 | n->obj = obj;
|
---|
143 | n->nxt = it->cur;
|
---|
144 | n->prv = it->cur->prv;
|
---|
145 |
|
---|
146 | it->cur->prv->nxt = n;
|
---|
147 | it->cur->prv = n;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | void *ll_iter_remove(LL_ITER *it)
|
---|
152 | {
|
---|
153 | if (it) {
|
---|
154 | LL_NODE *n;
|
---|
155 |
|
---|
156 | // if is last node, handle differently
|
---|
157 | if (!it->cur && it->l->initial) {
|
---|
158 | n = it->l->initial;
|
---|
159 | while (n->nxt) n = n->nxt;
|
---|
160 | } else
|
---|
161 | n = it->cur->prv;
|
---|
162 |
|
---|
163 | if (n) {
|
---|
164 | void *obj = n->obj;
|
---|
165 | if (n->nxt) n->nxt->prv = n->prv;
|
---|
166 | if (n->prv) n->prv->nxt = n->nxt;
|
---|
167 | else it->l->initial = n->nxt;
|
---|
168 |
|
---|
169 | NULLFREE(n);
|
---|
170 | return obj;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | return NULL;
|
---|
175 | }
|
---|
176 |
|
---|
177 | void ll_iter_remove_data(LL_ITER *it)
|
---|
178 | {
|
---|
179 | void *obj = ll_iter_remove(it);
|
---|
180 | NULLFREE(obj);
|
---|
181 | }
|
---|
182 |
|
---|
183 | int ll_count(LLIST *l)
|
---|
184 | {
|
---|
185 | void *obj;
|
---|
186 | int c = 0;
|
---|
187 |
|
---|
188 | LL_ITER *it = ll_iter_create(l);;
|
---|
189 | while ((obj = ll_iter_next(it))) c++;
|
---|
190 | ll_iter_release(it);
|
---|
191 |
|
---|
192 | return c;
|
---|
193 | }
|
---|
194 |
|
---|
195 | void ll_insert_at(LLIST *l, void *obj, int pos)
|
---|
196 | {
|
---|
197 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
198 | LL_NODE *n = l->initial;
|
---|
199 | int i;
|
---|
200 |
|
---|
201 | for (i = 0; i < pos; i++)
|
---|
202 | if (n)
|
---|
203 | n = n->nxt;
|
---|
204 | else
|
---|
205 | break;
|
---|
206 |
|
---|
207 | new->obj = obj;
|
---|
208 | new->nxt = n;
|
---|
209 |
|
---|
210 | if (n && n->prv) n->prv->nxt = new;
|
---|
211 | else l->initial = new;
|
---|
212 |
|
---|
213 | if (n) n->prv = new;
|
---|
214 | }
|
---|
215 |
|
---|