summaryrefslogtreecommitdiff
path: root/src/list.h
blob: 742de5093595ef46def5e10341611123a8909027 (plain)
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
/*
 *	Ivy, C interface
 *
 *	Copyright (C) 1997-2000
 *	Centre d'Études de la Navigation Aérienne
 *
 * 	Simple lists in C
 *
 *	Authors: François-Régis Colin <fcolin@cena.fr>
 *
 *	$Id$
 * 
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 */
#if (__GNUC__ >= 3)
#define TYPEOF(p) typeof (p)
#else
#define  TYPEOF(p) void *
#endif

#define IVY_LIST_ITER( list, p, cond ) \
	p = list; \
	while ( p && (cond) ) p = p->next 



#define IVY_LIST_REMOVE( list, p ) \
	{ \
	TYPEOF(p)  toRemove; \
	if ( list == p ) \
		{ \
		list = p->next; \
		free(p);\
		} \
		else \
		{\
		toRemove = p;\
		IVY_LIST_ITER( list, p, ( p->next != toRemove ));\
		if ( p )\
			{\
			/* somme tricky swapping to use a untyped variable */\
			TYPEOF(p) suiv; \
			TYPEOF(p) prec = p;\
			p = toRemove;\
			suiv = p->next;\
			p = prec;\
			p->next = suiv;\
			free(toRemove);\
			}\
		} \
	}

#define IVY_LIST_ADD(list, p ) \
        if ((p = (TYPEOF(p)) (malloc( sizeof( *p ))))) \
		{ \
		memset( p, 0 , sizeof( *p ));\
		p->next = list; \
		list = p; \
		} 

#define IVY_LIST_EACH( list, p ) \
	for ( p = list ; p ; p = p -> next )

#define IVY_LIST_EACH_SAFE( list, p, next )\
for ( p = list ; (next = p ? p->next: p ),p ; p = next )

#define IVY_LIST_EMPTY( list ) \
	{ \
	TYPEOF(list) p; \
	while( list ) \
		{ \
		p = list;\
		list = list->next; \
		free(p);\
		} \
	}