-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.c
335 lines (286 loc) · 8.3 KB
/
list.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "stdafx.h"
#include "LoftyCAD.h"
#include <stdio.h>
// Link and unlink objects in a doubly linked list
void link(Object *new_obj, ListHead *obj_list)
{
new_obj->next = obj_list->head;
new_obj->prev = NULL;
if (obj_list->head == NULL)
obj_list->tail = new_obj;
else
obj_list->head->prev = new_obj;
obj_list->head = new_obj;
obj_list->count++;
ASSERT(obj_list->head->prev == NULL, "First element should not have a prev");
ASSERT(obj_list->tail->next == NULL, "Last element should not have a next");
}
void delink(Object *obj, ListHead *obj_list)
{
if (obj->prev != NULL)
obj->prev->next = obj->next;
else
obj_list->head = obj->next;
if (obj->next != NULL)
obj->next->prev = obj->prev;
else
obj_list->tail = obj->prev;
obj_list->count--;
ASSERT(obj_list->head == NULL || obj_list->head->prev == NULL, "First element should not have a prev");
ASSERT(obj_list->tail == NULL || obj_list->tail->next == NULL, "Last element should not have a next");
}
void
link_tail(Object *new_obj, ListHead *obj_list)
{
new_obj->next = NULL;
if (obj_list->head == NULL)
{
obj_list->head = new_obj;
new_obj->prev = NULL;
}
else
{
obj_list->tail->next = new_obj;
new_obj->prev = obj_list->tail;
}
obj_list->tail = new_obj;
obj_list->count++;
ASSERT(obj_list->head->prev == NULL, "First element should not have a prev");
ASSERT(obj_list->tail->next == NULL, "Last element should not have a next");
}
// Reverse a list. reverse(ABCD) --> DCBA
void
reverse(ListHead* list)
{
Object* e, * enext, * swap;
for (e = list->head; e != NULL; e = enext)
{
enext = e->next;
swap = e->next;
e->next = e->prev;
e->prev = swap;
}
swap = list->head;
list->head = list->tail;
list->tail = swap;
}
// Bring an element of the list to the front by rotating all the elements.
// rotate(ABCD, C) --> CDBA
void
rotate(ListHead* list, void* elt)
{
Object* e, *enext;
// Ensure elt is actually in the list.
for (e = list->head; e != NULL; e = e->next)
{
if (e == elt)
goto rotate_list;
}
ASSERT(FALSE, "Element not in list");
return; // avoid an infinite loop
rotate_list:
for (e = list->head; e != elt; e = enext)
{
enext = e->next;
delink(e, list);
link_tail(e, list);
}
}
// Link and delink objects into a group object list
void link_group(Object *new_obj, Group *group)
{
ASSERT(group->hdr.type == OBJ_GROUP, "Trying to link, but it's not a group");
link(new_obj, &group->obj_list);
new_obj->parent_group = group;
group->n_members++;
}
void delink_group(Object *obj, Group *group)
{
ASSERT(group->hdr.type == OBJ_GROUP, "Trying to delink, but it's not a group");
ASSERT(obj->parent_group == group, "Delinking from wrong group");
delink(obj, &group->obj_list);
group->n_members--;
obj->parent_group = NULL;
}
void link_tail_group(Object *new_obj, Group *group)
{
ASSERT(group->hdr.type == OBJ_GROUP, "Trying to link, but it's not a group");
link_tail(new_obj, &group->obj_list);
new_obj->parent_group = group;
group->n_members++;
}
// Link objects into a singly linked list, chained through the next pointer.
// the prev pointer is used to point to the object, so it doesn't interfere
// with whatever list the object is actually participating in.
void link_single(Object *new_obj, ListHead *obj_list)
{
Object *list_obj = obj_new();
list_obj->next = obj_list->head;
if (obj_list->head == NULL)
obj_list->tail = list_obj;
obj_list->head = list_obj;
list_obj->prev = new_obj;
}
// Like link_single, but first checks if the object is already in the list.
void link_single_checked(Object *new_obj, ListHead *obj_list)
{
Object *o;
for (o = obj_list->head; o != NULL; o = o->next)
{
if (o->prev == new_obj)
return;
}
link_single(new_obj, obj_list);
}
// Free an Edge to its free list.
void
free_edge(Object* obj)
{
ASSERT(obj->type == OBJ_EDGE, "This must be an Edge");
obj->next = (Object*)free_list_edge.head;
if (free_list_edge.head == NULL)
free_list_edge.tail = obj;
free_list_edge.head = obj;
}
// Free a Point to its free list.
void
free_point(Object* obj)
{
ASSERT(obj->type == OBJ_POINT, "This must be a Point");
obj->next = (Object*)free_list_pt.head;
if (free_list_pt.head == NULL)
free_list_pt.tail = obj;
free_list_pt.head = obj;
}
// Clean out a view list (a singly linked list of Points) by joining it to the free list.
// The points already have ID's of 0.
void
free_point_list(ListHead *pt_list)
{
if (pt_list->head == NULL)
return;
// we can't check them all, so just check the first one
ASSERT(pt_list->head->type == OBJ_POINT, "Only Points should be in here");
ASSERT(pt_list->head->ID == 0, "Only view list or temporary Points should be in here");
if (free_list_pt.head == NULL)
{
ASSERT(free_list_pt.tail == NULL, "Tail should be NULL");
free_list_pt.head = pt_list->head;
free_list_pt.tail = pt_list->tail;
}
else
{
ASSERT(free_list_pt.tail != NULL, "Tail should not be NULL");
free_list_pt.tail->next = pt_list->head;
free_list_pt.tail = pt_list->tail;
}
pt_list->head = NULL;
pt_list->tail = NULL;
pt_list->count = 0;
}
// Free all elements in a singly linked list of Objects, similarly to the above.
void free_obj_list(ListHead *obj_list)
{
if (obj_list->head == NULL)
return;
if (free_list_obj.head == NULL)
{
ASSERT(free_list_obj.tail == NULL, "Tail should be NULL");
free_list_obj.head = obj_list->head;
free_list_obj.tail = obj_list->tail;
}
else
{
ASSERT(free_list_obj.tail != NULL, "Tail should not be NULL");
free_list_obj.tail->next = obj_list->head;
free_list_obj.tail = obj_list->tail;
}
obj_list->head = NULL;
obj_list->tail = NULL;
obj_list->count = 0;
}
// Allocate the point bucket list structure.
Point ***
init_buckets(void)
{
Point ***bl = calloc(n_buckets, sizeof(Point **));
int i;
for (i = 0; i < n_buckets; i++)
bl[i] = calloc(n_buckets, sizeof(Point *));
return bl;
}
// Find a bucket header for a given point. The buckets are bottom-inclusive, top-exclusive in X and Y.
Point **
find_bucket(Point *p, Point ***bucket)
{
int bias = n_buckets / 2;
int i = (int)floor(p->x / bucket_size) + bias;
int j = (int)floor(p->y / bucket_size) + bias;
Point **bh;
if (i < 0)
i = 0;
else if (i >= n_buckets)
i = n_buckets - 1;
bh = bucket[i];
if (j < 0)
j = 0;
else if (j >= n_buckets)
j = n_buckets - 1;
return &bh[j];
}
// Clear a bucket structure to empty, but don't free anything.
void empty_bucket(Point ***bucket)
{
int i, j;
for (i = 0; i < n_buckets; i++)
{
Point **bh = bucket[i];
for (j = 0; j < n_buckets; j++)
bh[j] = NULL;
}
}
// Free all the Points a bucket references, and clear the buckets to empty.
void free_bucket_points(Point ***bucket)
{
int i, j;
Point *p, *nextp;
for (i = 0; i < n_buckets; i++)
{
Point **bh = bucket[i];
for (j = 0; j < n_buckets; j++)
{
for (p = bh[j]; p != NULL; p = nextp)
{
nextp = p->bucket_next;
p->hdr.next = free_list_pt.head;
if (free_list_pt.head == NULL)
free_list_pt.tail = (Object *)p;
free_list_pt.head = (Object *)p;
}
bh[j] = NULL;
}
}
}
// Free a bucket structure, and all the Points it references.
void free_bucket(Point ***bucket)
{
int i, j;
Point *p, *nextp;
for (i = 0; i < n_buckets; i++)
{
Point **bh = bucket[i];
for (j = 0; j < n_buckets; j++)
{
for (p = bh[j]; p != NULL; p = nextp)
{
nextp = p->bucket_next;
p->hdr.next = free_list_pt.head;
if (free_list_pt.head == NULL)
free_list_pt.tail = (Object *)p;
free_list_pt.head = (Object *)p;
}
}
free(bucket[i]);
}
free(bucket);
}