Check nsl library for gethostbyname_r() on all platforms (HP-UX uses it
[PostgreSQL.git] / contrib / intarray / _int.h
blobe84bf03e4815b3468e2f1fa7b15e6698c02b5f48
1 /*
2 * $PostgreSQL:$
3 */
4 #ifndef ___INT_H__
5 #define ___INT_H__
7 #include "utils/array.h"
9 /* number ranges for compression */
10 #define MAXNUMRANGE 100
12 /* dimension of array */
13 #define NDIM 1
15 /* useful macros for accessing int4 arrays */
16 #define ARRPTR(x) ( (int4 *) ARR_DATA_PTR(x) )
17 #define ARRNELEMS(x) ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x))
19 /* reject arrays we can't handle; but allow a NULL or empty array */
20 #define CHECKARRVALID(x) \
21 do { \
22 if (x) { \
23 if (ARR_NDIM(x) != NDIM && ARR_NDIM(x) != 0) \
24 ereport(ERROR, \
25 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), \
26 errmsg("array must be one-dimensional"))); \
27 if (ARR_HASNULL(x)) \
28 ereport(ERROR, \
29 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \
30 errmsg("array must not contain nulls"))); \
31 } \
32 } while(0)
34 #define ARRISVOID(x) ((x) == NULL || ARRNELEMS(x) == 0)
36 #define SORT(x) \
37 do { \
38 if ( ARRNELEMS( x ) > 1 ) \
39 isort( ARRPTR( x ), ARRNELEMS( x ) ); \
40 } while(0)
42 #define PREPAREARR(x) \
43 do { \
44 if ( ARRNELEMS( x ) > 1 ) \
45 if ( isort( ARRPTR( x ), ARRNELEMS( x ) ) ) \
46 x = _int_unique( x ); \
47 } while(0)
49 /* "wish" function */
50 #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
53 /* bigint defines */
54 #define SIGLENINT 63 /* >122 => key will toast, so very slow!!! */
55 #define SIGLEN ( sizeof(int)*SIGLENINT )
56 #define SIGLENBIT (SIGLEN*BITS_PER_BYTE)
58 typedef char BITVEC[SIGLEN];
59 typedef char *BITVECP;
61 #define LOOPBYTE \
62 for(i=0;i<SIGLEN;i++)
64 /* beware of multiple evaluation of arguments to these macros! */
65 #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
66 #define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
67 #define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
68 #define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
69 #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
70 #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
71 #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
74 * type of index key
76 typedef struct
78 int32 vl_len_; /* varlena header (do not touch directly!) */
79 int4 flag;
80 char data[1];
81 } GISTTYPE;
83 #define ALLISTRUE 0x04
85 #define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
87 #define GTHDRSIZE (VARHDRSZ + sizeof(int4))
88 #define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
90 #define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
93 ** types for functions
95 typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
96 typedef void (*formfloat) (ArrayType *, float *);
99 ** useful function
101 bool isort(int4 *a, int len);
102 ArrayType *new_intArrayType(int num);
103 ArrayType *copy_intArrayType(ArrayType *a);
104 ArrayType *resize_intArrayType(ArrayType *a, int num);
105 int internal_size(int *a, int len);
106 ArrayType *_int_unique(ArrayType *a);
107 int32 intarray_match_first(ArrayType *a, int32 elem);
108 ArrayType *intarray_add_elem(ArrayType *a, int32 elem);
109 ArrayType *intarray_concat_arrays(ArrayType *a, ArrayType *b);
110 ArrayType *int_to_intset(int32 elem);
111 bool inner_int_overlap(ArrayType *a, ArrayType *b);
112 bool inner_int_contains(ArrayType *a, ArrayType *b);
113 ArrayType *inner_int_union(ArrayType *a, ArrayType *b);
114 ArrayType *inner_int_inter(ArrayType *a, ArrayType *b);
115 void rt__int_size(ArrayType *a, float *size);
116 void gensign(BITVEC sign, int *a, int len);
119 /*****************************************************************************
120 * Boolean Search
121 *****************************************************************************/
123 #define BooleanSearchStrategy 20
126 * item in polish notation with back link
127 * to left operand
129 typedef struct ITEM
131 int2 type;
132 int2 left;
133 int4 val;
134 } ITEM;
136 typedef struct
138 int32 vl_len_; /* varlena header (do not touch directly!) */
139 int4 size;
140 char data[1];
141 } QUERYTYPE;
143 #define HDRSIZEQT (VARHDRSZ + sizeof(int4))
144 #define COMPUTESIZE(size) ( HDRSIZEQT + size * sizeof(ITEM) )
145 #define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
147 #define END 0
148 #define ERR 1
149 #define VAL 2
150 #define OPR 3
151 #define OPEN 4
152 #define CLOSE 5
154 bool signconsistent(QUERYTYPE * query, BITVEC sign, bool calcnot);
155 bool execconsistent(QUERYTYPE * query, ArrayType *array, bool calcnot);
156 bool ginconsistent(QUERYTYPE * query, bool *check);
157 int4 shorterquery(ITEM * q, int4 len);
159 int compASC(const void *a, const void *b);
161 int compDESC(const void *a, const void *b);
163 #define QSORT(a, direction) \
164 if (ARRNELEMS(a) > 1) \
165 qsort((void*)ARRPTR(a), ARRNELEMS(a),sizeof(int4), \
166 (direction) ? compASC : compDESC )
168 #endif /* ___INT_H__ */