dlist.c 5.46 KB
Newer Older
Lindsay Knupp's avatar
Lindsay Knupp committed
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
/*
 * Copyright (c) 2012 Bucknell University
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: L. Felipe Perrone (perrone@bucknell.edu)
 */

#include <stdlib.h>
#include <stdio.h>

#include "dnode.h"
#include "dlist.h"

struct dlist *
dlist_create() {
	return calloc(1, sizeof(struct dlist));
}

void 
dlist_destroy(struct dlist *l) {
	struct dnode *p = l->front;

	do {
		l->front = l->front->next;
		free(p->data);
		free(p);
		p = l->front;
	} while (l->front != NULL);

	l->front = l->back = NULL;
	l->counter = 0;
}


void 
dlist_obliterate(struct dlist *l) {
	dlist_destroy(l);
	free(l);
}

void 
Lindsay Knupp's avatar
Lindsay Knupp committed
54
dlist_add_front(struct dlist *l, void *ptr, size_t size) {
Lindsay Knupp's avatar
Lindsay Knupp committed
55
	struct dnode *n = dnode_create();
Lindsay Knupp's avatar
Lindsay Knupp committed
56
57
	n->data = ptr;	
	n->size = size;
Lindsay Knupp's avatar
Lindsay Knupp committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

	if (0 == l->counter) {
		l->front = l->back = n;	
		l->counter = 1;
	} else {
		n->next = l->front;
		l->front->prev = n;
		l->front = n;
		(l->counter)++;
	}

#ifdef DEBUG
	printf("counter= %d, %s\n", l->counter, (char *) ptr);
	printf("front= %s\n", (char *) l->front->data);
	printf("back= %s\n\n", (char *) l->back->data);
#endif /* DEBUG */
}

void 
Lindsay Knupp's avatar
Lindsay Knupp committed
77
dlist_add_back(struct dlist *l, void *ptr,size_t size) {
Lindsay Knupp's avatar
Lindsay Knupp committed
78
79
	struct dnode *n = dnode_create();
	n->data = ptr;
Lindsay Knupp's avatar
Lindsay Knupp committed
80
	n->size = size;
Lindsay Knupp's avatar
Lindsay Knupp committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

	if (0 == l->counter) {
		l->front = l->back = n;	
		l->counter = 1;
	} else {
		n->prev = l->back;
		l->back->next = n;
		l->back = n;
		(l->counter)++;
	}

#ifdef DEBUG
	printf("counter= %d, %s\n", l->counter, (char *) ptr);
	printf("front= %s\n", (char *) l->front->data);
	printf("back= %s\n\n", (char *) l->back->data);
#endif /* DEBUG */
}

Lindsay Knupp's avatar
Lindsay Knupp committed
99
struct dnode *
Lindsay Knupp's avatar
Lindsay Knupp committed
100
101
dlist_remove_front(struct dlist *l) {
	struct dnode *n = l->front;
Lindsay Knupp's avatar
Lindsay Knupp committed
102
103
104
//	void* ptr = n->data;

	struct dnode* ptr = n;
Lindsay Knupp's avatar
Lindsay Knupp committed
105
106
107
108
109
110
111
112
113
114
115
116
117

	if (1 == l->counter) {
		l->front = l->back = NULL;
	} else {
		l->front = l->front->next;
		l->front->prev = NULL;
	}

	(l->counter)--;
	free(n);
	return ptr;
}

Lindsay Knupp's avatar
Lindsay Knupp committed
118
struct dnode *
Lindsay Knupp's avatar
Lindsay Knupp committed
119
120
dlist_remove_back(struct dlist *l) {
	struct dnode *n = l->back;
Lindsay Knupp's avatar
Lindsay Knupp committed
121
122
123
	//void *ptr = n->data;

	struct dnode *ptr = n;
Lindsay Knupp's avatar
Lindsay Knupp committed
124
125
126
127
128
129
130
131
132
133
134
135
136

	if (1 == l->counter) {
		l->front = l->back = NULL;
	} else {
		l->back = l->back->prev;
		l->back->next = NULL;
	}

	(l->counter)--;
	free(n);
	return ptr;
}

Lindsay Knupp's avatar
Lindsay Knupp committed
137
struct dnode *
Lindsay Knupp's avatar
Lindsay Knupp committed
138
139
dlist_find_remove(struct dlist *l, void *ptr) {
	struct dnode *n = l->front;
Lindsay Knupp's avatar
Lindsay Knupp committed
140

Lindsay Knupp's avatar
Lindsay Knupp committed
141
142
	//void *ret_ptr = NULL;

Lindsay Knupp's avatar
Lindsay Knupp committed
143
144
	//printf("pointer value %p\n",ptr);

Lindsay Knupp's avatar
Lindsay Knupp committed
145
	struct dnode *ret_p = NULL;
Lindsay Knupp's avatar
Lindsay Knupp committed
146
147

	while ((n != NULL) && (n->data != ptr)) {
Lindsay Knupp's avatar
Lindsay Knupp committed
148
149
	    	//printf("n->data %p\n",n->data);
		n = n->next;
Lindsay Knupp's avatar
Lindsay Knupp committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
	}

	if (n != NULL) {
	  if (l->front == n) {
	    return dlist_remove_front(l);
	  } else if (l->back == n) {
	    return dlist_remove_back(l);
	  } else {
	    if (1 == l->counter) {
	      l->front = l->back = NULL;
	    } else {
	      n->prev->next = n->next;
	      n->next->prev = n->prev;
	    }
	    (l->counter)--;
	  }
	  
Lindsay Knupp's avatar
Lindsay Knupp committed
167
168
 	  ret_p = n;	
	  //ret_ptr = n->data;
Lindsay Knupp's avatar
Lindsay Knupp committed
169
170
171
	  free(n);
	}
	
Lindsay Knupp's avatar
Lindsay Knupp committed
172
	return ret_p;
Lindsay Knupp's avatar
Lindsay Knupp committed
173
174
175
176
177
178
179
}

uint32_t 
dlist_num_elems(struct dlist *l) {
	return l->counter;
}

Lindsay Knupp's avatar
Lindsay Knupp committed
180
struct dnode * 
Lindsay Knupp's avatar
Lindsay Knupp committed
181
dlist_iter_begin(struct dlist *l) {
Lindsay Knupp's avatar
Lindsay Knupp committed
182
183
184
185
186
187
188
189
//	void *ret_val = NULL;
//
//	l->iter = l->front;
//	if (l->iter != NULL) {
//		ret_val = l->iter->data; 
//	}
//	return ret_val;

Lindsay Knupp's avatar
Lindsay Knupp committed
190

Lindsay Knupp's avatar
Lindsay Knupp committed
191
	struct dnode *ret_p = NULL;
Lindsay Knupp's avatar
Lindsay Knupp committed
192
	l->iter = l->front;
Lindsay Knupp's avatar
Lindsay Knupp committed
193
194
	if(l->iter != NULL){
		ret_p = l->iter;
Lindsay Knupp's avatar
Lindsay Knupp committed
195
196
	}

Lindsay Knupp's avatar
Lindsay Knupp committed
197
	return ret_p;
Lindsay Knupp's avatar
Lindsay Knupp committed
198
199
}

Lindsay Knupp's avatar
Lindsay Knupp committed
200
struct dnode *
Lindsay Knupp's avatar
Lindsay Knupp committed
201
dlist_iter_next(struct dlist *l) {
Lindsay Knupp's avatar
Lindsay Knupp committed
202
//	void *ret_val = NULL;
Lindsay Knupp's avatar
Lindsay Knupp committed
203

Lindsay Knupp's avatar
Lindsay Knupp committed
204
205
206
207
208
209
210
211
212
213
214
215
//	if (l->iter != NULL) {
//		l->iter = l->iter->next;
//		if (l->iter != NULL) {
//			ret_val = l->iter->data;
//		}
//	}

//	return ret_val;

	struct dnode *ret_p = NULL;

	if(l->iter != NULL) {
Lindsay Knupp's avatar
Lindsay Knupp committed
216
217
		l->iter = l->iter->next;
		if (l->iter != NULL) {
Lindsay Knupp's avatar
Lindsay Knupp committed
218
			ret_p = l->iter;
Lindsay Knupp's avatar
Lindsay Knupp committed
219
		}
Lindsay Knupp's avatar
Lindsay Knupp committed
220
	
Lindsay Knupp's avatar
Lindsay Knupp committed
221
	}
Lindsay Knupp's avatar
Lindsay Knupp committed
222
	return ret_p;
Lindsay Knupp's avatar
Lindsay Knupp committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
}

bool 
dlist_iter_has_next(struct dlist *l) {
	bool ret_val = false;

	if (l->iter != NULL) {
		ret_val = (l->iter->next != NULL);
	}

#ifdef DEBUG
	if (ret_val) {
		printf("dlist_has_next: current is %s\n", (char *) l->iter->data);
		printf("dlist_has_next: returning %d\n\n", ret_val);
	}
#endif /* DEBUG */

	return ret_val;
}


Lindsay Knupp's avatar
Lindsay Knupp committed
244
struct dnode *
Lindsay Knupp's avatar
Lindsay Knupp committed
245
dlist_iter_end(struct dlist *l) {
Lindsay Knupp's avatar
Lindsay Knupp committed
246
//	void *ret_val = NULL;
Lindsay Knupp's avatar
Lindsay Knupp committed
247

Lindsay Knupp's avatar
Lindsay Knupp committed
248
249
250
251
252
253
254
255
//	l->iter = l->back;
//	if (l->iter != NULL) {
//		ret_val = l->iter->data;
//	}
//
//	return ret_val;

	struct dnode *ret_p = NULL;
Lindsay Knupp's avatar
Lindsay Knupp committed
256
	l->iter = l->back;
Lindsay Knupp's avatar
Lindsay Knupp committed
257
258
259
	if (l->iter != NULL){
		ret_p = l->iter;

Lindsay Knupp's avatar
Lindsay Knupp committed
260
261
	}

Lindsay Knupp's avatar
Lindsay Knupp committed
262
	return ret_p;
Lindsay Knupp's avatar
Lindsay Knupp committed
263
264
}

Lindsay Knupp's avatar
Lindsay Knupp committed
265
struct dnode *
Lindsay Knupp's avatar
Lindsay Knupp committed
266
dlist_iter_prev(struct dlist *l) {
Lindsay Knupp's avatar
Lindsay Knupp committed
267
//	void *ret_val = NULL;
Lindsay Knupp's avatar
Lindsay Knupp committed
268

Lindsay Knupp's avatar
Lindsay Knupp committed
269
270
271
272
273
274
275
276
277
278
279
280
//	if (l->iter != NULL) {
//		l->iter = l->iter->prev;
//		if (l->iter != NULL) {
//			ret_val = l->iter->data;
//		}
//	}

//	return ret_val;

	struct dnode *ret_p = NULL;
	
	if (l->iter != NULL){
Lindsay Knupp's avatar
Lindsay Knupp committed
281
		l->iter = l->iter->prev;
Lindsay Knupp's avatar
Lindsay Knupp committed
282
283
		if (l->iter != NULL){
			ret_p = l->iter;
Lindsay Knupp's avatar
Lindsay Knupp committed
284
285
286
		}
	}

Lindsay Knupp's avatar
Lindsay Knupp committed
287
	return ret_p;
Lindsay Knupp's avatar
Lindsay Knupp committed
288
289
290
291
292
293
294
295
296
297
298
299
300
}

bool 
dlist_iter_has_prev(struct dlist *l) {
	bool ret_val = false;

	if (l->iter != NULL) {
		ret_val = (l->iter->prev != NULL);
	}

	return ret_val;
}

Lindsay Knupp's avatar
Lindsay Knupp committed
301
302
void dlist_print(struct dlist *l){
	printf("dlist: -");
Lindsay Knupp's avatar
Lindsay Knupp committed
303

Lindsay Knupp's avatar
Lindsay Knupp committed
304
305
306
307
//	char *data; 

	struct dnode *data;

Lindsay Knupp's avatar
Lindsay Knupp committed
308
	for (data = dlist_iter_begin(l); data != NULL;
Lindsay Knupp's avatar
Lindsay Knupp committed
309
	     data = dlist_iter_next(l)) {
Lindsay Knupp's avatar
Lindsay Knupp committed
310
		printf("[%zu,%p]-- ",data->size,data->data);
Lindsay Knupp's avatar
Lindsay Knupp committed
311
312
313
314
	}
	printf("\n");	

}