Header And Logo

PostgreSQL
| The world's most advanced open source database.

_int.h

Go to the documentation of this file.
00001 /*
00002  * contrib/intarray/_int.h
00003  */
00004 #ifndef ___INT_H__
00005 #define ___INT_H__
00006 
00007 #include "utils/array.h"
00008 
00009 /* number ranges for compression */
00010 #define MAXNUMRANGE 100
00011 
00012 /* useful macros for accessing int4 arrays */
00013 #define ARRPTR(x)  ( (int32 *) ARR_DATA_PTR(x) )
00014 #define ARRNELEMS(x)  ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x))
00015 
00016 /* reject arrays we can't handle; to wit, those containing nulls */
00017 #define CHECKARRVALID(x) \
00018     do { \
00019         if (ARR_HASNULL(x) && array_contains_nulls(x)) \
00020             ereport(ERROR, \
00021                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \
00022                      errmsg("array must not contain nulls"))); \
00023     } while(0)
00024 
00025 #define ARRISEMPTY(x)  (ARRNELEMS(x) == 0)
00026 
00027 /* sort the elements of the array */
00028 #define SORT(x) \
00029     do { \
00030         int     _nelems_ = ARRNELEMS(x); \
00031         if (_nelems_ > 1) \
00032             isort(ARRPTR(x), _nelems_); \
00033     } while(0)
00034 
00035 /* sort the elements of the array and remove duplicates */
00036 #define PREPAREARR(x) \
00037     do { \
00038         int     _nelems_ = ARRNELEMS(x); \
00039         if (_nelems_ > 1) \
00040             if (isort(ARRPTR(x), _nelems_)) \
00041                 (x) = _int_unique(x); \
00042     } while(0)
00043 
00044 /* "wish" function */
00045 #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
00046 
00047 
00048 /* bigint defines */
00049 #define SIGLENINT  63           /* >122 => key will toast, so very slow!!! */
00050 #define SIGLEN  ( sizeof(int)*SIGLENINT )
00051 #define SIGLENBIT (SIGLEN*BITS_PER_BYTE)
00052 
00053 typedef char BITVEC[SIGLEN];
00054 typedef char *BITVECP;
00055 
00056 #define LOOPBYTE \
00057             for(i=0;i<SIGLEN;i++)
00058 
00059 /* beware of multiple evaluation of arguments to these macros! */
00060 #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
00061 #define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
00062 #define CLRBIT(x,i)   GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
00063 #define SETBIT(x,i)   GETBYTE(x,i) |=  ( 0x01 << ( (i) % BITS_PER_BYTE ) )
00064 #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
00065 #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
00066 #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
00067 
00068 /*
00069  * type of index key
00070  */
00071 typedef struct
00072 {
00073     int32       vl_len_;        /* varlena header (do not touch directly!) */
00074     int32       flag;
00075     char        data[1];
00076 } GISTTYPE;
00077 
00078 #define ALLISTRUE       0x04
00079 
00080 #define ISALLTRUE(x)    ( ((GISTTYPE*)x)->flag & ALLISTRUE )
00081 
00082 #define GTHDRSIZE       (VARHDRSZ + sizeof(int32))
00083 #define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
00084 
00085 #define GETSIGN(x)      ( (BITVECP)( (char*)x+GTHDRSIZE ) )
00086 
00087 /*
00088  * types for functions
00089  */
00090 typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
00091 typedef void (*formfloat) (ArrayType *, float *);
00092 
00093 /*
00094  * useful functions
00095  */
00096 bool        isort(int32 *a, int len);
00097 ArrayType  *new_intArrayType(int num);
00098 ArrayType  *copy_intArrayType(ArrayType *a);
00099 ArrayType  *resize_intArrayType(ArrayType *a, int num);
00100 int         internal_size(int *a, int len);
00101 ArrayType  *_int_unique(ArrayType *a);
00102 int32       intarray_match_first(ArrayType *a, int32 elem);
00103 ArrayType  *intarray_add_elem(ArrayType *a, int32 elem);
00104 ArrayType  *intarray_concat_arrays(ArrayType *a, ArrayType *b);
00105 ArrayType  *int_to_intset(int32 elem);
00106 bool        inner_int_overlap(ArrayType *a, ArrayType *b);
00107 bool        inner_int_contains(ArrayType *a, ArrayType *b);
00108 ArrayType  *inner_int_union(ArrayType *a, ArrayType *b);
00109 ArrayType  *inner_int_inter(ArrayType *a, ArrayType *b);
00110 void        rt__int_size(ArrayType *a, float *size);
00111 void        gensign(BITVEC sign, int *a, int len);
00112 
00113 
00114 /*****************************************************************************
00115  *          Boolean Search
00116  *****************************************************************************/
00117 
00118 #define BooleanSearchStrategy   20
00119 
00120 /*
00121  * item in polish notation with back link
00122  * to left operand
00123  */
00124 typedef struct ITEM
00125 {
00126     int16       type;
00127     int16       left;
00128     int32       val;
00129 } ITEM;
00130 
00131 typedef struct QUERYTYPE
00132 {
00133     int32       vl_len_;        /* varlena header (do not touch directly!) */
00134     int32       size;           /* number of ITEMs */
00135     ITEM        items[1];       /* variable length array */
00136 } QUERYTYPE;
00137 
00138 #define HDRSIZEQT   offsetof(QUERYTYPE, items)
00139 #define COMPUTESIZE(size)   ( HDRSIZEQT + (size) * sizeof(ITEM) )
00140 #define GETQUERY(x)  ( (x)->items )
00141 
00142 /* "type" codes for ITEM */
00143 #define END     0
00144 #define ERR     1
00145 #define VAL     2
00146 #define OPR     3
00147 #define OPEN    4
00148 #define CLOSE   5
00149 
00150 /* fmgr macros for QUERYTYPE objects */
00151 #define DatumGetQueryTypeP(X)         ((QUERYTYPE *) PG_DETOAST_DATUM(X))
00152 #define DatumGetQueryTypePCopy(X)     ((QUERYTYPE *) PG_DETOAST_DATUM_COPY(X))
00153 #define PG_GETARG_QUERYTYPE_P(n)      DatumGetQueryTypeP(PG_GETARG_DATUM(n))
00154 #define PG_GETARG_QUERYTYPE_P_COPY(n) DatumGetQueryTypePCopy(PG_GETARG_DATUM(n))
00155 
00156 bool        signconsistent(QUERYTYPE *query, BITVEC sign, bool calcnot);
00157 bool        execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot);
00158 
00159 bool        gin_bool_consistent(QUERYTYPE *query, bool *check);
00160 bool        query_has_required_values(QUERYTYPE *query);
00161 
00162 int         compASC(const void *a, const void *b);
00163 int         compDESC(const void *a, const void *b);
00164 
00165 /* sort, either ascending or descending */
00166 #define QSORT(a, direction) \
00167     do { \
00168         int     _nelems_ = ARRNELEMS(a); \
00169         if (_nelems_ > 1) \
00170             qsort((void*) ARRPTR(a), _nelems_, sizeof(int32), \
00171                   (direction) ? compASC : compDESC ); \
00172     } while(0)
00173 
00174 #endif   /* ___INT_H__ */