-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
dlist.d
334 lines (283 loc) · 6.99 KB
/
dlist.d
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* Interface to the C linked list type.
*
* List is a complete package of functions to deal with singly linked
* lists of pointers or integers.
* Features:
* 1. Uses mem package.
* 2. Has loop-back tests.
* 3. Each item in the list can have multiple predecessors, enabling
* different lists to 'share' a common tail.
*
* Copyright: Copyright (C) 1986-1990 by Northwest Software
* Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/dlist.d, backend/dlist.d)
*/
module dmd.backend.dlist;
import core.stdc.stdarg;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
extern (C++):
nothrow:
@nogc:
struct LIST
{
/* Do not access items in this struct directly, use the */
/* functions designed for that purpose. */
LIST* next; /* next element in list */
int count; /* when 0, element may be deleted */
union
{ void* ptr; /* data pointer */
int data;
}
}
alias list_t = LIST*; /* pointer to a list entry */
/* FPNULL is a null function pointer designed to be an argument to
* list_free().
*/
alias list_free_fp = void function(void*) @nogc nothrow;
enum FPNULL = cast(list_free_fp)null;
private __gshared list_t list_freelist;
/* **************** PUBLIC FUNCTIONS ********************* */
/********************************
* Returns:
* pointer to next entry in list.
*/
list_t list_next(list_t list) { return list.next; }
/********************************
* Returns:
* ptr from list entry.
*/
inout(void)* list_ptr(inout list_t list) { return list.ptr; }
/********************************
* Returns:
* integer item from list entry.
*/
int list_data(list_t list) { return list.data; }
/********************************
* Prepend integer item to list.
*/
void list_prependdata(list_t *plist,int d)
{
list_prepend(plist, null).data = d;
}
list_t list_alloc()
{
list_t list;
if (list_freelist)
{
list = list_freelist;
list_freelist = list_next(list);
//mem_setnewfileline(list,file,line);
}
else
{
list = list_new();
}
return list;
}
list_t list_new() { return cast(list_t)malloc(LIST.sizeof); }
void list_delete(list_t list) { free(list); }
/********************
* Free list.
* Params:
* plist = Pointer to list to free
* freeptr = Pointer to freeing function for the data pointer
* (use FPNULL if none)
* Output:
* *plist is null
*/
void list_free(list_t* plist, list_free_fp freeptr)
{
list_t list = *plist;
*plist = null; // block any circular reference bugs
while (list && --list.count == 0)
{
list_t listnext = list_next(list);
if (freeptr)
(*freeptr)(list_ptr(list));
debug memset(list, 0, (*list).sizeof);
list.next = list_freelist;
list_freelist = list;
list = listnext;
}
}
void list_free(list_t *l)
{
list_free(l, FPNULL);
}
/***************************
* Remove ptr from the list pointed to by *plist.
* Output:
* *plist is updated to be the start of the new list
* Returns:
* null if *plist is null
* otherwise ptr
*/
void* list_subtract(list_t* plist, void* ptr)
{
list_t list;
while ((list = *plist) != null)
{
if (list_ptr(list) == ptr)
{
if (--list.count == 0)
{
*plist = list_next(list);
list.next = list_freelist;
list_freelist = list;
}
return ptr;
}
else
plist = &list.next;
}
return null; // it wasn't in the list
}
/***************************
* Remove first element in list pointed to by *plist.
* Returns:
* First element, null if *plist is null
*/
void* list_pop(list_t* plist)
{
return list_subtract(plist, list_ptr(*plist));
}
/*************************
* Append ptr to *plist.
* Returns:
* pointer to list item created.
* null if out of memory
*/
list_t list_append(list_t* plist, void* ptr)
{
while (*plist)
plist = &(*plist).next; // find end of list
list_t list = list_alloc();
if (list)
{
*plist = list;
list.next = null;
list.ptr = ptr;
list.count = 1;
}
return list;
}
/*************************
* Prepend ptr to *plist.
* Returns:
* pointer to list item created (which is also the start of the list).
* null if out of memory
*/
list_t list_prepend(list_t *plist, void *ptr)
{
list_t list = list_alloc();
if (list)
{
list.next = *plist;
list.ptr = ptr;
list.count = 1;
*plist = list;
}
return list;
}
/*************************
* Count up and return number of items in list.
* Returns:
* # of entries in list
*/
int list_nitems(list_t list)
{
int n = 0;
foreach (l; ListRange(list))
{
++n;
}
return n;
}
/*************************
* Returns:
* nth list entry in list.
*/
list_t list_nth(list_t list, int n)
{
for (int i = 0; i < n; i++)
{
assert(list);
list = list_next(list);
}
return list;
}
/************************
* Compare two lists.
* Returns:
* If they have the same ptrs, return 1 else 0.
*/
int list_equal(list_t list1, list_t list2)
{
while (list1 && list2)
{
if (list_ptr(list1) != list_ptr(list2))
break;
list1 = list_next(list1);
list2 = list_next(list2);
}
return list1 == list2;
}
/*************************
* Search for ptr in list.
* Returns:
* If found, return list entry that it is, else null.
*/
list_t list_inlist(list_t list, void* ptr)
{
foreach (l; ListRange(list))
if (l.ptr == ptr)
return l;
return null;
}
/***************************************
* Apply a function fp to each member of a list.
*/
void list_apply(list_t* plist, void function(void*) @nogc nothrow fp)
{
if (fp)
foreach (l; ListRange(*plist))
{
(*fp)(list_ptr(l));
}
}
/********************************************
* Reverse a list in place.
*/
list_t list_reverse(list_t l)
{
list_t r = null;
while (l)
{
list_t ln = list_next(l);
l.next = r;
r = l;
l = ln;
}
return r;
}
/********************************
* Range for Lists.
*/
struct ListRange
{
pure nothrow @nogc @safe:
this(list_t li)
{
this.li = li;
}
list_t front() return { return li; }
void popFront() { li = li.next; }
bool empty() const { return !li; }
private:
list_t li;
}