collections.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * collections.h
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #ifndef _COLLECTIONS_H
  10. #define _COLLECTIONS_H
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. /* macros */
  15. #define LNEXT(item) ((item)->next)
  16. #define LPREV(item) ((item)->prev)
  17. #define LDATA(item) ((item)->data)
  18. #define SET_DATA(item, _data) ((item)->data = (_data))
  19. #define SET_NEXT(item, _next) ((item)->next = (_next))
  20. #define SET_PREV(item, _prev) ((item)->prev = (_prev))
  21. #ifndef _ALLOC_FUNCTION
  22. # define _ALLOC_FUNCTION malloc
  23. #endif
  24. #ifndef _FREE_FUNCTION
  25. # define _FREE_FUNCTION free
  26. #endif
  27. /* a list element */
  28. typedef struct t_list
  29. {
  30. void *data;
  31. struct t_list *next;
  32. struct t_list *prev;
  33. }t_list;
  34. /* add an element `data' to the list `list' at position `pos'. If pos is negative
  35. * , or is larger than the number of elements in the list, the new element is
  36. * added on to the end of the list. Function `addElement' returns a pointer
  37. * to the new head of the list */
  38. extern t_list * addElement(t_list *list, void * data, int pos);
  39. /* add sorted */
  40. extern t_list * addSorted(t_list *list, void * data
  41. , int (*compareFunc)(void *a, void *b));
  42. /* add an element to the end of the list */
  43. extern t_list * addLast(t_list *list, void * data);
  44. /* add an element at the beginning of the list */
  45. extern t_list * addFirst(t_list *list, void * data);
  46. /* remove an element at the beginning of the list */
  47. extern t_list * removeFirst(t_list *list);
  48. /* remove an element from the list */
  49. extern t_list * removeElement(t_list *list, void * data);
  50. /* remove a link from the list `list' */
  51. extern t_list * removeElementLink(t_list *list, t_list *element);
  52. /* find an element inside the list `list'. The current implementation calls the
  53. * CustomfindElement' passing a NULL reference as `func' */
  54. extern t_list * findElement(t_list *list, void *data);
  55. /* find an element inside the list `list'. */
  56. extern t_list * CustomfindElement(t_list *list, void *data
  57. , int (*compareFunc)(void *a, void *b));
  58. /* find the position of an `element' inside the `list'. -1 if not found */
  59. extern int getPosition(t_list *list, t_list *element);
  60. /* find the length of `list' */
  61. extern int getLength(t_list *list);
  62. /* remove all the elements of a list */
  63. extern void freeList(t_list *list);
  64. /* get the last element of the list. Returns NULL if the list is empty
  65. * or list is a NULL pointer */
  66. extern t_list * getLastElement(t_list *list);
  67. /* retrieve the list element at position `position' inside the `list'.
  68. * Returns NULL if: the list is empty, the list is a NULL pointer or
  69. * the list holds less than `position' elements. */
  70. extern t_list * getElementAt(t_list *list, unsigned int position);
  71. /* create a new list with the same elements */
  72. extern t_list * cloneList(t_list *list);
  73. /* add a list of elements to another list */
  74. extern t_list * addList(t_list *list, t_list *elements);
  75. /* add a list of elements to a set */
  76. extern t_list * addListToSet(t_list *list, t_list *elements
  77. , int (*compareFunc)(void *a, void *b), int *modified);
  78. #endif