12 #define check_list(list) \
13 assert((list->len == 0 && !list->top && !list->bottom) || \
14 (list->len > 0 && list->top && list->bottom))
20 d_list_t *list = malloc(sizeof(d_list_t));
29 list_ref(d_list_t *list)
35 list_unref(d_list_t *list)
37 if (list && --list->ref == 0) {
39 list_delete_link(list, list->top);
45 list_prepend(d_list_t *list, void *data)
47 d_list_it_t *n = malloc(sizeof(d_list_it_t));
52 if (n->next) n->next->prev = n;
55 if (!list->bottom) list->bottom = n;
61 list_append(d_list_t *list, void *data)
63 d_list_it_t *n = malloc(sizeof(d_list_it_t));
66 n->prev = list->bottom;
68 if (n->prev) n->prev->next = n;
71 if (!list->top) list->top = n;
77 list_delete_link(d_list_t *list, d_list_it_t *pos)
79 d_list_it_t *prev = pos->prev;
80 d_list_it_t *next = pos->next;
87 assert(list->bottom == pos);
91 assert(list->top == pos);
101 list_move_before(d_list_t *list, d_list_it_t *move, d_list_it_t *before)
103 d_list_it_t *prev, *next;
105 /* these won't move it anywhere */
106 if (move == before || move->next == before) return;
111 /* remove it from the list */
112 if (next) next->prev = prev;
113 else list->bottom = prev;
114 if (prev) prev->next = next;
115 else list->top = next;
120 move->prev = before->prev;
121 move->next->prev = move;
122 if (move->prev) move->prev->next = move;
125 /* after the bottom */
126 move->prev = list->bottom;
128 if (move->prev) move->prev->next = move;
132 if (!move->prev) list->top = move;
136 list_remove(d_list_t *list, void *data)
138 d_list_it_t *it = list_find(list, data);
139 if (it) list_delete_link(list, it);
143 list_length(d_list_t *list)
149 list_top(d_list_t *list)
155 list_bottom(d_list_t *list)
161 list_nth(d_list_t *list, int n)
167 assert(n < list->len);
170 for (i = 0; i < n; ++i)
176 list_find(d_list_t *list, void *data)
180 for (it = list->top; it; it = it->next)
181 if (it->data == data) return it;