-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslist.c
More file actions
209 lines (170 loc) · 3.2 KB
/
Copy pathslist.c
File metadata and controls
209 lines (170 loc) · 3.2 KB
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
208
209
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "slist.h"
struct sorted_list {
int (*compare)(const void *, const void *);
void **list;
unsigned int alloc, pos;
int is_sorted;
};
void slist_sort(slist *sl)
{
/* ignore empty and already sorted lists */
if (!sl || !sl->list || !sl->pos || sl->is_sorted) {
return;
}
qsort(sl->list, sl->pos, sizeof(void *), sl->compare);
sl->is_sorted = 1;
}
/*
* The engine of the "sorted list lookup". Arrives at the right
* conclusion by bisecting its way around inside the sorted list.
*/
int slist_find_pos(slist *sl, const void *key)
{
int value;
unsigned int high, low = 0;
if (!sl || !sl->pos || !sl->list) {
return -1;
}
if (!sl->is_sorted)
slist_sort(sl);
high = sl->pos;
while (high - low > 0) {
unsigned int mid = low + ((high - low) / 2);
value = sl->compare(&key, &sl->list[mid]);
if (value > 0) {
low = mid + 1;
continue;
} else if (value < 0) {
high = mid;
continue;
} else {
return mid;
}
}
return -1;
}
void *slist_find(slist *sl, const void *key)
{
int slot = slist_find_pos(sl, key);
if (slot < 0)
return NULL;
return sl->list[slot];
}
static int slist_grow(slist *sl, unsigned int hint)
{
void *ptr;
if (!hint)
return 0;
ptr = realloc(sl->list, (sl->alloc + hint) * sizeof(void *));
if (!ptr)
return -1;
sl->list = ptr;
sl->alloc += hint;
return 0;
}
int slist_push(slist *sl, void *item)
{
if (sl->pos >= sl->alloc - 1 && slist_grow(sl, 5) < 0) {
return -1;
}
/*
* No sorting is required when there's either only
* one item in the list, or when the list is added
* to in sequential order.
*/
sl->list[sl->pos] = item;
if (sl->is_sorted && sl->pos
&& sl->compare(&sl->list[sl->pos - 1], &sl->list[sl->pos]) > 0)
{
sl->is_sorted = 0;
}
sl->pos++;
return 0;
}
void *slist_pop(slist *sl)
{
void *item;
if (!sl->pos)
return NULL;
sl->pos--;
item = sl->list[sl->pos];
sl->list[sl->pos] = NULL;
return item;
}
slist *slist_init(unsigned int hint, int (*cmp)(const void *, const void *))
{
slist *sl;
sl = calloc(1, sizeof(*sl));
if (!sl)
return NULL;
if (hint)
slist_grow(sl, hint);
sl->compare = cmp;
return sl;
}
int slist_set_list(slist *sl, void **list, unsigned int items, int sorted)
{
if (!sl || !list || !items)
return -1;
sl->list = list;
sl->pos = items;
sl->alloc = 0;
if (!sorted) {
slist_sort(sl);
}
return 0;
}
void slist_release(slist *sl)
{
if (!sl)
return;
if (sl->list)
free(sl->list);
sl->list = NULL;
sl->pos = sl->alloc = 0;
sl->is_sorted = 1;
}
void slist_free_items(slist *sl)
{
unsigned int i;
if (!sl || !sl->list)
return;
for (i = 0; i < sl->pos; i++)
free(sl->list[i]);
sl->pos = 0;
}
void *slist_destroy(slist *sl, int items_too)
{
if (!sl)
return NULL;
if (items_too)
slist_free_items(sl);
slist_release(sl);
free(sl);
return NULL;
}
unsigned int slist_entries(slist *sl)
{
if (!sl)
return 0;
return sl->pos;
}
void *slist_get_list(slist *sl)
{
if (!sl)
return 0;
return sl->list;
}
void slist_walk(slist *sl, void *arg, int (*cb)(void *, void *))
{
int i;
if (!sl || !sl->list || !sl->pos)
return;
for (i = 0; i < sl->pos; i++) {
cb(arg, sl->list[i]);
}
}