list.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /** @file
  17. * @brief Listes doublement chainées automatiques
  18. */
  19. /** @remarks list how-to
  20. * *********************************************************
  21. * LIST_TYPE(FOO, contenu);
  22. * - déclare les types suivants
  23. * + pour les cellules : FOO_t
  24. * + pour les listes : FOO_list_t
  25. * + pour les itérateurs : FOO_itor_t
  26. * - déclare les accesseurs suivants :
  27. * * création d'une cellule
  28. * FOO_t FOO_new(void);
  29. * * suppression d'une cellule
  30. * void FOO_delete(FOO_t);
  31. * * création d'une liste (vide)
  32. * FOO_list_t FOO_list_new(void);
  33. * * suppression d'une liste
  34. * void FOO_list_delete(FOO_list_t);
  35. * * teste si une liste est vide
  36. * int FOO_list_empty(FOO_list_t);
  37. * * retire un élément de la liste
  38. * void FOO_list_erase(FOO_list_t, FOO_t);
  39. * * ajoute une élément en queue de liste
  40. * void FOO_list_push_back(FOO_list_t, FOO_t);
  41. * * ajoute un élément en tête de list
  42. * void FOO_list_push_front(FOO_list_t, FOO_t);
  43. * * retire l'élément en queue de liste
  44. * FOO_t FOO_list_pop_back(FOO_list_t);
  45. * * retire l'élement en tête de liste
  46. * FOO_t FOO_list_pop_front(FOO_list_t);
  47. * * retourne l'élément en queue de liste
  48. * FOO_t FOO_list_back(FOO_list_t);
  49. * * retourne l'élement en tête de liste
  50. * FOO_t FOO_list_front(FOO_list_t);
  51. * * vérifie si la liste chainée est cohérente
  52. * int FOO_list_check(FOO_list_t);
  53. * *********************************************************
  54. * Exemples d'utilisation :
  55. * - au départ, on a :
  56. * struct ma_structure_s
  57. * {
  58. * int a;
  59. * int b;
  60. * };
  61. * - on veut en faire une liste. On remplace la déclaration par :
  62. * LIST_TYPE(ma_structure,
  63. * int a;
  64. * int b;
  65. * );
  66. * qui crée les types ma_structure_t et ma_structure_list_t.
  67. * - allocation d'une liste vide :
  68. * ma_structure_list_t l = ma_structure_list_new();
  69. * - ajouter un élément 'e' en tête de la liste 'l' :
  70. * ma_structure_t e = ma_structure_new();
  71. * e->a = 0;
  72. * e->b = 1;
  73. * ma_structure_list_push_front(l, e);
  74. * - itérateur de liste :
  75. * ma_structure_itor_t i;
  76. * for(i = ma_structure_list_begin(l);
  77. * i != ma_structure_list_end(l);
  78. * i = ma_structure_list_next(i))
  79. * {
  80. * printf("a=%d; b=%d\n", i->a, i->b);
  81. * }
  82. * *********************************************************
  83. */
  84. /**@hideinitializer
  85. * Generates a new type for list of elements */
  86. #define LIST_TYPE(ENAME, DECL) \
  87. LIST_DECLARE_TYPE(ENAME) \
  88. LIST_CREATE_TYPE(ENAME, DECL)
  89. /**@hideinitializer
  90. * Forward type declaration for lists */
  91. #define LIST_DECLARE_TYPE(ENAME) \
  92. /** automatic type: ENAME##_list_t is a list of ENAME##_t */ \
  93. typedef struct ENAME##_list_s* ENAME##_list_t; \
  94. /** automatic type: defines ENAME##_t */ \
  95. typedef struct ENAME##_s* ENAME##_t; \
  96. /** automatic type: ENAME##_itor_t is an iterator on lists of ENAME##_t */ \
  97. typedef ENAME##_t ENAME##_itor_t;
  98. /**@hideinitializer
  99. * The effective type declaration for lists */
  100. #define LIST_CREATE_TYPE(ENAME, DECL) \
  101. /** from automatic type: ENAME##_t */ \
  102. struct ENAME##_s \
  103. { \
  104. struct ENAME##_s*_prev; /**< @internal previous cell */ \
  105. struct ENAME##_s*_next; /**< @internal next cell */ \
  106. DECL \
  107. }; \
  108. /** @internal */ \
  109. struct ENAME##_list_s \
  110. { \
  111. struct ENAME##_s* _head; /**< @internal head of the list */ \
  112. struct ENAME##_s* _tail; /**< @internal tail of the list */ \
  113. }; \
  114. /** @internal */static inline ENAME##_t ENAME##_new(void) \
  115. { ENAME##_t e = (ENAME##_t)malloc(sizeof(struct ENAME##_s)); \
  116. e->_next = NULL; e->_prev = NULL; return e; } \
  117. /** @internal */static inline void ENAME##_delete(ENAME##_t e) \
  118. { free(e); } \
  119. /** @internal */static inline void ENAME##_list_push_front(ENAME##_list_t l, ENAME##_t e) \
  120. { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
  121. e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
  122. /** @internal */static inline void ENAME##_list_push_back(ENAME##_list_t l, ENAME##_t e) \
  123. { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
  124. e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
  125. /** @internal */static inline ENAME##_t ENAME##_list_front(ENAME##_list_t l) \
  126. { return l->_head; } \
  127. /** @internal */static inline ENAME##_t ENAME##_list_back(ENAME##_list_t l) \
  128. { return l->_tail; } \
  129. /** @internal */static inline ENAME##_list_t ENAME##_list_new(void) \
  130. { ENAME##_list_t l; l=(ENAME##_list_t)malloc(sizeof(struct ENAME##_list_s)); \
  131. l->_head=NULL; l->_tail=l->_head; return l; } \
  132. /** @internal */static inline int ENAME##_list_empty(ENAME##_list_t l) \
  133. { return (l->_head == NULL); } \
  134. /** @internal */static inline void ENAME##_list_delete(ENAME##_list_t l) \
  135. { free(l); } \
  136. /** @internal */static inline void ENAME##_list_erase(ENAME##_list_t l, ENAME##_t c) \
  137. { ENAME##_t p = c->_prev; if(p) p->_next = c->_next; else l->_head = c->_next; \
  138. if(c->_next) c->_next->_prev = p; else l->_tail = p; } \
  139. /** @internal */static inline ENAME##_t ENAME##_list_pop_front(ENAME##_list_t l) \
  140. { ENAME##_t e = ENAME##_list_front(l); \
  141. ENAME##_list_erase(l, e); return e; } \
  142. /** @internal */static inline ENAME##_t ENAME##_list_pop_back(ENAME##_list_t l) \
  143. { ENAME##_t e = ENAME##_list_back(l); \
  144. ENAME##_list_erase(l, e); return e; } \
  145. /** @internal */static inline ENAME##_itor_t ENAME##_list_begin(ENAME##_list_t l) \
  146. { return l->_head; } \
  147. /** @internal */static inline ENAME##_itor_t ENAME##_list_end(ENAME##_list_t l __attribute__ ((unused))) \
  148. { return NULL; } \
  149. /** @internal */static inline ENAME##_itor_t ENAME##_list_next(ENAME##_itor_t i) \
  150. { return i->_next; } \
  151. /** @internal */static inline int ENAME##_list_size(ENAME##_list_t l) \
  152. { ENAME##_itor_t i=l->_head; int k=0; while(i!=NULL){k++;i=i->_next;} return k; } \
  153. /** @internal */static inline int ENAME##_list_check(ENAME##_list_t l) \
  154. { ENAME##_itor_t i=l->_head; while(i) \
  155. { if ((i->_next == NULL) && i != l->_tail) return 0; \
  156. if (i->_next == i) return 0; \
  157. i=i->_next;} return 1; }