remove.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <stdlib.h>
  2. #include "sllist.h"
  3. static inline void swap_items(sl_list *list, struct sl_list_item *pos, int dataindex)
  4. {
  5. struct sl_list_item *i = list->first;
  6. int _i, size;
  7. void *tmp;
  8. for (_i =0, size = sllist_count(list); _i + SL_LIST_BUFLEN < size && i; _i += SL_LIST_BUFLEN)
  9. i = i->next;
  10. tmp = pos->data[dataindex];
  11. pos->data[dataindex] = i->data[size - _i];
  12. i->data[size - _i] = tmp;
  13. }
  14. int sllist_remove(sl_list *list, int (*fnc)(const void *a, const void *b), void *item)
  15. {
  16. return sllist_n_remove(list, fnc, item, sllist_count(list));
  17. }
  18. int sllist_n_remove(sl_list *list, int (*fnc)(const void *, const void *), void *item, unsigned int n)
  19. {
  20. int i, j, size, result;
  21. struct sl_list_item *item_ptr;
  22. for (i =result =0, size = sllist_count(list), item_ptr = list->first; i < size && item_ptr; i += SL_LIST_BUFLEN)
  23. {
  24. for (j =0; i+j < size && j < SL_LIST_BUFLEN; ++j)
  25. {
  26. if (!fnc(item_ptr->data[j], item))
  27. continue;
  28. if (i+j < size -1)
  29. {
  30. swap_items(list, item_ptr, j);
  31. --j;
  32. }
  33. --list->count;
  34. ++result;
  35. if (!--n)
  36. return result;
  37. }
  38. item_ptr = item_ptr->next;
  39. }
  40. return result;
  41. }