list.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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
  25. * + pour les listes : FOO_list
  26. * + pour les itérateurs : FOO
  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. * FOO_t FOO_list_begin(FOO_list_t);
  60. * *
  61. * FOO_t FOO_list_end(FOO_list_t);
  62. * *
  63. * FOO_t FOO_list_next(FOO_t)
  64. * *
  65. * int FOO_list_size(FOO_list_t)
  66. * *********************************************************
  67. * Exemples d'utilisation :
  68. * - au départ, on a :
  69. * struct ma_structure_s
  70. * {
  71. * int a;
  72. * int b;
  73. * };
  74. * - on veut en faire une liste. On remplace la déclaration par :
  75. * LIST_TYPE(ma_structure,
  76. * int a;
  77. * int b;
  78. * );
  79. * qui crée les types ma_structure_t et ma_structure_list_t.
  80. * - allocation d'une liste vide :
  81. * ma_structure_list_t l = ma_structure_list_new();
  82. * - ajouter un élément 'e' en tête de la liste 'l' :
  83. * ma_structure_t e = ma_structure_new();
  84. * e->a = 0;
  85. * e->b = 1;
  86. * ma_structure_list_push_front(l, e);
  87. * - itérateur de liste :
  88. * ma_structure i;
  89. * for(i = ma_structure_list_begin(l);
  90. * i != ma_structure_list_end(l);
  91. * i = ma_structure_list_next(i))
  92. * {
  93. * printf("a=%d; b=%d\n", i->a, i->b);
  94. * }
  95. * *********************************************************
  96. */
  97. /**@hideinitializer
  98. * Generates a new type for list of elements */
  99. #define LIST_TYPE(ENAME, DECL) \
  100. LIST_CREATE_TYPE(ENAME, DECL)
  101. /**@hideinitializer
  102. * The effective type declaration for lists */
  103. #define LIST_CREATE_TYPE(ENAME, DECL) \
  104. /** from automatic type: struct ENAME */ \
  105. struct ENAME \
  106. { \
  107. struct ENAME *_prev; /**< @internal previous cell */ \
  108. struct ENAME *_next; /**< @internal next cell */ \
  109. DECL \
  110. }; \
  111. /** @internal */ \
  112. struct ENAME##_list \
  113. { \
  114. struct ENAME *_head; /**< @internal head of the list */ \
  115. struct ENAME *_tail; /**< @internal tail of the list */ \
  116. }; \
  117. /** @internal */static inline struct ENAME *ENAME##_new(void) \
  118. { struct ENAME *e = (struct ENAME *)malloc(sizeof(struct ENAME)); \
  119. e->_next = NULL; e->_prev = NULL; return e; } \
  120. /** @internal */static inline void ENAME##_delete(struct ENAME *e) \
  121. { free(e); } \
  122. /** @internal */static inline void ENAME##_list_push_front(struct ENAME##_list *l, struct ENAME *e) \
  123. { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
  124. e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
  125. /** @internal */static inline void ENAME##_list_push_back(struct ENAME##_list *l, struct ENAME *e) \
  126. { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
  127. e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
  128. /** @internal */static inline void ENAME##_list_push_list_front(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  129. { if (l2->_head == NULL) { l2->_head = l1->_head; l2->_tail = l1->_tail; } \
  130. else if (l1->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l2->_head = l1->_head; } } \
  131. /** @internal */static inline void ENAME##_list_push_list_back(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  132. { if(l1->_head == NULL) { l1->_head = l2->_head; l1->_tail = l2->_tail; } \
  133. else if (l2->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l1->_tail = l2->_tail; } } \
  134. /** @internal */static inline struct ENAME *ENAME##_list_front(struct ENAME##_list *l) \
  135. { return l->_head; } \
  136. /** @internal */static inline struct ENAME *ENAME##_list_back(struct ENAME##_list *l) \
  137. { return l->_tail; } \
  138. /** @internal */static inline struct ENAME##_list *ENAME##_list_new(void) \
  139. { struct ENAME##_list *l; l=(struct ENAME##_list *)malloc(sizeof(struct ENAME##_list)); \
  140. l->_head=NULL; l->_tail=l->_head; return l; } \
  141. /** @internal */static inline int ENAME##_list_empty(struct ENAME##_list *l) \
  142. { return (l->_head == NULL); } \
  143. /** @internal */static inline void ENAME##_list_delete(struct ENAME##_list *l) \
  144. { free(l); } \
  145. /** @internal */static inline void ENAME##_list_erase(struct ENAME##_list *l, struct ENAME *c) \
  146. { struct ENAME *p = c->_prev; if(p) p->_next = c->_next; else l->_head = c->_next; \
  147. if(c->_next) c->_next->_prev = p; else l->_tail = p; } \
  148. /** @internal */static inline struct ENAME *ENAME##_list_pop_front(struct ENAME##_list *l) \
  149. { struct ENAME *e = ENAME##_list_front(l); \
  150. ENAME##_list_erase(l, e); return e; } \
  151. /** @internal */static inline struct ENAME *ENAME##_list_pop_back(struct ENAME##_list *l) \
  152. { struct ENAME *e = ENAME##_list_back(l); \
  153. ENAME##_list_erase(l, e); return e; } \
  154. /** @internal */static inline struct ENAME *ENAME##_list_begin(struct ENAME##_list *l) \
  155. { return l->_head; } \
  156. /** @internal */static inline struct ENAME *ENAME##_list_end(struct ENAME##_list *l __attribute__ ((unused))) \
  157. { return NULL; } \
  158. /** @internal */static inline struct ENAME *ENAME##_list_next(struct ENAME *i) \
  159. { return i->_next; } \
  160. /** @internal */static inline int ENAME##_list_size(struct ENAME##_list *l) \
  161. { struct ENAME *i=l->_head; int k=0; while(i!=NULL){k++;i=i->_next;} return k; } \
  162. /** @internal */static inline int ENAME##_list_check(struct ENAME##_list *l) \
  163. { struct ENAME *i=l->_head; while(i) \
  164. { if ((i->_next == NULL) && i != l->_tail) return 0; \
  165. if (i->_next == i) return 0; \
  166. i=i->_next;} return 1; }