14 #define check_list(list) \
15 assert((list->len == 0 && !list->top && !list->bottom) || \
16 (list->len > 0 && list->top && list->bottom))
22 d_list_t *list = malloc(sizeof(d_list_t));
31 list_ref(d_list_t *list)
37 list_unref(d_list_t *list)
39 if (list && --list->ref == 0) {
41 list_delete_link(list, list->top);
47 list_prepend(d_list_t *list, void *data)
49 d_list_it_t *n = malloc(sizeof(d_list_it_t));
54 if (n->next) n->next->prev = n;
57 if (!list->bottom) list->bottom = n;
63 list_append(d_list_t *list, void *data)
65 d_list_it_t *n = malloc(sizeof(d_list_it_t));
68 n->prev = list->bottom;
70 if (n->prev) n->prev->next = n;
73 if (!list->top) list->top = n;
79 list_delete_link(d_list_t *list, d_list_it_t *pos)
81 d_list_it_t *prev = pos->prev;
82 d_list_it_t *next = pos->next;
89 assert(list->bottom == pos);
93 assert(list->top == pos);
103 list_move_before(d_list_t *list, d_list_it_t *move, d_list_it_t *before)
105 d_list_it_t *prev, *next;
107 /* these won't move it anywhere */
108 if (move == before || move->next == before) return;
113 /* remove it from the list */
114 if (next) next->prev = prev;
115 else list->bottom = prev;
116 if (prev) prev->next = next;
117 else list->top = next;
122 move->prev = before->prev;
123 move->next->prev = move;
124 if (move->prev) move->prev->next = move;
127 /* after the bottom */
128 move->prev = list->bottom;
130 if (move->prev) move->prev->next = move;
134 if (!move->prev) list->top = move;
138 list_remove(d_list_t *list, void *data)
140 d_list_it_t *it = list_find(list, data);
141 if (it) list_delete_link(list, it);
145 list_length(d_list_t *list)
151 list_top(d_list_t *list)
157 list_bottom(d_list_t *list)
163 list_nth(d_list_t *list, int n)
169 assert(n < list->len);
172 for (i = 0; i < n; ++i)
178 list_find(d_list_t *list, void *data)
182 for (it = list->top; it; it = it->next)
183 if (it->data == data) return it;