pg_list.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pg_list.h
  4. * interface for PostgreSQL generic linked list package
  5. *
  6. * This package implements singly-linked homogeneous lists.
  7. *
  8. * It is important to have constant-time length, append, and prepend
  9. * operations. To achieve this, we deal with two distinct data
  10. * structures:
  11. *
  12. * 1. A set of "list cells": each cell contains a data field and
  13. * a link to the next cell in the list or NULL.
  14. * 2. A single structure containing metadata about the list: the
  15. * type of the list, pointers to the head and tail cells, and
  16. * the length of the list.
  17. *
  18. * We support three types of lists:
  19. *
  20. * T_List: lists of pointers
  21. * (in practice usually pointers to Nodes, but not always;
  22. * declared as "void *" to minimize casting annoyances)
  23. * T_IntList: lists of integers
  24. * T_OidList: lists of Oids
  25. *
  26. * (At the moment, ints and Oids are the same size, but they may not
  27. * always be so; try to be careful to maintain the distinction.)
  28. *
  29. *
  30. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  31. * Portions Copyright (c) 1994, Regents of the University of California
  32. *
  33. * src/include/nodes/pg_list.h
  34. *
  35. *-------------------------------------------------------------------------
  36. */
  37. #ifndef PG_LIST_H
  38. #define PG_LIST_H
  39. #include "nodes/nodes.h"
  40. typedef struct ListCell ListCell;
  41. typedef struct List
  42. {
  43. NodeTag type; /* T_List, T_IntList, or T_OidList */
  44. int length;
  45. ListCell *head;
  46. ListCell *tail;
  47. } List;
  48. struct ListCell
  49. {
  50. union
  51. {
  52. void *ptr_value;
  53. int int_value;
  54. Oid oid_value;
  55. } data;
  56. ListCell *next;
  57. };
  58. /*
  59. * The *only* valid representation of an empty list is NIL; in other
  60. * words, a non-NIL list is guaranteed to have length >= 1 and
  61. * head/tail != NULL
  62. */
  63. #define NIL ((List *) NULL)
  64. /*
  65. * These routines are used frequently. However, we can't implement
  66. * them as macros, since we want to avoid double-evaluation of macro
  67. * arguments.
  68. */
  69. static inline ListCell *
  70. list_head(const List *l)
  71. {
  72. return l ? l->head : NULL;
  73. }
  74. static inline ListCell *
  75. list_tail(List *l)
  76. {
  77. return l ? l->tail : NULL;
  78. }
  79. static inline int
  80. list_length(const List *l)
  81. {
  82. return l ? l->length : 0;
  83. }
  84. /*
  85. * NB: There is an unfortunate legacy from a previous incarnation of
  86. * the List API: the macro lfirst() was used to mean "the data in this
  87. * cons cell". To avoid changing every usage of lfirst(), that meaning
  88. * has been kept. As a result, lfirst() takes a ListCell and returns
  89. * the data it contains; to get the data in the first cell of a
  90. * List, use linitial(). Worse, lsecond() is more closely related to
  91. * linitial() than lfirst(): given a List, lsecond() returns the data
  92. * in the second cons cell.
  93. */
  94. #define lnext(lc) ((lc)->next)
  95. #define lfirst(lc) ((lc)->data.ptr_value)
  96. #define lfirst_int(lc) ((lc)->data.int_value)
  97. #define lfirst_oid(lc) ((lc)->data.oid_value)
  98. #define lfirst_node(type,lc) castNode(type, lfirst(lc))
  99. #define linitial(l) lfirst(list_head(l))
  100. #define linitial_int(l) lfirst_int(list_head(l))
  101. #define linitial_oid(l) lfirst_oid(list_head(l))
  102. #define linitial_node(type,l) castNode(type, linitial(l))
  103. #define lsecond(l) lfirst(lnext(list_head(l)))
  104. #define lsecond_int(l) lfirst_int(lnext(list_head(l)))
  105. #define lsecond_oid(l) lfirst_oid(lnext(list_head(l)))
  106. #define lsecond_node(type,l) castNode(type, lsecond(l))
  107. #define lthird(l) lfirst(lnext(lnext(list_head(l))))
  108. #define lthird_int(l) lfirst_int(lnext(lnext(list_head(l))))
  109. #define lthird_oid(l) lfirst_oid(lnext(lnext(list_head(l))))
  110. #define lthird_node(type,l) castNode(type, lthird(l))
  111. #define lfourth(l) lfirst(lnext(lnext(lnext(list_head(l)))))
  112. #define lfourth_int(l) lfirst_int(lnext(lnext(lnext(list_head(l)))))
  113. #define lfourth_oid(l) lfirst_oid(lnext(lnext(lnext(list_head(l)))))
  114. #define lfourth_node(type,l) castNode(type, lfourth(l))
  115. #define llast(l) lfirst(list_tail(l))
  116. #define llast_int(l) lfirst_int(list_tail(l))
  117. #define llast_oid(l) lfirst_oid(list_tail(l))
  118. #define llast_node(type,l) castNode(type, llast(l))
  119. /*
  120. * Convenience macros for building fixed-length lists
  121. */
  122. #define list_make1(x1) lcons(x1, NIL)
  123. #define list_make2(x1,x2) lcons(x1, list_make1(x2))
  124. #define list_make3(x1,x2,x3) lcons(x1, list_make2(x2, x3))
  125. #define list_make4(x1,x2,x3,x4) lcons(x1, list_make3(x2, x3, x4))
  126. #define list_make5(x1,x2,x3,x4,x5) lcons(x1, list_make4(x2, x3, x4, x5))
  127. #define list_make1_int(x1) lcons_int(x1, NIL)
  128. #define list_make2_int(x1,x2) lcons_int(x1, list_make1_int(x2))
  129. #define list_make3_int(x1,x2,x3) lcons_int(x1, list_make2_int(x2, x3))
  130. #define list_make4_int(x1,x2,x3,x4) lcons_int(x1, list_make3_int(x2, x3, x4))
  131. #define list_make5_int(x1,x2,x3,x4,x5) lcons_int(x1, list_make4_int(x2, x3, x4, x5))
  132. #define list_make1_oid(x1) lcons_oid(x1, NIL)
  133. #define list_make2_oid(x1,x2) lcons_oid(x1, list_make1_oid(x2))
  134. #define list_make3_oid(x1,x2,x3) lcons_oid(x1, list_make2_oid(x2, x3))
  135. #define list_make4_oid(x1,x2,x3,x4) lcons_oid(x1, list_make3_oid(x2, x3, x4))
  136. #define list_make5_oid(x1,x2,x3,x4,x5) lcons_oid(x1, list_make4_oid(x2, x3, x4, x5))
  137. /*
  138. * foreach -
  139. * a convenience macro which loops through the list
  140. */
  141. #define foreach(cell, l) \
  142. for ((cell) = list_head(l); (cell) != NULL; (cell) = lnext(cell))
  143. /*
  144. * for_each_cell -
  145. * a convenience macro which loops through a list starting from a
  146. * specified cell
  147. */
  148. #define for_each_cell(cell, initcell) \
  149. for ((cell) = (initcell); (cell) != NULL; (cell) = lnext(cell))
  150. /*
  151. * forboth -
  152. * a convenience macro for advancing through two linked lists
  153. * simultaneously. This macro loops through both lists at the same
  154. * time, stopping when either list runs out of elements. Depending
  155. * on the requirements of the call site, it may also be wise to
  156. * assert that the lengths of the two lists are equal.
  157. */
  158. #define forboth(cell1, list1, cell2, list2) \
  159. for ((cell1) = list_head(list1), (cell2) = list_head(list2); \
  160. (cell1) != NULL && (cell2) != NULL; \
  161. (cell1) = lnext(cell1), (cell2) = lnext(cell2))
  162. /*
  163. * forthree -
  164. * the same for three lists
  165. */
  166. #define forthree(cell1, list1, cell2, list2, cell3, list3) \
  167. for ((cell1) = list_head(list1), (cell2) = list_head(list2), (cell3) = list_head(list3); \
  168. (cell1) != NULL && (cell2) != NULL && (cell3) != NULL; \
  169. (cell1) = lnext(cell1), (cell2) = lnext(cell2), (cell3) = lnext(cell3))
  170. extern List *lappend(List *list, void *datum);
  171. extern List *lappend_int(List *list, int datum);
  172. extern List *lappend_oid(List *list, Oid datum);
  173. extern ListCell *lappend_cell(List *list, ListCell *prev, void *datum);
  174. extern ListCell *lappend_cell_int(List *list, ListCell *prev, int datum);
  175. extern ListCell *lappend_cell_oid(List *list, ListCell *prev, Oid datum);
  176. extern List *lcons(void *datum, List *list);
  177. extern List *lcons_int(int datum, List *list);
  178. extern List *lcons_oid(Oid datum, List *list);
  179. extern List *list_concat(List *list1, List *list2);
  180. extern List *list_truncate(List *list, int new_size);
  181. extern ListCell *list_nth_cell(const List *list, int n);
  182. extern void *list_nth(const List *list, int n);
  183. extern int list_nth_int(const List *list, int n);
  184. extern Oid list_nth_oid(const List *list, int n);
  185. #define list_nth_node(type,list,n) castNode(type, list_nth(list, n))
  186. extern bool list_member(const List *list, const void *datum);
  187. extern bool list_member_ptr(const List *list, const void *datum);
  188. extern bool list_member_int(const List *list, int datum);
  189. extern bool list_member_oid(const List *list, Oid datum);
  190. extern List *list_delete(List *list, void *datum);
  191. extern List *list_delete_ptr(List *list, void *datum);
  192. extern List *list_delete_int(List *list, int datum);
  193. extern List *list_delete_oid(List *list, Oid datum);
  194. extern List *list_delete_first(List *list);
  195. extern List *list_delete_cell(List *list, ListCell *cell, ListCell *prev);
  196. extern List *list_union(const List *list1, const List *list2);
  197. extern List *list_union_ptr(const List *list1, const List *list2);
  198. extern List *list_union_int(const List *list1, const List *list2);
  199. extern List *list_union_oid(const List *list1, const List *list2);
  200. extern List *list_intersection(const List *list1, const List *list2);
  201. extern List *list_intersection_int(const List *list1, const List *list2);
  202. /* currently, there's no need for list_intersection_ptr etc */
  203. extern List *list_difference(const List *list1, const List *list2);
  204. extern List *list_difference_ptr(const List *list1, const List *list2);
  205. extern List *list_difference_int(const List *list1, const List *list2);
  206. extern List *list_difference_oid(const List *list1, const List *list2);
  207. extern List *list_append_unique(List *list, void *datum);
  208. extern List *list_append_unique_ptr(List *list, void *datum);
  209. extern List *list_append_unique_int(List *list, int datum);
  210. extern List *list_append_unique_oid(List *list, Oid datum);
  211. extern List *list_concat_unique(List *list1, List *list2);
  212. extern List *list_concat_unique_ptr(List *list1, List *list2);
  213. extern List *list_concat_unique_int(List *list1, List *list2);
  214. extern List *list_concat_unique_oid(List *list1, List *list2);
  215. extern void list_free(List *list);
  216. extern void list_free_deep(List *list);
  217. extern List *list_copy(const List *list);
  218. extern List *list_copy_tail(const List *list, int nskip);
  219. /*
  220. * To ease migration to the new list API, a set of compatibility
  221. * macros are provided that reduce the impact of the list API changes
  222. * as far as possible. Until client code has been rewritten to use the
  223. * new list API, the ENABLE_LIST_COMPAT symbol can be defined before
  224. * including pg_list.h
  225. */
  226. #ifdef ENABLE_LIST_COMPAT
  227. #define lfirsti(lc) lfirst_int(lc)
  228. #define lfirsto(lc) lfirst_oid(lc)
  229. #define makeList1(x1) list_make1(x1)
  230. #define makeList2(x1, x2) list_make2(x1, x2)
  231. #define makeList3(x1, x2, x3) list_make3(x1, x2, x3)
  232. #define makeList4(x1, x2, x3, x4) list_make4(x1, x2, x3, x4)
  233. #define makeListi1(x1) list_make1_int(x1)
  234. #define makeListi2(x1, x2) list_make2_int(x1, x2)
  235. #define makeListo1(x1) list_make1_oid(x1)
  236. #define makeListo2(x1, x2) list_make2_oid(x1, x2)
  237. #define lconsi(datum, list) lcons_int(datum, list)
  238. #define lconso(datum, list) lcons_oid(datum, list)
  239. #define lappendi(list, datum) lappend_int(list, datum)
  240. #define lappendo(list, datum) lappend_oid(list, datum)
  241. #define nconc(l1, l2) list_concat(l1, l2)
  242. #define nth(n, list) list_nth(list, n)
  243. #define member(datum, list) list_member(list, datum)
  244. #define ptrMember(datum, list) list_member_ptr(list, datum)
  245. #define intMember(datum, list) list_member_int(list, datum)
  246. #define oidMember(datum, list) list_member_oid(list, datum)
  247. /*
  248. * Note that the old lremove() determined equality via pointer
  249. * comparison, whereas the new list_delete() uses equal(); in order to
  250. * keep the same behavior, we therefore need to map lremove() calls to
  251. * list_delete_ptr() rather than list_delete()
  252. */
  253. #define lremove(elem, list) list_delete_ptr(list, elem)
  254. #define LispRemove(elem, list) list_delete(list, elem)
  255. #define lremovei(elem, list) list_delete_int(list, elem)
  256. #define lremoveo(elem, list) list_delete_oid(list, elem)
  257. #define ltruncate(n, list) list_truncate(list, n)
  258. #define set_union(l1, l2) list_union(l1, l2)
  259. #define set_uniono(l1, l2) list_union_oid(l1, l2)
  260. #define set_ptrUnion(l1, l2) list_union_ptr(l1, l2)
  261. #define set_difference(l1, l2) list_difference(l1, l2)
  262. #define set_differenceo(l1, l2) list_difference_oid(l1, l2)
  263. #define set_ptrDifference(l1, l2) list_difference_ptr(l1, l2)
  264. #define equali(l1, l2) equal(l1, l2)
  265. #define equalo(l1, l2) equal(l1, l2)
  266. #define freeList(list) list_free(list)
  267. #define listCopy(list) list_copy(list)
  268. extern int length(List *list);
  269. #endif /* ENABLE_LIST_COMPAT */
  270. #endif /* PG_LIST_H */