forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.h
More file actions
185 lines (182 loc) · 13.7 KB
/
Copy pathlist.h
File metadata and controls
185 lines (182 loc) · 13.7 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
/*
* list.h - useful list handling header
*
* Copyright © 2008 Julien Danjou <julien@danjou.info>
* Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef AWESOME_COMMON_LIST_H
#define AWESOME_COMMON_LIST_H
#define DO_SLIST(type, prefix, dtor) \
static inline type *prefix##_list_next(type **list __attribute__ ((unused)), \
type *item) \
{ \
return item->next; \
} \
\
static inline type *prefix##_list_pop(type **list) \
{ \
if (*list) \
{ \
type *res = *list; \
*list = res->next; \
if(res->next) \
res->next->prev = NULL; \
res->next = NULL; \
return res; \
} \
return NULL; \
} \
\
static inline void prefix##_list_push(type **list, type *item) \
{ \
if(*list) \
(*list)->prev = item; \
item->next = *list; \
*list = item; \
} \
\
static inline type **prefix##_list_last(type **list) \
{ \
while (*list && (*list)->next) \
list = &(*list)->next; \
return list; \
} \
\
static inline void prefix##_list_append(type **list, type *item) \
{ \
if(*(list = prefix##_list_last(list))) \
{ \
(*list)->next = item; \
item->prev = *list; \
} \
else \
(*list) = item; \
} \
\
static inline type *prefix##_list_rev(type *list) \
{ \
type *l = NULL; \
while (list) \
prefix##_list_push(&l, prefix##_list_pop(&list)); \
return l; \
} \
\
static inline type **prefix##_list_init(type **list) \
{ \
*list = NULL; \
return list; \
} \
\
static inline void prefix##_list_wipe(type **list) \
{ \
while (*list) \
{ \
type *item = prefix##_list_pop(list); \
dtor(&item); \
} \
} \
\
static inline type *prefix##_list_prev(type **list __attribute__ ((unused)), \
type *item) \
{ \
return item->prev; \
} \
\
static inline void prefix##_list_swap(type **list, type *item1, type *item2) \
{ \
type *i1n, *i2n, *i1p, *i2p; \
\
if(!item1 || !item2) return; \
\
i1n = item1->next; \
i2n = item2->next; \
i1p = item1->prev; \
i2p = item2->prev; \
\
if(item1 == i2n) \
{ \
item1->next = item2; \
item2->prev = item1; \
if((item1->prev = i2p)) \
i2p->next = item1; \
if((item2->next = i1n)) \
i1n->prev = item2; \
} \
else if(item2 == i1n) \
{ \
item2->next = item1; \
item1->prev = item2; \
if((item2->prev = i1p)) \
i1p->next = item2; \
if((item1->next = i2n)) \
i2n->prev = item1; \
} \
else \
{ \
if((item1->next = i2n)) \
item1->next->prev = item1; \
if((item1->prev = i2p)) \
item1->prev->next = item1; \
if((item2->prev = i1p)) \
item2->prev->next = item2; \
if((item2->next = i1n)) \
item2->next->prev = item2; \
} \
if(*list == item1) \
*list = item2; \
else if(*list == item2) \
*list = item1; \
} \
\
static inline type *prefix##_list_prev_cycle(type **list, type *item) \
{ \
if(!item->prev) \
return *prefix##_list_last(list); \
return item->prev; \
} \
\
static inline type *prefix##_list_next_cycle(type **list, type *item) \
{ \
if(item->next) \
return item->next; \
return *list; \
} \
\
static inline type *prefix##_list_detach(type **list, type *item) \
{ \
if(item == *list) \
*list = item->next; \
else if(item->prev) \
item->prev->next = item->next; \
if(item->next) \
item->next->prev = item->prev; \
item->next = NULL; \
item->prev = NULL; \
return item; \
} \
static inline type *prefix##_list_attach_after(type *item1, type *item2) \
{ \
item2->prev = item1; \
item2->next = item1->next; \
if(item1->next) \
item1->next->prev = item2; \
item1->next = item2; \
return item2; \
}
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80