sllist.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef SL_LIST_H__
  2. # define SL_LIST_H__
  3. # define SL_LIST_BUFLEN 8
  4. struct sl_list_item {
  5. void *data[SL_LIST_BUFLEN];
  6. struct sl_list_item *next;
  7. };
  8. typedef struct {
  9. unsigned int count;
  10. struct sl_list_item *first;
  11. } sl_list;
  12. /**
  13. * Allocate a new list and return it
  14. * result should be unallocated with sllist_destroy()
  15. **/
  16. sl_list *sllist_create();
  17. /**
  18. * Free all ressources allocated by sllist_create
  19. **/
  20. void sllist_destroy(sl_list *list);
  21. /**
  22. * Get the numbers of items contained in sl_list
  23. **/
  24. unsigned int sllist_count(const sl_list * const list);
  25. /**
  26. * Add an item at the end of the list
  27. **/
  28. sl_list *sllist_pushback(sl_list *list, void *item);
  29. /**
  30. * Remove the last item of the list and return it
  31. **/
  32. void *sllist_popback(sl_list *list);
  33. /**
  34. * Get the item at position %pos
  35. **/
  36. void *sllist_at(const sl_list * const list, unsigned int pos);
  37. /**
  38. * Remove the item at position %pos, and return it
  39. * TODO
  40. **/
  41. void *sllist_removeat(sl_list *list, unsigned int pos);
  42. /**
  43. * Remove all elements
  44. **/
  45. sl_list *sllist_clear(sl_list *list);
  46. /**
  47. * Return index of first item, using compare fnc
  48. * Return -1 on failure
  49. * TODO
  50. **/
  51. int sllist_find(sl_list *list, int(*fnc)(const void *cmp, const void *param), const void *param);
  52. /**
  53. * Execute function foreach values of list
  54. * Param 2 is custom_data
  55. * A -1 return value will act as a `break` instruction
  56. * return the number of iterated objects
  57. **/
  58. int sllist_foreach(sl_list *list, int(*fnc)(void **item, void *custom), void *custom);
  59. /**
  60. * Find and remove item(s)
  61. * return nb of items successfully removed
  62. **/
  63. int sllist_remove(sl_list *list, int(*fnc)(const void *a, const void *b), void *item);
  64. /**
  65. * Find and remove item(s), maximum n items will be removed
  66. * return nb of items successfully removed
  67. **/
  68. int sllist_n_remove(sl_list *list, int(*fnc)(const void *a, const void *b), void *item, unsigned int n);
  69. /**
  70. * Comp functions
  71. * usable with sllist_find
  72. **/
  73. int sllist_cmp_int(const void *a, const void *b);
  74. int sllist_cmp_string(const void *a, const void *b);
  75. #endif /* SL_LIST_H__ */