2008-05-17 05:28:26 +04:00
|
|
|
/*
|
2010-09-21 00:08:53 +04:00
|
|
|
* contrib/intarray/_int_tool.c
|
2008-05-17 05:28:26 +04:00
|
|
|
*/
|
2008-05-12 04:00:54 +04:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
|
2003-06-11 23:31:05 +04:00
|
|
|
#include "_int.h"
|
|
|
|
|
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
/* arguments are assumed sorted & unique-ified */
|
2003-06-11 23:31:05 +04:00
|
|
|
bool
|
|
|
|
inner_int_contains(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
n;
|
|
|
|
int *da,
|
|
|
|
*db;
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
|
|
|
|
|
|
|
i = j = n = 0;
|
|
|
|
while (i < na && j < nb)
|
2011-01-09 08:39:21 +03:00
|
|
|
{
|
2003-06-11 23:31:05 +04:00
|
|
|
if (da[i] < db[j])
|
|
|
|
i++;
|
|
|
|
else if (da[i] == db[j])
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else
|
2011-01-09 08:39:21 +03:00
|
|
|
break; /* db[j] is not in da */
|
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
return (n == nb) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
/* arguments are assumed sorted */
|
2003-06-11 23:31:05 +04:00
|
|
|
bool
|
|
|
|
inner_int_overlap(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int *da,
|
|
|
|
*db;
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
|
|
|
|
|
|
|
i = j = 0;
|
|
|
|
while (i < na && j < nb)
|
2011-01-09 08:39:21 +03:00
|
|
|
{
|
2003-06-11 23:31:05 +04:00
|
|
|
if (da[i] < db[j])
|
|
|
|
i++;
|
|
|
|
else if (da[i] == db[j])
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
j++;
|
2011-01-09 08:39:21 +03:00
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayType *
|
|
|
|
inner_int_union(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r = NULL;
|
|
|
|
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(a);
|
|
|
|
CHECKARRVALID(b);
|
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
if (ARRISEMPTY(a) && ARRISEMPTY(b))
|
2003-06-11 23:31:05 +04:00
|
|
|
return new_intArrayType(0);
|
2011-01-09 08:39:21 +03:00
|
|
|
if (ARRISEMPTY(a))
|
2003-06-11 23:31:05 +04:00
|
|
|
r = copy_intArrayType(b);
|
2011-01-09 08:39:21 +03:00
|
|
|
if (ARRISEMPTY(b))
|
2003-06-11 23:31:05 +04:00
|
|
|
r = copy_intArrayType(a);
|
|
|
|
|
2006-05-10 15:39:12 +04:00
|
|
|
if (!r)
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
2006-10-04 04:30:14 +04:00
|
|
|
int na = ARRNELEMS(a),
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
int *da = ARRPTR(a),
|
|
|
|
*db = ARRPTR(b);
|
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
*dr;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
r = new_intArrayType(na + nb);
|
|
|
|
dr = ARRPTR(r);
|
|
|
|
|
|
|
|
/* union */
|
|
|
|
i = j = 0;
|
2006-10-04 04:30:14 +04:00
|
|
|
while (i < na && j < nb)
|
|
|
|
{
|
|
|
|
if (da[i] == db[j])
|
|
|
|
{
|
2006-05-10 15:39:12 +04:00
|
|
|
*dr++ = da[i++];
|
|
|
|
j++;
|
2006-10-04 04:30:14 +04:00
|
|
|
}
|
|
|
|
else if (da[i] < db[j])
|
2003-06-11 23:31:05 +04:00
|
|
|
*dr++ = da[i++];
|
|
|
|
else
|
|
|
|
*dr++ = db[j++];
|
2006-05-10 15:39:12 +04:00
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
while (i < na)
|
|
|
|
*dr++ = da[i++];
|
|
|
|
while (j < nb)
|
|
|
|
*dr++ = db[j++];
|
|
|
|
|
2006-10-04 04:30:14 +04:00
|
|
|
r = resize_intArrayType(r, dr - ARRPTR(r));
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-02-17 05:00:11 +04:00
|
|
|
j,
|
|
|
|
k;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
if (ARRISEMPTY(a) || ARRISEMPTY(b))
|
2003-06-11 23:31:05 +04:00
|
|
|
return new_intArrayType(0);
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2004-10-21 23:28:36 +04:00
|
|
|
r = new_intArrayType(Min(na, nb));
|
2003-06-11 23:31:05 +04:00
|
|
|
dr = ARRPTR(r);
|
|
|
|
|
2012-02-17 05:00:11 +04:00
|
|
|
i = j = k = 0;
|
2003-06-11 23:31:05 +04:00
|
|
|
while (i < na && j < nb)
|
2011-01-09 08:39:21 +03:00
|
|
|
{
|
2003-06-11 23:31:05 +04:00
|
|
|
if (da[i] < db[j])
|
|
|
|
i++;
|
|
|
|
else if (da[i] == db[j])
|
|
|
|
{
|
2012-02-17 05:00:11 +04:00
|
|
|
if (k == 0 || dr[k - 1] != db[j])
|
|
|
|
dr[k++] = db[j];
|
2003-06-11 23:31:05 +04:00
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
j++;
|
2011-01-09 08:39:21 +03:00
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2012-02-17 05:00:11 +04:00
|
|
|
if (k == 0)
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
|
|
|
pfree(r);
|
|
|
|
return new_intArrayType(0);
|
|
|
|
}
|
|
|
|
else
|
2012-02-17 05:00:11 +04:00
|
|
|
return resize_intArrayType(r, k);
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rt__int_size(ArrayType *a, float *size)
|
|
|
|
{
|
|
|
|
*size = (float) ARRNELEMS(a);
|
|
|
|
}
|
|
|
|
|
2011-04-10 19:42:00 +04:00
|
|
|
/* Sort the given data (len >= 2). Return true if any duplicates found */
|
2003-06-11 23:31:05 +04:00
|
|
|
bool
|
2012-06-25 02:51:46 +04:00
|
|
|
isort(int32 *a, int len)
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
2012-06-25 02:51:46 +04:00
|
|
|
int32 cur,
|
2011-01-09 08:39:21 +03:00
|
|
|
prev;
|
2012-06-25 02:51:46 +04:00
|
|
|
int32 *pcur,
|
2011-01-09 08:39:21 +03:00
|
|
|
*pprev,
|
2003-06-11 23:31:05 +04:00
|
|
|
*end;
|
|
|
|
bool r = FALSE;
|
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
/*
|
2011-04-10 19:42:00 +04:00
|
|
|
* We use a simple insertion sort. While this is O(N^2) in the worst
|
2011-01-09 08:39:21 +03:00
|
|
|
* case, it's quite fast if the input is already sorted or nearly so.
|
|
|
|
* Also, for not-too-large inputs it's faster than more complex methods
|
|
|
|
* anyhow.
|
|
|
|
*/
|
2003-06-11 23:31:05 +04:00
|
|
|
end = a + len;
|
2011-01-09 08:39:21 +03:00
|
|
|
for (pcur = a + 1; pcur < end; pcur++)
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
2011-01-09 08:39:21 +03:00
|
|
|
cur = *pcur;
|
|
|
|
for (pprev = pcur - 1; pprev >= a; pprev--)
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
2011-01-09 08:39:21 +03:00
|
|
|
prev = *pprev;
|
|
|
|
if (prev <= cur)
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
2011-01-09 08:39:21 +03:00
|
|
|
if (prev == cur)
|
|
|
|
r = TRUE;
|
|
|
|
break;
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
2011-01-09 08:39:21 +03:00
|
|
|
pprev[1] = prev;
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
2011-01-09 08:39:21 +03:00
|
|
|
pprev[1] = cur;
|
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
/* Create a new int array with room for "num" elements */
|
2003-06-11 23:31:05 +04:00
|
|
|
ArrayType *
|
|
|
|
new_intArrayType(int num)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
2011-01-09 08:39:21 +03:00
|
|
|
int nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
r = (ArrayType *) palloc0(nbytes);
|
|
|
|
|
2007-02-28 02:48:10 +03:00
|
|
|
SET_VARSIZE(r, nbytes);
|
2011-01-09 08:39:21 +03:00
|
|
|
ARR_NDIM(r) = 1;
|
2005-11-18 01:14:56 +03:00
|
|
|
r->dataoffset = 0; /* marker for no null bitmap */
|
2003-06-11 23:31:05 +04:00
|
|
|
ARR_ELEMTYPE(r) = INT4OID;
|
2011-01-09 08:39:21 +03:00
|
|
|
ARR_DIMS(r)[0] = num;
|
|
|
|
ARR_LBOUND(r)[0] = 1;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayType *
|
|
|
|
resize_intArrayType(ArrayType *a, int num)
|
|
|
|
{
|
2011-01-09 08:39:21 +03:00
|
|
|
int nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
|
|
|
|
int i;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
if (num == ARRNELEMS(a))
|
|
|
|
return a;
|
|
|
|
|
|
|
|
a = (ArrayType *) repalloc(a, nbytes);
|
|
|
|
|
2007-02-28 02:48:10 +03:00
|
|
|
SET_VARSIZE(a, nbytes);
|
2011-01-09 08:39:21 +03:00
|
|
|
/* usually the array should be 1-D already, but just in case ... */
|
|
|
|
for (i = 0; i < ARR_NDIM(a); i++)
|
|
|
|
{
|
|
|
|
ARR_DIMS(a)[i] = num;
|
|
|
|
num = 1;
|
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayType *
|
|
|
|
copy_intArrayType(ArrayType *a)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
2011-01-09 08:39:21 +03:00
|
|
|
int n = ARRNELEMS(a);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
r = new_intArrayType(n);
|
2012-06-25 02:51:46 +04:00
|
|
|
memcpy(ARRPTR(r), ARRPTR(a), n * sizeof(int32));
|
2003-06-11 23:31:05 +04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* num for compressed key */
|
|
|
|
int
|
|
|
|
internal_size(int *a, int len)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i += 2)
|
2011-01-09 08:39:21 +03:00
|
|
|
{
|
2003-06-11 23:31:05 +04:00
|
|
|
if (!i || a[i] != a[i - 1]) /* do not count repeated range */
|
|
|
|
size += a[i + 1] - a[i] + 1;
|
2011-01-09 08:39:21 +03:00
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
/* unique-ify elements of r in-place ... r must be sorted already */
|
2003-06-11 23:31:05 +04:00
|
|
|
ArrayType *
|
|
|
|
_int_unique(ArrayType *r)
|
|
|
|
{
|
|
|
|
int *tmp,
|
|
|
|
*dr,
|
|
|
|
*data;
|
|
|
|
int num = ARRNELEMS(r);
|
|
|
|
|
2003-08-04 04:43:34 +04:00
|
|
|
if (num < 2)
|
2003-06-11 23:31:05 +04:00
|
|
|
return r;
|
|
|
|
|
|
|
|
data = tmp = dr = ARRPTR(r);
|
|
|
|
while (tmp - data < num)
|
2011-01-09 08:39:21 +03:00
|
|
|
{
|
2003-06-11 23:31:05 +04:00
|
|
|
if (*tmp != *dr)
|
|
|
|
*(++dr) = *tmp++;
|
|
|
|
else
|
|
|
|
tmp++;
|
2011-01-09 08:39:21 +03:00
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
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;
|
|
|
|
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(a);
|
|
|
|
c = ARRNELEMS(a);
|
2003-06-11 23:31:05 +04:00
|
|
|
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;
|
2005-11-19 06:00:09 +03:00
|
|
|
int32 c;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(a);
|
2011-01-09 08:39:21 +03:00
|
|
|
c = ARRNELEMS(a);
|
2003-06-11 23:31:05 +04:00
|
|
|
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;
|
2011-01-09 08:39:21 +03:00
|
|
|
int32 ac = ARRNELEMS(a);
|
|
|
|
int32 bc = ARRNELEMS(b);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(a);
|
|
|
|
CHECKARRVALID(b);
|
2003-06-11 23:31:05 +04:00
|
|
|
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)
|
|
|
|
{
|
2012-06-25 02:51:46 +04:00
|
|
|
if (*(const int32 *) a == *(const int32 *) b)
|
2003-06-11 23:31:05 +04:00
|
|
|
return 0;
|
2012-06-25 02:51:46 +04:00
|
|
|
return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
compDESC(const void *a, const void *b)
|
|
|
|
{
|
2012-06-25 02:51:46 +04:00
|
|
|
if (*(const int32 *) a == *(const int32 *) b)
|
2003-06-11 23:31:05 +04:00
|
|
|
return 0;
|
2012-06-25 02:51:46 +04:00
|
|
|
return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|