list.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // list.h
  3. //
  4. // Copyright (c) 2010 TJ Holowaychuk <tj@vision-media.ca>
  5. //
  6. #ifndef LIST_H
  7. #define LIST_H
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. // Library version
  14. #define LIST_VERSION "0.0.5"
  15. // Memory management macros
  16. #ifndef LIST_MALLOC
  17. #define LIST_MALLOC malloc
  18. #endif
  19. #ifndef LIST_FREE
  20. #define LIST_FREE free
  21. #endif
  22. /*
  23. * list_t iterator direction.
  24. */
  25. typedef enum {
  26. LIST_HEAD
  27. , LIST_TAIL
  28. } list_direction_t;
  29. /*
  30. * list_t node struct.
  31. */
  32. typedef struct list_node {
  33. struct list_node *prev;
  34. struct list_node *next;
  35. void *val;
  36. } list_node_t;
  37. /*
  38. * list_t struct.
  39. */
  40. typedef struct {
  41. list_node_t *head;
  42. list_node_t *tail;
  43. unsigned int len;
  44. void (*free)(void *val);
  45. int (*match)(void *a, void *b);
  46. } list_t;
  47. /*
  48. * list_t iterator struct.
  49. */
  50. typedef struct {
  51. list_node_t *next;
  52. list_direction_t direction;
  53. } list_iterator_t;
  54. // Node prototypes.
  55. list_node_t *
  56. list_node_new(void *val);
  57. // list_t prototypes.
  58. list_t *
  59. list_new();
  60. list_node_t *
  61. list_rpush(list_t *self, list_node_t *node);
  62. list_node_t *
  63. list_lpush(list_t *self, list_node_t *node);
  64. list_node_t *
  65. list_find(list_t *self, void *val);
  66. list_node_t *
  67. list_at(list_t *self, int index);
  68. list_node_t *
  69. list_rpop(list_t *self);
  70. list_node_t *
  71. list_lpop(list_t *self);
  72. void
  73. list_remove(list_t *self, list_node_t *node);
  74. void
  75. list_destroy(list_t *self);
  76. // list_t iterator prototypes.
  77. list_iterator_t *
  78. list_iterator_new(list_t *list, list_direction_t direction);
  79. list_iterator_t *
  80. list_iterator_new_from_node(list_node_t *node, list_direction_t direction);
  81. list_node_t *
  82. list_iterator_next(list_iterator_t *self);
  83. void
  84. list_iterator_destroy(list_iterator_t *self);
  85. extern int list_len( list_t* self );
  86. extern void list_clear(list_t *self);
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. #endif /* LIST_H */