/* * Append to list1 each member of list2 that isn't already in list1. * * Whether an element is already a member of the list is determined * via equal(). * * This is almost the same functionality as list_union(), but list1 is * modified in-place rather than being copied. Note also that list2's cells * are not inserted in list1, so the analogy to list_concat() isn't perfect. */ List * list_concat_unique(List *list1, List *list2) { ListCell *cell; Assert(IsPointerList(list1)); Assert(IsPointerList(list2)); foreach(cell, list2) { if (!list_member(list1, lfirst(cell))) list1 = lappend(list1, lfirst(cell)); } check_list_invariants(list1); return list1; }
/* * Free all the cells of the list, the list itself, and all the * objects pointed-to by the cells of the list (each element in the * list must contain a pointer to a palloc()'d region of memory!) * * On return, the argument to this function has been freed, so the * caller would be wise to set it to NIL for safety's sake. */ void list_free_deep(List *list) { /* * A "deep" free operation only makes sense on a list of pointers. */ Assert(IsPointerList(list)); list_free_private(list, true); }
/* * Free all the cells of the list, the list itself, and all the * objects pointed-to by the cells of the list (each element in the * list must contain a pointer to a malloc()'d region of memory!) * * On return, the argument to this function has been freed, so the * caller would be wise to set it to NIL for safety's sake. */ void list_free_deep(List *list) { /* * A "deep" free operation only makes sense on a list of pointers. */ Assert(IsPointerList(list)); list_destroy(list, free); }
/* * This variant of list_union() determines duplicates via simple * pointer comparison. */ List * list_union_ptr(const List *list1, const List *list2) { List *result; const ListCell *cell; Assert(IsPointerList(list1)); Assert(IsPointerList(list2)); result = list_copy(list1); foreach(cell, list2) { if (!list_member_ptr(result, lfirst(cell))) result = lappend(result, lfirst(cell)); } check_list_invariants(result); return result; }
/* * Add a new cell to the specified list (which must be non-NIL); * it will be placed after the list cell 'prev' (which must be * non-NULL and a member of 'list'). The data placed in the new cell * is 'datum'. The newly-constructed cell is returned. */ ListCell * lappend_cell(List *list, ListCell *prev, void *datum) { ListCell *new_cell; Assert(IsPointerList(list)); new_cell = add_new_cell(list, prev); lfirst(new_cell) = datum; return new_cell; }
/* * This variant of list_difference() determines list membership via * simple pointer equality. */ List * list_difference_ptr(const List *list1, const List *list2) { const ListCell *cell; List *result = NIL; Assert(IsPointerList(list1)); Assert(IsPointerList(list2)); if (list2 == NIL) return list_copy(list1); foreach(cell, list1) { if (!list_member_ptr(list2, lfirst(cell))) result = lappend(result, lfirst(cell)); } check_list_invariants(result); return result; }
/* * Return a list that contains all the cells that are in both list1 and * list2. The returned list is freshly allocated via palloc(), but the * cells themselves point to the same objects as the cells of the * input lists. * * Duplicate entries in list1 will not be suppressed, so it's only a true * "intersection" if list1 is known unique beforehand. * * This variant works on lists of pointers, and determines list * membership via equal(). Note that the list1 member will be pointed * to in the result. */ List * list_intersection(const List *list1, const List *list2) { List *result; const ListCell *cell; if (list1 == NIL || list2 == NIL) return NIL; Assert(IsPointerList(list1)); Assert(IsPointerList(list2)); result = NIL; foreach(cell, list1) { if (list_member(list2, lfirst(cell))) result = lappend(result, lfirst(cell)); } check_list_invariants(result); return result; }
/* * Append a pointer to the list. A pointer to the modified list is * returned. Note that this function may or may not destructively * modify the list; callers should always use this function's return * value, rather than continuing to use the pointer passed as the * first argument. */ List * lappend(List *list, void *datum) { Assert(IsPointerList(list)); if (list == NIL) list = new_list(T_List); else new_tail_cell(list); lfirst(list->tail) = datum; return list; }
/* * Prepend a new element to the list. A pointer to the modified list * is returned. Note that this function may or may not destructively * modify the list; callers should always use this function's return * value, rather than continuing to use the pointer passed as the * second argument. * * Caution: before Postgres 8.0, the original List was unmodified and * could be considered to retain its separate identity. This is no longer * the case. */ List * lcons(void *datum, List *list) { Assert(IsPointerList(list)); if (list == NIL) list = new_list(T_List); else new_head_cell(list); lfirst(list->head) = datum; return list; }
/* * Return true iff 'datum' is a member of the list. Equality is * determined by using simple pointer comparison. */ bool list_member_ptr(List *list, void *datum) { ListCell *cell; Assert(IsPointerList(list)); foreach(cell, list) { if (lfirst(cell) == datum) return true; } return false; }
/* * Return true iff 'datum' is a member of the list. Equality is * determined by using simple pointer comparison. */ bool list_member_ptr(const List *list, const void *datum) { const ListCell *cell; Assert(IsPointerList(list)); check_list_invariants(list); foreach(cell, list) { if (lfirst(cell) == datum) return true; } return false; }
/* * Return true iff 'datum' is a member of the list. Equality is * determined via equal(), so callers should ensure that they pass a * Node as 'datum'. */ bool list_member(List *list, void *datum) { ListCell *cell; Assert(IsPointerList(list)); check_list_invariants(list); foreach(cell, list) { if (equal(lfirst(cell), datum)) return true; } return false; }
/* As above, but use simple pointer equality */ List * list_delete_ptr(List *list, void *datum) { ListCell *cell; ListCell *prev; Assert(IsPointerList(list)); prev = NULL; foreach(cell, list) { if (lfirst(cell) == datum) return list_delete_cell(list, cell, prev); prev = cell; } /* Didn't find a match: return the list unmodified */ return list; }
/* * Delete the first cell in list that matches datum, if any. * Equality is determined via equal(). */ List * list_delete(List *list, void *datum) { ListCell *cell; ListCell *prev; Assert(IsPointerList(list)); check_list_invariants(list); prev = NULL; foreach(cell, list) { if (equal(lfirst(cell), datum)) return list_delete_cell(list, cell, prev); prev = cell; } /* Didn't find a match: return the list unmodified */ return list; }
/* * Return the data value contained in the n'th element of the * specified list. (List elements begin at 0.) */ void * list_nth(const List *list, int n) { Assert(IsPointerList(list)); return lfirst(list_nth_cell(list, n)); }