6 #define __DEFAULT_BLOCK_CAPACITY 128
25 if(!(link = (link_t)allocator_alloc(allocator)))
28 link->next = link->prev = NULL;
35 link_free(link_t* ref)
40 allocator_dealloc(allocator, *ref);
47 search(list_t list, void* item)
49 register link_t cur = list->next;
51 while(cur != ((link_t)&(list->item)))
66 list_new(fn_finalize_t fn_finalize)
68 list_t list = (list_t)calloc(1,sizeof(s_list_t));
75 if(!(allocator = allocator_new(__DEFAULT_BLOCK_CAPACITY, sizeof(s_link_t), NULL)))
82 list->next = list->prev = list->cur = (link_t)(&(list->item));
83 list->fn_finalize = fn_finalize;
93 list_rewind(list_t list)
107 list->cur = list->next;
114 list_unwind(list_t list)
128 list->cur = list->prev;
129 list->pos = list->size - 1;
135 list_clear(list_t list)
145 if(list->fn_finalize)
149 while((item = list_pop_back(list)))
150 if(0 != (rv = (*(list->fn_finalize))(&item)))
154 while(list_pop_back(list));
160 list_free(list_t* list_ptr)
165 if((*list_ptr)->size)
166 list_clear(*list_ptr);
169 allocator_free(&allocator);
178 list_push_front(list_t list, void* item)
180 link_t what, where, next;
185 if(!(what = (link_t)link_new(item)))
188 where = (link_t)(&(list->item));
198 /* the iteration functions are now not permited */
200 list->cur = (link_t)(&(list->item));
206 list_push_back(list_t list, void* item)
208 link_t what, where, prev;
213 if(!(what = (link_t)link_new(item)))
216 where = (link_t)(&(list->item));
225 /* the iteration functions are now not permited */
227 list->cur = (link_t)(&(list->item));
233 list_insert_after(list_t list, void* what, void* where)
235 link_t __what, __where, __next;
237 if(!list || !what || !where)
240 if(!(__what = link_new(what)))
243 if((__where = search(list, where)))
246 __next = __where->next;
248 __what->prev = __where;
249 __what->next = __next;
250 __next->prev = __what;
251 __where->next = __what;
259 list_insert_before(list_t list, void* what, void* where)
261 link_t __what, __where, __prev;
263 if(!list || !what || !where)
266 if(!(__what = link_new(what)))
269 if((__where = search(list, where)))
272 __prev = __where->prev;
274 __what->next = __where;
275 __what->prev = __prev;
276 __prev->next = __what;
277 __where->prev = __what;
286 list_pop_back(list_t list)
288 link_t link, next, prev;
314 link_free((link_t*)&link);
316 /* the iteration functions are now not permited */
318 list->cur = (link_t)(&(list->item));
324 list_pop_front(list_t list)
327 link_t link, next, prev;
352 link_free((link_t*)&link);
354 /* the iteration functions are now not permited */
356 list->cur = (link_t)(&(list->item));
362 list_remove(list_t list, void* item)
364 link_t link, next, prev;
372 if(!(link = search(list, item)))
382 /* the iteration functions are now not permited */
384 list->cur = (link_t)(&(list->item));
386 return link_free((link_t*)&link);
390 list_get_size(list_t list)
402 list_contains(list_t list, void* item)
420 while(cur != ((link_t)&(list->item)))
422 if(cur->item == item)
432 list_is_empty(list_t list)
444 list_is_autodelete(list_t list)
452 return NULL != list->fn_finalize;
456 list_move_next(list_t list)
464 if(!list->size || (-1 == list->pos))
470 if(list->cur != (link_t)(&(list->item)))
472 list->cur = list->cur->next;
483 list_get(list_t list)
491 if(!list->size || (-1 == list->pos))
497 return list->cur->item;
501 list_set(list_t list, void* item)
511 if(!list->size || (-1 == list->pos))
517 prev_item = list->cur->item;
518 list->cur->item = item;
524 list_get_at(list_t list, int pos)
534 if(pos < 0 || pos >= list->size)
556 list_set_at(list_t list, int pos, void* item)
567 if(pos < 0 || pos >= list->size)
584 prev_item = cur->item;
592 list_move_prev(list_t list)
600 if(!list->size || (-1 == list->pos))
606 if(list->cur != (link_t)(&(list->item)))
608 list->cur = list->cur->prev;
619 seek_set(list_t list, int offset)
621 if(offset > list->size)
627 list_move_next(list);
633 seek_end(list_t list, int offset)
635 if(offset > list->size)
641 list_move_prev(list);
648 seek_cur(list_t list, int offset)
650 if(list->cur == list->next)
652 /* we are at the begin of the list */
653 seek_set(list, offset);
655 else if(list->cur == list->prev)
657 /* we are at the end of the list */
658 seek_end(list, offset);
662 if(offset > (list->size - list->pos + 1))
666 list_move_next(list);
674 list_seek(list_t list, int offset, int whence)
685 return seek_set(list, offset);
688 return seek_cur(list, offset);
691 return seek_end(list, offset);
699 list_tell(list_t list)
707 if(!list->size || (-1 == list->pos))
717 list_getpos(list_t list, int* pos)
726 if(!list->size || (-1 == list->pos))
737 list_setpos(list_t list, int pos)
745 if(pos < 0 || pos >= list->size)
757 list->cur = list->next;
761 list->cur = list->cur->next;
768 list_get_front(list_t list)
782 return list->next->item;
787 list_get_back(list_t list)
801 return list->prev->item;