1 /* 2 * contrib/intarray/_int.h 3 */ 4 #ifndef ___INT_H__ 5 #define ___INT_H__ 6 7 #include "utils/array.h" 8 #include "utils/memutils.h" 9 10 /* number ranges for compression */ 11 #define MAXNUMRANGE 100 12 13 /* useful macros for accessing int4 arrays */ 14 #define ARRPTR(x) ( (int32 *) ARR_DATA_PTR(x) ) 15 #define ARRNELEMS(x) ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x)) 16 17 /* reject arrays we can't handle; to wit, those containing nulls */ 18 #define CHECKARRVALID(x) \ 19 do { \ 20 if (ARR_HASNULL(x) && array_contains_nulls(x)) \ 21 ereport(ERROR, \ 22 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \ 23 errmsg("array must not contain nulls"))); \ 24 } while(0) 25 26 #define ARRISEMPTY(x) (ARRNELEMS(x) == 0) 27 28 /* sort the elements of the array */ 29 #define SORT(x) \ 30 do { \ 31 int _nelems_ = ARRNELEMS(x); \ 32 if (_nelems_ > 1) \ 33 isort(ARRPTR(x), _nelems_); \ 34 } while(0) 35 36 /* sort the elements of the array and remove duplicates */ 37 #define PREPAREARR(x) \ 38 do { \ 39 int _nelems_ = ARRNELEMS(x); \ 40 if (_nelems_ > 1) \ 41 if (isort(ARRPTR(x), _nelems_)) \ 42 (x) = _int_unique(x); \ 43 } while(0) 44 45 /* "wish" function */ 46 #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) ) 47 48 49 /* bigint defines */ 50 #define SIGLENINT 63 /* >122 => key will toast, so very slow!!! */ 51 #define SIGLEN ( sizeof(int)*SIGLENINT ) 52 #define SIGLENBIT (SIGLEN*BITS_PER_BYTE) 53 54 typedef char BITVEC[SIGLEN]; 55 typedef char *BITVECP; 56 57 #define LOOPBYTE \ 58 for(i=0;i<SIGLEN;i++) 59 60 /* beware of multiple evaluation of arguments to these macros! */ 61 #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) ) 62 #define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 ) 63 #define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) ) 64 #define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) ) 65 #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 ) 66 #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT) 67 #define HASH(sign, val) SETBIT((sign), HASHVAL(val)) 68 69 /* 70 * type of index key 71 */ 72 typedef struct 73 { 74 int32 vl_len_; /* varlena header (do not touch directly!) */ 75 int32 flag; 76 char data[FLEXIBLE_ARRAY_MEMBER]; 77 } GISTTYPE; 78 79 #define ALLISTRUE 0x04 80 81 #define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE ) 82 83 #define GTHDRSIZE (VARHDRSZ + sizeof(int32)) 84 #define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) ) 85 86 #define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) ) 87 88 /* 89 * types for functions 90 */ 91 typedef ArrayType *(*formarray) (ArrayType *, ArrayType *); 92 typedef void (*formfloat) (ArrayType *, float *); 93 94 /* 95 * useful functions 96 */ 97 bool isort(int32 *a, int len); 98 ArrayType *new_intArrayType(int num); 99 ArrayType *copy_intArrayType(ArrayType *a); 100 ArrayType *resize_intArrayType(ArrayType *a, int num); 101 int internal_size(int *a, int len); 102 ArrayType *_int_unique(ArrayType *a); 103 int32 intarray_match_first(ArrayType *a, int32 elem); 104 ArrayType *intarray_add_elem(ArrayType *a, int32 elem); 105 ArrayType *intarray_concat_arrays(ArrayType *a, ArrayType *b); 106 ArrayType *int_to_intset(int32 elem); 107 bool inner_int_overlap(ArrayType *a, ArrayType *b); 108 bool inner_int_contains(ArrayType *a, ArrayType *b); 109 ArrayType *inner_int_union(ArrayType *a, ArrayType *b); 110 ArrayType *inner_int_inter(ArrayType *a, ArrayType *b); 111 void rt__int_size(ArrayType *a, float *size); 112 void gensign(BITVEC sign, int *a, int len); 113 114 115 /***************************************************************************** 116 * Boolean Search 117 *****************************************************************************/ 118 119 #define BooleanSearchStrategy 20 120 121 /* 122 * item in polish notation with back link 123 * to left operand 124 */ 125 typedef struct ITEM 126 { 127 int16 type; 128 int16 left; 129 int32 val; 130 } ITEM; 131 132 typedef struct QUERYTYPE 133 { 134 int32 vl_len_; /* varlena header (do not touch directly!) */ 135 int32 size; /* number of ITEMs */ 136 ITEM items[FLEXIBLE_ARRAY_MEMBER]; 137 } QUERYTYPE; 138 139 #define HDRSIZEQT offsetof(QUERYTYPE, items) 140 #define COMPUTESIZE(size) ( HDRSIZEQT + (size) * sizeof(ITEM) ) 141 #define QUERYTYPEMAXITEMS ((MaxAllocSize - HDRSIZEQT) / sizeof(ITEM)) 142 #define GETQUERY(x) ( (x)->items ) 143 144 /* "type" codes for ITEM */ 145 #define END 0 146 #define ERR 1 147 #define VAL 2 148 #define OPR 3 149 #define OPEN 4 150 #define CLOSE 5 151 152 /* fmgr macros for QUERYTYPE objects */ 153 #define DatumGetQueryTypeP(X) ((QUERYTYPE *) PG_DETOAST_DATUM(X)) 154 #define DatumGetQueryTypePCopy(X) ((QUERYTYPE *) PG_DETOAST_DATUM_COPY(X)) 155 #define PG_GETARG_QUERYTYPE_P(n) DatumGetQueryTypeP(PG_GETARG_DATUM(n)) 156 #define PG_GETARG_QUERYTYPE_P_COPY(n) DatumGetQueryTypePCopy(PG_GETARG_DATUM(n)) 157 158 bool signconsistent(QUERYTYPE *query, BITVEC sign, bool calcnot); 159 bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot); 160 161 bool gin_bool_consistent(QUERYTYPE *query, bool *check); 162 bool query_has_required_values(QUERYTYPE *query); 163 164 int compASC(const void *a, const void *b); 165 int compDESC(const void *a, const void *b); 166 167 /* sort, either ascending or descending */ 168 #define QSORT(a, direction) \ 169 do { \ 170 int _nelems_ = ARRNELEMS(a); \ 171 if (_nelems_ > 1) \ 172 qsort((void*) ARRPTR(a), _nelems_, sizeof(int32), \ 173 (direction) ? compASC : compDESC ); \ 174 } while(0) 175 176 #endif /* ___INT_H__ */ 177