1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/* vim: set ts=4 sw=4 ai:
* tq.c -- tail queue implementation derived from NetBSD's queue.h
*/
#include <stdlib.h>
#include "tq.h"
struct tqh *tq_new(void)
{
struct tqh *hd;
hd = malloc(sizeof(*hd));
if (hd != NULL)
tq_init(hd);
return hd;
}
struct tqe *tq_elem_new(void *data)
{
struct tqe *e;
e = malloc(sizeof(*e));
if (e != NULL)
e->data = data;
return e;
}
void tq_insert_head(struct tqh *hd, struct tqe *e)
{
if ((e->next = hd->first) != NULL)
hd->first->pprev = &e->next;
else
hd->last = &e->next;
hd->first = e;
e->pprev = &hd->first;
}
void tq_insert_tail(struct tqh *hd, struct tqe *e)
{
e->next = NULL;
e->pprev = hd->last;
*hd->last = e;
hd->last = &e->next;
}
void tq_insert_after(struct tqh *hd, struct tqe *e, struct tqe *n)
{
if ((n->next = e->next) != NULL)
n->next->pprev = &n->next;
else
hd->last = &n->next;
e->next = n;
n->pprev = &e->next;
}
void tq_insert_before(struct tqh *hd, struct tqe *e, struct tqe *n)
{
n->pprev = e->pprev;
n->next = e;
*e->pprev = n;
e->pprev = &n->next;
if (hd->first == e)
hd->first = n;
}
void tq_remove(struct tqh *hd, struct tqe *e)
{
if ((e->next) != NULL)
e->next->pprev = e->pprev;
else
hd->last = e->pprev;
*e->pprev = e->next;
}
void tq_replace(struct tqh *hd, struct tqe *e, struct tqe *n)
{
if ((n->next = e->next) != NULL)
n->next->pprev = &n->next;
else
hd->last = &n->next;
n->pprev = e->pprev;
*n->pprev = n;
}
void tq_concat(struct tqh *hd1, struct tqh *hd2)
{
if (hd2->first != NULL) {
*hd1->last = hd2->first;
hd2->first->pprev = hd1->last;
hd1->last = hd2->last;
hd2->first = NULL;
hd2->last = &hd2->first;
}
}
void tq_cleanup(struct tqh *hd, void (*func)(void *))
{
struct tqe *e, *t;
tq_foreach_safe(e, hd, t) {
tq_remove(hd, e);
if (func)
func(e->data);
free(e);
}
}
struct tqe *tq_find_from_data(struct tqh *hd, const void *data, int (*cmp)(const void *, const void *))
{
struct tqe *e;
if (hd && !tq_empty(hd))
tq_foreach(e, hd)
if (e->data && cmp(e->data, data) == 0)
return e;
return NULL;
}
|