4 /* Our doubly linked lists have two header elements: the "head"
5 just before the first element and the "tail" just after the
6 last element. The `prev' link of the front header is null, as
7 is the `next' link of the back header. Their other two links
8 point toward each other via the interior elements of the list.
10 An empty list looks like this:
13 <---| head |<--->| tail |--->
16 A list with two elements in it looks like this:
18 +------+ +-------+ +-------+ +------+
19 <---| head |<--->| 1 |<--->| 2 |<--->| tail |<--->
20 +------+ +-------+ +-------+ +------+
22 The symmetry of this arrangement eliminates lots of special
23 cases in list processing. For example, take a look at
24 list_remove(): it takes only two pointer assignments and no
25 conditionals. That's a lot simpler than the code would be
26 without header elements.
28 (Because only one of the pointers in each header element is used,
29 we could in fact combine them into a single header element
30 without sacrificing this simplicity. But using two separate
31 elements allows us to do a little bit of checking on some
32 operations, which can be valuable.) */
34 /* Returns true if ELEM is a head, false otherwise. */
36 is_head (list_elem *elem)
38 return elem != NULL && elem->prev == NULL && elem->next != NULL;
41 /* Returns true if ELEM is an interior element,
44 is_interior (list_elem *elem)
46 return elem != NULL && elem->prev != NULL && elem->next != NULL;
49 /* Returns true if ELEM is a tail, false otherwise. */
51 is_tail (list_elem *elem)
53 return elem != NULL && elem->prev != NULL && elem->next == NULL;
56 /* Initializes LIST as an empty list. */
58 list_init (struct list *list)
60 ASSERT (list != NULL);
61 list->head.prev = NULL;
62 list->head.next = &list->tail;
63 list->tail.prev = &list->head;
64 list->tail.next = NULL;
67 /* Returns the beginning of LIST. */
69 list_begin (struct list *list)
71 ASSERT (list != NULL);
72 return list->head.next;
75 /* Returns the element after ELEM in its list. If ELEM is the
76 last element in its list, returns the list tail. Results are
77 undefined if ELEM is itself a list tail. */
79 list_next (list_elem *elem)
81 ASSERT (is_head (elem) || is_interior (elem));
85 /* Returns LIST's tail.
87 list_end() is often used in iterating through a list from
88 front to back. See the big comment at the top of list.h for
91 list_end (struct list *list)
93 ASSERT (list != NULL);
97 /* Returns the LIST's reverse beginning, for iterating through
98 LIST in reverse order, from back to front. */
100 list_rbegin (struct list *list)
102 ASSERT (list != NULL);
103 return list->tail.prev;
106 /* Returns the element before ELEM in its list. If ELEM is the
107 first element in its list, returns the list head. Results are
108 undefined if ELEM is itself a list head. */
110 list_prev (list_elem *elem)
112 ASSERT (is_interior (elem) || is_tail (elem));
116 /* Returns LIST's head.
118 list_rend() is often used in iterating through a list in
119 reverse order, from back to front. Here's typical usage,
120 following the example from the top of list.h:
122 for (e = list_rbegin (&foo_list); e != list_rend (&foo_list);
125 struct foo *f = list_entry (e, struct foo, elem);
126 ...do something with f...
130 list_rend (struct list *list)
132 ASSERT (list != NULL);
136 /* Return's LIST's head.
138 list_head() can be used for an alternate style of iterating
139 through a list, e.g.:
141 e = list_head (&list);
142 while ((e = list_next (e)) != list_end (&list))
148 list_head (struct list *list)
150 ASSERT (list != NULL);
154 /* Return's LIST's tail. */
156 list_tail (struct list *list)
158 ASSERT (list != NULL);
162 /* Inserts ELEM just before BEFORE, which may be either an
163 interior element or a tail. The latter case is equivalent to
166 list_insert (list_elem *before, list_elem *elem)
168 ASSERT (is_interior (before) || is_tail (before));
169 ASSERT (elem != NULL);
171 elem->prev = before->prev;
173 before->prev->next = elem;
177 /* Removes elements FIRST though LAST (exclusive) from their
178 current list, then inserts them just before BEFORE, which may
179 be either an interior element or a tail. */
181 list_splice (list_elem *before,
182 list_elem *first, list_elem *last)
184 ASSERT (is_interior (before) || is_tail (before));
187 last = list_prev (last);
189 ASSERT (is_interior (first));
190 ASSERT (is_interior (last));
192 /* Cleanly remove FIRST...LAST from its current list. */
193 first->prev->next = last->next;
194 last->next->prev = first->prev;
196 /* Splice FIRST...LAST into new list. */
197 first->prev = before->prev;
199 before->prev->next = first;
203 /* Inserts ELEM at the beginning of LIST, so that it becomes the
206 list_push_front (struct list *list, list_elem *elem)
208 list_insert (list_begin (list), elem);
211 /* Inserts ELEM at the end of LIST, so that it becomes the
214 list_push_back (struct list *list, list_elem *elem)
216 list_insert (list_end (list), elem);
219 /* Removes ELEM from its list and returns the element that
220 followed it. Undefined behavior if ELEM is not in a list. */
222 list_remove (list_elem *elem)
224 ASSERT (is_interior (elem));
225 elem->prev->next = elem->next;
226 elem->next->prev = elem->prev;
230 /* Removes the front element from LIST and returns it.
231 Undefined behavior if LIST is empty before removal. */
233 list_pop_front (struct list *list)
235 list_elem *front = list_front (list);
240 /* Removes the back element from LIST and returns it.
241 Undefined behavior if LIST is empty before removal. */
243 list_pop_back (struct list *list)
245 list_elem *back = list_back (list);
250 /* Returns the front element in LIST.
251 Undefined behavior if LIST is empty. */
253 list_front (struct list *list)
255 ASSERT (!list_empty (list));
256 return list->head.next;
259 /* Returns the back element in LIST.
260 Undefined behavior if LIST is empty. */
262 list_back (struct list *list)
264 ASSERT (!list_empty (list));
265 return list->tail.prev;
268 /* Returns the number of elements in LIST.
269 Runs in O(n) in the number of elements. */
271 list_size (struct list *list)
276 for (e = list_begin (list); e != list_end (list); e = list_next (e))
281 /* Returns true if LIST is empty, false otherwise. */
283 list_empty (struct list *list)
285 return list_begin (list) == list_end (list);
288 /* Swaps the `list_elem *'s that A and B point to. */
290 swap (list_elem **a, list_elem **b)
297 /* Reverses the order of LIST. */
299 list_reverse (struct list *list)
301 if (!list_empty (list))
305 for (e = list_begin (list); e != list_end (list); e = e->prev)
306 swap (&e->prev, &e->next);
307 swap (&list->head.next, &list->tail.prev);
308 swap (&list->head.next->prev, &list->tail.prev->next);
312 /* Merges lists AL and BL, which must each be sorted according to
313 LESS given auxiliary data AUX, by inserting each element of BL
314 at the proper place in AL to preserve the ordering.
315 Runs in O(n) in the combined length of AL and BL. */
317 list_merge (struct list *al, struct list *bl,
318 list_less_func *less, void *aux)
324 ASSERT (less != NULL);
327 while (a != list_end (al))
329 list_elem *b = list_begin (bl);
330 if (less (b, a, aux))
332 list_splice (a, b, list_next (b));
339 list_splice (list_end (al), list_begin (bl), list_end (bl));
342 /* Returns the middle element in LIST, that is, the N/2'th
343 element (rounding down) in a N-element list.
344 Given an empty list, returns the list tail. */
346 middle_of_list (struct list *list)
348 list_elem *middle, *last;
350 middle = last = list_begin (list);
351 while (last != list_end (list) && list_next (last) != list_end (list))
353 middle = list_next (middle);
354 last = list_next (list_next (last));
359 /* Sorts LIST according to LESS given auxiliary data AUX.
360 Runs in O(n lg n) time in the number of elements in LIST. */
362 list_sort (struct list *list,
363 list_less_func *less, void *aux)
365 /* Find the middle of the list. */
366 list_elem *middle = middle_of_list (list);
367 if (middle != list_begin (list))
369 /* Extract first half of LIST into a temporary list. */
372 list_splice (list_begin (&tmp), list_begin (list), middle);
374 /* Sort each half-list and merge the result. */
375 list_sort (&tmp, less, aux);
376 list_sort (list, less, aux);
377 list_merge (list, &tmp, less, aux);
381 /* The middle is at the beginning of the list.
382 This only happens in empty lists and 1-element lists.
383 Because such lists are already sorted, we have nothing
388 /* Inserts ELEM in the proper position in LIST, which must be
389 sorted according to LESS given auxiliary data AUX.
390 Runs in O(n) average case in the number of elements in LIST. */
392 list_insert_ordered (struct list *list, list_elem *elem,
393 list_less_func *less, void *aux)
397 ASSERT (list != NULL);
398 ASSERT (elem != NULL);
399 ASSERT (less != NULL);
401 for (e = list_begin (list); e != list_end (list); e = list_next (e))
402 if (less (elem, e, aux))
404 return list_insert (e, elem);
407 /* Iterates through LIST and removes all but the first in each
408 set of adjacent elements that are equal according to LESS
409 given auxiliary data AUX. If DUPLICATES is non-null, then the
410 elements from LIST are appended to DUPLICATES. */
412 list_unique (struct list *list, struct list *duplicates,
413 list_less_func *less, void *aux)
415 list_elem *elem, *next;
417 ASSERT (list != NULL);
418 ASSERT (less != NULL);
419 if (list_empty (list))
422 elem = list_begin (list);
423 while ((next = list_next (elem)) != list_end (list))
424 if (!less (elem, next, aux) && !less (next, elem, aux))
427 if (duplicates != NULL)
428 list_push_back (duplicates, next);
434 /* Returns the element in LIST with the largest value according
435 to LESS given auxiliary data AUX. If there is more than one
436 maximum, returns the one that appears earlier in the list. If
437 the list is empty, returns its tail. */
439 list_max (struct list *list, list_less_func *less, void *aux)
441 list_elem *max = list_begin (list);
442 if (max != list_end (list))
446 for (e = list_next (max); e != list_end (list); e = list_next (e))
447 if (less (max, e, aux))
453 /* Returns the element in LIST with the smallest value according
454 to LESS given auxiliary data AUX. If there is more than one
455 minimum, returns the one that appears earlier in the list. If
456 the list is empty, returns its tail. */
458 list_min (struct list *list, list_less_func *less, void *aux)
460 list_elem *min = list_begin (list);
461 if (min != list_end (list))
465 for (e = list_next (min); e != list_end (list); e = list_next (e))
466 if (less (e, min, aux))