Eneboo - Documentación para desarrolladores
|
00001 /*------------------------------------------------------------------------- 00002 * 00003 * pg_list.h 00004 * interface for PostgreSQL generic linked list package 00005 * 00006 * This package implements singly-linked homogeneous lists. 00007 * 00008 * It is important to have constant-time length, append, and prepend 00009 * operations. To achieve this, we deal with two distinct data 00010 * structures: 00011 * 00012 * 1. A set of "list cells": each cell contains a data field and 00013 * a link to the next cell in the list or NULL. 00014 * 2. A single structure containing metadata about the list: the 00015 * type of the list, pointers to the head and tail cells, and 00016 * the length of the list. 00017 * 00018 * We support three types of lists: 00019 * 00020 * T_List: lists of pointers 00021 * (in practice usually pointers to Nodes, but not always; 00022 * declared as "void *" to minimize casting annoyances) 00023 * T_IntList: lists of integers 00024 * T_OidList: lists of Oids 00025 * 00026 * (At the moment, ints and Oids are the same size, but they may not 00027 * always be so; try to be careful to maintain the distinction.) 00028 * 00029 * 00030 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group 00031 * Portions Copyright (c) 1994, Regents of the University of California 00032 * 00033 * $PostgreSQL: pgsql/src/include/nodes/pg_list.h,v 1.53 2005/10/15 02:49:45 momjian Exp $ 00034 * 00035 *------------------------------------------------------------------------- 00036 */ 00037 #ifndef PG_LIST_H 00038 #define PG_LIST_H 00039 00040 #include "nodes/nodes.h" 00041 00042 00043 typedef struct ListCell ListCell; 00044 00045 typedef struct List 00046 { 00047 NodeTag type; /* T_List, T_IntList, or T_OidList */ 00048 int length; 00049 ListCell *head; 00050 ListCell *tail; 00051 } List; 00052 00053 struct ListCell 00054 { 00055 union 00056 { 00057 void *ptr_value; 00058 int int_value; 00059 Oid oid_value; 00060 } data; 00061 ListCell *next; 00062 }; 00063 00064 /* 00065 * The *only* valid representation of an empty list is NIL; in other 00066 * words, a non-NIL list is guaranteed to have length >= 1 and 00067 * head/tail != NULL 00068 */ 00069 #define NIL ((List *) NULL) 00070 00071 /* 00072 * These routines are used frequently. However, we can't implement 00073 * them as macros, since we want to avoid double-evaluation of macro 00074 * arguments. Therefore, we implement them using GCC inline functions, 00075 * and as regular functions with non-GCC compilers. 00076 */ 00077 #ifdef __GNUC__ 00078 00079 static __inline__ ListCell * 00080 list_head(List *l) 00081 { 00082 return l ? l->head : NULL; 00083 } 00084 00085 static __inline__ ListCell * 00086 list_tail(List *l) 00087 { 00088 return l ? l->tail : NULL; 00089 } 00090 00091 static __inline__ int 00092 list_length(List *l) 00093 { 00094 return l ? l->length : 0; 00095 } 00096 #else 00097 00098 extern ListCell *list_head(List *l); 00099 extern ListCell *list_tail(List *l); 00100 extern int list_length(List *l); 00101 #endif /* __GNUC__ */ 00102 00103 /* 00104 * NB: There is an unfortunate legacy from a previous incarnation of 00105 * the List API: the macro lfirst() was used to mean "the data in this 00106 * cons cell". To avoid changing every usage of lfirst(), that meaning 00107 * has been kept. As a result, lfirst() takes a ListCell and returns 00108 * the data it contains; to get the data in the first cell of a 00109 * List, use linitial(). Worse, lsecond() is more closely related to 00110 * linitial() than lfirst(): given a List, lsecond() returns the data 00111 * in the second cons cell. 00112 */ 00113 00114 #define lnext(lc) ((lc)->next) 00115 #define lfirst(lc) ((lc)->data.ptr_value) 00116 #define lfirst_int(lc) ((lc)->data.int_value) 00117 #define lfirst_oid(lc) ((lc)->data.oid_value) 00118 00119 #define linitial(l) lfirst(list_head(l)) 00120 #define linitial_int(l) lfirst_int(list_head(l)) 00121 #define linitial_oid(l) lfirst_oid(list_head(l)) 00122 00123 #define lsecond(l) lfirst(lnext(list_head(l))) 00124 #define lsecond_int(l) lfirst_int(lnext(list_head(l))) 00125 #define lsecond_oid(l) lfirst_oid(lnext(list_head(l))) 00126 00127 #define lthird(l) lfirst(lnext(lnext(list_head(l)))) 00128 #define lthird_int(l) lfirst_int(lnext(lnext(list_head(l)))) 00129 #define lthird_oid(l) lfirst_oid(lnext(lnext(list_head(l)))) 00130 00131 #define lfourth(l) lfirst(lnext(lnext(lnext(list_head(l))))) 00132 #define lfourth_int(l) lfirst_int(lnext(lnext(lnext(list_head(l))))) 00133 #define lfourth_oid(l) lfirst_oid(lnext(lnext(lnext(list_head(l))))) 00134 00135 #define llast(l) lfirst(list_tail(l)) 00136 #define llast_int(l) lfirst_int(list_tail(l)) 00137 #define llast_oid(l) lfirst_oid(list_tail(l)) 00138 00139 /* 00140 * Convenience macros for building fixed-length lists 00141 */ 00142 #define list_make1(x1) lcons(x1, NIL) 00143 #define list_make2(x1,x2) lcons(x1, list_make1(x2)) 00144 #define list_make3(x1,x2,x3) lcons(x1, list_make2(x2, x3)) 00145 #define list_make4(x1,x2,x3,x4) lcons(x1, list_make3(x2, x3, x4)) 00146 00147 #define list_make1_int(x1) lcons_int(x1, NIL) 00148 #define list_make2_int(x1,x2) lcons_int(x1, list_make1_int(x2)) 00149 #define list_make3_int(x1,x2,x3) lcons_int(x1, list_make2_int(x2, x3)) 00150 #define list_make4_int(x1,x2,x3,x4) lcons_int(x1, list_make3_int(x2, x3, x4)) 00151 00152 #define list_make1_oid(x1) lcons_oid(x1, NIL) 00153 #define list_make2_oid(x1,x2) lcons_oid(x1, list_make1_oid(x2)) 00154 #define list_make3_oid(x1,x2,x3) lcons_oid(x1, list_make2_oid(x2, x3)) 00155 #define list_make4_oid(x1,x2,x3,x4) lcons_oid(x1, list_make3_oid(x2, x3, x4)) 00156 00157 /* 00158 * foreach - 00159 * a convenience macro which loops through the list 00160 */ 00161 #define foreach(cell, l) \ 00162 for ((cell) = list_head(l); (cell) != NULL; (cell) = lnext(cell)) 00163 00164 /* 00165 * for_each_cell - 00166 * a convenience macro which loops through a list starting from a 00167 * specified cell 00168 */ 00169 #define for_each_cell(cell, initcell) \ 00170 for ((cell) = (initcell); (cell) != NULL; (cell) = lnext(cell)) 00171 00172 /* 00173 * forboth - 00174 * a convenience macro for advancing through two linked lists 00175 * simultaneously. This macro loops through both lists at the same 00176 * time, stopping when either list runs out of elements. Depending 00177 * on the requirements of the call site, it may also be wise to 00178 * assert that the lengths of the two lists are equal. 00179 */ 00180 #define forboth(cell1, list1, cell2, list2) \ 00181 for ((cell1) = list_head(list1), (cell2) = list_head(list2); \ 00182 (cell1) != NULL && (cell2) != NULL; \ 00183 (cell1) = lnext(cell1), (cell2) = lnext(cell2)) 00184 00185 extern List *lappend(List *list, void *datum); 00186 extern List *lappend_int(List *list, int datum); 00187 extern List *lappend_oid(List *list, Oid datum); 00188 00189 extern ListCell *lappend_cell(List *list, ListCell *prev, void *datum); 00190 extern ListCell *lappend_cell_int(List *list, ListCell *prev, int datum); 00191 extern ListCell *lappend_cell_oid(List *list, ListCell *prev, Oid datum); 00192 00193 extern List *lcons(void *datum, List *list); 00194 extern List *lcons_int(int datum, List *list); 00195 extern List *lcons_oid(Oid datum, List *list); 00196 00197 extern List *list_concat(List *list1, List *list2); 00198 extern List *list_truncate(List *list, int new_size); 00199 00200 extern void *list_nth(List *list, int n); 00201 extern int list_nth_int(List *list, int n); 00202 extern Oid list_nth_oid(List *list, int n); 00203 00204 extern bool list_member(List *list, void *datum); 00205 extern bool list_member_ptr(List *list, void *datum); 00206 extern bool list_member_int(List *list, int datum); 00207 extern bool list_member_oid(List *list, Oid datum); 00208 00209 extern List *list_delete(List *list, void *datum); 00210 extern List *list_delete_ptr(List *list, void *datum); 00211 extern List *list_delete_int(List *list, int datum); 00212 extern List *list_delete_oid(List *list, Oid datum); 00213 extern List *list_delete_first(List *list); 00214 extern List *list_delete_cell(List *list, ListCell *cell, ListCell *prev); 00215 00216 extern List *list_union(List *list1, List *list2); 00217 extern List *list_union_ptr(List *list1, List *list2); 00218 extern List *list_union_int(List *list1, List *list2); 00219 extern List *list_union_oid(List *list1, List *list2); 00220 00221 extern List *list_difference(List *list1, List *list2); 00222 extern List *list_difference_ptr(List *list1, List *list2); 00223 extern List *list_difference_int(List *list1, List *list2); 00224 extern List *list_difference_oid(List *list1, List *list2); 00225 00226 extern List *list_append_unique(List *list, void *datum); 00227 extern List *list_append_unique_ptr(List *list, void *datum); 00228 extern List *list_append_unique_int(List *list, int datum); 00229 extern List *list_append_unique_oid(List *list, Oid datum); 00230 00231 extern List *list_concat_unique(List *list1, List *list2); 00232 extern List *list_concat_unique_ptr(List *list1, List *list2); 00233 extern List *list_concat_unique_int(List *list1, List *list2); 00234 extern List *list_concat_unique_oid(List *list1, List *list2); 00235 00236 extern void list_free(List *list); 00237 extern void list_free_deep(List *list); 00238 00239 extern List *list_copy(List *list); 00240 extern List *list_copy_tail(List *list, int nskip); 00241 00242 /* 00243 * To ease migration to the new list API, a set of compatibility 00244 * macros are provided that reduce the impact of the list API changes 00245 * as far as possible. Until client code has been rewritten to use the 00246 * new list API, the ENABLE_LIST_COMPAT symbol can be defined before 00247 * including pg_list.h 00248 */ 00249 #ifdef ENABLE_LIST_COMPAT 00250 00251 #define lfirsti(lc) lfirst_int(lc) 00252 #define lfirsto(lc) lfirst_oid(lc) 00253 00254 #define makeList1(x1) list_make1(x1) 00255 #define makeList2(x1, x2) list_make2(x1, x2) 00256 #define makeList3(x1, x2, x3) list_make3(x1, x2, x3) 00257 #define makeList4(x1, x2, x3, x4) list_make4(x1, x2, x3, x4) 00258 00259 #define makeListi1(x1) list_make1_int(x1) 00260 #define makeListi2(x1, x2) list_make2_int(x1, x2) 00261 00262 #define makeListo1(x1) list_make1_oid(x1) 00263 #define makeListo2(x1, x2) list_make2_oid(x1, x2) 00264 00265 #define lconsi(datum, list) lcons_int(datum, list) 00266 #define lconso(datum, list) lcons_oid(datum, list) 00267 00268 #define lappendi(list, datum) lappend_int(list, datum) 00269 #define lappendo(list, datum) lappend_oid(list, datum) 00270 00271 #define nconc(l1, l2) list_concat(l1, l2) 00272 00273 #define nth(n, list) list_nth(list, n) 00274 00275 #define member(datum, list) list_member(list, datum) 00276 #define ptrMember(datum, list) list_member_ptr(list, datum) 00277 #define intMember(datum, list) list_member_int(list, datum) 00278 #define oidMember(datum, list) list_member_oid(list, datum) 00279 00280 /* 00281 * Note that the old lremove() determined equality via pointer 00282 * comparison, whereas the new list_delete() uses equal(); in order to 00283 * keep the same behavior, we therefore need to map lremove() calls to 00284 * list_delete_ptr() rather than list_delete() 00285 */ 00286 #define lremove(elem, list) list_delete_ptr(list, elem) 00287 #define LispRemove(elem, list) list_delete(list, elem) 00288 #define lremovei(elem, list) list_delete_int(list, elem) 00289 #define lremoveo(elem, list) list_delete_oid(list, elem) 00290 00291 #define ltruncate(n, list) list_truncate(list, n) 00292 00293 #define set_union(l1, l2) list_union(l1, l2) 00294 #define set_uniono(l1, l2) list_union_oid(l1, l2) 00295 #define set_ptrUnion(l1, l2) list_union_ptr(l1, l2) 00296 00297 #define set_difference(l1, l2) list_difference(l1, l2) 00298 #define set_differenceo(l1, l2) list_difference_oid(l1, l2) 00299 #define set_ptrDifference(l1, l2) list_difference_ptr(l1, l2) 00300 00301 #define equali(l1, l2) equal(l1, l2) 00302 #define equalo(l1, l2) equal(l1, l2) 00303 00304 #define freeList(list) list_free(list) 00305 00306 #define listCopy(list) list_copy(list) 00307 00308 extern int length(List *list); 00309 #endif /* ENABLE_LIST_COMPAT */ 00310 00311 #endif /* PG_LIST_H */