list.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* This file is part of nit.
  2. *
  3. * Nit is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * Nit is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with nit. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef LIST_H
  17. #define LIST_H
  18. #include <stdlib.h>
  19. #include "err.h"
  20. #include "dispose.h"
  21. #define list_foreach(entry, list) \
  22. for ((entry) = (list)->head; (entry); \
  23. (entry) = (entry)->next)
  24. typedef struct List_entry List_entry;
  25. struct List_entry {
  26. void *data;
  27. List_entry *next;
  28. };
  29. typedef struct List {
  30. Disposer *disposer;
  31. size_t len;
  32. List_entry *head;
  33. List_entry *tail;
  34. } List;
  35. void
  36. list_init(List *list, Disposer *disposer);
  37. void
  38. list_dispose(List *list);
  39. Nit_error
  40. list_add(List *list, List_entry *prev, const void *data);
  41. Nit_error
  42. list_remove(List *list, List_entry *prev, void **data);
  43. void **
  44. list_peek(List *list);
  45. #endif