pg_list.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pg_list.h
  4. * interface for PostgreSQL generic list package
  5. *
  6. * Once upon a time, parts of Postgres were written in Lisp and used real
  7. * cons-cell lists for major data structures. When that code was rewritten
  8. * in C, we initially had a faithful emulation of cons-cell lists, which
  9. * unsurprisingly was a performance bottleneck. A couple of major rewrites
  10. * later, these data structures are actually simple expansible arrays;
  11. * but the "List" name and a lot of the notation survives.
  12. *
  13. * One important concession to the original implementation is that an empty
  14. * list is always represented by a null pointer (preferentially written NIL).
  15. * Non-empty lists have a header, which will not be relocated as long as the
  16. * list remains non-empty, and an expansible data array.
  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-2022, 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 union ListCell
  41. {
  42. void *ptr_value;
  43. int int_value;
  44. Oid oid_value;
  45. } ListCell;
  46. typedef struct List
  47. {
  48. NodeTag type; /* T_List, T_IntList, or T_OidList */
  49. int length; /* number of elements currently present */
  50. int max_length; /* allocated length of elements[] */
  51. ListCell *elements; /* re-allocatable array of cells */
  52. /* We may allocate some cells along with the List header: */
  53. ListCell initial_elements[FLEXIBLE_ARRAY_MEMBER];
  54. /* If elements == initial_elements, it's not a separate allocation */
  55. } List;
  56. /*
  57. * The *only* valid representation of an empty list is NIL; in other
  58. * words, a non-NIL list is guaranteed to have length >= 1.
  59. */
  60. #define NIL ((List *) NULL)
  61. /*
  62. * State structs for various looping macros below.
  63. */
  64. typedef struct ForEachState
  65. {
  66. const List *l; /* list we're looping through */
  67. int i; /* current element index */
  68. } ForEachState;
  69. typedef struct ForBothState
  70. {
  71. const List *l1; /* lists we're looping through */
  72. const List *l2;
  73. int i; /* common element index */
  74. } ForBothState;
  75. typedef struct ForBothCellState
  76. {
  77. const List *l1; /* lists we're looping through */
  78. const List *l2;
  79. int i1; /* current element indexes */
  80. int i2;
  81. } ForBothCellState;
  82. typedef struct ForThreeState
  83. {
  84. const List *l1; /* lists we're looping through */
  85. const List *l2;
  86. const List *l3;
  87. int i; /* common element index */
  88. } ForThreeState;
  89. typedef struct ForFourState
  90. {
  91. const List *l1; /* lists we're looping through */
  92. const List *l2;
  93. const List *l3;
  94. const List *l4;
  95. int i; /* common element index */
  96. } ForFourState;
  97. typedef struct ForFiveState
  98. {
  99. const List *l1; /* lists we're looping through */
  100. const List *l2;
  101. const List *l3;
  102. const List *l4;
  103. const List *l5;
  104. int i; /* common element index */
  105. } ForFiveState;
  106. /*
  107. * These routines are small enough, and used often enough, to justify being
  108. * inline.
  109. */
  110. /* Fetch address of list's first cell; NULL if empty list */
  111. static inline ListCell *
  112. list_head(const List *l)
  113. {
  114. return l ? &l->elements[0] : NULL;
  115. }
  116. /* Fetch address of list's last cell; NULL if empty list */
  117. static inline ListCell *
  118. list_tail(const List *l)
  119. {
  120. return l ? &l->elements[l->length - 1] : NULL;
  121. }
  122. /* Fetch address of list's second cell, if it has one, else NULL */
  123. static inline ListCell *
  124. list_second_cell(const List *l)
  125. {
  126. if (l && l->length >= 2)
  127. return &l->elements[1];
  128. else
  129. return NULL;
  130. }
  131. /* Fetch list's length */
  132. static inline int
  133. list_length(const List *l)
  134. {
  135. return l ? l->length : 0;
  136. }
  137. /*
  138. * Macros to access the data values within List cells.
  139. *
  140. * Note that with the exception of the "xxx_node" macros, these are
  141. * lvalues and can be assigned to.
  142. *
  143. * NB: There is an unfortunate legacy from a previous incarnation of
  144. * the List API: the macro lfirst() was used to mean "the data in this
  145. * cons cell". To avoid changing every usage of lfirst(), that meaning
  146. * has been kept. As a result, lfirst() takes a ListCell and returns
  147. * the data it contains; to get the data in the first cell of a
  148. * List, use linitial(). Worse, lsecond() is more closely related to
  149. * linitial() than lfirst(): given a List, lsecond() returns the data
  150. * in the second list cell.
  151. */
  152. #define lfirst(lc) ((lc)->ptr_value)
  153. #define lfirst_int(lc) ((lc)->int_value)
  154. #define lfirst_oid(lc) ((lc)->oid_value)
  155. #define lfirst_node(type,lc) castNode(type, lfirst(lc))
  156. #define linitial(l) lfirst(list_nth_cell(l, 0))
  157. #define linitial_int(l) lfirst_int(list_nth_cell(l, 0))
  158. #define linitial_oid(l) lfirst_oid(list_nth_cell(l, 0))
  159. #define linitial_node(type,l) castNode(type, linitial(l))
  160. #define lsecond(l) lfirst(list_nth_cell(l, 1))
  161. #define lsecond_int(l) lfirst_int(list_nth_cell(l, 1))
  162. #define lsecond_oid(l) lfirst_oid(list_nth_cell(l, 1))
  163. #define lsecond_node(type,l) castNode(type, lsecond(l))
  164. #define lthird(l) lfirst(list_nth_cell(l, 2))
  165. #define lthird_int(l) lfirst_int(list_nth_cell(l, 2))
  166. #define lthird_oid(l) lfirst_oid(list_nth_cell(l, 2))
  167. #define lthird_node(type,l) castNode(type, lthird(l))
  168. #define lfourth(l) lfirst(list_nth_cell(l, 3))
  169. #define lfourth_int(l) lfirst_int(list_nth_cell(l, 3))
  170. #define lfourth_oid(l) lfirst_oid(list_nth_cell(l, 3))
  171. #define lfourth_node(type,l) castNode(type, lfourth(l))
  172. #define llast(l) lfirst(list_last_cell(l))
  173. #define llast_int(l) lfirst_int(list_last_cell(l))
  174. #define llast_oid(l) lfirst_oid(list_last_cell(l))
  175. #define llast_node(type,l) castNode(type, llast(l))
  176. /*
  177. * Convenience macros for building fixed-length lists
  178. */
  179. #define list_make_ptr_cell(v) ((ListCell) {.ptr_value = (v)})
  180. #define list_make_int_cell(v) ((ListCell) {.int_value = (v)})
  181. #define list_make_oid_cell(v) ((ListCell) {.oid_value = (v)})
  182. #define list_make1(x1) \
  183. list_make1_impl(T_List, list_make_ptr_cell(x1))
  184. #define list_make2(x1,x2) \
  185. list_make2_impl(T_List, list_make_ptr_cell(x1), list_make_ptr_cell(x2))
  186. #define list_make3(x1,x2,x3) \
  187. list_make3_impl(T_List, list_make_ptr_cell(x1), list_make_ptr_cell(x2), \
  188. list_make_ptr_cell(x3))
  189. #define list_make4(x1,x2,x3,x4) \
  190. list_make4_impl(T_List, list_make_ptr_cell(x1), list_make_ptr_cell(x2), \
  191. list_make_ptr_cell(x3), list_make_ptr_cell(x4))
  192. #define list_make5(x1,x2,x3,x4,x5) \
  193. list_make5_impl(T_List, list_make_ptr_cell(x1), list_make_ptr_cell(x2), \
  194. list_make_ptr_cell(x3), list_make_ptr_cell(x4), \
  195. list_make_ptr_cell(x5))
  196. #define list_make1_int(x1) \
  197. list_make1_impl(T_IntList, list_make_int_cell(x1))
  198. #define list_make2_int(x1,x2) \
  199. list_make2_impl(T_IntList, list_make_int_cell(x1), list_make_int_cell(x2))
  200. #define list_make3_int(x1,x2,x3) \
  201. list_make3_impl(T_IntList, list_make_int_cell(x1), list_make_int_cell(x2), \
  202. list_make_int_cell(x3))
  203. #define list_make4_int(x1,x2,x3,x4) \
  204. list_make4_impl(T_IntList, list_make_int_cell(x1), list_make_int_cell(x2), \
  205. list_make_int_cell(x3), list_make_int_cell(x4))
  206. #define list_make5_int(x1,x2,x3,x4,x5) \
  207. list_make5_impl(T_IntList, list_make_int_cell(x1), list_make_int_cell(x2), \
  208. list_make_int_cell(x3), list_make_int_cell(x4), \
  209. list_make_int_cell(x5))
  210. #define list_make1_oid(x1) \
  211. list_make1_impl(T_OidList, list_make_oid_cell(x1))
  212. #define list_make2_oid(x1,x2) \
  213. list_make2_impl(T_OidList, list_make_oid_cell(x1), list_make_oid_cell(x2))
  214. #define list_make3_oid(x1,x2,x3) \
  215. list_make3_impl(T_OidList, list_make_oid_cell(x1), list_make_oid_cell(x2), \
  216. list_make_oid_cell(x3))
  217. #define list_make4_oid(x1,x2,x3,x4) \
  218. list_make4_impl(T_OidList, list_make_oid_cell(x1), list_make_oid_cell(x2), \
  219. list_make_oid_cell(x3), list_make_oid_cell(x4))
  220. #define list_make5_oid(x1,x2,x3,x4,x5) \
  221. list_make5_impl(T_OidList, list_make_oid_cell(x1), list_make_oid_cell(x2), \
  222. list_make_oid_cell(x3), list_make_oid_cell(x4), \
  223. list_make_oid_cell(x5))
  224. /*
  225. * Locate the n'th cell (counting from 0) of the list.
  226. * It is an assertion failure if there is no such cell.
  227. */
  228. static inline ListCell *
  229. list_nth_cell(const List *list, int n)
  230. {
  231. Assert(list != NIL);
  232. Assert(n >= 0 && n < list->length);
  233. return &list->elements[n];
  234. }
  235. /*
  236. * Return the last cell in a non-NIL List.
  237. */
  238. static inline ListCell *
  239. list_last_cell(const List *list)
  240. {
  241. Assert(list != NIL);
  242. return &list->elements[list->length - 1];
  243. }
  244. /*
  245. * Return the pointer value contained in the n'th element of the
  246. * specified list. (List elements begin at 0.)
  247. */
  248. static inline void *
  249. list_nth(const List *list, int n)
  250. {
  251. Assert(IsA(list, List));
  252. return lfirst(list_nth_cell(list, n));
  253. }
  254. /*
  255. * Return the integer value contained in the n'th element of the
  256. * specified list.
  257. */
  258. static inline int
  259. list_nth_int(const List *list, int n)
  260. {
  261. Assert(IsA(list, IntList));
  262. return lfirst_int(list_nth_cell(list, n));
  263. }
  264. /*
  265. * Return the OID value contained in the n'th element of the specified
  266. * list.
  267. */
  268. static inline Oid
  269. list_nth_oid(const List *list, int n)
  270. {
  271. Assert(IsA(list, OidList));
  272. return lfirst_oid(list_nth_cell(list, n));
  273. }
  274. #define list_nth_node(type,list,n) castNode(type, list_nth(list, n))
  275. /*
  276. * Get the given ListCell's index (from 0) in the given List.
  277. */
  278. static inline int
  279. list_cell_number(const List *l, const ListCell *c)
  280. {
  281. Assert(c >= &l->elements[0] && c < &l->elements[l->length]);
  282. return c - l->elements;
  283. }
  284. /*
  285. * Get the address of the next cell after "c" within list "l", or NULL if none.
  286. */
  287. static inline ListCell *
  288. lnext(const List *l, const ListCell *c)
  289. {
  290. Assert(c >= &l->elements[0] && c < &l->elements[l->length]);
  291. c++;
  292. if (c < &l->elements[l->length])
  293. return (ListCell *) c;
  294. else
  295. return NULL;
  296. }
  297. /*
  298. * foreach -
  299. * a convenience macro for looping through a list
  300. *
  301. * "cell" must be the name of a "ListCell *" variable; it's made to point
  302. * to each List element in turn. "cell" will be NULL after normal exit from
  303. * the loop, but an early "break" will leave it pointing at the current
  304. * List element.
  305. *
  306. * Beware of changing the List object while the loop is iterating.
  307. * The current semantics are that we examine successive list indices in
  308. * each iteration, so that insertion or deletion of list elements could
  309. * cause elements to be re-visited or skipped unexpectedly. Previous
  310. * implementations of foreach() behaved differently. However, it's safe
  311. * to append elements to the List (or in general, insert them after the
  312. * current element); such new elements are guaranteed to be visited.
  313. * Also, the current element of the List can be deleted, if you use
  314. * foreach_delete_current() to do so. BUT: either of these actions will
  315. * invalidate the "cell" pointer for the remainder of the current iteration.
  316. */
  317. #define foreach(cell, lst) \
  318. for (ForEachState cell##__state = {(lst), 0}; \
  319. (cell##__state.l != NIL && \
  320. cell##__state.i < cell##__state.l->length) ? \
  321. (cell = &cell##__state.l->elements[cell##__state.i], true) : \
  322. (cell = NULL, false); \
  323. cell##__state.i++)
  324. /*
  325. * foreach_delete_current -
  326. * delete the current list element from the List associated with a
  327. * surrounding foreach() loop, returning the new List pointer.
  328. *
  329. * This is equivalent to list_delete_cell(), but it also adjusts the foreach
  330. * loop's state so that no list elements will be missed. Do not delete
  331. * elements from an active foreach loop's list in any other way!
  332. */
  333. #define foreach_delete_current(lst, cell) \
  334. (cell##__state.i--, \
  335. (List *) (cell##__state.l = list_delete_cell(lst, cell)))
  336. /*
  337. * foreach_current_index -
  338. * get the zero-based list index of a surrounding foreach() loop's
  339. * current element; pass the name of the "ListCell *" iterator variable.
  340. *
  341. * Beware of using this after foreach_delete_current(); the value will be
  342. * out of sync for the rest of the current loop iteration. Anyway, since
  343. * you just deleted the current element, the value is pretty meaningless.
  344. */
  345. #define foreach_current_index(cell) (cell##__state.i)
  346. /*
  347. * for_each_from -
  348. * Like foreach(), but start from the N'th (zero-based) list element,
  349. * not necessarily the first one.
  350. *
  351. * It's okay for N to exceed the list length, but not for it to be negative.
  352. *
  353. * The caveats for foreach() apply equally here.
  354. */
  355. #define for_each_from(cell, lst, N) \
  356. for (ForEachState cell##__state = for_each_from_setup(lst, N); \
  357. (cell##__state.l != NIL && \
  358. cell##__state.i < cell##__state.l->length) ? \
  359. (cell = &cell##__state.l->elements[cell##__state.i], true) : \
  360. (cell = NULL, false); \
  361. cell##__state.i++)
  362. static inline ForEachState
  363. for_each_from_setup(const List *lst, int N)
  364. {
  365. ForEachState r = {lst, N};
  366. Assert(N >= 0);
  367. return r;
  368. }
  369. /*
  370. * for_each_cell -
  371. * a convenience macro which loops through a list starting from a
  372. * specified cell
  373. *
  374. * The caveats for foreach() apply equally here.
  375. */
  376. #define for_each_cell(cell, lst, initcell) \
  377. for (ForEachState cell##__state = for_each_cell_setup(lst, initcell); \
  378. (cell##__state.l != NIL && \
  379. cell##__state.i < cell##__state.l->length) ? \
  380. (cell = &cell##__state.l->elements[cell##__state.i], true) : \
  381. (cell = NULL, false); \
  382. cell##__state.i++)
  383. static inline ForEachState
  384. for_each_cell_setup(const List *lst, const ListCell *initcell)
  385. {
  386. ForEachState r = {lst,
  387. initcell ? list_cell_number(lst, initcell) : list_length(lst)};
  388. return r;
  389. }
  390. /*
  391. * forboth -
  392. * a convenience macro for advancing through two linked lists
  393. * simultaneously. This macro loops through both lists at the same
  394. * time, stopping when either list runs out of elements. Depending
  395. * on the requirements of the call site, it may also be wise to
  396. * assert that the lengths of the two lists are equal. (But, if they
  397. * are not, some callers rely on the ending cell values being separately
  398. * NULL or non-NULL as defined here; don't try to optimize that.)
  399. *
  400. * The caveats for foreach() apply equally here.
  401. */
  402. #define forboth(cell1, list1, cell2, list2) \
  403. for (ForBothState cell1##__state = {(list1), (list2), 0}; \
  404. multi_for_advance_cell(cell1, cell1##__state, l1, i), \
  405. multi_for_advance_cell(cell2, cell1##__state, l2, i), \
  406. (cell1 != NULL && cell2 != NULL); \
  407. cell1##__state.i++)
  408. #define multi_for_advance_cell(cell, state, l, i) \
  409. (cell = (state.l != NIL && state.i < state.l->length) ? \
  410. &state.l->elements[state.i] : NULL)
  411. /*
  412. * for_both_cell -
  413. * a convenience macro which loops through two lists starting from the
  414. * specified cells of each. This macro loops through both lists at the same
  415. * time, stopping when either list runs out of elements. Depending on the
  416. * requirements of the call site, it may also be wise to assert that the
  417. * lengths of the two lists are equal, and initcell1 and initcell2 are at
  418. * the same position in the respective lists.
  419. *
  420. * The caveats for foreach() apply equally here.
  421. */
  422. #define for_both_cell(cell1, list1, initcell1, cell2, list2, initcell2) \
  423. for (ForBothCellState cell1##__state = \
  424. for_both_cell_setup(list1, initcell1, list2, initcell2); \
  425. multi_for_advance_cell(cell1, cell1##__state, l1, i1), \
  426. multi_for_advance_cell(cell2, cell1##__state, l2, i2), \
  427. (cell1 != NULL && cell2 != NULL); \
  428. cell1##__state.i1++, cell1##__state.i2++)
  429. static inline ForBothCellState
  430. for_both_cell_setup(const List *list1, const ListCell *initcell1,
  431. const List *list2, const ListCell *initcell2)
  432. {
  433. ForBothCellState r = {list1, list2,
  434. initcell1 ? list_cell_number(list1, initcell1) : list_length(list1),
  435. initcell2 ? list_cell_number(list2, initcell2) : list_length(list2)};
  436. return r;
  437. }
  438. /*
  439. * forthree -
  440. * the same for three lists
  441. */
  442. #define forthree(cell1, list1, cell2, list2, cell3, list3) \
  443. for (ForThreeState cell1##__state = {(list1), (list2), (list3), 0}; \
  444. multi_for_advance_cell(cell1, cell1##__state, l1, i), \
  445. multi_for_advance_cell(cell2, cell1##__state, l2, i), \
  446. multi_for_advance_cell(cell3, cell1##__state, l3, i), \
  447. (cell1 != NULL && cell2 != NULL && cell3 != NULL); \
  448. cell1##__state.i++)
  449. /*
  450. * forfour -
  451. * the same for four lists
  452. */
  453. #define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4) \
  454. for (ForFourState cell1##__state = {(list1), (list2), (list3), (list4), 0}; \
  455. multi_for_advance_cell(cell1, cell1##__state, l1, i), \
  456. multi_for_advance_cell(cell2, cell1##__state, l2, i), \
  457. multi_for_advance_cell(cell3, cell1##__state, l3, i), \
  458. multi_for_advance_cell(cell4, cell1##__state, l4, i), \
  459. (cell1 != NULL && cell2 != NULL && cell3 != NULL && cell4 != NULL); \
  460. cell1##__state.i++)
  461. /*
  462. * forfive -
  463. * the same for five lists
  464. */
  465. #define forfive(cell1, list1, cell2, list2, cell3, list3, cell4, list4, cell5, list5) \
  466. for (ForFiveState cell1##__state = {(list1), (list2), (list3), (list4), (list5), 0}; \
  467. multi_for_advance_cell(cell1, cell1##__state, l1, i), \
  468. multi_for_advance_cell(cell2, cell1##__state, l2, i), \
  469. multi_for_advance_cell(cell3, cell1##__state, l3, i), \
  470. multi_for_advance_cell(cell4, cell1##__state, l4, i), \
  471. multi_for_advance_cell(cell5, cell1##__state, l5, i), \
  472. (cell1 != NULL && cell2 != NULL && cell3 != NULL && \
  473. cell4 != NULL && cell5 != NULL); \
  474. cell1##__state.i++)
  475. /* Functions in src/backend/nodes/list.c */
  476. extern List *list_make1_impl(NodeTag t, ListCell datum1);
  477. extern List *list_make2_impl(NodeTag t, ListCell datum1, ListCell datum2);
  478. extern List *list_make3_impl(NodeTag t, ListCell datum1, ListCell datum2,
  479. ListCell datum3);
  480. extern List *list_make4_impl(NodeTag t, ListCell datum1, ListCell datum2,
  481. ListCell datum3, ListCell datum4);
  482. extern List *list_make5_impl(NodeTag t, ListCell datum1, ListCell datum2,
  483. ListCell datum3, ListCell datum4,
  484. ListCell datum5);
  485. extern pg_nodiscard List *lappend(List *list, void *datum);
  486. extern pg_nodiscard List *lappend_int(List *list, int datum);
  487. extern pg_nodiscard List *lappend_oid(List *list, Oid datum);
  488. extern pg_nodiscard List *list_insert_nth(List *list, int pos, void *datum);
  489. extern pg_nodiscard List *list_insert_nth_int(List *list, int pos, int datum);
  490. extern pg_nodiscard List *list_insert_nth_oid(List *list, int pos, Oid datum);
  491. extern pg_nodiscard List *lcons(void *datum, List *list);
  492. extern pg_nodiscard List *lcons_int(int datum, List *list);
  493. extern pg_nodiscard List *lcons_oid(Oid datum, List *list);
  494. extern pg_nodiscard List *list_concat(List *list1, const List *list2);
  495. extern pg_nodiscard List *list_concat_copy(const List *list1, const List *list2);
  496. extern pg_nodiscard List *list_truncate(List *list, int new_size);
  497. extern bool list_member(const List *list, const void *datum);
  498. extern bool list_member_ptr(const List *list, const void *datum);
  499. extern bool list_member_int(const List *list, int datum);
  500. extern bool list_member_oid(const List *list, Oid datum);
  501. extern pg_nodiscard List *list_delete(List *list, void *datum);
  502. extern pg_nodiscard List *list_delete_ptr(List *list, void *datum);
  503. extern pg_nodiscard List *list_delete_int(List *list, int datum);
  504. extern pg_nodiscard List *list_delete_oid(List *list, Oid datum);
  505. extern pg_nodiscard List *list_delete_first(List *list);
  506. extern pg_nodiscard List *list_delete_last(List *list);
  507. extern pg_nodiscard List *list_delete_first_n(List *list, int n);
  508. extern pg_nodiscard List *list_delete_nth_cell(List *list, int n);
  509. extern pg_nodiscard List *list_delete_cell(List *list, ListCell *cell);
  510. extern List *list_union(const List *list1, const List *list2);
  511. extern List *list_union_ptr(const List *list1, const List *list2);
  512. extern List *list_union_int(const List *list1, const List *list2);
  513. extern List *list_union_oid(const List *list1, const List *list2);
  514. extern List *list_intersection(const List *list1, const List *list2);
  515. extern List *list_intersection_int(const List *list1, const List *list2);
  516. /* currently, there's no need for list_intersection_ptr etc */
  517. extern List *list_difference(const List *list1, const List *list2);
  518. extern List *list_difference_ptr(const List *list1, const List *list2);
  519. extern List *list_difference_int(const List *list1, const List *list2);
  520. extern List *list_difference_oid(const List *list1, const List *list2);
  521. extern pg_nodiscard List *list_append_unique(List *list, void *datum);
  522. extern pg_nodiscard List *list_append_unique_ptr(List *list, void *datum);
  523. extern pg_nodiscard List *list_append_unique_int(List *list, int datum);
  524. extern pg_nodiscard List *list_append_unique_oid(List *list, Oid datum);
  525. extern pg_nodiscard List *list_concat_unique(List *list1, const List *list2);
  526. extern pg_nodiscard List *list_concat_unique_ptr(List *list1, const List *list2);
  527. extern pg_nodiscard List *list_concat_unique_int(List *list1, const List *list2);
  528. extern pg_nodiscard List *list_concat_unique_oid(List *list1, const List *list2);
  529. extern void list_deduplicate_oid(List *list);
  530. extern void list_free(List *list);
  531. extern void list_free_deep(List *list);
  532. extern pg_nodiscard List *list_copy(const List *list);
  533. extern pg_nodiscard List *list_copy_head(const List *oldlist, int len);
  534. extern pg_nodiscard List *list_copy_tail(const List *list, int nskip);
  535. extern pg_nodiscard List *list_copy_deep(const List *oldlist);
  536. typedef int (*list_sort_comparator) (const ListCell *a, const ListCell *b);
  537. extern void list_sort(List *list, list_sort_comparator cmp);
  538. extern int list_int_cmp(const ListCell *p1, const ListCell *p2);
  539. extern int list_oid_cmp(const ListCell *p1, const ListCell *p2);
  540. #endif /* PG_LIST_H */