2008-05-17 09:28:26 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/intarray/_int.h
|
2008-05-17 09:28:26 +08:00
|
|
|
*/
|
2006-07-11 06:06:11 +08:00
|
|
|
#ifndef ___INT_H__
|
|
|
|
#define ___INT_H__
|
|
|
|
|
2003-06-12 03:31:05 +08:00
|
|
|
#include "utils/array.h"
|
2014-02-17 22:33:31 +08:00
|
|
|
#include "utils/memutils.h"
|
2003-06-12 03:31:05 +08:00
|
|
|
|
|
|
|
/* number ranges for compression */
|
|
|
|
#define MAXNUMRANGE 100
|
|
|
|
|
|
|
|
/* useful macros for accessing int4 arrays */
|
2012-06-25 06:51:46 +08:00
|
|
|
#define ARRPTR(x) ( (int32 *) ARR_DATA_PTR(x) )
|
2005-11-19 11:00:09 +08:00
|
|
|
#define ARRNELEMS(x) ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x))
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/* reject arrays we can't handle; to wit, those containing nulls */
|
2005-11-19 11:00:09 +08:00
|
|
|
#define CHECKARRVALID(x) \
|
|
|
|
do { \
|
2011-01-09 13:39:21 +08:00
|
|
|
if (ARR_HASNULL(x) && array_contains_nulls(x)) \
|
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \
|
|
|
|
errmsg("array must not contain nulls"))); \
|
2005-11-19 11:00:09 +08:00
|
|
|
} while(0)
|
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
#define ARRISEMPTY(x) (ARRNELEMS(x) == 0)
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/* sort the elements of the array */
|
2003-06-12 03:31:05 +08:00
|
|
|
#define SORT(x) \
|
|
|
|
do { \
|
2011-01-09 13:39:21 +08:00
|
|
|
int _nelems_ = ARRNELEMS(x); \
|
|
|
|
if (_nelems_ > 1) \
|
|
|
|
isort(ARRPTR(x), _nelems_); \
|
2003-06-12 03:31:05 +08:00
|
|
|
} while(0)
|
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/* sort the elements of the array and remove duplicates */
|
2003-06-12 03:31:05 +08:00
|
|
|
#define PREPAREARR(x) \
|
|
|
|
do { \
|
2011-01-09 13:39:21 +08:00
|
|
|
int _nelems_ = ARRNELEMS(x); \
|
|
|
|
if (_nelems_ > 1) \
|
|
|
|
if (isort(ARRPTR(x), _nelems_)) \
|
|
|
|
(x) = _int_unique(x); \
|
2003-06-12 03:31:05 +08:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* "wish" function */
|
|
|
|
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
|
|
|
|
|
|
|
|
|
|
|
|
/* bigint defines */
|
|
|
|
#define SIGLENINT 63 /* >122 => key will toast, so very slow!!! */
|
|
|
|
#define SIGLEN ( sizeof(int)*SIGLENINT )
|
2005-11-15 00:11:37 +08:00
|
|
|
#define SIGLENBIT (SIGLEN*BITS_PER_BYTE)
|
2003-06-12 03:31:05 +08:00
|
|
|
|
|
|
|
typedef char BITVEC[SIGLEN];
|
|
|
|
typedef char *BITVECP;
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
#define LOOPBYTE \
|
|
|
|
for(i=0;i<SIGLEN;i++)
|
2003-06-12 03:31:05 +08:00
|
|
|
|
|
|
|
/* beware of multiple evaluation of arguments to these macros! */
|
2005-11-15 00:11:37 +08:00
|
|
|
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
|
2003-06-12 03:31:05 +08:00
|
|
|
#define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
|
2005-11-15 00:11:37 +08:00
|
|
|
#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
|
|
|
|
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
|
|
|
|
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
|
2003-06-12 03:31:05 +08:00
|
|
|
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
|
|
|
|
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* type of index key
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2012-06-25 06:51:46 +08:00
|
|
|
int32 flag;
|
2015-02-20 13:11:42 +08:00
|
|
|
char data[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} GISTTYPE;
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
#define ALLISTRUE 0x04
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2012-06-25 06:51:46 +08:00
|
|
|
#define GTHDRSIZE (VARHDRSZ + sizeof(int32))
|
2003-06-12 03:31:05 +08:00
|
|
|
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
|
2003-06-12 03:31:05 +08:00
|
|
|
|
|
|
|
/*
|
2011-01-09 13:39:21 +08:00
|
|
|
* types for functions
|
|
|
|
*/
|
2003-06-12 03:31:05 +08:00
|
|
|
typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
|
|
|
|
typedef void (*formfloat) (ArrayType *, float *);
|
|
|
|
|
|
|
|
/*
|
2011-01-09 13:39:21 +08:00
|
|
|
* useful functions
|
|
|
|
*/
|
2012-06-25 06:51:46 +08:00
|
|
|
bool isort(int32 *a, int len);
|
2003-08-04 08:43:34 +08:00
|
|
|
ArrayType *new_intArrayType(int num);
|
|
|
|
ArrayType *copy_intArrayType(ArrayType *a);
|
|
|
|
ArrayType *resize_intArrayType(ArrayType *a, int num);
|
|
|
|
int internal_size(int *a, int len);
|
|
|
|
ArrayType *_int_unique(ArrayType *a);
|
|
|
|
int32 intarray_match_first(ArrayType *a, int32 elem);
|
|
|
|
ArrayType *intarray_add_elem(ArrayType *a, int32 elem);
|
|
|
|
ArrayType *intarray_concat_arrays(ArrayType *a, ArrayType *b);
|
|
|
|
ArrayType *int_to_intset(int32 elem);
|
|
|
|
bool inner_int_overlap(ArrayType *a, ArrayType *b);
|
|
|
|
bool inner_int_contains(ArrayType *a, ArrayType *b);
|
|
|
|
ArrayType *inner_int_union(ArrayType *a, ArrayType *b);
|
|
|
|
ArrayType *inner_int_inter(ArrayType *a, ArrayType *b);
|
|
|
|
void rt__int_size(ArrayType *a, float *size);
|
|
|
|
void gensign(BITVEC sign, int *a, int len);
|
2003-06-12 03:31:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Boolean Search
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#define BooleanSearchStrategy 20
|
|
|
|
|
|
|
|
/*
|
|
|
|
* item in polish notation with back link
|
|
|
|
* to left operand
|
|
|
|
*/
|
|
|
|
typedef struct ITEM
|
|
|
|
{
|
2012-06-25 06:51:46 +08:00
|
|
|
int16 type;
|
|
|
|
int16 left;
|
|
|
|
int32 val;
|
2009-06-11 22:49:15 +08:00
|
|
|
} ITEM;
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
typedef struct QUERYTYPE
|
2003-06-12 03:31:05 +08:00
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2012-06-25 06:51:46 +08:00
|
|
|
int32 size; /* number of ITEMs */
|
2015-02-20 13:11:42 +08:00
|
|
|
ITEM items[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} QUERYTYPE;
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
#define HDRSIZEQT offsetof(QUERYTYPE, items)
|
|
|
|
#define COMPUTESIZE(size) ( HDRSIZEQT + (size) * sizeof(ITEM) )
|
2014-02-17 22:33:31 +08:00
|
|
|
#define QUERYTYPEMAXITEMS ((MaxAllocSize - HDRSIZEQT) / sizeof(ITEM))
|
2011-01-09 13:39:21 +08:00
|
|
|
#define GETQUERY(x) ( (x)->items )
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/* "type" codes for ITEM */
|
2006-10-04 08:30:14 +08:00
|
|
|
#define END 0
|
|
|
|
#define ERR 1
|
|
|
|
#define VAL 2
|
|
|
|
#define OPR 3
|
|
|
|
#define OPEN 4
|
|
|
|
#define CLOSE 5
|
2006-05-04 00:31:07 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/* fmgr macros for QUERYTYPE objects */
|
|
|
|
#define DatumGetQueryTypeP(X) ((QUERYTYPE *) PG_DETOAST_DATUM(X))
|
|
|
|
#define DatumGetQueryTypePCopy(X) ((QUERYTYPE *) PG_DETOAST_DATUM_COPY(X))
|
|
|
|
#define PG_GETARG_QUERYTYPE_P(n) DatumGetQueryTypeP(PG_GETARG_DATUM(n))
|
|
|
|
#define PG_GETARG_QUERYTYPE_P_COPY(n) DatumGetQueryTypePCopy(PG_GETARG_DATUM(n))
|
|
|
|
|
2009-06-11 22:49:15 +08:00
|
|
|
bool signconsistent(QUERYTYPE *query, BITVEC sign, bool calcnot);
|
|
|
|
bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot);
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
bool gin_bool_consistent(QUERYTYPE *query, bool *check);
|
|
|
|
bool query_has_required_values(QUERYTYPE *query);
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
int compASC(const void *a, const void *b);
|
2003-08-04 08:43:34 +08:00
|
|
|
int compDESC(const void *a, const void *b);
|
2003-06-12 03:31:05 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/* sort, either ascending or descending */
|
|
|
|
#define QSORT(a, direction) \
|
|
|
|
do { \
|
|
|
|
int _nelems_ = ARRNELEMS(a); \
|
|
|
|
if (_nelems_ > 1) \
|
2012-06-25 06:51:46 +08:00
|
|
|
qsort((void*) ARRPTR(a), _nelems_, sizeof(int32), \
|
2011-01-09 13:39:21 +08:00
|
|
|
(direction) ? compASC : compDESC ); \
|
|
|
|
} while(0)
|
2006-07-11 06:06:11 +08:00
|
|
|
|
2009-06-11 22:49:15 +08:00
|
|
|
#endif /* ___INT_H__ */
|