|
| 1 | +#include"postgres.h" |
| 2 | + |
| 3 | +#include<float.h> |
| 4 | + |
| 5 | +#include"access/gist.h" |
| 6 | +#include"access/itup.h" |
| 7 | +#include"access/rtree.h" |
| 8 | +#include"catalog/pg_type.h" |
| 9 | +#include"utils/elog.h" |
| 10 | +#include"utils/palloc.h" |
| 11 | +#include"utils/array.h" |
| 12 | +#include"utils/builtins.h" |
| 13 | +#include"storage/bufpage.h" |
| 14 | +#include"lib/stringinfo.h" |
| 15 | + |
| 16 | +/* number ranges for compression */ |
| 17 | +#defineMAXNUMRANGE 100 |
| 18 | + |
| 19 | +#definemax(a,b)((a) >(b) ? (a) : (b)) |
| 20 | +#definemin(a,b)((a) <= (b) ? (a) : (b)) |
| 21 | +#defineabs(a)((a) <(0) ? -(a) : (a)) |
| 22 | + |
| 23 | +/* dimension of array */ |
| 24 | +#defineNDIM 1 |
| 25 | + |
| 26 | +/* |
| 27 | + * flags for gist__int_ops, use ArrayType->flags |
| 28 | + * which is unused (see array.h) |
| 29 | + */ |
| 30 | +#defineLEAFKEY(1<<31) |
| 31 | +#defineISLEAFKEY(x)( ((ArrayType*)(x))->flags & LEAFKEY ) |
| 32 | + |
| 33 | +/* useful macros for accessing int4 arrays */ |
| 34 | +#defineARRPTR(x) ( (int4 *) ARR_DATA_PTR(x) ) |
| 35 | +#defineARRNELEMS(x) ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x)) |
| 36 | + |
| 37 | +#defineARRISVOID(x) ( (x) ? ( ( ARR_NDIM(x) == NDIM ) ? ( ( ARRNELEMS( x ) ) ? 0 : 1 ) : ( ( ARR_NDIM(x) ) ? (elog(ERROR,"Array is not one-dimensional: %d dimensions",ARRNELEMS( x )),1) : 0 ) ) : 0 ) |
| 38 | + |
| 39 | +#defineSORT(x) \ |
| 40 | +do { \ |
| 41 | + if ( ARRNELEMS( x ) > 1 ) \ |
| 42 | +isort( ARRPTR( x ), ARRNELEMS( x ) ); \ |
| 43 | +} while(0) |
| 44 | + |
| 45 | +#definePREPAREARR(x) \ |
| 46 | +do { \ |
| 47 | + if ( ARRNELEMS( x ) > 1 ) \ |
| 48 | +if ( isort( ARRPTR( x ), ARRNELEMS( x ) ) ) \ |
| 49 | +x = _int_unique( x ); \ |
| 50 | +} while(0) |
| 51 | + |
| 52 | +/* "wish" function */ |
| 53 | +#defineWISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) ) |
| 54 | + |
| 55 | + |
| 56 | +/* bigint defines */ |
| 57 | +#defineBITBYTE 8 |
| 58 | +#defineSIGLENINT 63/* >122 => key will toast, so very slow!!! */ |
| 59 | +#defineSIGLEN( sizeof(int)*SIGLENINT ) |
| 60 | +#defineSIGLENBIT (SIGLEN*BITBYTE) |
| 61 | + |
| 62 | +typedefcharBITVEC[SIGLEN]; |
| 63 | +typedefchar*BITVECP; |
| 64 | + |
| 65 | +#defineSIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) ) |
| 66 | + |
| 67 | + |
| 68 | +#defineLOOPBYTE(a) \ |
| 69 | +for(i=0;i<SIGLEN;i++) {\ |
| 70 | +a;\ |
| 71 | +} |
| 72 | + |
| 73 | +#defineLOOPBIT(a) \ |
| 74 | +for(i=0;i<SIGLENBIT;i++) {\ |
| 75 | +a;\ |
| 76 | +} |
| 77 | + |
| 78 | +/* beware of multiple evaluation of arguments to these macros! */ |
| 79 | +#defineGETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) ) |
| 80 | +#defineGETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 ) |
| 81 | +#defineCLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) ) |
| 82 | +#defineSETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) ) |
| 83 | +#defineGETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 ) |
| 84 | +#defineHASHVAL(val) (((unsigned int)(val)) % SIGLENBIT) |
| 85 | +#defineHASH(sign,val) SETBIT((sign), HASHVAL(val)) |
| 86 | + |
| 87 | +/* |
| 88 | + * type of index key |
| 89 | + */ |
| 90 | +typedefstruct |
| 91 | +{ |
| 92 | +int4len; |
| 93 | +int4flag; |
| 94 | +chardata[1]; |
| 95 | +}GISTTYPE; |
| 96 | + |
| 97 | +#defineALLISTRUE 0x04 |
| 98 | + |
| 99 | +#defineISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE ) |
| 100 | + |
| 101 | +#defineGTHDRSIZE ( sizeof(int4)*2 ) |
| 102 | +#defineCALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) ) |
| 103 | + |
| 104 | +#defineGETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) ) |
| 105 | + |
| 106 | +/* |
| 107 | +** types for functions |
| 108 | +*/ |
| 109 | +typedefArrayType*(*formarray) (ArrayType*,ArrayType*); |
| 110 | +typedefvoid (*formfloat) (ArrayType*,float*); |
| 111 | + |
| 112 | +/* |
| 113 | +** useful function |
| 114 | +*/ |
| 115 | +boolisort(int4*a,constintlen); |
| 116 | +ArrayType*new_intArrayType(intnum); |
| 117 | +ArrayType*copy_intArrayType(ArrayType*a); |
| 118 | +ArrayType*resize_intArrayType(ArrayType*a,intnum); |
| 119 | +intinternal_size(int*a,intlen); |
| 120 | +ArrayType*_int_unique(ArrayType*a); |
| 121 | +int32intarray_match_first(ArrayType*a,int32elem); |
| 122 | +ArrayType*intarray_add_elem(ArrayType*a,int32elem); |
| 123 | +ArrayType*intarray_concat_arrays(ArrayType*a,ArrayType*b); |
| 124 | +ArrayType*int_to_intset(int32elem); |
| 125 | +boolinner_int_overlap(ArrayType*a,ArrayType*b); |
| 126 | +boolinner_int_contains(ArrayType*a,ArrayType*b); |
| 127 | +ArrayType*inner_int_union(ArrayType*a,ArrayType*b); |
| 128 | +ArrayType*inner_int_inter(ArrayType*a,ArrayType*b); |
| 129 | +voidrt__int_size(ArrayType*a,float*size); |
| 130 | +voidgensign(BITVECsign,int*a,intlen); |
| 131 | + |
| 132 | + |
| 133 | +/***************************************************************************** |
| 134 | + *Boolean Search |
| 135 | + *****************************************************************************/ |
| 136 | + |
| 137 | +#defineBooleanSearchStrategy20 |
| 138 | + |
| 139 | +/* |
| 140 | + * item in polish notation with back link |
| 141 | + * to left operand |
| 142 | + */ |
| 143 | +typedefstructITEM |
| 144 | +{ |
| 145 | +int2type; |
| 146 | +int2left; |
| 147 | +int4val; |
| 148 | +}ITEM; |
| 149 | + |
| 150 | +typedefstruct |
| 151 | +{ |
| 152 | +int4len; |
| 153 | +int4size; |
| 154 | +chardata[1]; |
| 155 | +}QUERYTYPE; |
| 156 | + |
| 157 | +#defineHDRSIZEQT( 2*sizeof(int4) ) |
| 158 | +#defineCOMPUTESIZE(size)( HDRSIZEQT + size * sizeof(ITEM) ) |
| 159 | +#defineGETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT ) |
| 160 | + |
| 161 | +boolsignconsistent(QUERYTYPE*query,BITVECsign,boolcalcnot); |
| 162 | +boolexecconsistent(QUERYTYPE*query,ArrayType*array,boolcalcnot); |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | +intcompASC(constvoid*a,constvoid*b); |
| 167 | + |
| 168 | +intcompDESC(constvoid*a,constvoid*b); |
| 169 | + |
| 170 | +#defineQSORT(a,direction) \ |
| 171 | +if (ARRNELEMS(a) > 1) \ |
| 172 | + qsort((void*)ARRPTR(a), ARRNELEMS(a),sizeof(int4), \ |
| 173 | + (direction) ? compASC : compDESC ) |
| 174 | + |
| 175 | + |