-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIList.c
357 lines (282 loc) · 5.63 KB
/
MIList.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/******************************************************************************
* Copyright (c) 2005, 2010 The Regents of the University of California and others
* This material was produced under U.S. Government contract W-7405-ENG-36
* for Los Alamos National Laboratory, which is operated by the University
* of California for the U.S. Department of Energy. The U.S. Government has
* rights to use, reproduce, and distribute this software. NEITHER THE
* GOVERNMENT NOR THE UNIVERSITY MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
* ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified
* to produce derivative works, such modified software should be clearly
* marked, so as not to confuse it with the version available from LANL.
*
* Additionally, this program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* LA-CC 04-115
*
* Copyright 2012-2014 Cray Inc. All Rights Reserved.
*
******************************************************************************/
/*
* NOTE: This list implementation is not thread safe.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include "MIList.h"
/*
* Create a new empty list
*/
MIList *
MIListNew(void)
{
MIList * l;
l = (MIList *)malloc(sizeof(MIList));
l->l_head = (MIListElement *)NULL;
l->l_tail = &l->l_head;
l->l_nel = 0;
l->l_scan = NULL;
return l;
}
/*
* Add new element to end of list.
*/
void
MIListAdd(MIList *l, void *v)
{
MIListElement * e;
if ( l == (MIList *)NULL)
return;
e = malloc(sizeof(MIListElement));
e->l_value = v;
e->l_next = (MIListElement *)NULL;
*(l->l_tail) = e;
l->l_tail = &e->l_next;
l->l_nel++;
}
/*
* Add new element to beginning of list (stack emulation).
*/
void
MIListAddFirst(MIList *l, void *v)
{
MIListElement * e;
MIListElement * ep;
if (l == (MIList *)NULL)
return;
e = malloc(sizeof(MIListElement));
e->l_value = v;
e->l_next = l->l_head;
/*
** Adjust tail if necessary.
*/
if
(
(ep = l->l_head) != (MIListElement *)NULL
&&
ep->l_next == (MIListElement *)NULL
)
l->l_tail = &ep->l_next;
l->l_head = e;
l->l_nel++;
}
/*
* Insert new element before given element
*/
void
MIListInsertBefore(MIList *l, void *val, void *new_val)
{
MIListElement ** e;
MIListElement * ne;
if (l == (MIList *)NULL) {
return;
}
/*
* Find the element corresponding to val
*/
for (e = &l->l_head ; *e != (MIListElement *)NULL ; e = &(*e)->l_next) {
if ((*e)->l_value == val) {
break;
}
}
if (*e == NULL) {
return;
}
ne = malloc(sizeof(MIListElement));
ne->l_value = new_val;
ne->l_next = *e;
*e = ne;
l->l_nel++;
}
/*
* Append source list to destination list
*/
void
MIListAppend(MIList *dst, MIList *src)
{
void * e;
MIListSet(src);
while ( (e = MIListGet(src)) != (void *)NULL) {
MIListAdd(dst, e);
}
}
/*
* Remove element from list
*/
void
MIListRemove(MIList *l, void *v)
{
MIListElement * e;
MIListElement * ep;
if ( l == (MIList *)NULL || l->l_nel == 0 ) {
return;
}
for ( ep = e = l->l_head ; e != (MIListElement *)NULL ; )
{
if ( e->l_value != v )
{
if ( ep != e )
ep = ep->l_next;
e = e->l_next;
continue;
}
if ( l->l_scan == e )
l->l_scan = ep;
if ( ep == e )
{
l->l_head = e->l_next;
if ( e->l_next == (MIListElement *)NULL )
l->l_tail = &l->l_head;
}
else
{
ep->l_next = e->l_next;
if ( e->l_next == (MIListElement *)NULL )
l->l_tail = &ep->l_next;
}
free(e);
break;
}
l->l_nel--;
}
/*
* Remove first element of list (stack emulation)
*/
void *
MIListRemoveFirst(MIList *l)
{
void * v;
MIListElement * e;
if ( l == (MIList *)NULL || l->l_nel == 0 ) {
return (void *)NULL;
}
e = l->l_head;
if ( l->l_scan == e )
l->l_scan = e->l_next;
l->l_head = e->l_next;
if ( e->l_next == (MIListElement *)NULL )
l->l_tail = &l->l_head;
v = e->l_value;
free(e);
l->l_nel--;
return v;
}
/*
* Destroy list and its contents
*/
void
MIListFree(MIList *l, void (*destroy)())
{
MIListElement * ep;
MIListElement * en;
if ( l == (MIList *)NULL )
return;
ep = l->l_head;
while ( ep != (MIListElement *)NULL )
{
en = ep->l_next;
if ( destroy != (void (*)())NULL )
destroy(ep->l_value);
free(ep);
ep = en;
}
free(l);
}
/*
* Initialize list iterator
*/
void
MIListSet(MIList *l)
{
if ( l == (MIList *)NULL )
return;
l->l_scan = l->l_head;
}
/*
* Get next element from list. Returns NULL when there are no more elements
*/
void *
MIListGet(MIList *l)
{
void * val;
MIListElement * le;
if ( l == (MIList *)NULL || l->l_scan == (MIListElement *)NULL ) {
return (void *)NULL;
}
le = l->l_scan;
l->l_scan = le->l_next;
val = le->l_value;
return val;
}
/*
* Get the first element in the list
*/
void *
MIListGetFirst(MIList *l)
{
void * val;
if ( l == (MIList *)NULL || l->l_head == (MIListElement *) NULL) {
return (void *)NULL;
}
val = l->l_head->l_value;
return val;
}
/*
* Check if the list is empty. Returns true (1) if it is.
*/
int
MIListIsEmpty(MIList *l)
{
return (int)(l == (MIList *)NULL || l->l_nel == 0);
}
/*
* Check if element is in the list
*/
int
MIListTest(MIList *l, void *v)
{
MIListElement * e;
if ( l == (MIList *)NULL ) {
return 0;
}
for ( e = l->l_head ; e != (MIListElement *)NULL ; e = e->l_next ) {
if ( e->l_value == v ) {
return 1;
}
}
return 0;
}
/*
* Get the number of elements in the list
*/
int
MIListSize(MIList *l)
{
if ( l == (MIList *)NULL )
return 0;
return l->l_nel;
}