1 /*-------------------------------------------------------------------------
4 * implementation for PostgreSQL generic linked list package
7 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/nodes/list.c,v 1.58 2004/05/30 23:40:27 neilc Exp $
14 *-------------------------------------------------------------------------
17 #include "nodes/pg_list.h"
20 * Routines to simplify writing assertions about the type of a list; a
21 * NIL list is considered to be an empty list of any type.
23 #define IsPointerList(l) ((l) == NIL || IsA((l), List))
24 #define IsIntegerList(l) ((l) == NIL || IsA((l), IntList))
25 #define IsOidList(l) ((l) == NIL || IsA((l), OidList))
27 #ifdef USE_ASSERT_CHECKING
29 * Check that the specified List is valid (so far as we can tell).
32 check_list_invariants(List *list)
37 Assert(list->length > 0);
38 Assert(list->head != NULL);
39 Assert(list->tail != NULL);
41 Assert(list->type == T_List ||
42 list->type == T_IntList ||
43 list->type == T_OidList);
45 if (list->length == 1)
46 Assert(list->head == list->tail);
47 if (list->length == 2)
48 Assert(list->head->next == list->tail);
49 Assert(list->tail->next == NULL);
52 #define check_list_invariants(l)
53 #endif /* USE_ASSERT_CHECKING */
56 * Return a freshly allocated List. Since empty non-NIL lists are
57 * invalid, new_list() also allocates the head cell of the new list:
58 * the caller should be sure to fill in that cell's data.
61 new_list(NodeTag type)
66 new_head = (ListCell *) palloc(sizeof(*new_head));
67 new_head->next = NULL;
68 /* new_head->data is left undefined! */
70 new_list = (List *) palloc(sizeof(*new_list));
71 new_list->type = type;
73 new_list->head = new_head;
74 new_list->tail = new_head;
80 * Allocate a new cell and make it the head of the specified
81 * list. Assumes the list it is passed is non-NIL.
83 * The data in the new head cell is undefined; the caller should be
87 new_head_cell(List *list)
91 new_head = (ListCell *) palloc(sizeof(*new_head));
92 new_head->next = list->head;
94 list->head = new_head;
99 * Allocate a new cell and make it the tail of the specified
100 * list. Assumes the list it is passed is non-NIL.
102 * The data in the new tail cell is undefined; the caller should be
106 new_tail_cell(List *list)
110 new_tail = (ListCell *) palloc(sizeof(*new_tail));
111 new_tail->next = NULL;
113 list->tail->next = new_tail;
114 list->tail = new_tail;
119 * Append a pointer to the list. A pointer to the modified list is
120 * returned. Note that this function may or may not destructively
121 * modify the list; callers should always use this function's return
122 * value, rather than continuing to use the pointer passed as the
126 lappend(List *list, void *datum)
128 Assert(IsPointerList(list));
131 list = new_list(T_List);
135 lfirst(list->tail) = datum;
136 check_list_invariants(list);
141 * Append an integer to the specified list. See lappend()
144 lappend_int(List *list, int datum)
146 Assert(IsIntegerList(list));
149 list = new_list(T_IntList);
153 lfirst_int(list->tail) = datum;
154 check_list_invariants(list);
159 * Append an OID to the specified list. See lappend()
162 lappend_oid(List *list, Oid datum)
164 Assert(IsOidList(list));
167 list = new_list(T_OidList);
171 lfirst_oid(list->tail) = datum;
172 check_list_invariants(list);
177 * Add a new cell to the list, in the position after 'prev_cell'. The
178 * data in the cell is left undefined, and must be filled in by the
179 * caller. 'list' is assumed to be non-NIL, and 'prev_cell' is assumed
180 * to be non-NULL and a member of 'list'.
183 add_new_cell(List *list, ListCell *prev_cell)
187 new_cell = (ListCell *) palloc(sizeof(*new_cell));
188 /* new_cell->data is left undefined! */
189 new_cell->next = prev_cell->next;
190 prev_cell->next = new_cell;
192 if (list->tail == prev_cell)
193 list->tail = new_cell;
201 * Add a new cell to the specified list (which must be non-NIL);
202 * it will be placed after the list cell 'prev' (which must be
203 * non-NULL and a member of 'list'). The data placed in the new cell
204 * is 'datum'. The newly-constructed cell is returned.
207 lappend_cell(List *list, ListCell *prev, void *datum)
211 Assert(IsPointerList(list));
213 new_cell = add_new_cell(list, prev);
214 lfirst(new_cell) = datum;
215 check_list_invariants(list);
220 lappend_cell_int(List *list, ListCell *prev, int datum)
224 Assert(IsIntegerList(list));
226 new_cell = add_new_cell(list, prev);
227 lfirst_int(new_cell) = datum;
228 check_list_invariants(list);
233 lappend_cell_oid(List *list, ListCell *prev, Oid datum)
237 Assert(IsOidList(list));
239 new_cell = add_new_cell(list, prev);
240 lfirst_oid(new_cell) = datum;
241 check_list_invariants(list);
246 * Prepend a new element to the list. A pointer to the modified list
247 * is returned. Note that this function may or may not destructively
248 * modify the list; callers should always use this function's return
249 * value, rather than continuing to use the pointer passed as the
252 * Caution: before Postgres 7.5, the original List was unmodified and
253 * could be considered to retain its separate identity. This is no longer
257 lcons(void *datum, List *list)
259 Assert(IsPointerList(list));
262 list = new_list(T_List);
266 lfirst(list->head) = datum;
267 check_list_invariants(list);
272 * Prepend an integer to the list. See lcons()
275 lcons_int(int datum, List *list)
277 Assert(IsIntegerList(list));
280 list = new_list(T_IntList);
284 lfirst_int(list->head) = datum;
285 check_list_invariants(list);
290 * Prepend an OID to the list. See lcons()
293 lcons_oid(Oid datum, List *list)
295 Assert(IsOidList(list));
298 list = new_list(T_OidList);
302 lfirst_oid(list->head) = datum;
303 check_list_invariants(list);
308 * Concatenate list2 to the end of list1, and return list1. list1 is
309 * destructively changed. Callers should be sure to use the return
310 * value as the new pointer to the concatenated list: the 'list1'
311 * input pointer may or may not be the same as the returned pointer.
313 * The nodes in list2 are merely appended to the end of list1 in-place
314 * (i.e. they aren't copied; the two lists will share some of the same
315 * storage). Therefore, invoking list_free() on list2 will also
316 * invalidate a portion of list1.
319 list_concat(List *list1, List *list2)
326 elog(ERROR, "cannot list_concat() a list to itself");
328 Assert(list1->type == list2->type);
330 list1->length += list2->length;
331 list1->tail->next = list2->head;
332 list1->tail = list2->tail;
334 check_list_invariants(list1);
339 * Truncate 'list' to contain no more than 'new_size' elements. This
340 * modifies the list in-place! Despite this, callers should use the
341 * pointer returned by this function to refer to the newly truncated
342 * list -- it may or may not be the same as the pointer that was
345 * Note that any cells removed by list_truncate() are NOT pfree'd.
348 list_truncate(List *list, int new_size)
354 return NIL; /* truncate to zero length */
356 /* If asked to effectively extend the list, do nothing */
357 if (new_size >= list_length(list))
367 list->length = new_size;
368 check_list_invariants(list);
374 /* keep the compiler quiet; never reached */
380 * Locate the n'th cell (counting from 0) of the list. It is an assertion
381 * error if there isn't one.
384 list_nth_cell(List *list, int n)
390 Assert(n < list->length);
391 check_list_invariants(list);
393 /* Does the caller actually mean to fetch the tail? */
394 if (n == list->length - 1)
397 for (match = list->head; n-- > 0; match = match->next)
404 * Return the data value contained in the n'th element of the
405 * specified list. (List elements begin at 0.)
408 list_nth(List *list, int n)
410 Assert(IsPointerList(list));
411 return lfirst(list_nth_cell(list, n));
415 * Return the integer value contained in the n'th element of the
419 list_nth_int(List *list, int n)
421 Assert(IsIntegerList(list));
422 return lfirst_int(list_nth_cell(list, n));
426 * Return the OID value contained in the n'th element of the specified
430 list_nth_oid(List *list, int n)
432 Assert(IsOidList(list));
433 return lfirst_oid(list_nth_cell(list, n));
437 * Return true iff 'datum' is a member of the list. Equality is
438 * determined via equal(), so callers should ensure that they pass a
442 list_member(List *list, void *datum)
446 Assert(IsPointerList(list));
447 check_list_invariants(list);
451 if (equal(lfirst(cell), datum))
459 * Return true iff 'datum' is a member of the list. Equality is
460 * determined by using simple pointer comparison.
463 list_member_ptr(List *list, void *datum)
467 Assert(IsPointerList(list));
468 check_list_invariants(list);
472 if (lfirst(cell) == datum)
480 * Return true iff the integer 'datum' is a member of the list.
483 list_member_int(List *list, int datum)
487 Assert(IsIntegerList(list));
488 check_list_invariants(list);
492 if (lfirst_int(cell) == datum)
500 * Return true iff the OID 'datum' is a member of the list.
503 list_member_oid(List *list, Oid datum)
507 Assert(IsOidList(list));
508 check_list_invariants(list);
512 if (lfirst_oid(cell) == datum)
520 * Delete 'cell' from 'list'; 'prev' is the previous element to 'cell'
521 * in 'list', if any (i.e. prev == NULL iff list->head == cell)
523 * The cell is pfree'd, as is the List header if this was the last member.
526 list_delete_cell(List *list, ListCell *cell, ListCell *prev)
528 check_list_invariants(list);
529 Assert(prev != NULL ? lnext(prev) == cell : list_head(list) == cell);
532 * If we're about to delete the last node from the list, free the
533 * whole list instead and return NIL, which is the only valid
534 * representation of a zero-length list.
536 if (list->length == 1)
543 * Otherwise, adjust the necessary list links, deallocate the
544 * particular node we have just removed, and return the list we
550 prev->next = cell->next;
552 list->head = cell->next;
554 if (list->tail == cell)
562 * Delete the first cell in list that matches datum, if any.
563 * Equality is determined via equal().
566 list_delete(List *list, void *datum)
571 Assert(IsPointerList(list));
572 check_list_invariants(list);
577 if (equal(lfirst(cell), datum))
578 return list_delete_cell(list, cell, prev);
583 /* Didn't find a match: return the list unmodified */
587 /* As above, but use simple pointer equality */
589 list_delete_ptr(List *list, void *datum)
594 Assert(IsPointerList(list));
595 check_list_invariants(list);
600 if (lfirst(cell) == datum)
601 return list_delete_cell(list, cell, prev);
606 /* Didn't find a match: return the list unmodified */
610 /* As above, but for integers */
612 list_delete_int(List *list, int datum)
617 Assert(IsIntegerList(list));
618 check_list_invariants(list);
623 if (lfirst_int(cell) == datum)
624 return list_delete_cell(list, cell, prev);
629 /* Didn't find a match: return the list unmodified */
633 /* As above, but for OIDs */
635 list_delete_oid(List *list, Oid datum)
640 Assert(IsOidList(list));
641 check_list_invariants(list);
646 if (lfirst_oid(cell) == datum)
647 return list_delete_cell(list, cell, prev);
652 /* Didn't find a match: return the list unmodified */
657 * Delete the first element of the list.
659 * This is useful to replace the Lisp-y code "list = lnext(list);" in cases
660 * where the intent is to alter the list rather than just traverse it.
661 * Beware that the removed cell is freed, whereas the lnext() coding leaves
662 * the original list head intact if there's another pointer to it.
665 list_delete_first(List *list)
667 check_list_invariants(list);
670 return NIL; /* would an error be better? */
672 return list_delete_cell(list, list_head(list), NULL);
676 * Generate the union of two lists. This is calculated by copying
677 * list1 via list_copy(), then adding to it all the members of list2
678 * that aren't already in list1.
680 * Whether an element is already a member of the list is determined
683 * The returned list is newly-allocated, although the content of the
684 * cells is the same (i.e. any pointed-to objects are not copied).
686 * NB: Bizarrely, this function will NOT remove any duplicates that
687 * are present in list1 (so it is not really a union at all!). Also,
688 * this function could probably be implemented a lot faster if it is a
689 * performance bottleneck.
692 list_union(List *list1, List *list2)
697 Assert(IsPointerList(list1));
698 Assert(IsPointerList(list2));
700 result = list_copy(list1);
703 if (!list_member(result, lfirst(cell)))
704 result = lappend(result, lfirst(cell));
707 check_list_invariants(result);
712 * This variant of list_union() determines duplicates via simple
713 * pointer comparison.
716 list_union_ptr(List *list1, List *list2)
721 Assert(IsPointerList(list1));
722 Assert(IsPointerList(list2));
724 result = list_copy(list1);
727 if (!list_member_ptr(result, lfirst(cell)))
728 result = lappend(result, lfirst(cell));
731 check_list_invariants(result);
736 * This variant of list_union() operates upon lists of integers.
739 list_union_int(List *list1, List *list2)
744 Assert(IsIntegerList(list1));
745 Assert(IsIntegerList(list2));
747 result = list_copy(list1);
750 if (!list_member_int(result, lfirst_int(cell)))
751 result = lappend_int(result, lfirst_int(cell));
754 check_list_invariants(result);
759 * This variant of list_union() operates upon lists of OIDs.
762 list_union_oid(List *list1, List *list2)
767 Assert(IsOidList(list1));
768 Assert(IsOidList(list2));
770 result = list_copy(list1);
773 if (!list_member_oid(result, lfirst_oid(cell)))
774 result = lappend_oid(result, lfirst_oid(cell));
777 check_list_invariants(result);
782 * Return a list that contains all the cells in list1 that are not in
783 * list2. The returned list is freshly allocated via palloc(), but the
784 * cells themselves point to the same objects as the cells of the
787 * This variant works on lists of pointers, and determines list
788 * membership via equal()
791 list_difference(List *list1, List *list2)
796 Assert(IsPointerList(list1));
797 Assert(IsPointerList(list2));
800 return list_copy(list1);
802 foreach (cell, list1)
804 if (!list_member(list2, lfirst(cell)))
805 result = lappend(result, lfirst(cell));
808 check_list_invariants(result);
813 * This variant of list_difference() determines list membership via
814 * simple pointer equality.
817 list_difference_ptr(List *list1, List *list2)
822 Assert(IsPointerList(list1));
823 Assert(IsPointerList(list2));
826 return list_copy(list1);
828 foreach (cell, list1)
830 if (!list_member_ptr(list2, lfirst(cell)))
831 result = lappend(result, lfirst(cell));
834 check_list_invariants(result);
839 * This variant of list_difference() operates upon lists of integers.
842 list_difference_int(List *list1, List *list2)
847 Assert(IsIntegerList(list1));
848 Assert(IsIntegerList(list2));
851 return list_copy(list1);
853 foreach (cell, list1)
855 if (!list_member_int(list2, lfirst_int(cell)))
856 result = lappend_int(result, lfirst_int(cell));
859 check_list_invariants(result);
864 * This variant of list_difference() operates upon lists of OIDs.
867 list_difference_oid(List *list1, List *list2)
872 Assert(IsOidList(list1));
873 Assert(IsOidList(list2));
876 return list_copy(list1);
878 foreach (cell, list1)
880 if (!list_member_oid(list2, lfirst_oid(cell)))
881 result = lappend_oid(result, lfirst_oid(cell));
884 check_list_invariants(result);
888 /* Free all storage in a list, and optionally the pointed-to elements */
890 list_free_private(List *list, bool deep)
894 check_list_invariants(list);
896 cell = list_head(list);
899 ListCell *tmp = cell;
912 * Free all the cells of the list, as well as the list itself. Any
913 * objects that are pointed-to by the cells of the list are NOT
916 * On return, the argument to this function has been freed, so the
917 * caller would be wise to set it to NIL for safety's sake.
920 list_free(List *list)
922 list_free_private(list, false);
926 * Free all the cells of the list, the list itself, and all the
927 * objects pointed-to by the cells of the list (each element in the
928 * list must contain a pointer to a palloc()'d region of memory!)
930 * On return, the argument to this function has been freed, so the
931 * caller would be wise to set it to NIL for safety's sake.
934 list_free_deep(List *list)
937 * A "deep" free operation only makes sense on a list of pointers.
939 Assert(IsPointerList(list));
940 list_free_private(list, true);
944 * Return a shallow copy of the specified list.
947 list_copy(List *oldlist)
950 ListCell *newlist_prev;
951 ListCell *oldlist_cur;
956 newlist = new_list(oldlist->type);
957 newlist->length = oldlist->length;
960 * Copy over the data in the first cell; new_list() has already
961 * allocated the head cell itself
963 newlist->head->data = oldlist->head->data;
965 newlist_prev = newlist->head;
966 oldlist_cur = oldlist->head->next;
969 ListCell *newlist_cur;
971 newlist_cur = (ListCell *) palloc(sizeof(*newlist_cur));
972 newlist_cur->data = oldlist_cur->data;
973 newlist_prev->next = newlist_cur;
975 newlist_prev = newlist_cur;
976 oldlist_cur = oldlist_cur->next;
979 newlist_prev->next = NULL;
980 newlist->tail = newlist_prev;
982 check_list_invariants(newlist);
987 * Return a shallow copy of the specified list, without the first N elements.
990 list_copy_tail(List *oldlist, int nskip)
993 ListCell *newlist_prev;
994 ListCell *oldlist_cur;
997 nskip = 0; /* would it be better to elog? */
999 if (oldlist == NIL || nskip >= oldlist->length)
1002 newlist = new_list(oldlist->type);
1003 newlist->length = oldlist->length - nskip;
1006 * Skip over the unwanted elements.
1008 oldlist_cur = oldlist->head;
1010 oldlist_cur = oldlist_cur->next;
1013 * Copy over the data in the first remaining cell; new_list() has already
1014 * allocated the head cell itself
1016 newlist->head->data = oldlist_cur->data;
1018 newlist_prev = newlist->head;
1019 oldlist_cur = oldlist_cur->next;
1022 ListCell *newlist_cur;
1024 newlist_cur = (ListCell *) palloc(sizeof(*newlist_cur));
1025 newlist_cur->data = oldlist_cur->data;
1026 newlist_prev->next = newlist_cur;
1028 newlist_prev = newlist_cur;
1029 oldlist_cur = oldlist_cur->next;
1032 newlist_prev->next = NULL;
1033 newlist->tail = newlist_prev;
1035 check_list_invariants(newlist);
1040 * When using non-GCC compilers, we can't define these as inline
1041 * functions in pg_list.h, so they are defined here.
1043 * TODO: investigate supporting inlining for some non-GCC compilers.
1050 return l ? l->head : NULL;
1056 return l ? l->tail : NULL;
1060 list_length(List *l)
1062 return l ? l->length : 0;
1065 #endif /* ! __GNUC__ */
1068 * Temporary compatibility functions
1070 * In order to avoid warnings for these function definitions, we need
1071 * to include a prototype here as well as in pg_list.h. That's because
1072 * we explicitly disable list API compatibility in list.c, so we
1073 * don't see the prototypes for these functions.
1077 * Given a list, return its length. This is merely defined for the
1078 * sake of backward compatibility: we can't afford to define a macro
1079 * called "length", so it must be a function. New code should use the
1080 * list_length() macro in order to avoid the overhead of a function
1083 int length(List *list);
1088 return list_length(list);
1092 * This code implements the old "Fast List" API, making use of the new
1093 * List code to do so. There's no need for FastList anymore, so this
1094 * code is a bit sloppy -- it will be removed soon.
1096 void FastListInit(FastList *fl);
1099 FastListInit(FastList *fl)
1104 void FastListFromList(FastList *fl, List *l);
1107 FastListFromList(FastList *fl, List *list)
1112 List *FastListValue(FastList *fl);
1115 FastListValue(FastList *fl)
1120 void makeFastList1(FastList *fl, void *elem);
1123 makeFastList1(FastList *fl, void *elem)
1125 fl->list = list_make1(elem);
1128 void FastAppend(FastList *fl, void *datum);
1131 FastAppend(FastList *fl, void *datum)
1133 fl->list = lappend(fl->list, datum);
1136 void FastAppendi(FastList *fl, int datum);
1139 FastAppendi(FastList *fl, int datum)
1141 fl->list = lappend_int(fl->list, datum);
1144 void FastAppendo(FastList *fl, Oid datum);
1147 FastAppendo(FastList *fl, Oid datum)
1149 fl->list = lappend_oid(fl->list, datum);
1152 void FastConc(FastList *fl, List *cells);
1155 FastConc(FastList *fl, List *cells)
1157 fl->list = list_concat(fl->list, cells);
1160 void FastConcFast(FastList *fl1, FastList *fl2);
1163 FastConcFast(FastList *fl1, FastList *fl2)
1165 fl1->list = list_concat(fl1->list, fl2->list);