1 /*-------------------------------------------------------------------------
4 * implementation for PostgreSQL generic linked list package
7 * Portions Copyright (c) 2003-2008, PgPool Global Development Group
8 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $PostgreSQL: pgsql/src/backend/nodes/list.c,v 1.66 2005/10/15 02:49:18 momjian Exp $
15 *-------------------------------------------------------------------------
17 /*#include "postgres.h"*/
20 #include "pool_memory.h"
28 * Routines to simplify writing assertions about the type of a list; a
29 * NIL list is considered to be an empty list of any type.
31 #define IsPointerList(l) ((l) == NIL || IsA((l), List))
32 #define IsIntegerList(l) ((l) == NIL || IsA((l), IntList))
33 #define IsOidList(l) ((l) == NIL || IsA((l), OidList))
35 #define check_list_invariants(l)
38 * Return a freshly allocated List. Since empty non-NIL lists are
39 * invalid, new_list() also allocates the head cell of the new list:
40 * the caller should be sure to fill in that cell's data.
43 new_list(NodeTag type)
48 new_head = (ListCell *) palloc(sizeof(*new_head));
49 new_head->next = NULL;
50 /* new_head->data is left undefined! */
52 new_list = (List *) palloc(sizeof(*new_list));
53 new_list->type = type;
55 new_list->head = new_head;
56 new_list->tail = new_head;
62 * Allocate a new cell and make it the head of the specified
63 * list. Assumes the list it is passed is non-NIL.
65 * The data in the new head cell is undefined; the caller should be
69 new_head_cell(List *list)
73 new_head = (ListCell *) palloc(sizeof(*new_head));
74 new_head->next = list->head;
76 list->head = new_head;
81 * Allocate a new cell and make it the tail of the specified
82 * list. Assumes the list it is passed is non-NIL.
84 * The data in the new tail cell is undefined; the caller should be
88 new_tail_cell(List *list)
92 new_tail = (ListCell *) palloc(sizeof(*new_tail));
93 new_tail->next = NULL;
95 list->tail->next = new_tail;
96 list->tail = new_tail;
101 * Append a pointer to the list. A pointer to the modified list is
102 * returned. Note that this function may or may not destructively
103 * modify the list; callers should always use this function's return
104 * value, rather than continuing to use the pointer passed as the
108 lappend(List *list, void *datum)
111 list = new_list(T_List);
115 lfirst(list->tail) = datum;
116 check_list_invariants(list);
121 * Append an integer to the specified list. See lappend()
124 lappend_int(List *list, int datum)
127 list = new_list(T_IntList);
131 lfirst_int(list->tail) = datum;
132 check_list_invariants(list);
137 * Append an OID to the specified list. See lappend()
140 lappend_oid(List *list, Oid datum)
143 list = new_list(T_OidList);
147 lfirst_oid(list->tail) = datum;
148 check_list_invariants(list);
153 * Add a new cell to the list, in the position after 'prev_cell'. The
154 * data in the cell is left undefined, and must be filled in by the
155 * caller. 'list' is assumed to be non-NIL, and 'prev_cell' is assumed
156 * to be non-NULL and a member of 'list'.
159 add_new_cell(List *list, ListCell *prev_cell)
163 new_cell = (ListCell *) palloc(sizeof(*new_cell));
164 /* new_cell->data is left undefined! */
165 new_cell->next = prev_cell->next;
166 prev_cell->next = new_cell;
168 if (list->tail == prev_cell)
169 list->tail = new_cell;
177 * Add a new cell to the specified list (which must be non-NIL);
178 * it will be placed after the list cell 'prev' (which must be
179 * non-NULL and a member of 'list'). The data placed in the new cell
180 * is 'datum'. The newly-constructed cell is returned.
183 lappend_cell(List *list, ListCell *prev, void *datum)
187 new_cell = add_new_cell(list, prev);
188 lfirst(new_cell) = datum;
189 check_list_invariants(list);
194 lappend_cell_int(List *list, ListCell *prev, int datum)
198 new_cell = add_new_cell(list, prev);
199 lfirst_int(new_cell) = datum;
200 check_list_invariants(list);
205 lappend_cell_oid(List *list, ListCell *prev, Oid datum)
209 new_cell = add_new_cell(list, prev);
210 lfirst_oid(new_cell) = datum;
211 check_list_invariants(list);
216 * Prepend a new element to the list. A pointer to the modified list
217 * is returned. Note that this function may or may not destructively
218 * modify the list; callers should always use this function's return
219 * value, rather than continuing to use the pointer passed as the
222 * Caution: before Postgres 8.0, the original List was unmodified and
223 * could be considered to retain its separate identity. This is no longer
227 lcons(void *datum, List *list)
230 list = new_list(T_List);
234 lfirst(list->head) = datum;
235 check_list_invariants(list);
240 * Prepend an integer to the list. See lcons()
243 lcons_int(int datum, List *list)
246 list = new_list(T_IntList);
250 lfirst_int(list->head) = datum;
251 check_list_invariants(list);
256 * Prepend an OID to the list. See lcons()
259 lcons_oid(Oid datum, List *list)
262 list = new_list(T_OidList);
266 lfirst_oid(list->head) = datum;
267 check_list_invariants(list);
272 * Concatenate list2 to the end of list1, and return list1. list1 is
273 * destructively changed. Callers should be sure to use the return
274 * value as the new pointer to the concatenated list: the 'list1'
275 * input pointer may or may not be the same as the returned pointer.
277 * The nodes in list2 are merely appended to the end of list1 in-place
278 * (i.e. they aren't copied; the two lists will share some of the same
279 * storage). Therefore, invoking list_free() on list2 will also
280 * invalidate a portion of list1.
283 list_concat(List *list1, List *list2)
290 elog(ERROR, "cannot list_concat() a list to itself");
292 list1->length += list2->length;
293 list1->tail->next = list2->head;
294 list1->tail = list2->tail;
296 check_list_invariants(list1);
301 * Truncate 'list' to contain no more than 'new_size' elements. This
302 * modifies the list in-place! Despite this, callers should use the
303 * pointer returned by this function to refer to the newly truncated
304 * list -- it may or may not be the same as the pointer that was
307 * Note that any cells removed by list_truncate() are NOT pfree'd.
310 list_truncate(List *list, int new_size)
316 return NIL; /* truncate to zero length */
318 /* If asked to effectively extend the list, do nothing */
319 if (new_size >= list_length(list))
329 list->length = new_size;
330 check_list_invariants(list);
336 /* keep the compiler quiet; never reached */
341 * Locate the n'th cell (counting from 0) of the list. It is an assertion
342 * failure if there is no such cell.
345 list_nth_cell(List *list, int n)
349 check_list_invariants(list);
351 /* Does the caller actually mean to fetch the tail? */
352 if (n == list->length - 1)
355 for (match = list->head; n-- > 0; match = match->next)
362 * Return the data value contained in the n'th element of the
363 * specified list. (List elements begin at 0.)
366 list_nth(List *list, int n)
368 return lfirst(list_nth_cell(list, n));
372 * Return the integer value contained in the n'th element of the
376 list_nth_int(List *list, int n)
378 return lfirst_int(list_nth_cell(list, n));
382 * Return the OID value contained in the n'th element of the specified
386 list_nth_oid(List *list, int n)
388 return lfirst_oid(list_nth_cell(list, n));
393 * Return true iff 'datum' is a member of the list. Equality is
394 * determined via equal(), so callers should ensure that they pass a
398 list_member(List *list, void *datum)
402 check_list_invariants(list);
406 if (equal(lfirst(cell), datum))
415 * Return true iff 'datum' is a member of the list. Equality is
416 * determined by using simple pointer comparison.
419 list_member_ptr(List *list, void *datum)
423 check_list_invariants(list);
427 if (lfirst(cell) == datum)
435 * Return true iff the integer 'datum' is a member of the list.
438 list_member_int(List *list, int datum)
442 check_list_invariants(list);
446 if (lfirst_int(cell) == datum)
454 * Return true iff the OID 'datum' is a member of the list.
457 list_member_oid(List *list, Oid datum)
461 check_list_invariants(list);
465 if (lfirst_oid(cell) == datum)
473 * Delete 'cell' from 'list'; 'prev' is the previous element to 'cell'
474 * in 'list', if any (i.e. prev == NULL iff list->head == cell)
476 * The cell is pfree'd, as is the List header if this was the last member.
479 list_delete_cell(List *list, ListCell *cell, ListCell *prev)
481 check_list_invariants(list);
484 * If we're about to delete the last node from the list, free the whole
485 * list instead and return NIL, which is the only valid representation of
486 * a zero-length list.
488 if (list->length == 1)
495 * Otherwise, adjust the necessary list links, deallocate the particular
496 * node we have just removed, and return the list we were given.
501 prev->next = cell->next;
503 list->head = cell->next;
505 if (list->tail == cell)
514 * Delete the first cell in list that matches datum, if any.
515 * Equality is determined via equal().
518 list_delete(List *list, void *datum)
523 check_list_invariants(list);
528 if (equal(lfirst(cell), datum))
529 return list_delete_cell(list, cell, prev);
534 /* Didn't find a match: return the list unmodified */
539 /* As above, but use simple pointer equality */
541 list_delete_ptr(List *list, void *datum)
546 check_list_invariants(list);
551 if (lfirst(cell) == datum)
552 return list_delete_cell(list, cell, prev);
557 /* Didn't find a match: return the list unmodified */
561 /* As above, but for integers */
563 list_delete_int(List *list, int datum)
568 check_list_invariants(list);
573 if (lfirst_int(cell) == datum)
574 return list_delete_cell(list, cell, prev);
579 /* Didn't find a match: return the list unmodified */
583 /* As above, but for OIDs */
585 list_delete_oid(List *list, Oid datum)
590 check_list_invariants(list);
595 if (lfirst_oid(cell) == datum)
596 return list_delete_cell(list, cell, prev);
601 /* Didn't find a match: return the list unmodified */
606 * Delete the first element of the list.
608 * This is useful to replace the Lisp-y code "list = lnext(list);" in cases
609 * where the intent is to alter the list rather than just traverse it.
610 * Beware that the removed cell is freed, whereas the lnext() coding leaves
611 * the original list head intact if there's another pointer to it.
614 list_delete_first(List *list)
616 check_list_invariants(list);
619 return NIL; /* would an error be better? */
621 return list_delete_cell(list, list_head(list), NULL);
626 * Generate the union of two lists. This is calculated by copying
627 * list1 via list_copy(), then adding to it all the members of list2
628 * that aren't already in list1.
630 * Whether an element is already a member of the list is determined
633 * The returned list is newly-allocated, although the content of the
634 * cells is the same (i.e. any pointed-to objects are not copied).
636 * NB: this function will NOT remove any duplicates that are present
637 * in list1 (so it only performs a "union" if list1 is known unique to
638 * start with). Also, if you are about to write "x = list_union(x, y)"
639 * you probably want to use list_concat_unique() instead to avoid wasting
640 * the list cells of the old x list.
642 * This function could probably be implemented a lot faster if it is a
643 * performance bottleneck.
646 list_union(List *list1, List *list2)
651 result = list_copy(list1);
654 if (!list_member(result, lfirst(cell)))
655 result = lappend(result, lfirst(cell));
658 check_list_invariants(result);
664 * This variant of list_union() determines duplicates via simple
665 * pointer comparison.
668 list_union_ptr(List *list1, List *list2)
673 result = list_copy(list1);
676 if (!list_member_ptr(result, lfirst(cell)))
677 result = lappend(result, lfirst(cell));
680 check_list_invariants(result);
685 * This variant of list_union() operates upon lists of integers.
688 list_union_int(List *list1, List *list2)
693 result = list_copy(list1);
696 if (!list_member_int(result, lfirst_int(cell)))
697 result = lappend_int(result, lfirst_int(cell));
700 check_list_invariants(result);
705 * This variant of list_union() operates upon lists of OIDs.
708 list_union_oid(List *list1, List *list2)
713 result = list_copy(list1);
716 if (!list_member_oid(result, lfirst_oid(cell)))
717 result = lappend_oid(result, lfirst_oid(cell));
720 check_list_invariants(result);
726 * Return a list that contains all the cells in list1 that are not in
727 * list2. The returned list is freshly allocated via palloc(), but the
728 * cells themselves point to the same objects as the cells of the
731 * This variant works on lists of pointers, and determines list
732 * membership via equal()
735 list_difference(List *list1, List *list2)
741 return list_copy(list1);
745 if (!list_member(list2, lfirst(cell)))
746 result = lappend(result, lfirst(cell));
749 check_list_invariants(result);
755 * This variant of list_difference() determines list membership via
756 * simple pointer equality.
759 list_difference_ptr(List *list1, List *list2)
765 return list_copy(list1);
769 if (!list_member_ptr(list2, lfirst(cell)))
770 result = lappend(result, lfirst(cell));
773 check_list_invariants(result);
778 * This variant of list_difference() operates upon lists of integers.
781 list_difference_int(List *list1, List *list2)
787 return list_copy(list1);
791 if (!list_member_int(list2, lfirst_int(cell)))
792 result = lappend_int(result, lfirst_int(cell));
795 check_list_invariants(result);
800 * This variant of list_difference() operates upon lists of OIDs.
803 list_difference_oid(List *list1, List *list2)
809 return list_copy(list1);
813 if (!list_member_oid(list2, lfirst_oid(cell)))
814 result = lappend_oid(result, lfirst_oid(cell));
817 check_list_invariants(result);
823 * Append datum to list, but only if it isn't already in the list.
825 * Whether an element is already a member of the list is determined
829 list_append_unique(List *list, void *datum)
831 if (list_member(list, datum))
834 return lappend(list, datum);
839 * This variant of list_append_unique() determines list membership via
840 * simple pointer equality.
843 list_append_unique_ptr(List *list, void *datum)
845 if (list_member_ptr(list, datum))
848 return lappend(list, datum);
852 * This variant of list_append_unique() operates upon lists of integers.
855 list_append_unique_int(List *list, int datum)
857 if (list_member_int(list, datum))
860 return lappend_int(list, datum);
864 * This variant of list_append_unique() operates upon lists of OIDs.
867 list_append_unique_oid(List *list, Oid datum)
869 if (list_member_oid(list, datum))
872 return lappend_oid(list, datum);
877 * Append to list1 each member of list2 that isn't already in list1.
879 * Whether an element is already a member of the list is determined
882 * This is almost the same functionality as list_union(), but list1 is
883 * modified in-place rather than being copied. Note also that list2's cells
884 * are not inserted in list1, so the analogy to list_concat() isn't perfect.
887 list_concat_unique(List *list1, List *list2)
893 if (!list_member(list1, lfirst(cell)))
894 list1 = lappend(list1, lfirst(cell));
897 check_list_invariants(list1);
903 * This variant of list_concat_unique() determines list membership via
904 * simple pointer equality.
907 list_concat_unique_ptr(List *list1, List *list2)
913 if (!list_member_ptr(list1, lfirst(cell)))
914 list1 = lappend(list1, lfirst(cell));
917 check_list_invariants(list1);
922 * This variant of list_concat_unique() operates upon lists of integers.
925 list_concat_unique_int(List *list1, List *list2)
931 if (!list_member_int(list1, lfirst_int(cell)))
932 list1 = lappend_int(list1, lfirst_int(cell));
935 check_list_invariants(list1);
940 * This variant of list_concat_unique() operates upon lists of OIDs.
943 list_concat_unique_oid(List *list1, List *list2)
949 if (!list_member_oid(list1, lfirst_oid(cell)))
950 list1 = lappend_oid(list1, lfirst_oid(cell));
953 check_list_invariants(list1);
958 * Free all storage in a list, and optionally the pointed-to elements
961 list_free_private(List *list, bool deep)
965 check_list_invariants(list);
967 cell = list_head(list);
970 ListCell *tmp = cell;
983 * Free all the cells of the list, as well as the list itself. Any
984 * objects that are pointed-to by the cells of the list are NOT
987 * On return, the argument to this function has been freed, so the
988 * caller would be wise to set it to NIL for safety's sake.
991 list_free(List *list)
993 list_free_private(list, false);
997 * Free all the cells of the list, the list itself, and all the
998 * objects pointed-to by the cells of the list (each element in the
999 * list must contain a pointer to a palloc()'d region of memory!)
1001 * On return, the argument to this function has been freed, so the
1002 * caller would be wise to set it to NIL for safety's sake.
1005 list_free_deep(List *list)
1008 * A "deep" free operation only makes sense on a list of pointers.
1010 list_free_private(list, true);
1014 * Return a shallow copy of the specified list.
1017 list_copy(List *oldlist)
1020 ListCell *newlist_prev;
1021 ListCell *oldlist_cur;
1026 newlist = new_list(oldlist->type);
1027 newlist->length = oldlist->length;
1030 * Copy over the data in the first cell; new_list() has already allocated
1031 * the head cell itself
1033 newlist->head->data = oldlist->head->data;
1035 newlist_prev = newlist->head;
1036 oldlist_cur = oldlist->head->next;
1039 ListCell *newlist_cur;
1041 newlist_cur = (ListCell *) palloc(sizeof(*newlist_cur));
1042 newlist_cur->data = oldlist_cur->data;
1043 newlist_prev->next = newlist_cur;
1045 newlist_prev = newlist_cur;
1046 oldlist_cur = oldlist_cur->next;
1049 newlist_prev->next = NULL;
1050 newlist->tail = newlist_prev;
1052 check_list_invariants(newlist);
1057 * Return a shallow copy of the specified list, without the first N elements.
1060 list_copy_tail(List *oldlist, int nskip)
1063 ListCell *newlist_prev;
1064 ListCell *oldlist_cur;
1067 nskip = 0; /* would it be better to elog? */
1069 if (oldlist == NIL || nskip >= oldlist->length)
1072 newlist = new_list(oldlist->type);
1073 newlist->length = oldlist->length - nskip;
1076 * Skip over the unwanted elements.
1078 oldlist_cur = oldlist->head;
1080 oldlist_cur = oldlist_cur->next;
1083 * Copy over the data in the first remaining cell; new_list() has already
1084 * allocated the head cell itself
1086 newlist->head->data = oldlist_cur->data;
1088 newlist_prev = newlist->head;
1089 oldlist_cur = oldlist_cur->next;
1092 ListCell *newlist_cur;
1094 newlist_cur = (ListCell *) palloc(sizeof(*newlist_cur));
1095 newlist_cur->data = oldlist_cur->data;
1096 newlist_prev->next = newlist_cur;
1098 newlist_prev = newlist_cur;
1099 oldlist_cur = oldlist_cur->next;
1102 newlist_prev->next = NULL;
1103 newlist->tail = newlist_prev;
1105 check_list_invariants(newlist);
1110 * When using non-GCC compilers, we can't define these as inline
1111 * functions in pg_list.h, so they are defined here.
1113 * TODO: investigate supporting inlining for some non-GCC compilers.
1120 return l ? l->head : NULL;
1126 return l ? l->tail : NULL;
1130 list_length(List *l)
1132 return l ? l->length : 0;
1134 #endif /* ! __GNUC__ */
1137 * Temporary compatibility functions
1139 * In order to avoid warnings for these function definitions, we need
1140 * to include a prototype here as well as in pg_list.h. That's because
1141 * we don't enable list API compatibility in list.c, so we
1142 * don't see the prototypes for these functions.
1146 * Given a list, return its length. This is merely defined for the
1147 * sake of backward compatibility: we can't afford to define a macro
1148 * called "length", so it must be a function. New code should use the
1149 * list_length() macro in order to avoid the overhead of a function
1152 int length(List *list);
1157 return list_length(list);