2001-01-12 01:16:26 +01:00
|
|
|
/******************************************************************************
|
|
|
|
This file contains routines that can be bound to a Postgres backend and
|
|
|
|
called by the backend in the process of processing queries. The calling
|
|
|
|
format for these routines is dictated by Postgres architecture.
|
|
|
|
******************************************************************************/
|
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/*
|
2001-09-23 06:16:16 +02:00
|
|
|
#define BS_DEBUG
|
2001-03-20 04:08:12 +01:00
|
|
|
#define GIST_DEBUG
|
2001-03-22 16:24:12 +01:00
|
|
|
#define GIST_QUERY_DEBUG
|
2001-03-20 04:08:12 +01:00
|
|
|
*/
|
|
|
|
|
2001-03-19 00:36:55 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2001-01-12 01:16:26 +01:00
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "access/rtree.h"
|
|
|
|
#include "utils/elog.h"
|
|
|
|
#include "utils/palloc.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "storage/bufpage.h"
|
2002-09-02 08:11:43 +02:00
|
|
|
#include "lib/stringinfo.h"
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/* number ranges for compression */
|
2001-03-22 16:24:12 +01:00
|
|
|
#define MAXNUMRANGE 100
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
#define max(a,b) ((a) > (b) ? (a) : (b))
|
|
|
|
#define min(a,b) ((a) <= (b) ? (a) : (b))
|
|
|
|
#define abs(a) ((a) < (0) ? -(a) : (a))
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/* dimension of array */
|
|
|
|
#define NDIM 1
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
/*
|
|
|
|
* flags for gist__int_ops, use ArrayType->flags
|
2001-09-30 18:16:44 +02:00
|
|
|
* which is unused (see array.h)
|
|
|
|
*/
|
|
|
|
#define LEAFKEY (1<<31)
|
|
|
|
#define ISLEAFKEY(x) ( ((ArrayType*)(x))->flags & LEAFKEY )
|
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/* useful macros for accessing int4 arrays */
|
2001-01-12 01:16:26 +01:00
|
|
|
#define ARRPTR(x) ( (int4 *) ARR_DATA_PTR(x) )
|
2001-03-20 04:08:12 +01:00
|
|
|
#define ARRNELEMS(x) ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x))
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
#define ARRISVOID(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 )
|
2001-03-20 04:08:12 +01:00
|
|
|
|
|
|
|
#define SORT(x) \
|
|
|
|
do { \
|
|
|
|
if ( ARRNELEMS( x ) > 1 ) \
|
2001-06-11 04:31:25 +02:00
|
|
|
isort( ARRPTR( x ), ARRNELEMS( x ) ); \
|
2001-03-20 04:08:12 +01:00
|
|
|
} while(0)
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2001-01-12 01:16:26 +01:00
|
|
|
#define PREPAREARR(x) \
|
2001-03-20 04:08:12 +01:00
|
|
|
do { \
|
|
|
|
if ( ARRNELEMS( x ) > 1 ) \
|
2001-06-11 04:31:25 +02:00
|
|
|
if ( isort( ARRPTR( x ), ARRNELEMS( x ) ) ) \
|
2001-03-20 04:08:12 +01:00
|
|
|
x = _int_unique( x ); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* "wish" function */
|
|
|
|
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
/* bigint defines */
|
|
|
|
#define BITBYTE 8
|
2001-03-22 16:24:12 +01:00
|
|
|
#define SIGLENINT 64 /* >122 => key will toast, so very slow!!! */
|
|
|
|
#define SIGLEN ( sizeof(int)*SIGLENINT )
|
2001-03-17 22:59:42 +01:00
|
|
|
#define SIGLENBIT (SIGLEN*BITBYTE)
|
|
|
|
|
|
|
|
typedef char BITVEC[SIGLEN];
|
2001-03-22 16:24:12 +01:00
|
|
|
typedef char *BITVECP;
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
#define SIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) )
|
|
|
|
|
|
|
|
|
|
|
|
#define LOOPBYTE(a) \
|
2001-03-22 16:24:12 +01:00
|
|
|
for(i=0;i<SIGLEN;i++) {\
|
|
|
|
a;\
|
|
|
|
}
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
#define LOOPBIT(a) \
|
2001-03-22 16:24:12 +01:00
|
|
|
for(i=0;i<SIGLENBIT;i++) {\
|
|
|
|
a;\
|
|
|
|
}
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2001-03-19 00:36:55 +01:00
|
|
|
/* beware of multiple evaluation of arguments to these macros! */
|
2001-09-23 06:16:16 +02:00
|
|
|
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
|
2001-09-30 18:16:44 +02:00
|
|
|
#define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
|
2001-09-23 06:16:16 +02:00
|
|
|
#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
|
2001-09-30 18:16:44 +02:00
|
|
|
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
|
2001-09-23 06:16:16 +02:00
|
|
|
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
|
2001-01-12 01:16:26 +01:00
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 16:24:12 +01:00
|
|
|
static void
|
|
|
|
printarr(ArrayType *a, int num)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
StringInfoData bbb;
|
2001-03-22 16:24:12 +01:00
|
|
|
char *cur;
|
|
|
|
int l;
|
|
|
|
int *d;
|
|
|
|
|
|
|
|
d = ARRPTR(a);
|
2002-09-02 08:11:43 +02:00
|
|
|
initStringInfo(&bbb);
|
2001-03-22 16:24:12 +01:00
|
|
|
for (l = 0; l < min(num, ARRNELEMS(a)); l++)
|
2002-09-02 08:11:43 +02:00
|
|
|
appendStringInfo(&bbb, "%d ", d[l]);
|
|
|
|
elog(DEBUG3, "\t\t%s", bbb.data);
|
|
|
|
pfree(bbb.data);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
2001-03-22 16:24:12 +01:00
|
|
|
static void
|
|
|
|
printbitvec(BITVEC bv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char str[SIGLENBIT + 1];
|
|
|
|
|
|
|
|
str[SIGLENBIT] = '\0';
|
|
|
|
LOOPBIT(str[i] = (GETBIT(bv, i)) ? '1' : '0');
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "BV: %s", str);
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
2001-01-12 01:16:26 +01:00
|
|
|
#endif
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
/*
|
|
|
|
** types for functions
|
|
|
|
*/
|
2001-03-22 16:24:12 +01:00
|
|
|
typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
|
|
|
|
typedef void (*formfloat) (ArrayType *, float *);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2001-01-12 01:16:26 +01:00
|
|
|
/*
|
|
|
|
** usefull function
|
|
|
|
*/
|
2001-06-11 04:31:25 +02:00
|
|
|
static bool isort(int4 *a, const int len);
|
2001-03-22 16:24:12 +01:00
|
|
|
static ArrayType *new_intArrayType(int num);
|
|
|
|
static ArrayType *copy_intArrayType(ArrayType *a);
|
|
|
|
static ArrayType *resize_intArrayType(ArrayType *a, int num);
|
|
|
|
static int internal_size(int *a, int len);
|
|
|
|
static ArrayType *_int_unique(ArrayType *a);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/* common GiST function*/
|
2001-03-22 16:24:12 +01:00
|
|
|
static GIST_SPLITVEC *_int_common_picksplit(bytea *entryvec,
|
|
|
|
GIST_SPLITVEC *v,
|
|
|
|
formarray unionf,
|
|
|
|
formarray interf,
|
|
|
|
formfloat sizef,
|
|
|
|
float coef);
|
|
|
|
static float *_int_common_penalty(GISTENTRY *origentry,
|
|
|
|
GISTENTRY *newentry,
|
|
|
|
float *result,
|
|
|
|
formarray unionf,
|
|
|
|
formfloat sizef);
|
|
|
|
static ArrayType *_int_common_union(bytea *entryvec,
|
|
|
|
int *sizep,
|
|
|
|
formarray unionf);
|
|
|
|
|
|
|
|
/*
|
2001-01-12 01:16:26 +01:00
|
|
|
** GiST support methods
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FUNCTION_INFO_V1(g_int_consistent);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_decompress);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_penalty);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_picksplit);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_union);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_same);
|
|
|
|
|
|
|
|
Datum g_int_consistent(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_decompress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_penalty(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_same(PG_FUNCTION_ARGS);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-03-20 04:08:12 +01:00
|
|
|
** R-tree support functions
|
2001-01-12 01:16:26 +01:00
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
static bool inner_int_contains(ArrayType *a, ArrayType *b);
|
|
|
|
static bool inner_int_overlap(ArrayType *a, ArrayType *b);
|
|
|
|
static ArrayType *inner_int_union(ArrayType *a, ArrayType *b);
|
|
|
|
static ArrayType *inner_int_inter(ArrayType *a, ArrayType *b);
|
|
|
|
static void rt__int_size(ArrayType *a, float *sz);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(_int_different);
|
|
|
|
PG_FUNCTION_INFO_V1(_int_same);
|
|
|
|
PG_FUNCTION_INFO_V1(_int_contains);
|
|
|
|
PG_FUNCTION_INFO_V1(_int_contained);
|
|
|
|
PG_FUNCTION_INFO_V1(_int_overlap);
|
|
|
|
PG_FUNCTION_INFO_V1(_int_union);
|
|
|
|
PG_FUNCTION_INFO_V1(_int_inter);
|
|
|
|
|
|
|
|
Datum _int_different(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_same(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_contains(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_contained(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_overlap(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_inter(PG_FUNCTION_ARGS);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
/*
|
|
|
|
** _intbig methods
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FUNCTION_INFO_V1(g_intbig_consistent);
|
|
|
|
PG_FUNCTION_INFO_V1(g_intbig_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(g_intbig_decompress);
|
|
|
|
PG_FUNCTION_INFO_V1(g_intbig_penalty);
|
|
|
|
PG_FUNCTION_INFO_V1(g_intbig_picksplit);
|
|
|
|
PG_FUNCTION_INFO_V1(g_intbig_union);
|
|
|
|
PG_FUNCTION_INFO_V1(g_intbig_same);
|
|
|
|
|
|
|
|
Datum g_intbig_consistent(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_decompress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_penalty(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_same(PG_FUNCTION_ARGS);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
static bool _intbig_contains(ArrayType *a, ArrayType *b);
|
|
|
|
static bool _intbig_overlap(ArrayType *a, ArrayType *b);
|
|
|
|
static ArrayType *_intbig_union(ArrayType *a, ArrayType *b);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static ArrayType *_intbig_inter(ArrayType *a, ArrayType *b);
|
2001-03-22 16:24:12 +01:00
|
|
|
static void rt__intbig_size(ArrayType *a, float *sz);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2002-09-04 22:31:48 +02:00
|
|
|
* Boolean Search
|
2001-09-23 06:16:16 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#define BooleanSearchStrategy 20
|
|
|
|
|
|
|
|
/*
|
|
|
|
* item in polish notation with back link
|
|
|
|
* to left operand
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
typedef struct ITEM
|
|
|
|
{
|
|
|
|
int2 type;
|
|
|
|
int2 left;
|
|
|
|
int4 val;
|
|
|
|
} ITEM;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int4 len;
|
|
|
|
int4 size;
|
|
|
|
char data[1];
|
|
|
|
} QUERYTYPE;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
#define HDRSIZEQT ( 2*sizeof(int4) )
|
|
|
|
#define COMPUTESIZE(size) ( HDRSIZEQT + size * sizeof(ITEM) )
|
|
|
|
#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(bqarr_in);
|
|
|
|
PG_FUNCTION_INFO_V1(bqarr_out);
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum bqarr_in(PG_FUNCTION_ARGS);
|
|
|
|
Datum bqarr_out(PG_FUNCTION_ARGS);
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
PG_FUNCTION_INFO_V1(boolop);
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum boolop(PG_FUNCTION_ARGS);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(rboolop);
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum rboolop(PG_FUNCTION_ARGS);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(querytree);
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum querytree(PG_FUNCTION_ARGS);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static bool signconsistent(QUERYTYPE * query, BITVEC sign, bool leaf);
|
|
|
|
static bool execconsistent(QUERYTYPE * query, ArrayType *array, bool leaf);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 16:24:12 +01:00
|
|
|
* GiST functions
|
2001-01-12 01:16:26 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Consistent method for _intments
|
|
|
|
** Should return false if for all data items x below entry,
|
|
|
|
** the predicate x op query == FALSE, where op is the oper
|
|
|
|
** corresponding to strategy in the pg_amop table.
|
|
|
|
*/
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
g_int_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *query = (ArrayType *) PG_GETARG_POINTER(1);
|
2001-06-11 04:31:25 +02:00
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2001-03-22 16:24:12 +01:00
|
|
|
bool retval;
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (strategy == BooleanSearchStrategy)
|
|
|
|
PG_RETURN_BOOL(execconsistent((QUERYTYPE *) query,
|
|
|
|
(ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
ISLEAFKEY((ArrayType *) DatumGetPointer(entry->key))));
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/* XXX are we sure it's safe to scribble on the query object here? */
|
|
|
|
/* XXX what about toasted input? */
|
2001-09-30 18:16:44 +02:00
|
|
|
/* sort query for fast search, key is already sorted */
|
2002-09-04 22:31:48 +02:00
|
|
|
if (ARRISVOID(query))
|
2001-09-30 18:16:44 +02:00
|
|
|
PG_RETURN_BOOL(false);
|
2001-03-22 16:24:12 +01:00
|
|
|
PREPAREARR(query);
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
2001-05-31 20:16:55 +02:00
|
|
|
retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
2001-03-22 16:24:12 +01:00
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
2002-09-04 22:31:48 +02:00
|
|
|
if (GIST_LEAF(entry))
|
2002-06-07 23:52:33 +02:00
|
|
|
DirectFunctionCall3(
|
2002-09-04 22:31:48 +02:00
|
|
|
g_int_same,
|
|
|
|
entry->key,
|
|
|
|
PointerGetDatum(query),
|
|
|
|
PointerGetDatum(&retval)
|
|
|
|
);
|
2002-06-07 23:52:33 +02:00
|
|
|
else
|
|
|
|
retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
|
2002-09-04 22:31:48 +02:00
|
|
|
query);
|
2002-06-07 23:52:33 +02:00
|
|
|
break;
|
2001-03-22 16:24:12 +01:00
|
|
|
case RTContainsStrategyNumber:
|
2001-05-31 20:16:55 +02:00
|
|
|
retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
2001-03-22 16:24:12 +01:00
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2002-09-04 22:31:48 +02:00
|
|
|
if (GIST_LEAF(entry))
|
2001-09-30 18:16:44 +02:00
|
|
|
retval = inner_int_contains(query,
|
2002-09-04 22:31:48 +02:00
|
|
|
(ArrayType *) DatumGetPointer(entry->key));
|
2002-03-06 07:10:59 +01:00
|
|
|
else
|
2001-09-30 18:16:44 +02:00
|
|
|
retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
|
2002-09-04 22:31:48 +02:00
|
|
|
query);
|
2001-03-22 16:24:12 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_BOOL(retval);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_int_union(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(_int_common_union(
|
|
|
|
(bytea *) PG_GETARG_POINTER(0),
|
|
|
|
(int *) PG_GETARG_POINTER(1),
|
|
|
|
inner_int_union
|
|
|
|
));
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GiST Compress and Decompress methods
|
|
|
|
*/
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_int_compress(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2001-03-22 16:24:12 +01:00
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r;
|
|
|
|
int len;
|
|
|
|
int *dr;
|
|
|
|
int i,
|
|
|
|
min,
|
|
|
|
cand;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (entry->leafkey)
|
|
|
|
{
|
2001-05-31 20:16:55 +02:00
|
|
|
r = (ArrayType *) PG_DETOAST_DATUM_COPY(entry->key);
|
2001-09-30 18:16:44 +02:00
|
|
|
PREPAREARR(r);
|
|
|
|
r->flags |= LEAFKEY;
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
2002-09-04 22:31:48 +02:00
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
2001-08-04 21:35:32 +02:00
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_POINTER(retval);
|
2001-03-22 16:24:12 +01:00
|
|
|
}
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
r = (ArrayType *) PG_DETOAST_DATUM(entry->key);
|
2002-09-04 22:31:48 +02:00
|
|
|
if (ISLEAFKEY(r) || ARRISVOID(r))
|
|
|
|
{
|
|
|
|
if (r != (ArrayType *) DatumGetPointer(entry->key))
|
2001-09-30 18:16:44 +02:00
|
|
|
pfree(r);
|
|
|
|
PG_RETURN_POINTER(entry);
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if ((len = ARRNELEMS(r)) >= 2 * MAXNUMRANGE)
|
|
|
|
{ /* compress */
|
|
|
|
if (r == (ArrayType *) DatumGetPointer(entry->key))
|
2001-09-30 18:16:44 +02:00
|
|
|
r = (ArrayType *) PG_DETOAST_DATUM_COPY(entry->key);
|
2001-03-22 16:24:12 +01:00
|
|
|
r = resize_intArrayType(r, 2 * (len));
|
|
|
|
|
|
|
|
dr = ARRPTR(r);
|
|
|
|
|
|
|
|
for (i = len - 1; i >= 0; i--)
|
|
|
|
dr[2 * i] = dr[2 * i + 1] = dr[i];
|
|
|
|
|
|
|
|
len *= 2;
|
|
|
|
cand = 1;
|
|
|
|
while (len > MAXNUMRANGE * 2)
|
|
|
|
{
|
|
|
|
min = 0x7fffffff;
|
|
|
|
for (i = 2; i < len; i += 2)
|
|
|
|
if (min > (dr[i] - dr[i - 1]))
|
|
|
|
{
|
|
|
|
min = (dr[i] - dr[i - 1]);
|
|
|
|
cand = i;
|
|
|
|
}
|
|
|
|
memmove((void *) &dr[cand - 1], (void *) &dr[cand + 1], (len - cand - 1) * sizeof(int));
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
r = resize_intArrayType(r, len);
|
2001-09-30 18:16:44 +02:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
2002-09-04 22:31:48 +02:00
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
2001-09-30 18:16:44 +02:00
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
else
|
|
|
|
PG_RETURN_POINTER(entry);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
PG_RETURN_POINTER(entry);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_int_decompress(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2001-03-22 16:24:12 +01:00
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r;
|
|
|
|
int *dr,
|
|
|
|
lenr;
|
|
|
|
ArrayType *in;
|
|
|
|
int lenin;
|
|
|
|
int *din;
|
|
|
|
int i,
|
|
|
|
j;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (ARRISVOID(in))
|
2001-09-30 18:16:44 +02:00
|
|
|
PG_RETURN_POINTER(entry);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
lenin = ARRNELEMS(in);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (lenin < 2 * MAXNUMRANGE || ISLEAFKEY(in))
|
|
|
|
{ /* not comressed value */
|
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
|
|
|
{
|
2001-09-30 18:16:44 +02:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
2002-03-06 07:10:59 +01:00
|
|
|
gistentryinit(*retval, PointerGetDatum(in),
|
2002-09-04 22:31:48 +02:00
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(in), FALSE);
|
2001-09-30 18:16:44 +02:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(retval);
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_POINTER(entry);
|
2001-03-22 16:24:12 +01:00
|
|
|
}
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
din = ARRPTR(in);
|
2001-03-22 16:24:12 +01:00
|
|
|
lenr = internal_size(din, lenin);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
r = new_intArrayType(lenr);
|
|
|
|
dr = ARRPTR(r);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
for (i = 0; i < lenin; i += 2)
|
|
|
|
for (j = din[i]; j <= din[i + 1]; j++)
|
|
|
|
if ((!i) || *(dr - 1) != j)
|
|
|
|
*dr++ = j;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-05-31 20:16:55 +02:00
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
2001-03-22 16:24:12 +01:00
|
|
|
pfree(in);
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
2002-03-06 07:10:59 +01:00
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
2002-09-04 22:31:48 +02:00
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_POINTER(retval);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Penalty method for _intments
|
|
|
|
*/
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_int_penalty(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(_int_common_penalty(
|
|
|
|
(GISTENTRY *) PG_GETARG_POINTER(0),
|
|
|
|
(GISTENTRY *) PG_GETARG_POINTER(1),
|
|
|
|
(float *) PG_GETARG_POINTER(2),
|
|
|
|
inner_int_union, rt__int_size
|
|
|
|
));
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_int_picksplit(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(_int_common_picksplit(
|
|
|
|
(bytea *) PG_GETARG_POINTER(0),
|
|
|
|
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
|
|
|
|
inner_int_union,
|
|
|
|
inner_int_inter,
|
|
|
|
rt__int_size,
|
|
|
|
0.01
|
|
|
|
));
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Equality methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_int_same(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ArrayType *a = (ArrayType *) PointerGetDatum(PG_GETARG_POINTER(0));
|
|
|
|
ArrayType *b = (ArrayType *) PointerGetDatum(PG_GETARG_POINTER(1));
|
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
|
|
|
int4 n = ARRNELEMS(a);
|
|
|
|
int4 *da,
|
|
|
|
*db;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (n != ARRNELEMS(b))
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
*result = false;
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
*result = TRUE;
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2002-09-04 22:31:48 +02:00
|
|
|
while (n--)
|
|
|
|
if (*da++ != *db++)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
*result = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
Datum
|
2001-06-11 04:31:25 +02:00
|
|
|
_int_contained(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_BOOL(DatumGetBool(
|
|
|
|
DirectFunctionCall2(
|
|
|
|
_int_contains,
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(1)),
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(0))
|
|
|
|
)
|
|
|
|
));
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
_int_contains(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *b = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2001-03-22 16:24:12 +01:00
|
|
|
bool res;
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a) || ARRISVOID(b))
|
2001-03-22 16:24:12 +01:00
|
|
|
return FALSE;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
PREPAREARR(a);
|
|
|
|
PREPAREARR(b);
|
|
|
|
res = inner_int_contains(a, b);
|
|
|
|
pfree(a);
|
|
|
|
pfree(b);
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_BOOL(res);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
static bool
|
2001-03-22 16:24:12 +01:00
|
|
|
inner_int_contains(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
n;
|
|
|
|
int *da,
|
|
|
|
*db;
|
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a) || ARRISVOID(b))
|
2001-03-22 16:24:12 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "contains %d %d", na, nb);
|
2001-01-12 01:16:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
i = j = n = 0;
|
2001-03-22 16:24:12 +01:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 01:16:26 +01:00
|
|
|
i++;
|
2001-03-22 16:24:12 +01:00
|
|
|
else if (da[i] == db[j])
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else
|
2001-01-12 01:16:26 +01:00
|
|
|
j++;
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
return (n == nb) ? TRUE : FALSE;
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Operator class for R-tree indexing
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
Datum
|
2001-06-11 04:31:25 +02:00
|
|
|
_int_different(PG_FUNCTION_ARGS)
|
2001-01-12 01:16:26 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_BOOL(!DatumGetBool(
|
|
|
|
DirectFunctionCall2(
|
|
|
|
_int_same,
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(0)),
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(1))
|
|
|
|
)
|
|
|
|
));
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
_int_same(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *b = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2001-03-22 16:24:12 +01:00
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int n;
|
|
|
|
int *da,
|
|
|
|
*db;
|
|
|
|
bool result;
|
2001-09-30 18:16:44 +02:00
|
|
|
bool avoid = ARRISVOID(a);
|
|
|
|
bool bvoid = ARRISVOID(b);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (avoid || bvoid)
|
|
|
|
return (avoid && bvoid) ? TRUE : FALSE;
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
SORT(a);
|
|
|
|
SORT(b);
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
|
|
|
result = FALSE;
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
if (na == nb)
|
2001-03-20 04:08:12 +01:00
|
|
|
{
|
|
|
|
result = TRUE;
|
2001-03-22 16:24:12 +01:00
|
|
|
for (n = 0; n < na; n++)
|
|
|
|
if (da[n] != db[n])
|
|
|
|
{
|
|
|
|
result = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2001-03-20 04:08:12 +01:00
|
|
|
}
|
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
pfree(a);
|
|
|
|
pfree(b);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_BOOL(result);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
/* _int_overlap -- does a overlap b?
|
2001-01-12 01:16:26 +01:00
|
|
|
*/
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
_int_overlap(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *b = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2001-03-22 16:24:12 +01:00
|
|
|
bool result;
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a) || ARRISVOID(b))
|
2001-03-22 16:24:12 +01:00
|
|
|
return FALSE;
|
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
SORT(a);
|
|
|
|
SORT(b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
result = inner_int_overlap(a, b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
pfree(a);
|
|
|
|
pfree(b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_BOOL(result);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
static bool
|
2001-03-22 16:24:12 +01:00
|
|
|
inner_int_overlap(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int *da,
|
|
|
|
*db;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a) || ARRISVOID(b))
|
2001-03-22 16:24:12 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "g_int_overlap");
|
2001-01-12 01:16:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
i = j = 0;
|
2001-03-22 16:24:12 +01:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 01:16:26 +01:00
|
|
|
i++;
|
2001-03-22 16:24:12 +01:00
|
|
|
else if (da[i] == db[j])
|
|
|
|
return TRUE;
|
|
|
|
else
|
2001-01-12 01:16:26 +01:00
|
|
|
j++;
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-01-12 01:16:26 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
_int_union(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *b = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2001-03-22 16:24:12 +01:00
|
|
|
ArrayType *result;
|
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (!ARRISVOID(a))
|
2001-09-23 06:16:16 +02:00
|
|
|
SORT(a);
|
2001-09-30 18:16:44 +02:00
|
|
|
if (!ARRISVOID(b))
|
2001-09-23 06:16:16 +02:00
|
|
|
SORT(b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
result = inner_int_union(a, b);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
if (a)
|
|
|
|
pfree(a);
|
|
|
|
if (b)
|
|
|
|
pfree(b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static ArrayType *
|
2001-03-22 16:24:12 +01:00
|
|
|
inner_int_union(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r = NULL;
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int *da,
|
|
|
|
*db,
|
|
|
|
*dr;
|
|
|
|
int i,
|
|
|
|
j;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a) && ARRISVOID(b))
|
2001-03-22 16:24:12 +01:00
|
|
|
return new_intArrayType(0);
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a))
|
2001-03-22 16:24:12 +01:00
|
|
|
r = copy_intArrayType(b);
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(b))
|
2001-03-22 16:24:12 +01:00
|
|
|
r = copy_intArrayType(a);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
dr = ARRPTR(r);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
r = new_intArrayType(na + nb);
|
|
|
|
dr = ARRPTR(r);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
/* union */
|
2001-01-12 01:16:26 +01:00
|
|
|
i = j = 0;
|
2001-03-22 16:24:12 +01:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 01:16:26 +01:00
|
|
|
*dr++ = da[i++];
|
|
|
|
else
|
|
|
|
*dr++ = db[j++];
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
while (i < na)
|
|
|
|
*dr++ = da[i++];
|
|
|
|
while (j < nb)
|
|
|
|
*dr++ = db[j++];
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ARRNELEMS(r) > 1)
|
|
|
|
r = _int_unique(r);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
_int_inter(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *b = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2001-03-22 16:24:12 +01:00
|
|
|
ArrayType *result;
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a) || ARRISVOID(b))
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_POINTER(new_intArrayType(0));
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
SORT(a);
|
|
|
|
SORT(b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
result = inner_int_inter(a, b);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
pfree(a);
|
|
|
|
pfree(b);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static ArrayType *
|
2001-03-22 16:24:12 +01:00
|
|
|
inner_int_inter(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int *da,
|
|
|
|
*db,
|
|
|
|
*dr;
|
|
|
|
int i,
|
|
|
|
j;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(a) || ARRISVOID(b))
|
2001-03-22 16:24:12 +01:00
|
|
|
return new_intArrayType(0);
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
|
|
|
r = new_intArrayType(min(na, nb));
|
|
|
|
dr = ARRPTR(r);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
|
|
|
i = j = 0;
|
2001-03-22 16:24:12 +01:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 01:16:26 +01:00
|
|
|
i++;
|
2001-03-22 16:24:12 +01:00
|
|
|
else if (da[i] == db[j])
|
|
|
|
{
|
|
|
|
if (i + j == 0 || (i + j > 0 && *(dr - 1) != db[j]))
|
2001-01-12 01:16:26 +01:00
|
|
|
*dr++ = db[j];
|
2001-03-22 16:24:12 +01:00
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else
|
2001-01-12 01:16:26 +01:00
|
|
|
j++;
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
if ((dr - ARRPTR(r)) == 0)
|
|
|
|
{
|
|
|
|
pfree(r);
|
2001-03-20 04:08:12 +01:00
|
|
|
return new_intArrayType(0);
|
2001-03-22 16:24:12 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return resize_intArrayType(r, dr - ARRPTR(r));
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
static void
|
2001-01-12 01:16:26 +01:00
|
|
|
rt__int_size(ArrayType *a, float *size)
|
|
|
|
{
|
2001-09-30 18:16:44 +02:00
|
|
|
*size = (float) ARRNELEMS(a);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
return;
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 16:24:12 +01:00
|
|
|
* Miscellaneous operators and functions
|
2001-01-12 01:16:26 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* len >= 2 */
|
2001-03-22 16:24:12 +01:00
|
|
|
static bool
|
2001-06-11 04:31:25 +02:00
|
|
|
isort(int4 *a, int len)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
int4 tmp,
|
2001-03-22 16:24:12 +01:00
|
|
|
index;
|
2002-09-04 22:31:48 +02:00
|
|
|
int4 *cur,
|
2001-03-22 16:24:12 +01:00
|
|
|
*end;
|
|
|
|
bool r = FALSE;
|
|
|
|
|
|
|
|
end = a + len;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
cur = a + 1;
|
|
|
|
while (cur < end)
|
|
|
|
{
|
|
|
|
if (*(cur - 1) > *cur)
|
|
|
|
{
|
|
|
|
tmp = *(cur - 1);
|
|
|
|
*(cur - 1) = *cur;
|
|
|
|
*cur = tmp;
|
|
|
|
index = 1;
|
|
|
|
}
|
|
|
|
else if (!r && *(cur - 1) == *cur)
|
2001-01-12 01:16:26 +01:00
|
|
|
r = TRUE;
|
2001-03-22 16:24:12 +01:00
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
} while (index);
|
2001-01-12 01:16:26 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
static ArrayType *
|
|
|
|
new_intArrayType(int num)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
|
|
|
|
|
|
|
|
r = (ArrayType *) palloc(nbytes);
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-01-12 01:16:26 +01:00
|
|
|
MemSet(r, 0, nbytes);
|
|
|
|
r->size = nbytes;
|
|
|
|
r->ndim = NDIM;
|
2001-09-30 18:16:44 +02:00
|
|
|
r->flags &= ~LEAFKEY;
|
2001-03-22 16:24:12 +01:00
|
|
|
*((int *) ARR_DIMS(r)) = num;
|
|
|
|
*((int *) ARR_LBOUND(r)) = 1;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayType *
|
|
|
|
resize_intArrayType(ArrayType *a, int num)
|
|
|
|
{
|
|
|
|
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
if (num == ARRNELEMS(a))
|
|
|
|
return a;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
a = (ArrayType *) repalloc(a, nbytes);
|
2001-01-12 01:16:26 +01:00
|
|
|
|
|
|
|
a->size = nbytes;
|
2001-03-22 16:24:12 +01:00
|
|
|
*((int *) ARR_DIMS(a)) = num;
|
2001-01-12 01:16:26 +01:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
static ArrayType *
|
|
|
|
copy_intArrayType(ArrayType *a)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
|
|
|
|
r = new_intArrayType(ARRNELEMS(a));
|
|
|
|
memmove(r, a, VARSIZE(a));
|
2001-01-12 01:16:26 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* num for compressed key */
|
2001-03-22 16:24:12 +01:00
|
|
|
static int
|
|
|
|
internal_size(int *a, int len)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
size = 0;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
for (i = 0; i < len; i += 2)
|
|
|
|
if (!i || a[i] != a[i - 1]) /* do not count repeated range */
|
|
|
|
size += a[i + 1] - a[i] + 1;
|
2001-01-12 01:16:26 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
return size;
|
2001-01-12 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* r is sorted and size of r > 1 */
|
2001-03-22 16:24:12 +01:00
|
|
|
static ArrayType *
|
|
|
|
_int_unique(ArrayType *r)
|
|
|
|
{
|
|
|
|
int *tmp,
|
|
|
|
*dr,
|
|
|
|
*data;
|
|
|
|
int num = ARRNELEMS(r);
|
|
|
|
|
|
|
|
data = tmp = dr = ARRPTR(r);
|
|
|
|
while (tmp - data < num)
|
|
|
|
if (*tmp != *dr)
|
2001-01-12 01:16:26 +01:00
|
|
|
*(++dr) = *tmp++;
|
2001-03-22 16:24:12 +01:00
|
|
|
else
|
|
|
|
tmp++;
|
|
|
|
return resize_intArrayType(r, dr + 1 - ARRPTR(r));
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
** intbig functions
|
|
|
|
*********************************************************************/
|
2001-03-22 16:24:12 +01:00
|
|
|
static void
|
|
|
|
gensign(BITVEC sign, int *a, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* we assume that the sign vector is previously zeroed */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
HASH(sign, *a);
|
2001-03-22 16:24:12 +01:00
|
|
|
a++;
|
|
|
|
}
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
static bool
|
|
|
|
_intbig_overlap(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BITVECP da,
|
|
|
|
db;
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
LOOPBYTE(if (da[i] & db[i]) return TRUE);
|
2001-03-17 22:59:42 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
static bool
|
|
|
|
_intbig_contains(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BITVECP da,
|
|
|
|
db;
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
LOOPBYTE(if (db[i] & ~da[i]) return FALSE);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
static void
|
|
|
|
rt__intbig_size(ArrayType *a, float *sz)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
len = 0;
|
2001-09-30 18:16:44 +02:00
|
|
|
BITVECP bv = SIGPTR(a);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
LOOPBYTE(
|
2002-09-04 22:31:48 +02:00
|
|
|
len +=
|
|
|
|
GETBITBYTE(bv, 0) +
|
|
|
|
GETBITBYTE(bv, 1) +
|
|
|
|
GETBITBYTE(bv, 2) +
|
|
|
|
GETBITBYTE(bv, 3) +
|
|
|
|
GETBITBYTE(bv, 4) +
|
|
|
|
GETBITBYTE(bv, 5) +
|
|
|
|
GETBITBYTE(bv, 6) +
|
|
|
|
GETBITBYTE(bv, 7);
|
|
|
|
bv = (BITVECP) (((char *) bv) + 1);
|
2001-09-23 06:16:16 +02:00
|
|
|
);
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
*sz = (float) len;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
static ArrayType *
|
|
|
|
_intbig_union(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
BITVECP da,
|
|
|
|
db,
|
|
|
|
dr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
r = new_intArrayType(SIGLENINT);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
dr = SIGPTR(r);
|
|
|
|
|
|
|
|
LOOPBYTE(dr[i] = da[i] | db[i]);
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
return r;
|
|
|
|
}
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
static ArrayType *
|
|
|
|
_intbig_inter(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
BITVECP da,
|
|
|
|
db,
|
|
|
|
dr;
|
|
|
|
int i;
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
r = new_intArrayType(SIGLENINT);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
dr = SIGPTR(r);
|
|
|
|
|
|
|
|
LOOPBYTE(dr[i] = da[i] & db[i]);
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
return r;
|
|
|
|
}
|
2001-03-20 04:08:12 +01:00
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_intbig_same(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ArrayType *a = (ArrayType *) PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *b = (ArrayType *) PG_GETARG_POINTER(1);
|
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
2001-03-22 16:24:12 +01:00
|
|
|
BITVECP da,
|
|
|
|
db;
|
|
|
|
int i;
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
|
|
|
|
LOOPBYTE(
|
2002-09-04 22:31:48 +02:00
|
|
|
if (da[i] != db[i])
|
|
|
|
{
|
2001-03-22 16:24:12 +01:00
|
|
|
*result = FALSE;
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
2001-03-22 16:24:12 +01:00
|
|
|
}
|
2001-03-17 22:59:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
*result = TRUE;
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_intbig_compress(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2001-03-22 16:24:12 +01:00
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r,
|
|
|
|
*in;
|
2002-09-04 22:31:48 +02:00
|
|
|
bool maycompress = true;
|
|
|
|
int i;
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-05-31 20:16:55 +02:00
|
|
|
if (DatumGetPointer(entry->key) != NULL)
|
|
|
|
in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
|
2001-03-22 16:24:12 +01:00
|
|
|
else
|
2001-03-17 22:59:42 +01:00
|
|
|
in = NULL;
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!entry->leafkey)
|
|
|
|
{
|
2002-03-06 07:10:59 +01:00
|
|
|
LOOPBYTE(
|
2002-09-04 22:31:48 +02:00
|
|
|
if ((((char *) ARRPTR(in))[i] & 0xff) != 0xff)
|
|
|
|
{
|
|
|
|
maycompress = false;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-06 07:10:59 +01:00
|
|
|
);
|
2002-09-04 22:31:48 +02:00
|
|
|
if (maycompress)
|
|
|
|
{
|
2001-09-30 18:16:44 +02:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
r = new_intArrayType(1);
|
2002-03-06 07:10:59 +01:00
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
2002-09-04 22:31:48 +02:00
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
|
|
|
PG_RETURN_POINTER(retval);
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(entry);
|
2001-06-11 04:31:25 +02:00
|
|
|
}
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
2002-09-04 22:31:48 +02:00
|
|
|
r = new_intArrayType(SIGLENINT);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
if (ARRISVOID(in))
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2001-09-30 18:16:44 +02:00
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
2002-09-04 22:31:48 +02:00
|
|
|
pfree(in);
|
|
|
|
PG_RETURN_POINTER(retval);
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
gensign(SIGPTR(r),
|
|
|
|
ARRPTR(in),
|
|
|
|
ARRNELEMS(in));
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
LOOPBYTE(
|
2002-09-04 22:31:48 +02:00
|
|
|
if ((((char *) ARRPTR(in))[i] & 0xff) != 0xff)
|
|
|
|
{
|
|
|
|
maycompress = false;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-06 07:10:59 +01:00
|
|
|
);
|
2001-06-11 04:31:25 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (maycompress)
|
|
|
|
{
|
2001-06-11 04:31:25 +02:00
|
|
|
pfree(r);
|
|
|
|
r = new_intArrayType(1);
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
2001-06-11 04:31:25 +02:00
|
|
|
|
2001-05-31 20:16:55 +02:00
|
|
|
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
|
|
|
pfree(in);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(retval);
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_intbig_decompress(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2001-03-22 16:24:12 +01:00
|
|
|
ArrayType *key;
|
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
key = (ArrayType *) PG_DETOAST_DATUM(entry->key);
|
2001-06-11 04:31:25 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (key != (ArrayType *) DatumGetPointer(entry->key))
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
|
|
|
GISTENTRY *retval;
|
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
gistentryinit(*retval, PointerGetDatum(key),
|
2002-09-04 22:31:48 +02:00
|
|
|
entry->rel, entry->page, entry->offset, (key) ? VARSIZE(key) : 0, FALSE);
|
|
|
|
PG_RETURN_POINTER(retval);
|
2001-03-20 04:08:12 +01:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
if (ARRNELEMS(key) == 1)
|
|
|
|
{
|
2001-09-30 18:16:44 +02:00
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *newkey;
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
newkey = new_intArrayType(SIGLENINT);
|
2002-09-04 22:31:48 +02:00
|
|
|
MemSet((void *) ARRPTR(newkey), 0xff, SIGLEN);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
gistentryinit(*retval, PointerGetDatum(newkey),
|
2002-09-04 22:31:48 +02:00
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(newkey), FALSE);
|
|
|
|
PG_RETURN_POINTER(retval);
|
2001-09-30 18:16:44 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(entry);
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_intbig_picksplit(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(_int_common_picksplit(
|
|
|
|
(bytea *) PG_GETARG_POINTER(0),
|
|
|
|
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
|
|
|
|
_intbig_union,
|
|
|
|
_intbig_inter,
|
|
|
|
rt__intbig_size,
|
|
|
|
0.1
|
|
|
|
));
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_intbig_union(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(_int_common_union(
|
|
|
|
(bytea *) PG_GETARG_POINTER(0),
|
|
|
|
(int *) PG_GETARG_POINTER(1),
|
|
|
|
_intbig_union
|
|
|
|
));
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
Datum
|
|
|
|
g_intbig_penalty(PG_FUNCTION_ARGS)
|
2001-03-22 16:24:12 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(_int_common_penalty(
|
|
|
|
(GISTENTRY *) PG_GETARG_POINTER(0),
|
|
|
|
(GISTENTRY *) PG_GETARG_POINTER(1),
|
|
|
|
(float *) PG_GETARG_POINTER(2),
|
|
|
|
_intbig_union, rt__intbig_size
|
|
|
|
));
|
2001-06-11 04:31:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
g_intbig_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *query = (ArrayType *) PG_GETARG_POINTER(1);
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2001-03-22 16:24:12 +01:00
|
|
|
bool retval;
|
|
|
|
ArrayType *q;
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (strategy == BooleanSearchStrategy)
|
|
|
|
PG_RETURN_BOOL(signconsistent((QUERYTYPE *) query,
|
|
|
|
SIGPTR((ArrayType *) DatumGetPointer(entry->key)),
|
|
|
|
false));
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/* XXX what about toasted input? */
|
2001-09-23 06:16:16 +02:00
|
|
|
if (ARRISVOID(query))
|
2001-03-22 16:24:12 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
q = new_intArrayType(SIGLENINT);
|
|
|
|
gensign(SIGPTR(q),
|
|
|
|
ARRPTR(query),
|
|
|
|
ARRNELEMS(query));
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
2001-05-31 20:16:55 +02:00
|
|
|
retval = _intbig_overlap((ArrayType *) DatumGetPointer(entry->key), q);
|
2001-03-22 16:24:12 +01:00
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
2002-09-04 22:31:48 +02:00
|
|
|
if (GIST_LEAF(entry))
|
2002-06-07 23:52:33 +02:00
|
|
|
DirectFunctionCall3(
|
2002-09-04 22:31:48 +02:00
|
|
|
g_intbig_same,
|
|
|
|
entry->key,
|
|
|
|
PointerGetDatum(q),
|
|
|
|
PointerGetDatum(&retval)
|
|
|
|
);
|
2002-06-07 23:52:33 +02:00
|
|
|
else
|
|
|
|
retval = _intbig_contains((ArrayType *) DatumGetPointer(entry->key), q);
|
|
|
|
break;
|
2001-03-22 16:24:12 +01:00
|
|
|
case RTContainsStrategyNumber:
|
2001-05-31 20:16:55 +02:00
|
|
|
retval = _intbig_contains((ArrayType *) DatumGetPointer(entry->key), q);
|
2001-03-22 16:24:12 +01:00
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2001-05-31 20:16:55 +02:00
|
|
|
retval = _intbig_overlap((ArrayType *) DatumGetPointer(entry->key), q);
|
2001-03-22 16:24:12 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
|
|
|
pfree(q);
|
2001-06-11 04:31:25 +02:00
|
|
|
PG_RETURN_BOOL(retval);
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
** Common GiST Method
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Union method for _intments
|
|
|
|
** returns the minimal set that encloses all the entries in entryvec
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
static ArrayType *
|
2001-03-22 16:24:12 +01:00
|
|
|
_int_common_union(bytea *entryvec, int *sizep, formarray unionf)
|
|
|
|
{
|
|
|
|
int numranges,
|
|
|
|
i;
|
|
|
|
ArrayType *out = (ArrayType *) NULL;
|
|
|
|
ArrayType *tmp;
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "_int_common_union in");
|
2001-03-17 22:59:42 +01:00
|
|
|
#endif
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
numranges = (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY);
|
2001-05-31 20:16:55 +02:00
|
|
|
tmp = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[0].key);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
for (i = 1; i < numranges; i++)
|
|
|
|
{
|
|
|
|
out = (*unionf) (tmp, (ArrayType *)
|
2002-09-04 22:31:48 +02:00
|
|
|
DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key));
|
2001-03-22 16:24:12 +01:00
|
|
|
if (i > 1 && tmp)
|
|
|
|
pfree(tmp);
|
|
|
|
tmp = out;
|
|
|
|
}
|
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
out->flags &= ~LEAFKEY;
|
2001-03-22 16:24:12 +01:00
|
|
|
*sizep = VARSIZE(out);
|
|
|
|
if (*sizep == 0)
|
|
|
|
{
|
|
|
|
pfree(out);
|
2001-03-17 22:59:42 +01:00
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "_int_common_union out1");
|
2001-03-17 22:59:42 +01:00
|
|
|
#endif
|
2001-03-22 16:24:12 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-03-17 22:59:42 +01:00
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "_int_common_union out");
|
2001-03-17 22:59:42 +01:00
|
|
|
#endif
|
2001-03-22 16:24:12 +01:00
|
|
|
return (out);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-03-20 04:08:12 +01:00
|
|
|
/*****************************************
|
|
|
|
* The GiST Penalty method for _intments *
|
|
|
|
*****************************************/
|
|
|
|
|
2001-06-11 04:31:25 +02:00
|
|
|
static float *
|
2001-03-17 22:59:42 +01:00
|
|
|
_int_common_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result,
|
2001-03-22 16:24:12 +01:00
|
|
|
formarray unionf,
|
|
|
|
formfloat sizef)
|
2001-03-17 22:59:42 +01:00
|
|
|
{
|
2001-05-31 20:16:55 +02:00
|
|
|
ArrayType *ud;
|
2001-03-22 16:24:12 +01:00
|
|
|
float tmp1,
|
|
|
|
tmp2;
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "penalty");
|
2001-03-17 22:59:42 +01:00
|
|
|
#endif
|
2001-05-31 20:16:55 +02:00
|
|
|
ud = (*unionf) ((ArrayType *) DatumGetPointer(origentry->key),
|
|
|
|
(ArrayType *) DatumGetPointer(newentry->key));
|
|
|
|
(*sizef) (ud, &tmp1);
|
|
|
|
(*sizef) ((ArrayType *) DatumGetPointer(origentry->key), &tmp2);
|
2001-03-22 16:24:12 +01:00
|
|
|
*result = tmp1 - tmp2;
|
2001-05-31 20:16:55 +02:00
|
|
|
pfree(ud);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "--penalty\t%g", *result);
|
2001-03-17 22:59:42 +01:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
return (result);
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
OffsetNumber pos;
|
2001-09-30 18:16:44 +02:00
|
|
|
float cost;
|
|
|
|
} SPLITCOST;
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
static int
|
2002-09-04 22:31:48 +02:00
|
|
|
comparecost(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (((SPLITCOST *) a)->cost == ((SPLITCOST *) b)->cost)
|
2001-09-30 18:16:44 +02:00
|
|
|
return 0;
|
|
|
|
else
|
2002-09-04 22:31:48 +02:00
|
|
|
return (((SPLITCOST *) a)->cost > ((SPLITCOST *) b)->cost) ? 1 : -1;
|
2001-09-30 18:16:44 +02:00
|
|
|
}
|
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
/*
|
|
|
|
** The GiST PickSplit method for _intments
|
2001-03-22 16:24:12 +01:00
|
|
|
** We use Guttman's poly time split algorithm
|
2001-03-17 22:59:42 +01:00
|
|
|
*/
|
2001-06-11 04:31:25 +02:00
|
|
|
static GIST_SPLITVEC *
|
2001-03-17 22:59:42 +01:00
|
|
|
_int_common_picksplit(bytea *entryvec,
|
2001-03-22 16:24:12 +01:00
|
|
|
GIST_SPLITVEC *v,
|
|
|
|
formarray unionf,
|
|
|
|
formarray interf,
|
|
|
|
formfloat sizef,
|
|
|
|
float coef)
|
2001-03-17 22:59:42 +01:00
|
|
|
{
|
2001-03-22 16:24:12 +01:00
|
|
|
OffsetNumber i,
|
|
|
|
j;
|
|
|
|
ArrayType *datum_alpha,
|
|
|
|
*datum_beta;
|
|
|
|
ArrayType *datum_l,
|
|
|
|
*datum_r;
|
|
|
|
ArrayType *union_d,
|
|
|
|
*union_dl,
|
|
|
|
*union_dr;
|
|
|
|
ArrayType *inter_d;
|
|
|
|
bool firsttime;
|
|
|
|
float size_alpha,
|
|
|
|
size_beta,
|
|
|
|
size_union,
|
|
|
|
size_inter;
|
|
|
|
float size_waste,
|
|
|
|
waste;
|
|
|
|
float size_l,
|
|
|
|
size_r;
|
|
|
|
int nbytes;
|
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
OffsetNumber maxoff;
|
2002-09-04 22:31:48 +02:00
|
|
|
SPLITCOST *costvector;
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "--------picksplit %d", (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY));
|
2001-03-17 22:59:42 +01:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 16:24:12 +01:00
|
|
|
maxoff = ((VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY)) - 2;
|
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
|
|
|
|
firsttime = true;
|
|
|
|
waste = 0.0;
|
|
|
|
for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
2001-05-31 20:16:55 +02:00
|
|
|
datum_alpha = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
2001-03-22 16:24:12 +01:00
|
|
|
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
2001-05-31 20:16:55 +02:00
|
|
|
datum_beta = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[j].key);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
/* compute the wasted space by unioning these guys */
|
|
|
|
/* size_waste = size_union - size_inter; */
|
|
|
|
union_d = (*unionf) (datum_alpha, datum_beta);
|
|
|
|
(*sizef) (union_d, &size_union);
|
|
|
|
inter_d = (*interf) (datum_alpha, datum_beta);
|
|
|
|
(*sizef) (inter_d, &size_inter);
|
|
|
|
size_waste = size_union - size_inter;
|
|
|
|
|
|
|
|
pfree(union_d);
|
|
|
|
|
|
|
|
if (inter_d != (ArrayType *) NULL)
|
|
|
|
pfree(inter_d);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* are these a more promising split that what we've already
|
|
|
|
* seen?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (size_waste > waste || firsttime)
|
|
|
|
{
|
|
|
|
waste = size_waste;
|
|
|
|
seed_1 = i;
|
|
|
|
seed_2 = j;
|
|
|
|
firsttime = false;
|
|
|
|
}
|
|
|
|
}
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
left = v->spl_left;
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
right = v->spl_right;
|
|
|
|
v->spl_nright = 0;
|
2002-09-04 22:31:48 +02:00
|
|
|
if (seed_1 == 0 || seed_2 == 0)
|
|
|
|
{
|
2002-02-07 23:11:43 +01:00
|
|
|
seed_1 = 1;
|
|
|
|
seed_2 = 2;
|
|
|
|
}
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-05-31 20:16:55 +02:00
|
|
|
datum_alpha = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_1].key);
|
2001-03-22 16:24:12 +01:00
|
|
|
datum_l = copy_intArrayType(datum_alpha);
|
2001-05-31 20:16:55 +02:00
|
|
|
(*sizef) (datum_l, &size_l);
|
|
|
|
datum_beta = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_2].key);
|
2001-03-22 16:24:12 +01:00
|
|
|
datum_r = copy_intArrayType(datum_beta);
|
2001-05-31 20:16:55 +02:00
|
|
|
(*sizef) (datum_r, &size_r);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* sort entries
|
2001-09-30 18:16:44 +02:00
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
costvector[i - 1].pos = i;
|
2001-09-30 18:16:44 +02:00
|
|
|
datum_alpha = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
2002-09-04 22:31:48 +02:00
|
|
|
union_d = (*unionf) (datum_l, datum_alpha);
|
|
|
|
(*sizef) (union_d, &size_alpha);
|
|
|
|
pfree(union_d);
|
|
|
|
union_d = (*unionf) (datum_r, datum_alpha);
|
|
|
|
(*sizef) (union_d, &size_beta);
|
|
|
|
pfree(union_d);
|
|
|
|
costvector[i - 1].cost = abs((size_alpha - size_l) - (size_beta - size_r));
|
2001-09-30 18:16:44 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-03-17 22:59:42 +01:00
|
|
|
/*
|
2001-03-22 16:24:12 +01:00
|
|
|
* Now split up the regions between the two seeds. An important
|
|
|
|
* property of this split algorithm is that the split vector v has the
|
|
|
|
* indices of items to be split in order in its left and right
|
|
|
|
* vectors. We exploit this property by doing a merge in the code
|
|
|
|
* that actually splits the page.
|
|
|
|
*
|
|
|
|
* For efficiency, we also place the new index tuple in this loop. This
|
|
|
|
* is handled at the very end, when we have placed all the existing
|
|
|
|
* tuples and i == maxoff + 1.
|
2001-03-17 22:59:42 +01:00
|
|
|
*/
|
2001-03-22 16:24:12 +01:00
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
for (j = 0; j < maxoff; j++)
|
|
|
|
{
|
2001-09-30 18:16:44 +02:00
|
|
|
i = costvector[j].pos;
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we've already decided where to place this item, just put it
|
|
|
|
* on the right list. Otherwise, we need to figure out which page
|
|
|
|
* needs the least enlargement in order to store the item.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (i == seed_1)
|
|
|
|
{
|
|
|
|
*left++ = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (i == seed_2)
|
|
|
|
{
|
|
|
|
*right++ = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* okay, which page needs least enlargement? */
|
2001-05-31 20:16:55 +02:00
|
|
|
datum_alpha = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
|
|
|
union_dl = (*unionf) (datum_l, datum_alpha);
|
|
|
|
union_dr = (*unionf) (datum_r, datum_alpha);
|
|
|
|
(*sizef) (union_dl, &size_alpha);
|
|
|
|
(*sizef) (union_dr, &size_beta);
|
2001-03-22 16:24:12 +01:00
|
|
|
|
|
|
|
/* pick which page to add it to */
|
|
|
|
if (size_alpha - size_l < size_beta - size_r + WISH_F(v->spl_nleft, v->spl_nright, coef))
|
|
|
|
{
|
|
|
|
if (datum_l)
|
|
|
|
pfree(datum_l);
|
|
|
|
if (union_dr)
|
|
|
|
pfree(union_dr);
|
|
|
|
datum_l = union_dl;
|
|
|
|
size_l = size_alpha;
|
|
|
|
*left++ = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (datum_r)
|
|
|
|
pfree(datum_r);
|
|
|
|
if (union_dl)
|
|
|
|
pfree(union_dl);
|
|
|
|
datum_r = union_dr;
|
|
|
|
size_r = size_beta;
|
|
|
|
*right++ = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
pfree(costvector);
|
2001-09-30 18:16:44 +02:00
|
|
|
*right = *left = FirstOffsetNumber;
|
2001-03-17 22:59:42 +01:00
|
|
|
|
2001-09-30 18:16:44 +02:00
|
|
|
datum_l->flags &= ~LEAFKEY;
|
|
|
|
datum_r->flags &= ~LEAFKEY;
|
2001-05-31 20:16:55 +02:00
|
|
|
v->spl_ldatum = PointerGetDatum(datum_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(datum_r);
|
2001-03-17 22:59:42 +01:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(DEBUG3, "--------ENDpicksplit %d %d", v->spl_nleft, v->spl_nright);
|
2001-03-17 22:59:42 +01:00
|
|
|
#endif
|
2001-03-22 16:24:12 +01:00
|
|
|
return v;
|
2001-03-17 22:59:42 +01:00
|
|
|
}
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
2002-09-04 22:31:48 +02:00
|
|
|
* BoolSearch
|
2001-09-23 06:16:16 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
#define END 0
|
|
|
|
#define ERR 1
|
|
|
|
#define VAL 2
|
|
|
|
#define OPR 3
|
|
|
|
#define OPEN 4
|
|
|
|
#define CLOSE 5
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
/* parser's states */
|
2002-09-04 22:31:48 +02:00
|
|
|
#define WAITOPERAND 1
|
2001-09-23 06:16:16 +02:00
|
|
|
#define WAITENDOPERAND 2
|
|
|
|
#define WAITOPERATOR 3
|
|
|
|
|
|
|
|
/*
|
|
|
|
* node of query tree, also used
|
|
|
|
* for storing polish notation in parser
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
typedef struct NODE
|
|
|
|
{
|
|
|
|
int4 type;
|
|
|
|
int4 val;
|
|
|
|
struct NODE *next;
|
|
|
|
} NODE;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int4 state;
|
|
|
|
int4 count;
|
|
|
|
/* reverse polish notation in list (for temprorary usage) */
|
|
|
|
NODE *str;
|
2001-09-23 06:16:16 +02:00
|
|
|
/* number in str */
|
2002-09-04 22:31:48 +02:00
|
|
|
int4 num;
|
|
|
|
} WORKSTATE;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get token from query string
|
|
|
|
*/
|
2002-03-06 07:10:59 +01:00
|
|
|
static int4
|
2002-09-04 22:31:48 +02:00
|
|
|
gettoken(WORKSTATE * state, int4 *val)
|
|
|
|
{
|
|
|
|
char nnn[16],
|
|
|
|
*curnnn;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
curnnn = nnn;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
switch (state->state)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
case WAITOPERAND:
|
2002-09-04 22:31:48 +02:00
|
|
|
curnnn = nnn;
|
|
|
|
if ((*(state->buf) >= '0' && *(state->buf) <= '9') ||
|
|
|
|
*(state->buf) == '-')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
state->state = WAITENDOPERAND;
|
|
|
|
*curnnn = *(state->buf);
|
|
|
|
curnnn++;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (*(state->buf) == '!')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
(state->buf)++;
|
2002-09-04 22:31:48 +02:00
|
|
|
*val = (int4) '!';
|
2001-09-23 06:16:16 +02:00
|
|
|
return OPR;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (*(state->buf) == '(')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
state->count++;
|
|
|
|
(state->buf)++;
|
|
|
|
return OPEN;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (*(state->buf) != ' ')
|
2001-09-23 06:16:16 +02:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case WAITENDOPERAND:
|
2002-09-04 22:31:48 +02:00
|
|
|
if (*(state->buf) >= '0' && *(state->buf) <= '9')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
*curnnn = *(state->buf);
|
|
|
|
curnnn++;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
*curnnn = '\0';
|
2002-09-04 22:31:48 +02:00
|
|
|
*val = (int4) atoi(nnn);
|
2001-09-23 06:16:16 +02:00
|
|
|
state->state = WAITOPERATOR;
|
2002-09-04 22:31:48 +02:00
|
|
|
return (state->count && *(state->buf) == '\0')
|
2001-09-23 06:16:16 +02:00
|
|
|
? ERR : VAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WAITOPERATOR:
|
2002-09-04 22:31:48 +02:00
|
|
|
if (*(state->buf) == '&' || *(state->buf) == '|')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
state->state = WAITOPERAND;
|
|
|
|
*val = (int4) *(state->buf);
|
2002-03-06 07:10:59 +01:00
|
|
|
(state->buf)++;
|
2001-09-23 06:16:16 +02:00
|
|
|
return OPR;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (*(state->buf) == ')')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
(state->buf)++;
|
|
|
|
state->count--;
|
2002-09-04 22:31:48 +02:00
|
|
|
return (state->count < 0) ? ERR : CLOSE;
|
|
|
|
}
|
|
|
|
else if (*(state->buf) == '\0')
|
|
|
|
return (state->count) ? ERR : END;
|
|
|
|
else if (*(state->buf) != ' ')
|
2001-09-23 06:16:16 +02:00
|
|
|
return ERR;
|
|
|
|
break;
|
2002-03-06 07:10:59 +01:00
|
|
|
default:
|
2001-09-23 06:16:16 +02:00
|
|
|
return ERR;
|
|
|
|
break;
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
|
|
|
(state->buf)++;
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
return END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* push new one in polish notation reverse view
|
|
|
|
*/
|
|
|
|
static void
|
2002-09-04 22:31:48 +02:00
|
|
|
pushquery(WORKSTATE * state, int4 type, int4 val)
|
|
|
|
{
|
|
|
|
NODE *tmp = (NODE *) palloc(sizeof(NODE));
|
|
|
|
|
|
|
|
tmp->type = type;
|
|
|
|
tmp->val = val;
|
2001-09-23 06:16:16 +02:00
|
|
|
tmp->next = state->str;
|
|
|
|
state->str = tmp;
|
|
|
|
state->num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STACKDEPTH 16
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make polish notaion of query
|
|
|
|
*/
|
2002-03-06 07:10:59 +01:00
|
|
|
static int4
|
2002-09-04 22:31:48 +02:00
|
|
|
makepol(WORKSTATE * state)
|
|
|
|
{
|
|
|
|
int4 val,
|
|
|
|
type;
|
|
|
|
int4 stack[STACKDEPTH];
|
|
|
|
int4 lenstack = 0;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
while ((type = gettoken(state, &val)) != END)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
case VAL:
|
|
|
|
pushquery(state, type, val);
|
2002-09-04 22:31:48 +02:00
|
|
|
while (lenstack && (stack[lenstack - 1] == (int4) '&' ||
|
|
|
|
stack[lenstack - 1] == (int4) '!'))
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
lenstack--;
|
2002-09-04 22:31:48 +02:00
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPR:
|
2002-09-04 22:31:48 +02:00
|
|
|
if (lenstack && val == (int4) '|')
|
2001-09-23 06:16:16 +02:00
|
|
|
pushquery(state, OPR, val);
|
2002-09-04 22:31:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lenstack == STACKDEPTH)
|
|
|
|
elog(ERROR, "Stack too short");
|
|
|
|
stack[lenstack] = val;
|
2001-09-23 06:16:16 +02:00
|
|
|
lenstack++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPEN:
|
2002-09-04 22:31:48 +02:00
|
|
|
if (makepol(state) == ERR)
|
|
|
|
return ERR;
|
|
|
|
if (lenstack && (stack[lenstack - 1] == (int4) '&' ||
|
|
|
|
stack[lenstack - 1] == (int4) '!'))
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
lenstack--;
|
2002-09-04 22:31:48 +02:00
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CLOSE:
|
2002-09-04 22:31:48 +02:00
|
|
|
while (lenstack)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
lenstack--;
|
2002-09-04 22:31:48 +02:00
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
2001-09-23 06:16:16 +02:00
|
|
|
};
|
|
|
|
return END;
|
|
|
|
break;
|
|
|
|
case ERR:
|
|
|
|
default:
|
2002-09-04 22:31:48 +02:00
|
|
|
elog(ERROR, "Syntax error");
|
2001-09-23 06:16:16 +02:00
|
|
|
return ERR;
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
while (lenstack)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
lenstack--;
|
2002-09-04 22:31:48 +02:00
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
2001-09-23 06:16:16 +02:00
|
|
|
};
|
|
|
|
return END;
|
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int4 *arrb;
|
|
|
|
int4 *arre;
|
|
|
|
} CHKVAL;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* is there value 'val' in array or not ?
|
2001-10-04 17:41:14 +02:00
|
|
|
*/
|
2001-09-23 06:16:16 +02:00
|
|
|
static bool
|
2002-09-04 22:31:48 +02:00
|
|
|
checkcondition_arr(void *checkval, int4 val)
|
|
|
|
{
|
|
|
|
int4 *StopLow = ((CHKVAL *) checkval)->arrb;
|
|
|
|
int4 *StopHigh = ((CHKVAL *) checkval)->arre;
|
|
|
|
int4 *StopMiddle;
|
2001-10-04 17:41:14 +02:00
|
|
|
|
|
|
|
/* Loop invariant: StopLow <= val < StopHigh */
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
while (StopLow < StopHigh)
|
|
|
|
{
|
2001-10-04 17:41:14 +02:00
|
|
|
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
|
|
|
|
if (*StopMiddle == val)
|
|
|
|
return (true);
|
2002-09-04 22:31:48 +02:00
|
|
|
else if (*StopMiddle < val)
|
2001-10-04 17:41:14 +02:00
|
|
|
StopLow = StopMiddle + 1;
|
|
|
|
else
|
|
|
|
StopHigh = StopMiddle;
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
2001-10-04 17:41:14 +02:00
|
|
|
return false;
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2002-09-04 22:31:48 +02:00
|
|
|
checkcondition_bit(void *checkval, int4 val)
|
|
|
|
{
|
|
|
|
return GETBIT(checkval, HASHVAL(val));
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for boolean condition
|
|
|
|
*/
|
|
|
|
static bool
|
2002-09-04 22:31:48 +02:00
|
|
|
execute(ITEM * curitem, void *checkval, bool calcnot, bool (*chkcond) (void *checkval, int4 val))
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (curitem->type == VAL)
|
|
|
|
return (*chkcond) (checkval, curitem->val);
|
|
|
|
else if (curitem->val == (int4) '!')
|
|
|
|
{
|
|
|
|
return (calcnot) ?
|
|
|
|
((execute(curitem - 1, checkval, calcnot, chkcond)) ? false : true)
|
2001-09-23 06:16:16 +02:00
|
|
|
: true;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (curitem->val == (int4) '&')
|
|
|
|
{
|
|
|
|
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
|
2001-09-23 06:16:16 +02:00
|
|
|
return execute(curitem - 1, checkval, calcnot, chkcond);
|
|
|
|
else
|
|
|
|
return false;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* |-operator */
|
|
|
|
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
|
2001-09-23 06:16:16 +02:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return execute(curitem - 1, checkval, calcnot, chkcond);
|
|
|
|
}
|
|
|
|
return false;
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* signconsistent & execconsistent called by *_consistent
|
|
|
|
*/
|
2002-03-06 07:10:59 +01:00
|
|
|
static bool
|
2002-09-04 22:31:48 +02:00
|
|
|
signconsistent(QUERYTYPE * query, BITVEC sign, bool calcnot)
|
|
|
|
{
|
2002-03-06 07:10:59 +01:00
|
|
|
return execute(
|
2002-09-04 22:31:48 +02:00
|
|
|
GETQUERY(query) + query->size - 1,
|
|
|
|
(void *) sign, calcnot,
|
|
|
|
checkcondition_bit
|
2002-03-06 07:10:59 +01:00
|
|
|
);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
2002-03-06 07:10:59 +01:00
|
|
|
static bool
|
2002-09-04 22:31:48 +02:00
|
|
|
execconsistent(QUERYTYPE * query, ArrayType *array, bool calcnot)
|
|
|
|
{
|
|
|
|
CHKVAL chkval;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
chkval.arrb = ARRPTR(array);
|
2001-10-04 17:41:14 +02:00
|
|
|
chkval.arre = chkval.arrb + ARRNELEMS(array);
|
2002-03-06 07:10:59 +01:00
|
|
|
return execute(
|
2002-09-04 22:31:48 +02:00
|
|
|
GETQUERY(query) + query->size - 1,
|
|
|
|
(void *) &chkval, calcnot,
|
|
|
|
checkcondition_arr
|
|
|
|
);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-03-06 07:10:59 +01:00
|
|
|
* boolean operations
|
2001-09-23 06:16:16 +02:00
|
|
|
*/
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
rboolop(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
return DirectFunctionCall2(
|
2002-09-04 22:31:48 +02:00
|
|
|
boolop,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
2001-09-23 06:16:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
boolop(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *val = (ArrayType *) PG_DETOAST_DATUM_COPY(PG_GETARG_POINTER(0));
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(1));
|
|
|
|
CHKVAL chkval;
|
|
|
|
bool result;
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (ARRISVOID(val))
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
pfree(val);
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(false);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PREPAREARR(val);
|
|
|
|
chkval.arrb = ARRPTR(val);
|
2001-10-04 17:41:14 +02:00
|
|
|
chkval.arre = chkval.arrb + ARRNELEMS(val);
|
2002-03-06 07:10:59 +01:00
|
|
|
result = execute(
|
2002-09-04 22:31:48 +02:00
|
|
|
GETQUERY(query) + query->size - 1,
|
|
|
|
&chkval, true,
|
|
|
|
checkcondition_arr
|
|
|
|
);
|
2001-09-23 06:16:16 +02:00
|
|
|
pfree(val);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(result);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-09-04 22:31:48 +02:00
|
|
|
findoprnd(ITEM * ptr, int4 *pos)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
#ifdef BS_DEBUG
|
2002-09-04 22:31:48 +02:00
|
|
|
elog(DEBUG3, (ptr[*pos].type == OPR) ?
|
|
|
|
"%d %c" : "%d %d ", *pos, ptr[*pos].val);
|
2001-09-23 06:16:16 +02:00
|
|
|
#endif
|
2002-09-04 22:31:48 +02:00
|
|
|
if (ptr[*pos].type == VAL)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
ptr[*pos].left = 0;
|
|
|
|
(*pos)--;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (ptr[*pos].val == (int4) '!')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
ptr[*pos].left = -1;
|
|
|
|
(*pos)--;
|
2002-09-04 22:31:48 +02:00
|
|
|
findoprnd(ptr, pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ITEM *curitem = &ptr[*pos];
|
|
|
|
int4 tmp = *pos;
|
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
(*pos)--;
|
2002-09-04 22:31:48 +02:00
|
|
|
findoprnd(ptr, pos);
|
2001-09-23 06:16:16 +02:00
|
|
|
curitem->left = *pos - tmp;
|
2002-09-04 22:31:48 +02:00
|
|
|
findoprnd(ptr, pos);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* input
|
|
|
|
*/
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
bqarr_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *buf = (char *) PG_GETARG_POINTER(0);
|
|
|
|
WORKSTATE state;
|
|
|
|
int4 i;
|
|
|
|
QUERYTYPE *query;
|
|
|
|
int4 commonlen;
|
|
|
|
ITEM *ptr;
|
|
|
|
NODE *tmp;
|
|
|
|
int4 pos = 0;
|
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
#ifdef BS_DEBUG
|
2002-09-04 22:31:48 +02:00
|
|
|
StringInfoData pbuf;
|
2001-09-23 06:16:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
state.buf = buf;
|
|
|
|
state.state = WAITOPERAND;
|
|
|
|
state.count = 0;
|
|
|
|
state.num = 0;
|
2002-09-04 22:31:48 +02:00
|
|
|
state.str = NULL;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
/* make polish notation (postfix, but in reverse order) */
|
2002-09-04 22:31:48 +02:00
|
|
|
makepol(&state);
|
2002-03-06 07:10:59 +01:00
|
|
|
if (!state.num)
|
2002-09-04 22:31:48 +02:00
|
|
|
elog(ERROR, "Empty query");
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
commonlen = COMPUTESIZE(state.num);
|
2002-09-04 22:31:48 +02:00
|
|
|
query = (QUERYTYPE *) palloc(commonlen);
|
2001-09-23 06:16:16 +02:00
|
|
|
query->len = commonlen;
|
|
|
|
query->size = state.num;
|
|
|
|
ptr = GETQUERY(query);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
for (i = state.num - 1; i >= 0; i--)
|
|
|
|
{
|
2002-03-06 07:10:59 +01:00
|
|
|
ptr[i].type = state.str->type;
|
2001-09-23 06:16:16 +02:00
|
|
|
ptr[i].val = state.str->val;
|
|
|
|
tmp = state.str->next;
|
2002-09-04 22:31:48 +02:00
|
|
|
pfree(state.str);
|
2001-09-23 06:16:16 +02:00
|
|
|
state.str = tmp;
|
|
|
|
}
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
pos = query->size - 1;
|
|
|
|
findoprnd(ptr, &pos);
|
2001-09-23 06:16:16 +02:00
|
|
|
#ifdef BS_DEBUG
|
2002-09-02 08:11:43 +02:00
|
|
|
initStringInfo(&pbuf);
|
2002-09-04 22:31:48 +02:00
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
{
|
|
|
|
if (ptr[i].type == OPR)
|
2002-09-02 08:11:43 +02:00
|
|
|
appendStringInfo(&pbuf, "%c(%d) ", ptr[i].val, ptr[i].left);
|
2002-03-06 07:10:59 +01:00
|
|
|
else
|
2002-09-04 22:31:48 +02:00
|
|
|
appendStringInfo(&pbuf, "%d ", ptr[i].val);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
elog(DEBUG3, "POR: %s", pbuf.data);
|
2002-09-02 08:11:43 +02:00
|
|
|
pfree(pbuf.data);
|
2001-09-23 06:16:16 +02:00
|
|
|
#endif
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(query);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* out function
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ITEM *curpol;
|
|
|
|
char *buf;
|
|
|
|
char *cur;
|
|
|
|
int4 buflen;
|
|
|
|
} INFIX;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
#define RESIZEBUF(inf,addsize) while( ( inf->cur - inf->buf ) + addsize + 1 >= inf->buflen ) { \
|
|
|
|
int4 len = inf->cur - inf->buf; \
|
|
|
|
inf->buflen *= 2; \
|
|
|
|
inf->buf = (char*) repalloc( (void*)inf->buf, inf->buflen ); \
|
|
|
|
inf->cur = inf->buf + len; \
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-09-04 22:31:48 +02:00
|
|
|
infix(INFIX * in, bool first)
|
|
|
|
{
|
|
|
|
if (in->curpol->type == VAL)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
RESIZEBUF(in, 11);
|
2002-09-04 22:31:48 +02:00
|
|
|
sprintf(in->cur, "%d", in->curpol->val);
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
2001-09-23 06:16:16 +02:00
|
|
|
in->curpol--;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (in->curpol->val == (int4) '!')
|
|
|
|
{
|
|
|
|
bool isopr = false;
|
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
RESIZEBUF(in, 1);
|
|
|
|
*(in->cur) = '!';
|
|
|
|
in->cur++;
|
|
|
|
*(in->cur) = '\0';
|
|
|
|
in->curpol--;
|
2002-09-04 22:31:48 +02:00
|
|
|
if (in->curpol->type == OPR)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
isopr = true;
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
2002-09-04 22:31:48 +02:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
infix(in, isopr);
|
|
|
|
if (isopr)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
2002-09-04 22:31:48 +02:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
2002-03-06 07:10:59 +01:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int4 op = in->curpol->val;
|
|
|
|
INFIX nrm;
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
in->curpol--;
|
2002-09-04 22:31:48 +02:00
|
|
|
if (op == (int4) '|' && !first)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
2002-09-04 22:31:48 +02:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nrm.curpol = in->curpol;
|
|
|
|
nrm.buflen = 16;
|
2002-09-04 22:31:48 +02:00
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
/* get right operand */
|
2002-09-04 22:31:48 +02:00
|
|
|
infix(&nrm, false);
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
/* get & print left operand */
|
|
|
|
in->curpol = nrm.curpol;
|
2002-09-04 22:31:48 +02:00
|
|
|
infix(in, false);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/* print operator & right operand */
|
|
|
|
RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
|
2001-09-23 06:16:16 +02:00
|
|
|
sprintf(in->cur, " %c %s", op, nrm.buf);
|
2002-09-04 22:31:48 +02:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
pfree(nrm.buf);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (op == (int4) '|' && !first)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
2002-09-04 22:31:48 +02:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
bqarr_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(0));
|
|
|
|
INFIX nrm;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (query->size == 0)
|
|
|
|
elog(ERROR, "Empty");
|
2001-09-23 06:16:16 +02:00
|
|
|
nrm.curpol = GETQUERY(query) + query->size - 1;
|
|
|
|
nrm.buflen = 32;
|
2002-09-04 22:31:48 +02:00
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
2001-09-23 06:16:16 +02:00
|
|
|
*(nrm.cur) = '\0';
|
2002-09-04 22:31:48 +02:00
|
|
|
infix(&nrm, true);
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
PG_RETURN_POINTER(nrm.buf);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
2002-09-04 22:31:48 +02:00
|
|
|
countdroptree(ITEM * q, int4 pos)
|
|
|
|
{
|
|
|
|
if (q[pos].type == VAL)
|
2001-09-23 06:16:16 +02:00
|
|
|
return 1;
|
2002-09-04 22:31:48 +02:00
|
|
|
else if (q[pos].val == (int4) '!')
|
|
|
|
return 1 + countdroptree(q, pos - 1);
|
|
|
|
else
|
|
|
|
return 1 + countdroptree(q, pos - 1) + countdroptree(q, pos + q[pos].left);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* common algorithm:
|
2002-03-06 07:10:59 +01:00
|
|
|
* result of all '!' will be = 'true', so
|
2001-09-23 06:16:16 +02:00
|
|
|
* we can modify query tree for clearing
|
|
|
|
*/
|
2002-03-06 07:10:59 +01:00
|
|
|
static int4
|
2002-09-04 22:31:48 +02:00
|
|
|
shorterquery(ITEM * q, int4 len)
|
|
|
|
{
|
|
|
|
int4 index,
|
|
|
|
posnot,
|
|
|
|
poscor;
|
|
|
|
bool notisleft = false;
|
|
|
|
int4 drop,
|
|
|
|
i;
|
2001-09-23 06:16:16 +02:00
|
|
|
|
|
|
|
/* out all '!' */
|
2002-09-04 22:31:48 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
drop = 0;
|
2001-09-23 06:16:16 +02:00
|
|
|
/* find ! */
|
2002-09-04 22:31:48 +02:00
|
|
|
for (posnot = 0; posnot < len; posnot++)
|
|
|
|
if (q[posnot].type == OPR && q[posnot].val == (int4) '!')
|
|
|
|
{
|
|
|
|
index = 1;
|
2001-09-23 06:16:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (posnot == len)
|
2001-09-23 06:16:16 +02:00
|
|
|
return len;
|
|
|
|
|
|
|
|
/* last operator is ! */
|
2002-09-04 22:31:48 +02:00
|
|
|
if (posnot == len - 1)
|
2001-09-23 06:16:16 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* find operator for this operand */
|
2002-09-04 22:31:48 +02:00
|
|
|
for (poscor = posnot + 1; poscor < len; poscor++)
|
|
|
|
{
|
|
|
|
if (q[poscor].type == OPR)
|
|
|
|
{
|
|
|
|
if (poscor == posnot + 1)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
notisleft = false;
|
|
|
|
break;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (q[poscor].left + poscor == posnot)
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
notisleft = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
if (q[poscor].val == (int4) '!')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
drop = countdroptree(q, poscor);
|
2002-09-04 22:31:48 +02:00
|
|
|
q[poscor - 1].type = VAL;
|
|
|
|
for (i = poscor + 1; i < len; i++)
|
|
|
|
if (q[i].type == OPR && q[i].left + i <= poscor)
|
2002-03-06 07:10:59 +01:00
|
|
|
q[i].left += drop - 2;
|
2002-09-04 22:31:48 +02:00
|
|
|
memcpy((void *) &q[poscor - drop + 1],
|
|
|
|
(void *) &q[poscor - 1],
|
|
|
|
sizeof(ITEM) * (len - (poscor - 1)));
|
2001-09-23 06:16:16 +02:00
|
|
|
len -= drop - 2;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else if (q[poscor].val == (int4) '|')
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
drop = countdroptree(q, poscor);
|
2002-09-04 22:31:48 +02:00
|
|
|
q[poscor - 1].type = VAL;
|
|
|
|
q[poscor].val = (int4) '!';
|
|
|
|
q[poscor].left = -1;
|
|
|
|
for (i = poscor + 1; i < len; i++)
|
|
|
|
if (q[i].type == OPR && q[i].left + i < poscor)
|
2002-03-06 07:10:59 +01:00
|
|
|
q[i].left += drop - 2;
|
2002-09-04 22:31:48 +02:00
|
|
|
memcpy((void *) &q[poscor - drop + 1],
|
|
|
|
(void *) &q[poscor - 1],
|
|
|
|
sizeof(ITEM) * (len - (poscor - 1)));
|
2001-09-23 06:16:16 +02:00
|
|
|
len -= drop - 2;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* &-operator */
|
2002-03-06 07:10:59 +01:00
|
|
|
if (
|
2002-09-04 22:31:48 +02:00
|
|
|
(notisleft && q[poscor - 1].type == OPR &&
|
|
|
|
q[poscor - 1].val == (int4) '!') ||
|
|
|
|
(!notisleft && q[poscor + q[poscor].left].type == OPR &&
|
|
|
|
q[poscor + q[poscor].left].val == (int4) '!')
|
|
|
|
)
|
|
|
|
{ /* drop subtree */
|
2001-09-23 06:16:16 +02:00
|
|
|
drop = countdroptree(q, poscor);
|
2002-09-04 22:31:48 +02:00
|
|
|
q[poscor - 1].type = VAL;
|
|
|
|
q[poscor].val = (int4) '!';
|
|
|
|
q[poscor].left = -1;
|
|
|
|
for (i = poscor + 1; i < len; i++)
|
|
|
|
if (q[i].type == OPR && q[i].left + i < poscor)
|
2002-03-06 07:10:59 +01:00
|
|
|
q[i].left += drop - 2;
|
2002-09-04 22:31:48 +02:00
|
|
|
memcpy((void *) &q[poscor - drop + 1],
|
|
|
|
(void *) &q[poscor - 1],
|
|
|
|
sizeof(ITEM) * (len - (poscor - 1)));
|
2001-09-23 06:16:16 +02:00
|
|
|
len -= drop - 2;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* drop only operator */
|
|
|
|
int4 subtreepos = (notisleft) ?
|
|
|
|
poscor - 1 : poscor + q[poscor].left;
|
|
|
|
int4 subtreelen = countdroptree(q, subtreepos);
|
|
|
|
|
2001-09-23 06:16:16 +02:00
|
|
|
drop = countdroptree(q, poscor);
|
2002-09-04 22:31:48 +02:00
|
|
|
for (i = poscor + 1; i < len; i++)
|
|
|
|
if (q[i].type == OPR && q[i].left + i < poscor)
|
2002-03-06 07:10:59 +01:00
|
|
|
q[i].left += drop - subtreelen;
|
2002-09-04 22:31:48 +02:00
|
|
|
memcpy((void *) &q[subtreepos + 1],
|
|
|
|
(void *) &q[poscor + 1],
|
|
|
|
sizeof(ITEM) * (len - (poscor - 1)));
|
|
|
|
memcpy((void *) &q[poscor - drop + 1],
|
|
|
|
(void *) &q[subtreepos - subtreelen + 1],
|
|
|
|
sizeof(ITEM) * (len - (drop - subtreelen)));
|
2002-03-06 07:10:59 +01:00
|
|
|
len -= drop - subtreelen;
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
} while (index);
|
2001-09-23 06:16:16 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
querytree(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(0));
|
|
|
|
INFIX nrm;
|
|
|
|
text *res;
|
|
|
|
ITEM *q;
|
|
|
|
int4 len;
|
|
|
|
|
|
|
|
if (query->size == 0)
|
|
|
|
elog(ERROR, "Empty");
|
|
|
|
|
|
|
|
q = (ITEM *) palloc(sizeof(ITEM) * query->size);
|
|
|
|
memcpy((void *) q, GETQUERY(query), sizeof(ITEM) * query->size);
|
|
|
|
len = shorterquery(q, query->size);
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
res = (text *) palloc(1 + VARHDRSZ);
|
2001-09-23 06:16:16 +02:00
|
|
|
VARATT_SIZEP(res) = 1 + VARHDRSZ;
|
2002-09-04 22:31:48 +02:00
|
|
|
*((char *) VARDATA(res)) = 'T';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-23 06:16:16 +02:00
|
|
|
nrm.curpol = q + len - 1;
|
|
|
|
nrm.buflen = 32;
|
2002-09-04 22:31:48 +02:00
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
2001-09-23 06:16:16 +02:00
|
|
|
*(nrm.cur) = '\0';
|
2002-09-04 22:31:48 +02:00
|
|
|
infix(&nrm, true);
|
2001-09-23 06:16:16 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
res = (text *) palloc(nrm.cur - nrm.buf + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(res) = nrm.cur - nrm.buf + VARHDRSZ;
|
|
|
|
strncpy(VARDATA(res), nrm.buf, nrm.cur - nrm.buf);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
|
|
|
pfree(q);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(res);
|
2001-09-23 06:16:16 +02:00
|
|
|
}
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Additional array functions
|
|
|
|
*/
|
|
|
|
static int32 intarray_match_first(ArrayType *a, int32 elem);
|
|
|
|
static ArrayType *intarray_add_elem(ArrayType *a, int32 elem);
|
|
|
|
static ArrayType *intarray_concat_arrays(ArrayType *a, ArrayType *b);
|
|
|
|
static ArrayType *int_to_intset(int32 elem);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FUNCTION_INFO_V1(intset);
|
|
|
|
PG_FUNCTION_INFO_V1(icount);
|
|
|
|
PG_FUNCTION_INFO_V1(sort);
|
|
|
|
PG_FUNCTION_INFO_V1(sort_asc);
|
|
|
|
PG_FUNCTION_INFO_V1(sort_desc);
|
|
|
|
PG_FUNCTION_INFO_V1(uniq);
|
|
|
|
PG_FUNCTION_INFO_V1(idx);
|
|
|
|
PG_FUNCTION_INFO_V1(subarray);
|
|
|
|
PG_FUNCTION_INFO_V1(intarray_push_elem);
|
|
|
|
PG_FUNCTION_INFO_V1(intarray_push_array);
|
|
|
|
PG_FUNCTION_INFO_V1(intarray_del_elem);
|
|
|
|
PG_FUNCTION_INFO_V1(intset_union_elem);
|
|
|
|
PG_FUNCTION_INFO_V1(intset_subtract);
|
|
|
|
Datum intset(PG_FUNCTION_ARGS);
|
|
|
|
Datum icount(PG_FUNCTION_ARGS);
|
|
|
|
Datum sort(PG_FUNCTION_ARGS);
|
|
|
|
Datum sort_asc(PG_FUNCTION_ARGS);
|
|
|
|
Datum sort_desc(PG_FUNCTION_ARGS);
|
|
|
|
Datum uniq(PG_FUNCTION_ARGS);
|
|
|
|
Datum idx(PG_FUNCTION_ARGS);
|
|
|
|
Datum subarray(PG_FUNCTION_ARGS);
|
|
|
|
Datum intarray_push_elem(PG_FUNCTION_ARGS);
|
|
|
|
Datum intarray_push_array(PG_FUNCTION_ARGS);
|
|
|
|
Datum intarray_del_elem(PG_FUNCTION_ARGS);
|
|
|
|
Datum intset_union_elem(PG_FUNCTION_ARGS);
|
|
|
|
Datum intset_subtract(PG_FUNCTION_ARGS);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
|
|
|
|
static int32
|
2002-09-04 22:31:48 +02:00
|
|
|
intarray_match_first(ArrayType *a, int32 elem)
|
|
|
|
{
|
|
|
|
int32 *aa,
|
|
|
|
c,
|
|
|
|
i;
|
|
|
|
|
|
|
|
c = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
aa = ARRPTR(a);
|
|
|
|
for (i = 0; i < c; i++)
|
2002-09-04 22:31:48 +02:00
|
|
|
if (aa[i] == elem)
|
|
|
|
return (i + 1);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayType *
|
2002-09-04 22:31:48 +02:00
|
|
|
intarray_add_elem(ArrayType *a, int32 elem)
|
|
|
|
{
|
|
|
|
ArrayType *result;
|
|
|
|
int32 *r;
|
|
|
|
int32 c = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
result = new_intArrayType(c + 1);
|
|
|
|
r = ARRPTR(result);
|
2002-09-04 22:31:48 +02:00
|
|
|
if (c > 0)
|
|
|
|
memcpy(r, ARRPTR(a), c * sizeof(int32));
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
r[c] = elem;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayType *
|
2002-09-04 22:31:48 +02:00
|
|
|
intarray_concat_arrays(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *result;
|
|
|
|
int32 ac = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
|
|
|
|
int32 bc = (ARRISVOID(b)) ? 0 : ARRNELEMS(b);
|
|
|
|
|
|
|
|
result = new_intArrayType(ac + bc);
|
|
|
|
if (ac)
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
memcpy(ARRPTR(result), ARRPTR(a), ac * sizeof(int32));
|
2002-09-04 22:31:48 +02:00
|
|
|
if (bc)
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
memcpy(ARRPTR(result) + ac, ARRPTR(b), bc * sizeof(int32));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayType *
|
2002-09-04 22:31:48 +02:00
|
|
|
int_to_intset(int32 n)
|
|
|
|
{
|
|
|
|
ArrayType *result;
|
|
|
|
int32 *aa;
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
result = new_intArrayType(1);
|
|
|
|
aa = ARRPTR(result);
|
|
|
|
aa[0] = n;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-09-04 22:31:48 +02:00
|
|
|
compASC(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (*(int4 *) a == *(int4 *) b)
|
|
|
|
return 0;
|
|
|
|
return (*(int4 *) a > *(int4 *) b) ? 1 : -1;
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-09-04 22:31:48 +02:00
|
|
|
compDESC(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (*(int4 *) a == *(int4 *) b)
|
|
|
|
return 0;
|
|
|
|
return (*(int4 *) a < *(int4 *) b) ? 1 : -1;
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
#define QSORT(a, direction) \
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
if (ARRNELEMS(a) > 1) \
|
2002-09-04 22:31:48 +02:00
|
|
|
qsort((void*)ARRPTR(a), ARRNELEMS(a),sizeof(int4), \
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
(direction) ? compASC : compDESC )
|
|
|
|
|
|
|
|
#define UNIX_UNIQ(a) a = resize_intArrayType(a, unix_uniq(ARRPTR(a), ARRNELEMS(a)))
|
|
|
|
|
|
|
|
static int32
|
2002-09-04 22:31:48 +02:00
|
|
|
unix_uniq(int32 *array, int32 count)
|
|
|
|
{
|
|
|
|
register int32 i,
|
|
|
|
k = 0;
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
for (i = 1; i < count; i++)
|
2002-09-04 22:31:48 +02:00
|
|
|
if (array[k] != array[i])
|
|
|
|
{
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
k++;
|
2002-09-04 22:31:48 +02:00
|
|
|
if (i > k)
|
|
|
|
array[k] = array[i];
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
return (k + 1);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
intset(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_POINTER(int_to_intset(PG_GETARG_INT32(0)));
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
icount(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
int32 count = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_RETURN_INT32(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
sort(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
text *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_P(1) : NULL;
|
|
|
|
int32 dc = (dirstr) ? VARSIZE(dirstr) - VARHDRSZ : 0;
|
|
|
|
char *d = (dirstr) ? VARDATA(dirstr) : NULL;
|
|
|
|
int dir = -1;
|
|
|
|
|
|
|
|
if (ARRISVOID(a) || ARRNELEMS(a) < 2)
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
|
|
|
|
if (dirstr == NULL || (dc == 3
|
|
|
|
&& (d[0] == 'A' || d[0] == 'a')
|
|
|
|
&& (d[1] == 'S' || d[1] == 's')
|
|
|
|
&& (d[2] == 'C' || d[2] == 'c')))
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
dir = 1;
|
|
|
|
else if (dc == 4
|
2002-09-04 22:31:48 +02:00
|
|
|
&& (d[0] == 'D' || d[0] == 'd')
|
|
|
|
&& (d[1] == 'E' || d[1] == 'e')
|
|
|
|
&& (d[2] == 'S' || d[2] == 's')
|
|
|
|
&& (d[3] == 'C' || d[3] == 'c'))
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
dir = 0;
|
|
|
|
if (dir == -1)
|
|
|
|
elog(ERROR, "Invalid second parameter in function sort. It must be 'ASC' or 'DESC'.");
|
|
|
|
QSORT(a, dir);
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
sort_asc(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
|
|
|
|
if (ARRISVOID(a))
|
|
|
|
PG_RETURN_POINTER(a);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
QSORT(a, 1);
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
sort_desc(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
|
|
|
|
if (ARRISVOID(a))
|
|
|
|
PG_RETURN_POINTER(a);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
QSORT(a, 0);
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
uniq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
|
|
|
|
if (ARRISVOID(a) || ARRNELEMS(a) < 2)
|
|
|
|
PG_RETURN_POINTER(a);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
UNIX_UNIQ(a);
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
idx(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
int32 result = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
result = intarray_match_first(a, PG_GETARG_INT32(1));
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_RETURN_INT32(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
subarray(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *result;
|
|
|
|
int32 start = (PG_GETARG_INT32(1) > 0) ? PG_GETARG_INT32(1) - 1 : PG_GETARG_INT32(1);
|
|
|
|
int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
|
|
|
|
int32 end = 0;
|
|
|
|
int32 c;
|
|
|
|
|
|
|
|
if (ARRISVOID(a))
|
|
|
|
{
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(new_intArrayType(0));
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c = ARRNELEMS(a);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
if (start < 0)
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
start = c + start;
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (len < 0)
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
end = c + len;
|
2002-09-04 22:31:48 +02:00
|
|
|
else if (len == 0)
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
end = c;
|
|
|
|
else
|
|
|
|
end = start + len;
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (end > c)
|
|
|
|
end = c;
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (start < 0)
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
start = 0;
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (start >= end || end <= 0)
|
|
|
|
{
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_RETURN_POINTER(new_intArrayType(0));
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
result = new_intArrayType(end - start);
|
|
|
|
if (end - start > 0)
|
|
|
|
memcpy(ARRPTR(result), ARRPTR(a) + start, (end - start) * sizeof(int32));
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
intarray_push_elem(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *result;
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
result = intarray_add_elem(a, PG_GETARG_INT32(1));
|
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
intarray_push_array(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *b = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
|
|
|
|
ArrayType *result;
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
result = intarray_concat_arrays(a, b);
|
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
intarray_del_elem(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
int32 c = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
|
|
|
|
int32 *aa = ARRPTR(a);
|
|
|
|
int32 n = 0,
|
|
|
|
i;
|
|
|
|
int32 elem = PG_GETARG_INT32(1);
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
for (i = 0; i < c; i++)
|
2002-09-04 22:31:48 +02:00
|
|
|
if (aa[i] != elem)
|
|
|
|
{
|
|
|
|
if (i > n)
|
|
|
|
aa[n++] = aa[i];
|
|
|
|
else
|
|
|
|
n++;
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
if (c > 0)
|
|
|
|
a = resize_intArrayType(a, n);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
intset_union_elem(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *result;
|
|
|
|
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
result = intarray_add_elem(a, PG_GETARG_INT32(1));
|
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
QSORT(result, 1);
|
|
|
|
UNIX_UNIQ(result);
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
intset_subtract(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
ArrayType *b = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
|
|
|
ArrayType *result;
|
|
|
|
int32 ca = ARRISVOID(a);
|
|
|
|
int32 cb = ARRISVOID(b);
|
|
|
|
int32 *aa,
|
|
|
|
*bb,
|
|
|
|
*r;
|
|
|
|
int32 n = 0,
|
|
|
|
i = 0,
|
|
|
|
k = 0;
|
|
|
|
|
|
|
|
QSORT(a, 1);
|
|
|
|
UNIX_UNIQ(a);
|
|
|
|
ca = ARRNELEMS(a);
|
|
|
|
QSORT(b, 1);
|
|
|
|
UNIX_UNIQ(b);
|
|
|
|
cb = ARRNELEMS(b);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
result = new_intArrayType(ca);
|
|
|
|
aa = ARRPTR(a);
|
|
|
|
bb = ARRPTR(b);
|
|
|
|
r = ARRPTR(result);
|
2002-09-04 22:31:48 +02:00
|
|
|
while (i < ca)
|
|
|
|
{
|
|
|
|
if (k == cb || aa[i] < bb[k])
|
|
|
|
r[n++] = aa[i++];
|
|
|
|
else if (aa[i] == bb[k])
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
k++;
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
}
|
|
|
|
result = resize_intArrayType(result, n);
|
2002-09-04 22:31:48 +02:00
|
|
|
pfree(a);
|
|
|
|
pfree(b);
|
August 6, 2002
1. Reworked patch from Andrey Oktyabrski (ano@spider.ru) with
functions: icount, sort, sort_asc, uniq, idx, subarray
operations: #, +, -, |, &
FUNCTIONS:
int icount(int[]) - the number of elements in intarray
int[] sort(int[], 'asc' | 'desc') - sort intarray
int[] sort(int[]) - sort in ascending order
int[] sort_asc(int[]),sort_desc(int[]) - shortcuts for sort
int[] uniq(int[]) - returns unique elements
int idx(int[], int item) - returns index of first intarray matching element
to item, or '0' if matching failed.
int[] subarray(int[],int START [, int LEN]) - returns part of intarray
starting from element number START (from 1)
and length LEN.
OPERATIONS:
int[] && int[] - overlap - returns TRUE if arrays has at least one common elements.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove left array from right
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
Oleg Bartunov
2002-08-10 22:38:29 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|