00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "sf_sdlist.h"
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 int sf_sdlist_init(sfSDList *list, void (*destroy)(void *data))
00037 {
00038 list->destroy = destroy;
00039 list->size = 0;
00040 list->head = NULL;
00041 list->tail = NULL;
00042
00043 return 0;
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 int sf_sdlist_delete(sfSDList *list)
00055 {
00056 while(list->head != NULL)
00057 {
00058 sf_sdlist_remove_next(list, NULL);
00059 }
00060
00061 return 0;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 int sf_sdlist_insert_next(sfSDList *list, SDListItem *item, void *data,
00077 SDListItem *container)
00078 {
00079 SDListItem *new = container;
00080
00081 if(!new) return -1;
00082
00083 new->data = data;
00084
00085 if(item == NULL)
00086 {
00087
00088
00089 if(list->size == 0)
00090 {
00091 list->tail = new;
00092 }
00093
00094 new->next = list->head;
00095 list->head = new;
00096 }
00097 else
00098 {
00099 if(item->next == NULL)
00100 {
00101
00102 list->tail = new;
00103 }
00104
00105 new->next = item->next;
00106 item->next = new;
00107 }
00108
00109 new->prev = item;
00110 list->size++;
00111 return 0;
00112 }
00113
00114 int sf_sdlist_append(sfSDList *list, void *data, SDListItem *container) {
00115 return sf_sdlist_insert_next(list, list->tail, data, container);
00116 }
00117
00118 int sf_sdlist_remove_next(sfSDList *list, SDListItem *item) {
00119 SDListItem *li = NULL;
00120 void *data;
00121
00122 if(list->size == 0)
00123 {
00124 return -1;
00125 }
00126
00127
00128 if(item == NULL)
00129 {
00130 li = list->head;
00131 data = li->data;
00132 list->head = li->next;
00133 }
00134 else
00135 {
00136 data = item->data;
00137 if(item->next == NULL)
00138 {
00139 return -1;
00140 }
00141
00142 li = item->next;
00143 item->next = li->next;
00144 item->prev = li->prev;
00145 }
00146
00147 if(li->next != NULL)
00148 {
00149 li->next->prev = item;
00150 }
00151
00152
00153 if(list->destroy != NULL)
00154 list->destroy(data);
00155
00156 list->size--;
00157
00158 if(list->size == 0) {
00159 list->tail = NULL;
00160 }
00161
00162 return 0;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 int sf_sdlist_remove(sfSDList *list, SDListItem *item)
00177 {
00178 SDListItem *next_item;
00179 SDListItem *prev_item;
00180
00181 if(item == NULL)
00182 {
00183 return -1;
00184 }
00185
00186 next_item = item->next;
00187 prev_item = item->prev;
00188
00189 if(next_item != NULL)
00190 {
00191 next_item->prev = prev_item;
00192 } else {
00193 list->tail = prev_item;
00194 }
00195
00196 if(prev_item != NULL)
00197 {
00198 prev_item->next = next_item;
00199 } else {
00200
00201 list->head = next_item;
00202 }
00203
00204
00205
00206
00207 if(list->destroy != NULL)
00208 list->destroy(item->data);
00209
00210
00211 list->size--;
00212
00213 if(list->size == 0)
00214 {
00215 list->head = NULL;
00216 list->tail = NULL;
00217 }
00218
00219 return 0;
00220 }
00221
00222
00223 void print_sdlist(sfSDList *a)
00224 {
00225 SDListItem *li;
00226 printf("***");
00227 printf(" size: %d\n", a->size);
00228 for(li = a->head; li != NULL; li = li->next) {
00229 printf(" `- %p\n", li);
00230 }
00231 }
00232
00233 #ifdef TEST_SDLIST
00234 void bad(void *d) {
00235 free(d);
00236 return;
00237 }
00238
00239 int main(void) {
00240 sfSDList a;
00241
00242 SDListItem *li;
00243 SDListItem listpool[1000];
00244
00245 sf_sdlist_init(&a, &bad);
00246 if(sf_sdlist_append(&a, (char *) strdup("hello"), &listpool[0]))
00247 {
00248 printf("error appending!\n");
00249 }
00250
00251 sf_sdlist_append(&a, (char *) strdup("goodbye"), &listpool[1]);
00252
00253 sf_sdlist_insert_next(&a, NULL, (char *) strdup("woo"), &listpool[2]);
00254
00255 printf("list size %d\n", a.size);
00256
00257 for(li = a.head; li != NULL; li = li->next)
00258 {
00259 printf("%s\n", (char *) li->data);
00260 }
00261
00262
00263 printf("*** removing ***\n");
00264 sf_sdlist_remove(&a, &listpool[1]);
00265 printf("list size %d\n", a.size);
00266 for(li = a.head; li != NULL; li = li->next)
00267 {
00268 printf("%s\n", (char *) li->data);
00269 }
00270
00271 sf_sdlist_delete(&a);
00272
00273 printf("list size %d\n", a.size);
00274 return 0;
00275 }
00276 #endif