1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* arrayutils.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* This file contains some support routines required for array functions.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-03-05 16:59:11 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.21 2006/03/05 15:58:41 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1996-11-06 07:52:23 +01:00
|
|
|
#include "postgres.h"
|
2000-07-22 05:34:43 +02:00
|
|
|
|
1999-07-15 21:21:43 +02:00
|
|
|
#include "utils/array.h"
|
2005-11-17 23:14:56 +01:00
|
|
|
#include "utils/memutils.h"
|
2000-07-22 05:34:43 +02:00
|
|
|
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* Convert subscript list into linear element number (from 0)
|
|
|
|
*
|
|
|
|
* We assume caller has already range-checked the dimensions and subscripts,
|
|
|
|
* so no overflow is possible.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
int
|
2005-11-17 23:14:56 +01:00
|
|
|
ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
2000-07-22 05:34:43 +02:00
|
|
|
scale = 1,
|
|
|
|
offset = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-22 05:34:43 +02:00
|
|
|
for (i = n - 1; i >= 0; i--)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
offset += (indx[i] - lb[i]) * scale;
|
2000-07-22 05:34:43 +02:00
|
|
|
scale *= dim[i];
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
return offset;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* Same, but subscripts are assumed 0-based, and use a scale array
|
2000-07-22 05:34:43 +02:00
|
|
|
* instead of raw dimension data (see mda_get_prod to create scale array)
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
int
|
2005-11-17 23:14:56 +01:00
|
|
|
ArrayGetOffset0(int n, const int *tup, const int *scale)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
2000-07-22 05:34:43 +02:00
|
|
|
lin = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-22 05:34:43 +02:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
lin += tup[i] * scale[i];
|
|
|
|
return lin;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* Convert array dimensions into number of elements
|
|
|
|
*
|
|
|
|
* This must do overflow checking, since it is used to validate that a user
|
|
|
|
* dimensionality request doesn't overflow what we can handle.
|
|
|
|
*
|
|
|
|
* We limit array sizes to at most about a quarter billion elements,
|
|
|
|
* so that it's not necessary to check for overflow in quite so many
|
|
|
|
* places --- for instance when palloc'ing Datum arrays.
|
|
|
|
*
|
|
|
|
* The multiplication overflow check only works on machines that have int64
|
|
|
|
* arithmetic, but that is nearly all platforms these days, and doing check
|
|
|
|
* divides for those that don't seems way too expensive.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
int
|
2005-11-17 23:14:56 +01:00
|
|
|
ArrayGetNItems(int ndim, const int *dims)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-11-17 23:14:56 +01:00
|
|
|
int32 ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
#define MaxArraySize ((Size) (MaxAllocSize / sizeof(Datum)))
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-09-16 05:15:54 +02:00
|
|
|
if (ndim <= 0)
|
2000-07-22 05:34:43 +02:00
|
|
|
return 0;
|
|
|
|
ret = 1;
|
2004-09-16 05:15:54 +02:00
|
|
|
for (i = 0; i < ndim; i++)
|
2005-11-17 23:14:56 +01:00
|
|
|
{
|
2005-11-22 19:17:34 +01:00
|
|
|
int64 prod;
|
2005-11-17 23:14:56 +01:00
|
|
|
|
|
|
|
/* A negative dimension implies that UB-LB overflowed ... */
|
|
|
|
if (dims[i] < 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("array size exceeds the maximum allowed (%d)",
|
|
|
|
(int) MaxArraySize)));
|
|
|
|
|
2005-11-22 19:17:34 +01:00
|
|
|
prod = (int64) ret *(int64) dims[i];
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
ret = (int32) prod;
|
|
|
|
if ((int64) ret != prod)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("array size exceeds the maximum allowed (%d)",
|
|
|
|
(int) MaxArraySize)));
|
|
|
|
}
|
|
|
|
Assert(ret >= 0);
|
|
|
|
if ((Size) ret > MaxArraySize)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("array size exceeds the maximum allowed (%d)",
|
|
|
|
(int) MaxArraySize)));
|
|
|
|
return (int) ret;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* Compute ranges (sub-array dimensions) for an array slice
|
|
|
|
*
|
|
|
|
* We assume caller has validated slice endpoints, so overflow is impossible
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
2005-11-17 23:14:56 +01:00
|
|
|
mda_get_range(int n, int *span, const int *st, const int *endp)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
span[i] = endp[i] - st[i] + 1;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* Compute products of array dimensions, ie, scale factors for subscripts
|
|
|
|
*
|
|
|
|
* We assume caller has validated dimensions, so overflow is impossible
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
2005-11-17 23:14:56 +01:00
|
|
|
mda_get_prod(int n, const int *range, int *prod)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-22 05:34:43 +02:00
|
|
|
prod[n - 1] = 1;
|
|
|
|
for (i = n - 2; i >= 0; i--)
|
|
|
|
prod[i] = prod[i + 1] * range[i + 1];
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* From products of whole-array dimensions and spans of a sub-array,
|
2000-07-22 05:34:43 +02:00
|
|
|
* compute offset distances needed to step through subarray within array
|
2005-11-17 23:14:56 +01:00
|
|
|
*
|
|
|
|
* We assume caller has validated dimensions, so overflow is impossible
|
2000-07-22 05:34:43 +02:00
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
2005-11-17 23:14:56 +01:00
|
|
|
mda_get_offset_values(int n, int *dist, const int *prod, const int *span)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-07-22 05:34:43 +02:00
|
|
|
int i,
|
|
|
|
j;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-22 05:34:43 +02:00
|
|
|
dist[n - 1] = 0;
|
|
|
|
for (j = n - 2; j >= 0; j--)
|
|
|
|
{
|
|
|
|
dist[j] = prod[j] - 1;
|
|
|
|
for (i = j + 1; i < n; i++)
|
|
|
|
dist[j] -= (span[i] - 1) * prod[i];
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* Generates the tuple that is lexicographically one greater than the current
|
|
|
|
* n-tuple in "curr", with the restriction that the i-th element of "curr" is
|
|
|
|
* less than the i-th element of "span".
|
|
|
|
*
|
|
|
|
* Returns -1 if no next tuple exists, else the subscript position (0..n-1)
|
|
|
|
* corresponding to the dimension to advance along.
|
|
|
|
*
|
|
|
|
* We assume caller has validated dimensions, so overflow is impossible
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
int
|
2005-11-17 23:14:56 +01:00
|
|
|
mda_next_tuple(int n, int *curr, const int *span)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-07-22 05:34:43 +02:00
|
|
|
if (n <= 0)
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
2000-07-22 05:34:43 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
curr[n - 1] = (curr[n - 1] + 1) % span[n - 1];
|
2000-07-22 05:34:43 +02:00
|
|
|
for (i = n - 1; i && curr[i] == 0; i--)
|
1997-09-07 07:04:48 +02:00
|
|
|
curr[i - 1] = (curr[i - 1] + 1) % span[i - 1];
|
|
|
|
|
|
|
|
if (i)
|
1998-09-01 05:29:17 +02:00
|
|
|
return i;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (curr[0])
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
2000-07-22 05:34:43 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|