forked from blechschmidt/massdns
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist.h
207 lines (186 loc) · 4.51 KB
/
list.h
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef INC_LIST
#define INC_LIST
#include "buffers.h"
#include "security.h"
typedef struct single_list_element
{
void *data;
struct single_list_element *next;
} single_list_element_t;
typedef struct
{
size_t count;
single_list_element_t *first;
single_list_element_t *last;
} single_list_t;
typedef struct double_list_element
{
void *data;
struct double_list_element *previous;
struct double_list_element *next;
} double_list_element_t;
typedef struct
{
size_t count;
double_list_element_t *first;
double_list_element_t *last;
} double_list_t;
size_t single_list_count(single_list_t* list)
{
return list->count;
}
void single_list_iterate(single_list_t *list, void (*f)(single_list_element_t *, size_t, void *), void *param)
{
if(list == NULL)
{
return;
}
single_list_element_t* element = list->first;
size_t counter = 0;
while (element != NULL)
{
single_list_element_t *next = element->next;
f(element, counter++, param);
element = next;
}
}
void single_list_element_set_array_element(single_list_element_t *list, size_t index, void *param)
{
((void **) param)[index] = list->data;
}
buffer_t single_list_to_array(single_list_t *list)
{
buffer_t buf;
buf.len = list->count;
buf.data = safe_malloc(sizeof(buf.data) * buf.len);
single_list_iterate(list, single_list_element_set_array_element, buf.data);
return buf;
}
single_list_t *single_list_new()
{
single_list_t *list = safe_calloc(sizeof(*list));
return list;
}
void single_list_cat(single_list_t* left, single_list_t* right)
{
left->count += right->count;
left->last = right->last;
left->last->next = right->first;
safe_free(&right);
}
void single_list_free(single_list_t *list_holder)
{
if(list_holder == NULL)
{
return;
}
single_list_element_t *list = list_holder->first;
while (list != NULL)
{
single_list_element_t *next = list->next;
free(list);
list = next;
}
free(list_holder);
}
void single_list_free_with_elements(single_list_t *list)
{
if(list == NULL)
{
return;
}
single_list_element_t *current = list->first;
while(current != NULL)
{
single_list_element_t *next = current->next;
free(current->data);
free(current);
current = next;
}
free(list);
}
void single_list_push_front(single_list_t *list, void *data)
{
single_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = list->first;
if (list->last == NULL)
{
list->last = new_element;
}
list->first = new_element;
list->count++;
}
void single_list_push_back(single_list_t *list, void *data)
{
single_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = NULL;
list->count++;
if (list->last)
{
list->last->next = new_element;
}
else
{
list->first = new_element;
}
list->last = new_element;
}
double_list_t* double_list_new()
{
double_list_t *list = safe_calloc(sizeof(*list));
return list;
}
void double_list_free(double_list_t *list_holder)
{
double_list_element_t *list = list_holder->first;
while (list != NULL)
{
double_list_element_t *next = list->next;
free(list);
list = next;
}
}
void double_list_push_front(double_list_t *list, void *data)
{
double_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = list->first;
new_element->previous = NULL;
if (list->last == NULL)
{
list->last = new_element;
}
list->first = new_element;
list->count++;
}
void double_list_push_back(double_list_t *list, void *data)
{
double_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = NULL;
new_element->previous = list->last;
list->count++;
if (list->last)
{
list->last->next = new_element;
}
else
{
list->first = new_element;
}
list->last = new_element;
}
void double_list_iterate(double_list_t *list, void (*f)(double_list_element_t *, size_t, void *), void *param)
{
double_list_element_t* element = list->first;
size_t counter = 0;
while (element != NULL)
{
double_list_element_t *next = element->next;
f(element, counter++, param);
element = next;
}
}
#endif