-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathq_list.h
157 lines (121 loc) · 3.77 KB
/
q_list.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
151
152
153
154
155
156
157
/*
Copyright (C) 2003-2006 Andrey Nazarov
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 LIST_H
#define LIST_H
//
// list.h
//
typedef struct list_s {
struct list_s *next; // head
struct list_s *prev; // tail
} list_t;
static inline void List_Link(list_t *prev,
list_t *next,
list_t *elem)
{
prev->next = elem;
next->prev = elem;
elem->prev = prev;
elem->next = next;
}
static inline void List_Unlink(list_t *prev, list_t *next)
{
prev->next = next;
next->prev = prev;
}
static inline void List_Init(list_t *list)
{
list->prev = list->next = list;
}
static inline void List_Append(list_t *tail, list_t *elem)
{
List_Link(tail->prev, tail, elem);
}
static inline void List_Insert(list_t *head, list_t *elem)
{
List_Link(head, head->next, elem);
}
#define LIST_FOR_EACH_ELEM(cursor, list) \
for (cursor = (list)->next; cursor != list; cursor = (cursor)->next)
// insert element into the sorted list of array elements
static inline void List_SeqAdd(list_t *list, list_t *elem)
{
list_t *cursor;
LIST_FOR_EACH_ELEM(cursor, list) {
if (elem < cursor) {
break;
}
}
List_Append(cursor, elem);
}
static inline void List_Delete(list_t *elem)
{
List_Unlink(elem->prev, elem->next);
List_Init(elem);
}
static inline void List_Remove(list_t *elem)
{
List_Unlink(elem->prev, elem->next);
}
#define LIST_ENTRY(type, elem, member) \
((type *)((unsigned char *)elem - q_offsetof(type, member)))
#define LIST_EMPTY(list) \
((list)->next == list)
#define LIST_SINGLE(list) \
(!LIST_EMPTY(list) && (list)->next == (list)->prev)
#define LIST_TERM(cursor, list, member) \
(&(cursor)->member == list)
#define LIST_FIRST(type, list, member) \
LIST_ENTRY(type, (list)->next, member)
#define LIST_LAST(type, list, member) \
LIST_ENTRY(type, (list)->prev, member)
#define LIST_NEXT(type, entry, member) \
LIST_ENTRY(type, (entry)->member.next, member)
#define LIST_PREV(type, entry, member) \
LIST_ENTRY(type, (entry)->member.prev, member)
#define LIST_FOR_EACH(type, cursor, list, member) \
for (cursor = LIST_FIRST(type, list, member); \
!LIST_TERM(cursor, list, member); \
cursor = LIST_NEXT(type, cursor, member))
#define LIST_FOR_EACH_SAFE(type, cursor, next, list, member) \
for (cursor = LIST_FIRST(type, list, member); \
next = LIST_NEXT(type, cursor, member), \
!LIST_TERM(cursor, list, member); \
cursor = next)
#define LIST_DECL(list) list_t list = { &list, &list }
static inline int List_Count(list_t *list)
{
int count = 0;
list_t *cursor;
LIST_FOR_EACH_ELEM(cursor, list) {
count++;
}
return count;
}
static inline void *List_Index(list_t *list, size_t offset, int index)
{
int count = 0;
list_t *cursor;
LIST_FOR_EACH_ELEM(cursor, list) {
if (count == index) {
return (unsigned char *)cursor - offset;
}
count++;
}
return NULL;
}
#define LIST_INDEX(type, index, list, member) \
((type *)List_Index(list, q_offsetof(type, member), index))
#endif // LIST_H