Commit a237dd2b authored by Bruce Momjian's avatar Bruce Momjian

Add missing intarray files.

parent a24c5a7b
#include "postgres.h"
#include <float.h>
#include "access/gist.h"
#include "access/itup.h"
#include "access/rtree.h"
#include "catalog/pg_type.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "storage/bufpage.h"
#include "lib/stringinfo.h"
/* number ranges for compression */
#define MAXNUMRANGE 100
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) <= (b) ? (a) : (b))
#define abs(a) ((a) < (0) ? -(a) : (a))
/* dimension of array */
#define NDIM 1
/*
* flags for gist__int_ops, use ArrayType->flags
* which is unused (see array.h)
*/
#define LEAFKEY (1<<31)
#define ISLEAFKEY(x) ( ((ArrayType*)(x))->flags & LEAFKEY )
/* useful macros for accessing int4 arrays */
#define ARRPTR(x) ( (int4 *) ARR_DATA_PTR(x) )
#define ARRNELEMS(x) ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x))
#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 )
#define SORT(x) \
do { \
if ( ARRNELEMS( x ) > 1 ) \
isort( ARRPTR( x ), ARRNELEMS( x ) ); \
} while(0)
#define PREPAREARR(x) \
do { \
if ( ARRNELEMS( x ) > 1 ) \
if ( isort( ARRPTR( x ), ARRNELEMS( x ) ) ) \
x = _int_unique( x ); \
} while(0)
/* "wish" function */
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
/* bigint defines */
#define BITBYTE 8
#define SIGLENINT 63 /* >122 => key will toast, so very slow!!! */
#define SIGLEN ( sizeof(int)*SIGLENINT )
#define SIGLENBIT (SIGLEN*BITBYTE)
typedef char BITVEC[SIGLEN];
typedef char *BITVECP;
#define SIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) )
#define LOOPBYTE(a) \
for(i=0;i<SIGLEN;i++) {\
a;\
}
#define LOOPBIT(a) \
for(i=0;i<SIGLENBIT;i++) {\
a;\
}
/* beware of multiple evaluation of arguments to these macros! */
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
#define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
#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 )
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
/*
* type of index key
*/
typedef struct
{
int4 len;
int4 flag;
char data[1];
} GISTTYPE;
#define ALLISTRUE 0x04
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
#define GTHDRSIZE ( sizeof(int4)*2 )
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
/*
** types for functions
*/
typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
typedef void (*formfloat) (ArrayType *, float *);
/*
** useful function
*/
bool isort(int4 *a, const int len);
ArrayType *new_intArrayType(int num);
ArrayType *copy_intArrayType(ArrayType *a);
ArrayType *resize_intArrayType(ArrayType *a, int num);
int internal_size(int *a, int len);
ArrayType *_int_unique(ArrayType *a);
int32 intarray_match_first(ArrayType *a, int32 elem);
ArrayType *intarray_add_elem(ArrayType *a, int32 elem);
ArrayType *intarray_concat_arrays(ArrayType *a, ArrayType *b);
ArrayType *int_to_intset(int32 elem);
bool inner_int_overlap(ArrayType *a, ArrayType *b);
bool inner_int_contains(ArrayType *a, ArrayType *b);
ArrayType * inner_int_union(ArrayType *a, ArrayType *b);
ArrayType * inner_int_inter(ArrayType *a, ArrayType *b);
void rt__int_size(ArrayType *a, float *size);
void gensign(BITVEC sign, int *a, int len);
/*****************************************************************************
* Boolean Search
*****************************************************************************/
#define BooleanSearchStrategy 20
/*
* item in polish notation with back link
* to left operand
*/
typedef struct ITEM
{
int2 type;
int2 left;
int4 val;
} ITEM;
typedef struct
{
int4 len;
int4 size;
char data[1];
} QUERYTYPE;
#define HDRSIZEQT ( 2*sizeof(int4) )
#define COMPUTESIZE(size) ( HDRSIZEQT + size * sizeof(ITEM) )
#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
bool signconsistent(QUERYTYPE * query, BITVEC sign, bool calcnot);
bool execconsistent(QUERYTYPE * query, ArrayType *array, bool calcnot);
int compASC(const void *a, const void *b);
int compDESC(const void *a, const void *b);
#define QSORT(a, direction) \
if (ARRNELEMS(a) > 1) \
qsort((void*)ARRPTR(a), ARRNELEMS(a),sizeof(int4), \
(direction) ? compASC : compDESC )
This diff is collapsed.
This diff is collapsed.
#include "_int.h"
#include "lib/stringinfo.h"
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);
Datum
_int_contained(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(DatumGetBool(
DirectFunctionCall2(
_int_contains,
PointerGetDatum(PG_GETARG_POINTER(1)),
PointerGetDatum(PG_GETARG_POINTER(0))
)
));
}
Datum
_int_contains(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)));
bool res;
if (ARRISVOID(a) || ARRISVOID(b))
return FALSE;
PREPAREARR(a);
PREPAREARR(b);
res = inner_int_contains(a, b);
pfree(a);
pfree(b);
PG_RETURN_BOOL(res);
}
Datum
_int_different(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(!DatumGetBool(
DirectFunctionCall2(
_int_same,
PointerGetDatum(PG_GETARG_POINTER(0)),
PointerGetDatum(PG_GETARG_POINTER(1))
)
));
}
Datum
_int_same(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)));
int na,
nb;
int n;
int *da,
*db;
bool result;
bool avoid = ARRISVOID(a);
bool bvoid = ARRISVOID(b);
if (avoid || bvoid)
return (avoid && bvoid) ? TRUE : FALSE;
SORT(a);
SORT(b);
na = ARRNELEMS(a);
nb = ARRNELEMS(b);
da = ARRPTR(a);
db = ARRPTR(b);
result = FALSE;
if (na == nb)
{
result = TRUE;
for (n = 0; n < na; n++)
if (da[n] != db[n])
{
result = FALSE;
break;
}
}
pfree(a);
pfree(b);
PG_RETURN_BOOL(result);
}
/* _int_overlap -- does a overlap b?
*/
Datum
_int_overlap(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)));
bool result;
if (ARRISVOID(a) || ARRISVOID(b))
return FALSE;
SORT(a);
SORT(b);
result = inner_int_overlap(a, b);
pfree(a);
pfree(b);
PG_RETURN_BOOL(result);
}
Datum
_int_union(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;
if (!ARRISVOID(a))
SORT(a);
if (!ARRISVOID(b))
SORT(b);
result = inner_int_union(a, b);
if (a)
pfree(a);
if (b)
pfree(b);
PG_RETURN_POINTER(result);
}
Datum
_int_inter(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;
if (ARRISVOID(a) || ARRISVOID(b))
PG_RETURN_POINTER(new_intArrayType(0));
SORT(a);
SORT(b);
result = inner_int_inter(a, b);
pfree(a);
pfree(b);
PG_RETURN_POINTER(result);
}
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);
#define QSORT(a, direction) \
if (ARRNELEMS(a) > 1) \
qsort((void*)ARRPTR(a), ARRNELEMS(a),sizeof(int4), \
(direction) ? compASC : compDESC )
Datum
intset(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(int_to_intset(PG_GETARG_INT32(0)));
}
Datum
icount(PG_FUNCTION_ARGS)
{
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
int32 count = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
PG_FREE_IF_COPY(a, 0);
PG_RETURN_INT32(count);
}
Datum
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')))
dir = 1;
else if (dc == 4
&& (d[0] == 'D' || d[0] == 'd')
&& (d[1] == 'E' || d[1] == 'e')
&& (d[2] == 'S' || d[2] == 's')
&& (d[3] == 'C' || d[3] == 'c'))
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
sort_asc(PG_FUNCTION_ARGS)
{
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
if (ARRISVOID(a))
PG_RETURN_POINTER(a);
QSORT(a, 1);
PG_RETURN_POINTER(a);
}
Datum
sort_desc(PG_FUNCTION_ARGS)
{
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
if (ARRISVOID(a))
PG_RETURN_POINTER(a);
QSORT(a, 0);
PG_RETURN_POINTER(a);
}
Datum
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);
a = _int_unique(a);
PG_RETURN_POINTER(a);
}
Datum
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));
PG_FREE_IF_COPY(a, 0);
PG_RETURN_INT32(result);
}
Datum
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))
{
PG_FREE_IF_COPY(a, 0);
PG_RETURN_POINTER(new_intArrayType(0));
}
c = ARRNELEMS(a);
if (start < 0)
start = c + start;
if (len < 0)
end = c + len;
else if (len == 0)
end = c;
else
end = start + len;
if (end > c)
end = c;
if (start < 0)
start = 0;
if (start >= end || end <= 0)
{
PG_FREE_IF_COPY(a, 0);
PG_RETURN_POINTER(new_intArrayType(0));
}
result = new_intArrayType(end - start);
if (end - start > 0)
memcpy(ARRPTR(result), ARRPTR(a) + start, (end - start) * sizeof(int32));
PG_FREE_IF_COPY(a, 0);
PG_RETURN_POINTER(result);
}
Datum
intarray_push_elem(PG_FUNCTION_ARGS)
{
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
ArrayType *result;
result = intarray_add_elem(a, PG_GETARG_INT32(1));
PG_FREE_IF_COPY(a, 0);
PG_RETURN_POINTER(result);
}
Datum
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;
result = intarray_concat_arrays(a, b);
PG_FREE_IF_COPY(a, 0);
PG_FREE_IF_COPY(b, 1);
PG_RETURN_POINTER(result);
}
Datum
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);
for (i = 0; i < c; i++)
if (aa[i] != elem)
{
if (i > n)
aa[n++] = aa[i];
else
n++;
}
if (c > 0)
a = resize_intArrayType(a, n);
PG_RETURN_POINTER(a);
}
Datum
intset_union_elem(PG_FUNCTION_ARGS)
{
ArrayType *a = (ArrayType *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
ArrayType *result;
result = intarray_add_elem(a, PG_GETARG_INT32(1));
PG_FREE_IF_COPY(a, 0);
QSORT(result, 1);
PG_RETURN_POINTER(_int_unique(result));
}
Datum
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);
a = _int_unique(a);
ca = ARRNELEMS(a);
QSORT(b, 1);
b = _int_unique(b);
cb = ARRNELEMS(b);
result = new_intArrayType(ca);
aa = ARRPTR(a);
bb = ARRPTR(b);
r = ARRPTR(result);
while (i < ca)
{
if (k == cb || aa[i] < bb[k])
r[n++] = aa[i++];
else if (aa[i] == bb[k])
{
i++;
k++;
}
else
k++;
}
result = resize_intArrayType(result, n);
pfree(a);
pfree(b);
PG_RETURN_POINTER(result);
}
#include "_int.h"
bool
inner_int_contains(ArrayType *a, ArrayType *b)
{
int na,
nb;
int i,
j,
n;
int *da,
*db;
if (ARRISVOID(a) || ARRISVOID(b))
return FALSE;
na = ARRNELEMS(a);
nb = ARRNELEMS(b);
da = ARRPTR(a);
db = ARRPTR(b);
i = j = n = 0;
while (i < na && j < nb)
if (da[i] < db[j])
i++;
else if (da[i] == db[j])
{
n++;
i++;
j++;
}
else
j++;
return (n == nb) ? TRUE : FALSE;
}
bool
inner_int_overlap(ArrayType *a, ArrayType *b)
{
int na,
nb;
int i,
j;
int *da,
*db;
if (ARRISVOID(a) || ARRISVOID(b))
return FALSE;
na = ARRNELEMS(a);
nb = ARRNELEMS(b);
da = ARRPTR(a);
db = ARRPTR(b);
i = j = 0;
while (i < na && j < nb)
if (da[i] < db[j])
i++;
else if (da[i] == db[j])
return TRUE;
else
j++;
return FALSE;
}
ArrayType *
inner_int_union(ArrayType *a, ArrayType *b)
{
ArrayType *r = NULL;
int na,
nb;
int *da,
*db,
*dr;
int i,
j;
if (ARRISVOID(a) && ARRISVOID(b))
return new_intArrayType(0);
if (ARRISVOID(a))
r = copy_intArrayType(b);
if (ARRISVOID(b))
r = copy_intArrayType(a);
if (r)
dr = ARRPTR(r);
else
{
na = ARRNELEMS(a);
nb = ARRNELEMS(b);
da = ARRPTR(a);
db = ARRPTR(b);
r = new_intArrayType(na + nb);
dr = ARRPTR(r);
/* union */
i = j = 0;
while (i < na && j < nb)
if (da[i] < db[j])
*dr++ = da[i++];
else
*dr++ = db[j++];
while (i < na)
*dr++ = da[i++];
while (j < nb)
*dr++ = db[j++];
}
if (ARRNELEMS(r) > 1)
r = _int_unique(r);
return r;
}
ArrayType *
inner_int_inter(ArrayType *a, ArrayType *b)
{
ArrayType *r;
int na,
nb;
int *da,
*db,
*dr;
int i,
j;
if (ARRISVOID(a) || ARRISVOID(b))
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);
i = j = 0;
while (i < na && j < nb)
if (da[i] < db[j])
i++;
else if (da[i] == db[j])
{
if (i + j == 0 || (i + j > 0 && *(dr - 1) != db[j]))
*dr++ = db[j];
i++;
j++;
}
else
j++;
if ((dr - ARRPTR(r)) == 0)
{
pfree(r);
return new_intArrayType(0);
}
else
return resize_intArrayType(r, dr - ARRPTR(r));
}
void
rt__int_size(ArrayType *a, float *size)
{
*size = (float) ARRNELEMS(a);
return;
}
/* len >= 2 */
bool
isort(int4 *a, int len)
{
int4 tmp,
index;
int4 *cur,
*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)
r = TRUE;
cur++;
}
} while (index);
return r;
}
ArrayType *
new_intArrayType(int num)
{
ArrayType *r;
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
r = (ArrayType *) palloc0(nbytes);
ARR_SIZE(r) = nbytes;
ARR_NDIM(r) = NDIM;
ARR_ELEMTYPE(r) = INT4OID;
r->flags &= ~LEAFKEY;
*((int *) ARR_DIMS(r)) = num;
*((int *) ARR_LBOUND(r)) = 1;
return r;
}
ArrayType *
resize_intArrayType(ArrayType *a, int num)
{
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
if (num == ARRNELEMS(a))
return a;
a = (ArrayType *) repalloc(a, nbytes);
a->size = nbytes;
*((int *) ARR_DIMS(a)) = num;
return a;
}
ArrayType *
copy_intArrayType(ArrayType *a)
{
ArrayType *r;
r = new_intArrayType(ARRNELEMS(a));
memmove(r, a, VARSIZE(a));
return r;
}
/* num for compressed key */
int
internal_size(int *a, int len)
{
int i,
size = 0;
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;
return size;
}
/* r is sorted and size of r > 1 */
ArrayType *
_int_unique(ArrayType *r)
{
int *tmp,
*dr,
*data;
int num = ARRNELEMS(r);
if ( num<2 )
return r;
data = tmp = dr = ARRPTR(r);
while (tmp - data < num)
if (*tmp != *dr)
*(++dr) = *tmp++;
else
tmp++;
return resize_intArrayType(r, dr + 1 - ARRPTR(r));
}
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++)
{
HASH(sign, *a);
a++;
}
}
int32
intarray_match_first(ArrayType *a, int32 elem)
{
int32 *aa,
c,
i;
c = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
aa = ARRPTR(a);
for (i = 0; i < c; i++)
if (aa[i] == elem)
return (i + 1);
return 0;
}
ArrayType *
intarray_add_elem(ArrayType *a, int32 elem)
{
ArrayType *result;
int32 *r;
int32 c = (ARRISVOID(a)) ? 0 : ARRNELEMS(a);
result = new_intArrayType(c + 1);
r = ARRPTR(result);
if (c > 0)
memcpy(r, ARRPTR(a), c * sizeof(int32));
r[c] = elem;
return result;
}
ArrayType *
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)
memcpy(ARRPTR(result), ARRPTR(a), ac * sizeof(int32));
if (bc)
memcpy(ARRPTR(result) + ac, ARRPTR(b), bc * sizeof(int32));
return result;
}
ArrayType *
int_to_intset(int32 n)
{
ArrayType *result;
int32 *aa;
result = new_intArrayType(1);
aa = ARRPTR(result);
aa[0] = n;
return result;
}
int
compASC(const void *a, const void *b)
{
if (*(int4 *) a == *(int4 *) b)
return 0;
return (*(int4 *) a > *(int4 *) b) ? 1 : -1;
}
int
compDESC(const void *a, const void *b)
{
if (*(int4 *) a == *(int4 *) b)
return 0;
return (*(int4 *) a < *(int4 *) b) ? 1 : -1;
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment