-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
173 lines (138 loc) · 2.98 KB
/
list.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "list.h"
#define likely(exp) __builtin_expect (!!(exp), 1)
#define unlikely(exp) __builtin_expect (!!(exp), 0)
list_node_t *
list_at (const list_t *list, size_t index)
{
if (unlikely (index >= list->size))
return NULL;
list_node_t *ret = list->head;
for (; index; index--)
ret = ret->next;
return ret;
}
void
list_push_front (list_t *list, list_node_t *node)
{
node->prev = NULL;
node->next = list->head;
if (list->head)
list->head->prev = node;
else
list->tail = node;
list->head = node;
list->size++;
}
void
list_push_back (list_t *list, list_node_t *node)
{
node->next = NULL;
node->prev = list->tail;
if (list->tail)
list->tail->next = node;
else
list->head = node;
list->tail = node;
list->size++;
}
void
list_insert_at (list_t *list, size_t index, list_node_t *node)
{
size_t size = list->size;
if (unlikely (index > size))
return;
if (index == size)
return list_push_back (list, node);
if (index == 0)
return list_push_front (list, node);
list_node_t *old = list->head;
for (; index; index--)
old = old->next;
list_insert_front (list, old, node);
}
void
list_insert_front (list_t *list, list_node_t *pos, list_node_t *node)
{
node->next = pos;
node->prev = pos->prev;
if (pos->prev)
pos->prev->next = node;
else
list->head = node;
pos->prev = node;
list->size++;
}
void
list_insert_back (list_t *list, list_node_t *pos, list_node_t *node)
{
node->prev = pos;
node->next = pos->next;
if (pos->next)
pos->next->prev = node;
else
list->tail = node;
pos->next = node;
list->size++;
}
void
list_erase (list_t *list, list_node_t *node)
{
list_node_t *prev = node->prev;
list_node_t *next = node->next;
if (node == list->head)
list->head = next;
if (node == list->tail)
list->tail = prev;
if (prev)
prev->next = next;
if (next)
next->prev = prev;
list->size--;
}
void
list_erase_at (list_t *list, size_t index)
{
list_node_t *node;
if ((node = list_at (list, index)))
list_erase (list, node);
}
void
list_pop_front (list_t *list)
{
list_node_t *node;
if ((node = list->head))
list_erase (list, node);
}
void
list_pop_back (list_t *list)
{
list_node_t *node;
if ((node = list->tail))
list_erase (list, node);
}
/* **************************************************************** */
/* ext */
/* **************************************************************** */
list_node_t *
list_find (const list_t *list, const list_node_t *target, list_comp_t *comp)
{
list_node_t *curr = list->head;
for (size_t size = list->size; size; size--)
{
if (comp (target, curr) == 0)
return curr;
curr = curr->next;
}
return NULL;
}
void
list_visit (list_t *list, list_visit_t *visit)
{
list_node_t *curr = list->head, *next;
for (size_t size = list->size; size; size--)
{
next = curr->next;
visit (curr);
curr = next;
}
}