-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdlist.c
143 lines (119 loc) · 2.74 KB
/
dlist.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
#include "dlist.h"
#include <stdlib.h>
#include <assert.h>
struct dlist *dlist_create(void) /* {{{ */
{
struct dlist *dl = malloc(sizeof(struct dlist));
assert(dl);
dl->head = NULL;
dl->tail = NULL;
dl->count = 0;
return dl;
} /* }}} */
void dlist_destroy(struct dlist *dl, dlist_data_func cb) /* {{{ */
{
while(!dlist_empty(dl)) {
void *data = dlist_pop_left(dl);
if(cb) cb(data);
}
free(dl);
} /* }}} */
void *dlist_pop_left(struct dlist *dl) /* {{{ */
{
void *data;
struct dnode *node;
if(dlist_empty(dl)) return NULL;
node = dl->head;
dl->head = node->next;
if(dl->head == NULL) dl->tail = NULL;
if(node->next) node->next->prev = NULL;
data = node->data;
free(node);
dl->count--;
assert(dl->count >= 0);
return data;
} /* }}} */
void *dlist_pop_right(struct dlist *dl) /* {{{ */
{
void *data;
struct dnode *node;
if(dlist_empty(dl)) return NULL;
node = dl->tail;
dl->tail = node->prev;
if(dl->tail == NULL) dl->head = NULL;
if(node->prev) node->prev->next = NULL;
data = node->data;
free(node);
dl->count--;
assert(dl->count >= 0);
return data;
} /* }}} */
void *dlist_push_right(struct dlist *dl, void *data) /* {{{ */
{
struct dnode *node = malloc(sizeof(struct dnode));
assert(node);
node->data = data;
node->prev = dl->tail;
node->next = NULL;
if(dl->tail) dl->tail->next = node;
dl->tail = node;
if(dl->head == NULL) dl->head = node;
dl->count++;
return data;
} /* }}} */
void *dlist_find_remove(struct dlist *dl, void *data, /* {{{ */
dlist_cmp_func cmp, void *user_data)
{
struct dnode *curr;
for(curr = dl->head; curr; curr = curr->next) {
if(!curr->data) continue;
if(cmp(curr->data, data, user_data)) continue;
void *ptr = curr->data;
if(dl->head == curr) dl->head = curr->next;
if(dl->tail == curr) dl->tail = curr->prev;
if(curr->prev) curr->prev->next = curr->next;
if(curr->next) curr->next->prev = curr->prev;
dl->count--;
free(curr);
return ptr;
}
return NULL;
} /* }}} */
int dlist_empty(struct dlist *dl) /* {{{ */
{
if(dl->head == NULL) {
assert(dl->tail == NULL);
assert(dl->count == 0);
return 1;
} else {
assert(dl->tail != NULL);
assert(dl->count > 0);
return 0;
}
} /* }}} */
void * dlist_get_index(const struct dlist *dl, int idx) /* {{{ */
{
struct dnode *curr;
if(idx >= 0) {
curr = dl->head;
while(curr && idx--) curr = curr->next;
} else {
curr = dl->tail;
while(curr && ++idx) curr = curr->prev;
}
if(!curr) return NULL;
return curr->data;
} /* }}} */
void dlist_set_index(struct dlist *dl, int idx, void *data) /* {{{ */
{
struct dnode *curr;
if(idx >= 0) {
curr = dl->head;
while(curr && idx--) curr = curr->next;
} else {
curr = dl->tail;
while(curr && ++idx) curr = curr->prev;
}
if(!curr) return;
curr->data = data;
} /* }}} */