list.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  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 : FOO_t
  25. * + pour les listes : FOO_list_t
  26. * + pour les itérateurs : FOO_itor_t
  27. * - déclare les accesseurs suivants :
  28. * * création d'une cellule
  29. * FOO_t FOO_new(void);
  30. * * suppression d'une cellule
  31. * void FOO_delete(FOO_t);
  32. * * création d'une liste (vide)
  33. * FOO_list_t FOO_list_new(void);
  34. * * suppression d'une liste
  35. * void FOO_list_delete(FOO_list_t);
  36. * * teste si une liste est vide
  37. * int FOO_list_empty(FOO_list_t);
  38. * * retire un élément de la liste
  39. * void FOO_list_erase(FOO_list_t, FOO_t);
  40. * * ajoute une élément en queue de liste
  41. * void FOO_list_push_back(FOO_list_t, FOO_t);
  42. * * ajoute un élément en tête de list
  43. * void FOO_list_push_front(FOO_list_t, FOO_t);
  44. * * ajoute la deuxième liste à la fin de la première liste
  45. * FOO_t FOO_list_push_list_back(FOO_list_t, FOO_list_t);
  46. * * ajoute la première liste au début de la deuxième liste
  47. * FOO_t FOO_list_push_list_front(FOO_list_t, FOO_list_t);
  48. * * retire l'élément en queue de liste
  49. * FOO_t FOO_list_pop_back(FOO_list_t);
  50. * * retire l'élement en tête de liste
  51. * FOO_t FOO_list_pop_front(FOO_list_t);
  52. * * retourne l'élément en queue de liste
  53. * FOO_t FOO_list_back(FOO_list_t);
  54. * * retourne l'élement en tête de liste
  55. * FOO_t FOO_list_front(FOO_list_t);
  56. * * vérifie si la liste chainée est cohérente
  57. * int FOO_list_check(FOO_list_t);
  58. * *********************************************************
  59. * Exemples d'utilisation :
  60. * - au départ, on a :
  61. * struct ma_structure_s
  62. * {
  63. * int a;
  64. * int b;
  65. * };
  66. * - on veut en faire une liste. On remplace la déclaration par :
  67. * LIST_TYPE(ma_structure,
  68. * int a;
  69. * int b;
  70. * );
  71. * qui crée les types ma_structure_t et ma_structure_list_t.
  72. * - allocation d'une liste vide :
  73. * ma_structure_list_t l = ma_structure_list_new();
  74. * - ajouter un élément 'e' en tête de la liste 'l' :
  75. * ma_structure_t e = ma_structure_new();
  76. * e->a = 0;
  77. * e->b = 1;
  78. * ma_structure_list_push_front(l, e);
  79. * - itérateur de liste :
  80. * ma_structure_itor_t i;
  81. * for(i = ma_structure_list_begin(l);
  82. * i != ma_structure_list_end(l);
  83. * i = ma_structure_list_next(i))
  84. * {
  85. * printf("a=%d; b=%d\n", i->a, i->b);
  86. * }
  87. * *********************************************************
  88. */
  89. /**@hideinitializer
  90. * Generates a new type for list of elements */
  91. #define LIST_TYPE(ENAME, DECL) \
  92. LIST_DECLARE_TYPE(ENAME) \
  93. LIST_CREATE_TYPE(ENAME, DECL)
  94. /**@hideinitializer
  95. * Forward type declaration for lists */
  96. #define LIST_DECLARE_TYPE(ENAME) \
  97. /** automatic type: ENAME##_list_t is a list of ENAME##_t */ \
  98. typedef struct ENAME##_list_s* ENAME##_list_t; \
  99. /** automatic type: defines ENAME##_t */ \
  100. typedef struct ENAME##_s* ENAME##_t; \
  101. /** automatic type: ENAME##_itor_t is an iterator on lists of ENAME##_t */ \
  102. typedef ENAME##_t ENAME##_itor_t;
  103. /**@hideinitializer
  104. * The effective type declaration for lists */
  105. #define LIST_CREATE_TYPE(ENAME, DECL) \
  106. /** from automatic type: ENAME##_t */ \
  107. struct ENAME##_s \
  108. { \
  109. struct ENAME##_s*_prev; /**< @internal previous cell */ \
  110. struct ENAME##_s*_next; /**< @internal next cell */ \
  111. DECL \
  112. }; \
  113. /** @internal */ \
  114. struct ENAME##_list_s \
  115. { \
  116. struct ENAME##_s* _head; /**< @internal head of the list */ \
  117. struct ENAME##_s* _tail; /**< @internal tail of the list */ \
  118. }; \
  119. /** @internal */static inline ENAME##_t ENAME##_new(void) \
  120. { ENAME##_t e = (ENAME##_t)malloc(sizeof(struct ENAME##_s)); \
  121. e->_next = NULL; e->_prev = NULL; return e; } \
  122. /** @internal */static inline void ENAME##_delete(ENAME##_t e) \
  123. { free(e); } \
  124. /** @internal */static inline void ENAME##_list_push_front(ENAME##_list_t l, ENAME##_t e) \
  125. { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
  126. e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
  127. /** @internal */static inline void ENAME##_list_push_back(ENAME##_list_t l, ENAME##_t e) \
  128. { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
  129. e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
  130. /** @internal */static inline void ENAME##_list_push_list_front(ENAME##_list_t l1, ENAME##_list_t l2) \
  131. { if (l2->_head == NULL) { l2->_head = l1->_head; l2->_tail = l1->_tail; } \
  132. else if (l1->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l2->_head = l1->_head; } } \
  133. /** @internal */static inline void ENAME##_list_push_list_back(ENAME##_list_t l1, ENAME##_list_t l2) \
  134. { if(l1->_head == NULL) { l1->_head = l2->_head; l1->_tail = l2->_tail; } \
  135. else if (l2->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l1->_tail = l2->_head; } } \
  136. /** @internal */static inline ENAME##_t ENAME##_list_front(ENAME##_list_t l) \
  137. { return l->_head; } \
  138. /** @internal */static inline ENAME##_t ENAME##_list_back(ENAME##_list_t l) \
  139. { return l->_tail; } \
  140. /** @internal */static inline ENAME##_list_t ENAME##_list_new(void) \
  141. { ENAME##_list_t l; l=(ENAME##_list_t)malloc(sizeof(struct ENAME##_list_s)); \
  142. l->_head=NULL; l->_tail=l->_head; return l; } \
  143. /** @internal */static inline int ENAME##_list_empty(ENAME##_list_t l) \
  144. { return (l->_head == NULL); } \
  145. /** @internal */static inline void ENAME##_list_delete(ENAME##_list_t l) \
  146. { free(l); } \
  147. /** @internal */static inline void ENAME##_list_erase(ENAME##_list_t l, ENAME##_t c) \
  148. { ENAME##_t p = c->_prev; if(p) p->_next = c->_next; else l->_head = c->_next; \
  149. if(c->_next) c->_next->_prev = p; else l->_tail = p; } \
  150. /** @internal */static inline ENAME##_t ENAME##_list_pop_front(ENAME##_list_t l) \
  151. { ENAME##_t e = ENAME##_list_front(l); \
  152. ENAME##_list_erase(l, e); return e; } \
  153. /** @internal */static inline ENAME##_t ENAME##_list_pop_back(ENAME##_list_t l) \
  154. { ENAME##_t e = ENAME##_list_back(l); \
  155. ENAME##_list_erase(l, e); return e; } \
  156. /** @internal */static inline ENAME##_itor_t ENAME##_list_begin(ENAME##_list_t l) \
  157. { return l->_head; } \
  158. /** @internal */static inline ENAME##_itor_t ENAME##_list_end(ENAME##_list_t l __attribute__ ((unused))) \
  159. { return NULL; } \
  160. /** @internal */static inline ENAME##_itor_t ENAME##_list_next(ENAME##_itor_t i) \
  161. { return i->_next; } \
  162. /** @internal */static inline int ENAME##_list_size(ENAME##_list_t l) \
  163. { ENAME##_itor_t i=l->_head; int k=0; while(i!=NULL){k++;i=i->_next;} return k; } \
  164. /** @internal */static inline int ENAME##_list_check(ENAME##_list_t l) \
  165. { ENAME##_itor_t i=l->_head; while(i) \
  166. { if ((i->_next == NULL) && i != l->_tail) return 0; \
  167. if (i->_next == i) return 0; \
  168. i=i->_next;} return 1; }