| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include <stdlib.h>
- #include "sllist.h"
- static inline void swap_items(sl_list *list, struct sl_list_item *pos, int dataindex)
- {
- struct sl_list_item *i = list->first;
- int _i, size;
- void *tmp;
- for (_i =0, size = sllist_count(list); _i + SL_LIST_BUFLEN < size && i; _i += SL_LIST_BUFLEN)
- i = i->next;
- tmp = pos->data[dataindex];
- pos->data[dataindex] = i->data[size - _i];
- i->data[size - _i] = tmp;
- }
- int sllist_remove(sl_list *list, int (*fnc)(const void *a, const void *b), void *item)
- {
- return sllist_n_remove(list, fnc, item, sllist_count(list));
- }
- int sllist_n_remove(sl_list *list, int (*fnc)(const void *, const void *), void *item, unsigned int n)
- {
- int i, j, size, result;
- struct sl_list_item *item_ptr;
- for (i =result =0, size = sllist_count(list), item_ptr = list->first; i < size && item_ptr; i += SL_LIST_BUFLEN)
- {
- for (j =0; i+j < size && j < SL_LIST_BUFLEN; ++j)
- {
- if (!fnc(item_ptr->data[j], item))
- continue;
- if (i+j < size -1)
- {
- swap_items(list, item_ptr, j);
- --j;
- }
- --list->count;
- ++result;
- if (!--n)
- return result;
- }
- item_ptr = item_ptr->next;
- }
- return result;
- }
|