pg_list.h 4.55 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * pg_list.h
4
 *	  POSTGRES generic list package
5 6
 *
 *
7
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $Id: pg_list.h,v 1.27 2002/04/09 20:35:55 tgl Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14 15
#ifndef PG_LIST_H
#define PG_LIST_H
16

17
#include "nodes/nodes.h"
18 19

/* ----------------------------------------------------------------
20
 *						node definitions
21 22 23 24
 * ----------------------------------------------------------------
 */

/*----------------------
25
 *		Value node
26 27 28 29
 *
 * The same Value struct is used for three node types: T_Integer,
 * T_Float, and T_String.  Integral values are actually represented
 * by a machine integer, but both floats and strings are represented
30
 * as strings.	Using T_Float as the node type simply indicates that
31 32 33 34
 * the contents of the string look like a valid numeric literal.
 *
 * (Before Postgres 7.0, we used a double to represent T_Float,
 * but that creates loss-of-precision problems when the value is
35
 * ultimately destined to be converted to NUMERIC.	Since Value nodes
36 37 38 39 40
 * are only used in the parsing process, not for runtime data, it's
 * better to use the more general representation.)
 *
 * Note that an integer-looking string will get lexed as T_Float if
 * the value is too large to fit in a 'long'.
41 42
 *----------------------
 */
43 44
typedef struct Value
{
45
	NodeTag		type;			/* tag appropriately (eg. T_String) */
46 47
	union ValUnion
	{
48
		long		ival;		/* machine integer */
49 50
		char	   *str;		/* string */
	}			val;
51
} Value;
52

53
#define intVal(v)		(((Value *)(v))->val.ival)
54
#define floatVal(v)		atof(((Value *)(v))->val.str)
55
#define strVal(v)		(((Value *)(v))->val.str)
56 57 58


/*----------------------
59
 *		List node
60 61
 *----------------------
 */
62 63
typedef struct List
{
64
	NodeTag		type;
65 66
	union
	{
67 68 69 70
		void	   *ptr_value;
		int			int_value;
	}			elem;
	struct List *next;
71
} List;
72 73

#define    NIL			((List *) NULL)
74 75

/* ----------------
76
 *		accessor macros
77 78
 * ----------------
 */
79 80

/* anything that doesn't end in 'i' is assumed to be referring to the */
81 82 83
/* pointer version of the list (where it makes a difference)		  */
#define lfirst(l)								((l)->elem.ptr_value)
#define lnext(l)								((l)->next)
84
#define lsecond(l)								lfirst(lnext(l))
85

86
#define lfirsti(l)								((l)->elem.int_value)
87

88 89
/*
 * foreach -
90
 *	  a convenience macro which loops through the list
91
 */
92
#define foreach(_elt_,_list_)	\
93
	for(_elt_=(_list_); _elt_!=NIL; _elt_=lnext(_elt_))
94

95 96 97 98 99 100 101 102 103 104 105 106
/*
 * Convenience macros for building fixed-length lists
 */
#define makeList1(x1)				lcons(x1, NIL)
#define makeList2(x1,x2)			lcons(x1, makeList1(x2))
#define makeList3(x1,x2,x3)			lcons(x1, makeList2(x2,x3))
#define makeList4(x1,x2,x3,x4)		lcons(x1, makeList3(x2,x3,x4))

#define makeListi1(x1)				lconsi(x1, NIL)
#define makeListi2(x1,x2)			lconsi(x1, makeListi1(x2))
#define makeListi3(x1,x2,x3)		lconsi(x1, makeListi2(x2,x3))
#define makeListi4(x1,x2,x3,x4)		lconsi(x1, makeListi3(x2,x3,x4))
107 108 109 110

/*
 * function prototypes in nodes/list.c
 */
111
extern int	length(List *list);
112 113
extern void *llast(List *list);
extern int	llasti(List *list);
114 115
extern List *nconc(List *list1, List *list2);
extern List *lcons(void *datum, List *list);
116 117
extern List *lconsi(int datum, List *list);
extern bool member(void *datum, List *list);
118
extern bool ptrMember(void *datum, List *list);
119
extern bool intMember(int datum, List *list);
120
extern Value *makeInteger(long i);
121
extern Value *makeFloat(char *numericStr);
122
extern Value *makeString(char *str);
123
extern Value *makeBitString(char *str);
124 125
extern List *lappend(List *list, void *datum);
extern List *lappendi(List *list, int datum);
126 127
extern List *lremove(void *elem, List *list);
extern List *LispRemove(void *elem, List *list);
128
extern List *lremovei(int elem, List *list);
129
extern List *ltruncate(int n, List *list);
130

131
extern void *nth(int n, List *l);
132
extern int	nthi(int n, List *l);
133
extern void set_nth(List *l, int n, void *elem);
134

135 136
extern List *set_difference(List *list1, List *list2);
extern List *set_differencei(List *list1, List *list2);
137
extern List *lreverse(List *l);
138 139
extern List *set_union(List *list1, List *list2);
extern List *set_unioni(List *list1, List *list2);
140

141
extern bool equali(List *list1, List *list2);
142 143 144
extern bool sameseti(List *list1, List *list2);
extern bool nonoverlap_setsi(List *list1, List *list2);
extern bool is_subseti(List *list1, List *list2);
145

146
extern void freeList(List *list);
147 148 149 150

/* should be in nodes.h but needs List */

/* in copyfuncs.c */
151
extern List *listCopy(List *list);
152

153
#endif   /* PG_LIST_H */