This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ll.h
222 lines (189 loc) · 4.55 KB
/
ll.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
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
/* @file ll.h
** @author Robert Quattlebaum <[email protected]>
**
** Originally published 2010-8-31.
**
** This file was written by Robert Quattlebaum <[email protected]>.
**
** This work is provided as-is. Unless otherwise provided in writing,
** Robert Quattlebaum makes no representations or warranties of any
** kind concerning this work, express, implied, statutory or otherwise,
** including without limitation warranties of title, merchantability,
** fitness for a particular purpose, non infringement, or the absence
** of latent or other defects, accuracy, or the present or absence of
** errors, whether or not discoverable, all to the greatest extent
** permissible under applicable law.
**
** To the extent possible under law, Robert Quattlebaum has waived all
** copyright and related or neighboring rights to this work. This work
** is published from the United States.
**
** I, Robert Quattlebaum, dedicate any and all copyright interest in
** this work to the public domain. I make this dedication for the
** benefit of the public at large and to the detriment of my heirs and
** successors. I intend this dedication to be an overt act of
** relinquishment in perpetuity of all present and future rights to
** this code under copyright law. In jurisdictions where this is not
** possible, I hereby release this code under the Creative Commons
** Zero (CC0) license.
**
** * <http://creativecommons.org/publicdomain/zero/1.0/>
*/
#ifndef __LL_HEADER__
#define __LL_HEADER__ 1
#if !defined(__BEGIN_DECLS) || !defined(__END_DECLS)
#if defined(__cplusplus)
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS \
}
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif
#endif
#include <stdbool.h>
#if LL_DEBUG
#include <assert.h>
#define LL_ASSERT(x) assert(x)
#else
#define LL_ASSERT(x) do { } while(0)
#endif
__BEGIN_DECLS
struct ll_item_s;
typedef struct ll_item_s *ll_item_t;
struct ll_item_s {
ll_item_t next;
ll_item_t prev;
};
typedef int ll_compare_result_t;
typedef ll_compare_result_t (*ll_compare_func_t)(const void* lhs,
const void* rhs, void* context);
static inline void*
ll_last(
void* item
) {
ll_item_t iter = item;
if(iter)
while(iter->next) {
iter = iter->next;
}
return iter;
}
static inline bool
ll_verify(void* item) {
int ret = 0;
ll_item_t item_ = item;
if(item) {
while(item_->prev)
item_ = item_->prev;
for(; item_->next; item_ = item_->next)
ret++;
for(; item_->prev; item_ = item_->prev)
ret--;
}
return ret == 0;
}
static inline void
ll_insert_after(
void* where, void* item
) {
ll_item_t const location_ = where;
ll_item_t const item_ = item;
LL_ASSERT(ll_verify(where));
item_->prev = location_;
if((item_->next = location_->next))
item_->next->prev = item_;
location_->next = item_;
LL_ASSERT(ll_verify(where));
}
static inline void
ll_insert(
void* where, void* item
) {
ll_item_t const location_ = where;
ll_item_t const item_ = item;
LL_ASSERT(ll_verify(where));
item_->next = location_;
item_->prev = location_->prev;
location_->prev = item;
if(item_->prev)
item_->prev->next = item_;
LL_ASSERT(ll_verify(item));
}
static inline void
ll_prepend(
void** list, void* item
) {
ll_insert(*list, item);
*list = item;
LL_ASSERT(ll_verify(*list));
}
static inline void
ll_sorted_insert(
void** list, void* item, ll_compare_func_t compare_func, void* context
) {
ll_item_t location_ = *list;
LL_ASSERT(ll_verify(location_));
if(location_) {
while(compare_func(item, location_, context) >= 0) {
LL_ASSERT(ll_verify(location_));
if(location_->next) {
location_ = location_->next;
} else {
// We are at the end of the list,
// so go ahead and add us to the end.
ll_insert_after(location_, item);
return;
}
}
if(location_!=*list)
ll_insert(location_,item);
else
ll_prepend(list,item);
} else {
*list = item;
}
LL_ASSERT(ll_verify(*list));
}
static inline void
ll_remove(
void** list, void* item
) {
ll_item_t const item_ = item;
LL_ASSERT(ll_verify(*list));
if(item_->prev)
item_->prev->next = item_->next;
if(item_->next)
item_->next->prev = item_->prev;
if(*list == item_)
*list = item_->next;
LL_ASSERT(ll_verify(*list));
}
static inline void*
ll_pop(
void** list
) {
void* item = *list;
ll_remove(list,item);
return item;
}
static inline void
ll_push(
void** list, void* item
) {
if(*list)
ll_insert_after(ll_last(*list),item);
else
*list = item;
LL_ASSERT(ll_verify(*list));
}
static inline size_t
ll_count(void* item) {
size_t ret = 0;
ll_item_t item_ = item;
for(; item_; item_ = item_->next)
ret++;
return ret;
}
__END_DECLS
#endif