list.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012, 2015-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /** @file
  18. * @brief Listes doublement chainées automatiques
  19. */
  20. /** @remarks list how-to
  21. * *********************************************************
  22. * LIST_TYPE(FOO, contenu);
  23. * - déclare les types suivants
  24. * + pour les cellules : struct FOO
  25. * + pour les listes : struct FOO_list
  26. * + pour les itérateurs : struct FOO
  27. * - déclare les accesseurs suivants :
  28. * * création d'une cellule
  29. * struct FOO* FOO_new(void);
  30. * * suppression d'une cellule
  31. * void FOO_delete(struct FOO*);
  32. * * création d'une liste (vide)
  33. * struct FOO_list* FOO_list_new(void);
  34. * * initialisation d'une liste (vide)
  35. * void FOO_list_init(struct FOO_list*);
  36. * * suppression d'une liste
  37. * void FOO_list_delete(struct FOO_list*);
  38. * * teste si une liste est vide
  39. * int FOO_list_empty(struct FOO_list*);
  40. * * retire un élément de la liste
  41. * void FOO_list_erase(struct FOO_list*, struct FOO*);
  42. * * ajoute une élément en queue de liste
  43. * void FOO_list_push_back(struct FOO_list*, struct FOO*);
  44. * * ajoute un élément en tête de liste
  45. * void FOO_list_push_front(struct FOO_list*, struct FOO*);
  46. * * ajoute un élément avant un élément
  47. * void FOO_list_insert_before(struct FOO_list*, struct FOO*new, struct FOO*);
  48. * * ajoute un élément après un élément
  49. * void FOO_list_insert_after(struct FOO_list*, struct FOO*new, struct FOO*);
  50. * * ajoute la deuxième liste à la fin de la première liste
  51. * struct FOO* FOO_list_push_list_back(struct FOO_list*, struct FOO_list*);
  52. * * ajoute la première liste au début de la deuxième liste
  53. * struct FOO* FOO_list_push_list_front(struct FOO_list*, struct FOO_list*);
  54. * * retire l'élément en queue de liste
  55. * struct FOO* FOO_list_pop_back(struct FOO_list*);
  56. * * retire l'élement en tête de liste
  57. * struct FOO* FOO_list_pop_front(struct FOO_list*);
  58. * * retourne l'élément en queue de liste
  59. * struct FOO* FOO_list_back(struct FOO_list*);
  60. * * retourne l'élement en tête de liste
  61. * struct FOO* FOO_list_front(struct FOO_list*);
  62. * * vérifie si la liste chainée est cohérente
  63. * int FOO_list_check(struct FOO_list*);
  64. * * retourne le premier élément de la liste
  65. * struct FOO* FOO_list_begin(struct FOO_list*);
  66. * * retourne la valeur à tester en fin de liste
  67. * struct FOO* FOO_list_end(struct FOO_list*);
  68. * * retourne l'élément suivant de la liste
  69. * struct FOO* FOO_list_next(struct FOO*)
  70. * * retourne la taille de la liste
  71. * int FOO_list_size(struct FOO_list*)
  72. * * retourne la position de l'élément dans la liste (indexé à partir de 0)
  73. * int FOO_list_member(struct FOO_list*, struct FOO*)
  74. * * teste si l'élément est dans la liste
  75. * int FOO_list_ismember(struct FOO_list*, struct FOO*)
  76. * *********************************************************
  77. * Exemples d'utilisation :
  78. * - au départ, on a :
  79. * struct ma_structure_s
  80. * {
  81. * int a;
  82. * int b;
  83. * };
  84. * - on veut en faire une liste. On remplace la déclaration par :
  85. * LIST_TYPE(ma_structure,
  86. * int a;
  87. * int b;
  88. * );
  89. * qui crée les types struct ma_structure et struct ma_structure_list.
  90. * - allocation d'une liste vide :
  91. * struct ma_structure_list * l = ma_structure_list_new();
  92. * - ajouter un élément 'e' en tête de la liste 'l' :
  93. * struct ma_structure * e = ma_structure_new();
  94. * e->a = 0;
  95. * e->b = 0;
  96. * ma_structure_list_push_front(l, e);
  97. * - itérateur de liste :
  98. * struct ma_structure * i;
  99. * for(i = ma_structure_list_begin(l);
  100. * i != ma_structure_list_end(l);
  101. * i = ma_structure_list_next(i))
  102. * {
  103. * printf("a=%d; b=%d\n", i->a, i->b);
  104. * }
  105. * *********************************************************
  106. */
  107. /**@hideinitializer
  108. * Generates a new type for list of elements */
  109. #define LIST_TYPE(ENAME, DECL) \
  110. LIST_CREATE_TYPE(ENAME, DECL)
  111. /**@hideinitializer
  112. * The effective type declaration for lists */
  113. #define LIST_CREATE_TYPE(ENAME, DECL) \
  114. /** from automatic type: struct ENAME */ \
  115. struct ENAME \
  116. { \
  117. struct ENAME *_prev; /**< @internal previous cell */ \
  118. struct ENAME *_next; /**< @internal next cell */ \
  119. DECL \
  120. }; \
  121. /** @internal */ \
  122. struct ENAME##_list \
  123. { \
  124. struct ENAME *_head; /**< @internal head of the list */ \
  125. struct ENAME *_tail; /**< @internal tail of the list */ \
  126. }; \
  127. /** @internal */static inline struct ENAME *ENAME##_new(void) \
  128. { struct ENAME *e = (struct ENAME *)malloc(sizeof(struct ENAME)); \
  129. e->_next = NULL; e->_prev = NULL; return e; } \
  130. /** @internal */static inline void ENAME##_delete(struct ENAME *e) \
  131. { free(e); } \
  132. /** @internal */static inline void ENAME##_list_push_front(struct ENAME##_list *l, struct ENAME *e) \
  133. { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
  134. e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
  135. /** @internal */static inline void ENAME##_list_push_back(struct ENAME##_list *l, struct ENAME *e) \
  136. { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
  137. e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
  138. /** @internal */static inline void ENAME##_list_insert_before(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  139. { struct ENAME *p = o->_prev; if (p) { p->_next = e; e->_prev = p; } else { l->_head = e; e->_prev = NULL; } \
  140. e->_next = o; o->_prev = e; } \
  141. /** @internal */static inline void ENAME##_list_insert_after(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  142. { struct ENAME *n = o->_next; if (n) { n->_prev = e; e->_next = n; } else { l->_tail = e; e->_next = NULL; } \
  143. e->_prev = o; o->_next = e; } \
  144. /** @internal */static inline void ENAME##_list_push_list_front(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  145. { if (l2->_head == NULL) { l2->_head = l1->_head; l2->_tail = l1->_tail; } \
  146. else if (l1->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l2->_head = l1->_head; } } \
  147. /** @internal */static inline void ENAME##_list_push_list_back(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  148. { if(l1->_head == NULL) { l1->_head = l2->_head; l1->_tail = l2->_tail; } \
  149. else if (l2->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l1->_tail = l2->_tail; } } \
  150. /** @internal */static inline struct ENAME *ENAME##_list_front(const struct ENAME##_list *l) \
  151. { return l->_head; } \
  152. /** @internal */static inline struct ENAME *ENAME##_list_back(const struct ENAME##_list *l) \
  153. { return l->_tail; } \
  154. /** @internal */static inline void ENAME##_list_init(struct ENAME##_list *l) \
  155. { l->_head=NULL; l->_tail=l->_head; } \
  156. /** @internal */static inline struct ENAME##_list *ENAME##_list_new(void) \
  157. { struct ENAME##_list *l; l=(struct ENAME##_list *)malloc(sizeof(struct ENAME##_list)); \
  158. ENAME##_list_init(l); return l; } \
  159. /** @internal */static inline int ENAME##_list_empty(const struct ENAME##_list *l) \
  160. { return (l->_head == NULL); } \
  161. /** @internal */static inline void ENAME##_list_delete(struct ENAME##_list *l) \
  162. { free(l); } \
  163. /** @internal */static inline void ENAME##_list_erase(struct ENAME##_list *l, struct ENAME *c) \
  164. { struct ENAME *p = c->_prev; if(p) p->_next = c->_next; else l->_head = c->_next; \
  165. if(c->_next) c->_next->_prev = p; else l->_tail = p; } \
  166. /** @internal */static inline struct ENAME *ENAME##_list_pop_front(struct ENAME##_list *l) \
  167. { struct ENAME *e = ENAME##_list_front(l); \
  168. ENAME##_list_erase(l, e); return e; } \
  169. /** @internal */static inline struct ENAME *ENAME##_list_pop_back(struct ENAME##_list *l) \
  170. { struct ENAME *e = ENAME##_list_back(l); \
  171. ENAME##_list_erase(l, e); return e; } \
  172. /** @internal */static inline struct ENAME *ENAME##_list_begin(const struct ENAME##_list *l) \
  173. { return l->_head; } \
  174. /** @internal */static inline struct ENAME *ENAME##_list_end(const struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
  175. { return NULL; } \
  176. /** @internal */static inline struct ENAME *ENAME##_list_next(const struct ENAME *i) \
  177. { return i->_next; } \
  178. /** @internal */static inline int ENAME##_list_ismember(const struct ENAME##_list *l, const struct ENAME *e) \
  179. { struct ENAME *i=l->_head; while(i!=NULL){ if (i == e) return 1; i=i->_next; } return 0; } \
  180. /** @internal */static inline int ENAME##_list_member(const struct ENAME##_list *l, const struct ENAME *e) \
  181. { struct ENAME *i=l->_head; int k=0; while(i!=NULL){if (i == e) return k; k++; i=i->_next; } return -1; } \
  182. /** @internal */static inline int ENAME##_list_size(const struct ENAME##_list *l) \
  183. { struct ENAME *i=l->_head; int k=0; while(i!=NULL){k++;i=i->_next;} return k; } \
  184. /** @internal */static inline int ENAME##_list_check(const struct ENAME##_list *l) \
  185. { struct ENAME *i=l->_head; while(i) \
  186. { if ((i->_next == NULL) && i != l->_tail) return 0; \
  187. if (i->_next == i) return 0; \
  188. i=i->_next;} return 1; }