-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllist.h
150 lines (129 loc) · 4.33 KB
/
llist.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
/**
* @file llist.h
* (Doubly) Linked lists anchored at both ends
*
* $Id$
*
* (C) Copyright 2012 flemming.madsen at madsensoft.dk. Notice at end of file
*/
/// Struct mix-in. Use this inside your struct declaration
#define LListField(tn) tn * _llNext; tn * _llPrev
/// Anchor Declaration. Use like: LList(typename) varname;
#define LList(tn) struct { tn *head; tn *tail; int numElm; }
/// List init
#define lListInit(a) ((a)->head = (a)->tail = NULL, (a)->numElm = 0, (a))
/// List empty
#define lListEmpty(a) ((a)->head == NULL)
/// List head
#define lListHead(a) ((a)->head)
/// List tail
#define lListTail(a) ((a)->tail)
/// List length
#define lListLength(a) ((a)->numElm)
/// List element next
#define lListNext(e) ((e)->_llNext)
/// List element prev
#define lListPrev(e) ((e)->_llPrev)
/// Append element e to list e
#define lListAppend(a, e) \
{ \
if (lListEmpty(a)) (a)->head = (e); \
else (a)->tail->_llNext = (e); \
(e)->_llPrev = (a)->tail; \
(a)->tail = (e); \
(e)->_llNext = NULL; \
(a)->numElm++; \
}
/// Insert element n before element e in list a
#define lListInsertFirst(a, n) lListInsert(a, n, (a)->head)
/// Insert element n before element e in list a
#define lListInsert(a, n, e) \
{ \
if (!(e) || (a)->head == (e)) { \
n->_llNext = (a)->head; \
n->_llPrev = NULL; \
(a)->head = (n); \
} else { \
n->_llNext = (e); \
n->_llPrev = (e)->_llPrev; \
(e)->_llPrev->_llNext = n; \
} \
if (n->_llNext) n->_llNext->_llPrev = n; \
(a)->numElm++; \
}
/// Remove element e from list a. Caller must free element.
#define lListRemove(a, e) \
{ \
if ((a)->head) { \
if ((e) == (a)->head) (a)->head = (e)->_llNext; \
else (e)->_llPrev->_llNext = (e)->_llNext; \
if ((e) == (a)->tail) (a)->tail = (e)->_llPrev; \
else (e)->_llNext->_llPrev = (e)->_llPrev; \
(a)->numElm--; \
} \
}
/// Purge list a elements with free()
#define lListPurgeT(a, T) \
{ \
T p1, p2; \
for (p1 = (a)->head; p1; ) { \
p2 = p1; p1 = p1->_llNext; free(p2); \
} \
lListInit(a); \
}
/// Remove last element from list a
#define lListRemoveTailT(a, T) \
{ \
T p; \
if ((a)->tail) { \
p = (a)->tail; \
(a)->tail = (a)->tail->_llPrev; \
if ((a)->tail) (a)->tail->_llNext = NULL; \
else (a)->head = NULL; \
free(p); \
(a)->numElm--; \
} \
}
/// Remove first element from list a
#define lListRemoveHeadT(a, T) \
{ \
T p; \
if ((a)->head) { \
p = (a)->head; \
(a)->head = (a)->head->_llNext; \
if ((a)->head) (a)->head->_llPrev = NULL; \
else (a)->tail = NULL; \
free(p); \
(a)->numElm--; \
} \
}
// Convenience
// Only use if you have typeof() eg. gcc
#define lListPurge(a) lListPurgeT(a, typeof((a)->head))
#define lListRemoveTail(a) lListRemoveTailT(a, typeof((a)->head))
#define lListRemoveHead(a) lListRemoveHeadT(a, typeof((a)->head))
/// Traverse the list foreach e in a
#define lListForeachIn(e, a) for (e = (a)->head; e; e = e->_llNext)
/******************************************************************************
* Copyright (C) 2012 flemming.madsen at madsensoft.dk. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
// vim: set sw=3 sts=3 et: