2000-12-12 04:40:33 +08: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.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
2005-11-08 01:36:47 +08:00
|
|
|
#include "access/skey.h"
|
2000-12-12 04:40:33 +08:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
#include "segdata.h"
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/*
|
2000-12-12 04:40:33 +08:00
|
|
|
#define GIST_DEBUG
|
2001-03-22 12:01:46 +08:00
|
|
|
#define GIST_QUERY_DEBUG
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
|
2006-05-31 06:12:16 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
extern int seg_yyparse();
|
2003-09-14 10:18:49 +08:00
|
|
|
extern void seg_yyerror(const char *message);
|
|
|
|
extern void seg_scanner_init(const char *str);
|
|
|
|
extern void seg_scanner_finish(void);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
2001-03-22 12:01:46 +08:00
|
|
|
extern int seg_yydebug;
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Input/Output routines
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
SEG *seg_in(char *str);
|
|
|
|
char *seg_out(SEG * seg);
|
|
|
|
float32 seg_lower(SEG * seg);
|
|
|
|
float32 seg_upper(SEG * seg);
|
|
|
|
float32 seg_center(SEG * seg);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/*
|
2000-12-12 04:40:33 +08:00
|
|
|
** GiST support methods
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
bool gseg_consistent(GISTENTRY *entry, SEG * query, StrategyNumber strategy);
|
|
|
|
GISTENTRY *gseg_compress(GISTENTRY *entry);
|
|
|
|
GISTENTRY *gseg_decompress(GISTENTRY *entry);
|
|
|
|
float *gseg_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result);
|
2004-03-30 23:45:33 +08:00
|
|
|
GIST_SPLITVEC *gseg_picksplit(GistEntryVector *entryvec, GIST_SPLITVEC *v);
|
2001-03-22 12:01:46 +08:00
|
|
|
bool gseg_leaf_consistent(SEG * key, SEG * query, StrategyNumber strategy);
|
|
|
|
bool gseg_internal_consistent(SEG * key, SEG * query, StrategyNumber strategy);
|
2004-03-30 23:45:33 +08:00
|
|
|
SEG *gseg_union(GistEntryVector *entryvec, int *sizep);
|
2001-03-22 12:01:46 +08:00
|
|
|
SEG *gseg_binary_union(SEG * r1, SEG * r2, int *sizep);
|
|
|
|
bool *gseg_same(SEG * b1, SEG * b2, bool *result);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-11-08 01:36:47 +08:00
|
|
|
** R-tree support functions
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
bool seg_same(SEG * a, SEG * b);
|
|
|
|
bool seg_contains_int(SEG * a, int *b);
|
|
|
|
bool seg_contains_float4(SEG * a, float4 *b);
|
|
|
|
bool seg_contains_float8(SEG * a, float8 *b);
|
|
|
|
bool seg_contains(SEG * a, SEG * b);
|
|
|
|
bool seg_contained(SEG * a, SEG * b);
|
|
|
|
bool seg_overlap(SEG * a, SEG * b);
|
|
|
|
bool seg_left(SEG * a, SEG * b);
|
|
|
|
bool seg_over_left(SEG * a, SEG * b);
|
|
|
|
bool seg_right(SEG * a, SEG * b);
|
|
|
|
bool seg_over_right(SEG * a, SEG * b);
|
|
|
|
SEG *seg_union(SEG * a, SEG * b);
|
|
|
|
SEG *seg_inter(SEG * a, SEG * b);
|
|
|
|
void rt_seg_size(SEG * a, float *sz);
|
|
|
|
float *seg_size(SEG * a);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Various operators
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
int32 seg_cmp(SEG * a, SEG * b);
|
|
|
|
bool seg_lt(SEG * a, SEG * b);
|
|
|
|
bool seg_le(SEG * a, SEG * b);
|
|
|
|
bool seg_gt(SEG * a, SEG * b);
|
|
|
|
bool seg_ge(SEG * a, SEG * b);
|
|
|
|
bool seg_different(SEG * a, SEG * b);
|
|
|
|
|
|
|
|
/*
|
2000-12-12 04:40:33 +08:00
|
|
|
** Auxiliary funxtions
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
static int restore(char *s, float val, int n);
|
|
|
|
int significant_digits(char *s);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Input/Output functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
SEG *
|
|
|
|
seg_in(char *str)
|
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
SEG *result = palloc(sizeof(SEG));
|
|
|
|
|
2003-09-14 10:18:49 +08:00
|
|
|
seg_scanner_init(str);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
if (seg_yyparse(result) != 0)
|
2003-09-14 10:18:49 +08:00
|
|
|
seg_yyerror("bogus input");
|
|
|
|
|
|
|
|
seg_scanner_finish();
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_out(SEG * seg)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
char *result;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (seg == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
p = result = (char *) palloc(40);
|
|
|
|
|
|
|
|
if (seg->l_ext == '>' || seg->l_ext == '<' || seg->l_ext == '~')
|
|
|
|
p += sprintf(p, "%c", seg->l_ext);
|
|
|
|
|
|
|
|
if (seg->lower == seg->upper && seg->l_ext == seg->u_ext)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* indicates that this interval was built by seg_in off a single point
|
2001-03-22 12:01:46 +08:00
|
|
|
*/
|
|
|
|
p += restore(p, seg->lower, seg->l_sigd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (seg->l_ext != '-')
|
|
|
|
{
|
|
|
|
/* print the lower boudary if exists */
|
|
|
|
p += restore(p, seg->lower, seg->l_sigd);
|
|
|
|
p += sprintf(p, " ");
|
|
|
|
}
|
|
|
|
p += sprintf(p, "..");
|
|
|
|
if (seg->u_ext != '-')
|
|
|
|
{
|
|
|
|
/* print the upper boudary if exists */
|
|
|
|
p += sprintf(p, " ");
|
|
|
|
if (seg->u_ext == '>' || seg->u_ext == '<' || seg->l_ext == '~')
|
|
|
|
p += sprintf(p, "%c", seg->u_ext);
|
|
|
|
p += restore(p, seg->upper, seg->u_sigd);
|
|
|
|
}
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float32
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_center(SEG * seg)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
float32 result = (float32) palloc(sizeof(float32data));
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
if (!seg)
|
|
|
|
return (float32) NULL;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
*result = ((float) seg->lower + (float) seg->upper) / 2.0;
|
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float32
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_lower(SEG * seg)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
float32 result = (float32) palloc(sizeof(float32data));
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
if (!seg)
|
|
|
|
return (float32) NULL;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
*result = (float) seg->lower;
|
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float32
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_upper(SEG * seg)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
float32 result = (float32) palloc(sizeof(float32data));
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
if (!seg)
|
|
|
|
return (float32) NULL;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
*result = (float) seg->upper;
|
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 12:01:46 +08:00
|
|
|
* GiST functions
|
2000-12-12 04:40:33 +08:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Consistent method for segments
|
|
|
|
** 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-03-22 12:01:46 +08:00
|
|
|
bool
|
2000-12-12 04:40:33 +08:00
|
|
|
gseg_consistent(GISTENTRY *entry,
|
2001-03-22 12:01:46 +08:00
|
|
|
SEG * query,
|
|
|
|
StrategyNumber strategy)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
/*
|
2005-06-27 08:48:07 +08:00
|
|
|
* if entry is not leaf, use gseg_internal_consistent, else use
|
2001-03-22 12:01:46 +08:00
|
|
|
* gseg_leaf_consistent
|
|
|
|
*/
|
|
|
|
if (GIST_LEAF(entry))
|
2001-06-01 02:16:55 +08:00
|
|
|
return (gseg_leaf_consistent((SEG *) DatumGetPointer(entry->key), query, strategy));
|
2001-03-22 12:01:46 +08:00
|
|
|
else
|
2001-06-01 02:16:55 +08:00
|
|
|
return (gseg_internal_consistent((SEG *) DatumGetPointer(entry->key), query, strategy));
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Union method for segments
|
|
|
|
** returns the minimal bounding seg that encloses all the entries in entryvec
|
|
|
|
*/
|
|
|
|
SEG *
|
2004-03-30 23:45:33 +08:00
|
|
|
gseg_union(GistEntryVector *entryvec, int *sizep)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
int numranges,
|
|
|
|
i;
|
|
|
|
SEG *out = (SEG *) NULL;
|
|
|
|
SEG *tmp;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 12:01:46 +08:00
|
|
|
fprintf(stderr, "union\n");
|
2000-12-12 04:40:33 +08:00
|
|
|
#endif
|
|
|
|
|
2004-03-30 23:45:33 +08:00
|
|
|
numranges = entryvec->n;
|
|
|
|
tmp = (SEG *) DatumGetPointer(entryvec->vector[0].key);
|
2001-03-22 12:01:46 +08:00
|
|
|
*sizep = sizeof(SEG);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
for (i = 1; i < numranges; i++)
|
|
|
|
{
|
|
|
|
out = gseg_binary_union(tmp, (SEG *)
|
2004-08-29 13:07:03 +08:00
|
|
|
DatumGetPointer(entryvec->vector[i].key),
|
2001-03-22 12:01:46 +08:00
|
|
|
sizep);
|
|
|
|
tmp = out;
|
|
|
|
}
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (out);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GiST Compress and Decompress methods for segments
|
|
|
|
** do not do anything.
|
|
|
|
*/
|
2001-10-25 13:50:21 +08:00
|
|
|
GISTENTRY *
|
2000-12-12 04:40:33 +08:00
|
|
|
gseg_compress(GISTENTRY *entry)
|
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return (entry);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
GISTENTRY *
|
2000-12-12 04:40:33 +08:00
|
|
|
gseg_decompress(GISTENTRY *entry)
|
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return (entry);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Penalty method for segments
|
|
|
|
** As in the R-tree paper, we use change in area as our penalty metric
|
|
|
|
*/
|
|
|
|
float *
|
|
|
|
gseg_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result)
|
|
|
|
{
|
2001-06-01 02:16:55 +08:00
|
|
|
SEG *ud;
|
2001-03-22 12:01:46 +08:00
|
|
|
float tmp1,
|
|
|
|
tmp2;
|
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
ud = seg_union((SEG *) DatumGetPointer(origentry->key),
|
|
|
|
(SEG *) DatumGetPointer(newentry->key));
|
|
|
|
rt_seg_size(ud, &tmp1);
|
|
|
|
rt_seg_size((SEG *) DatumGetPointer(origentry->key), &tmp2);
|
2001-03-22 12:01:46 +08:00
|
|
|
*result = tmp1 - tmp2;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 12:01:46 +08:00
|
|
|
fprintf(stderr, "penalty\n");
|
|
|
|
fprintf(stderr, "\t%g\n", *result);
|
2000-12-12 04:40:33 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST PickSplit method for segments
|
2001-03-22 12:01:46 +08:00
|
|
|
** We use Guttman's poly time split algorithm
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
GIST_SPLITVEC *
|
2004-03-30 23:45:33 +08:00
|
|
|
gseg_picksplit(GistEntryVector *entryvec,
|
2001-03-22 12:01:46 +08:00
|
|
|
GIST_SPLITVEC *v)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
OffsetNumber i,
|
|
|
|
j;
|
|
|
|
SEG *datum_alpha,
|
|
|
|
*datum_beta;
|
|
|
|
SEG *datum_l,
|
|
|
|
*datum_r;
|
|
|
|
SEG *union_d,
|
|
|
|
*union_dl,
|
|
|
|
*union_dr;
|
|
|
|
SEG *inter_d;
|
|
|
|
bool firsttime;
|
|
|
|
float size_alpha,
|
|
|
|
size_beta,
|
|
|
|
size_union,
|
|
|
|
size_inter;
|
|
|
|
float size_waste,
|
|
|
|
waste;
|
|
|
|
float size_l,
|
|
|
|
size_r;
|
|
|
|
int nbytes;
|
2006-06-28 20:00:14 +08:00
|
|
|
OffsetNumber seed_1 = 1,
|
|
|
|
seed_2 = 2;
|
2001-03-22 12:01:46 +08:00
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
OffsetNumber maxoff;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 12:01:46 +08:00
|
|
|
fprintf(stderr, "picksplit\n");
|
2000-12-12 04:40:33 +08:00
|
|
|
#endif
|
|
|
|
|
2004-03-30 23:45:33 +08:00
|
|
|
maxoff = entryvec->n - 2;
|
2001-03-22 12:01:46 +08:00
|
|
|
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))
|
|
|
|
{
|
2004-03-30 23:45:33 +08:00
|
|
|
datum_alpha = (SEG *) DatumGetPointer(entryvec->vector[i].key);
|
2001-03-22 12:01:46 +08:00
|
|
|
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
2004-03-30 23:45:33 +08:00
|
|
|
datum_beta = (SEG *) DatumGetPointer(entryvec->vector[j].key);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
/* compute the wasted space by unioning these guys */
|
|
|
|
/* size_waste = size_union - size_inter; */
|
2001-06-01 02:16:55 +08:00
|
|
|
union_d = seg_union(datum_alpha, datum_beta);
|
2001-03-22 12:01:46 +08:00
|
|
|
rt_seg_size(union_d, &size_union);
|
2001-06-01 02:16:55 +08:00
|
|
|
inter_d = seg_inter(datum_alpha, datum_beta);
|
2001-03-22 12:01:46 +08:00
|
|
|
rt_seg_size(inter_d, &size_inter);
|
|
|
|
size_waste = size_union - size_inter;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* are these a more promising split that what we've already seen?
|
2001-03-22 12:01:46 +08:00
|
|
|
*/
|
|
|
|
if (size_waste > waste || firsttime)
|
|
|
|
{
|
|
|
|
waste = size_waste;
|
|
|
|
seed_1 = i;
|
|
|
|
seed_2 = j;
|
|
|
|
firsttime = false;
|
|
|
|
}
|
|
|
|
}
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
left = v->spl_left;
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
right = v->spl_right;
|
|
|
|
v->spl_nright = 0;
|
|
|
|
|
2004-03-30 23:45:33 +08:00
|
|
|
datum_alpha = (SEG *) DatumGetPointer(entryvec->vector[seed_1].key);
|
2001-06-01 02:16:55 +08:00
|
|
|
datum_l = seg_union(datum_alpha, datum_alpha);
|
|
|
|
rt_seg_size(datum_l, &size_l);
|
2004-03-30 23:45:33 +08:00
|
|
|
datum_beta = (SEG *) DatumGetPointer(entryvec->vector[seed_2].key);
|
2001-06-01 02:16:55 +08:00
|
|
|
datum_r = seg_union(datum_beta, datum_beta);
|
|
|
|
rt_seg_size(datum_r, &size_r);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08: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.
|
2001-03-22 12:01:46 +08:00
|
|
|
*
|
2005-10-15 10:49:52 +08:00
|
|
|
* 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.
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 10:49:52 +08: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.
|
2001-03-22 12:01:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
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? */
|
2004-03-30 23:45:33 +08:00
|
|
|
datum_alpha = (SEG *) DatumGetPointer(entryvec->vector[i].key);
|
2001-06-01 02:16:55 +08:00
|
|
|
union_dl = seg_union(datum_l, datum_alpha);
|
|
|
|
union_dr = seg_union(datum_r, datum_alpha);
|
|
|
|
rt_seg_size(union_dl, &size_alpha);
|
|
|
|
rt_seg_size(union_dr, &size_beta);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
/* pick which page to add it to */
|
|
|
|
if (size_alpha - size_l < size_beta - size_r)
|
|
|
|
{
|
|
|
|
datum_l = union_dl;
|
|
|
|
size_l = size_alpha;
|
|
|
|
*left++ = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
datum_r = union_dr;
|
|
|
|
size_r = size_alpha;
|
|
|
|
*right++ = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
2001-03-22 12:01:46 +08:00
|
|
|
*left = *right = FirstOffsetNumber; /* sentinel value, see dosplit() */
|
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
v->spl_ldatum = PointerGetDatum(datum_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(datum_r);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return v;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Equality methods
|
|
|
|
*/
|
|
|
|
bool *
|
2001-03-22 12:01:46 +08:00
|
|
|
gseg_same(SEG * b1, SEG * b2, bool *result)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
if (seg_same(b1, b2))
|
|
|
|
*result = TRUE;
|
|
|
|
else
|
|
|
|
*result = FALSE;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 12:01:46 +08:00
|
|
|
fprintf(stderr, "same: %s\n", (*result ? "TRUE" : "FALSE"));
|
2000-12-12 04:40:33 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/*
|
2000-12-12 04:40:33 +08:00
|
|
|
** SUPPORT ROUTINES
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
bool
|
|
|
|
gseg_leaf_consistent(SEG * key,
|
|
|
|
SEG * query,
|
|
|
|
StrategyNumber strategy)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
bool retval;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
#ifdef GIST_QUERY_DEBUG
|
2001-03-22 12:01:46 +08:00
|
|
|
fprintf(stderr, "leaf_consistent, %d\n", strategy);
|
2000-12-12 04:40:33 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTLeftStrategyNumber:
|
|
|
|
retval = (bool) seg_left(key, query);
|
|
|
|
break;
|
|
|
|
case RTOverLeftStrategyNumber:
|
|
|
|
retval = (bool) seg_over_left(key, query);
|
|
|
|
break;
|
|
|
|
case RTOverlapStrategyNumber:
|
|
|
|
retval = (bool) seg_overlap(key, query);
|
|
|
|
break;
|
|
|
|
case RTOverRightStrategyNumber:
|
|
|
|
retval = (bool) seg_over_right(key, query);
|
|
|
|
break;
|
|
|
|
case RTRightStrategyNumber:
|
|
|
|
retval = (bool) seg_right(key, query);
|
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
retval = (bool) seg_same(key, query);
|
|
|
|
break;
|
|
|
|
case RTContainsStrategyNumber:
|
2006-09-11 01:36:52 +08:00
|
|
|
case RTOldContainsStrategyNumber:
|
2001-03-22 12:01:46 +08:00
|
|
|
retval = (bool) seg_contains(key, query);
|
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2006-09-11 01:36:52 +08:00
|
|
|
case RTOldContainedByStrategyNumber:
|
2001-03-22 12:01:46 +08:00
|
|
|
retval = (bool) seg_contained(key, query);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
|
|
|
return (retval);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
bool
|
|
|
|
gseg_internal_consistent(SEG * key,
|
|
|
|
SEG * query,
|
|
|
|
StrategyNumber strategy)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
bool retval;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
|
|
|
#ifdef GIST_QUERY_DEBUG
|
2001-03-22 12:01:46 +08:00
|
|
|
fprintf(stderr, "internal_consistent, %d\n", strategy);
|
2000-12-12 04:40:33 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTLeftStrategyNumber:
|
2005-06-27 08:48:07 +08:00
|
|
|
retval = (bool) !seg_over_right(key, query);
|
|
|
|
break;
|
2001-03-22 12:01:46 +08:00
|
|
|
case RTOverLeftStrategyNumber:
|
2005-06-27 08:48:07 +08:00
|
|
|
retval = (bool) !seg_right(key, query);
|
2001-03-22 12:01:46 +08:00
|
|
|
break;
|
|
|
|
case RTOverlapStrategyNumber:
|
|
|
|
retval = (bool) seg_overlap(key, query);
|
|
|
|
break;
|
|
|
|
case RTOverRightStrategyNumber:
|
2005-06-27 08:48:07 +08:00
|
|
|
retval = (bool) !seg_left(key, query);
|
|
|
|
break;
|
2001-03-22 12:01:46 +08:00
|
|
|
case RTRightStrategyNumber:
|
2005-06-27 08:48:07 +08:00
|
|
|
retval = (bool) !seg_over_left(key, query);
|
2001-03-22 12:01:46 +08:00
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
case RTContainsStrategyNumber:
|
2006-09-11 01:36:52 +08:00
|
|
|
case RTOldContainsStrategyNumber:
|
2001-03-22 12:01:46 +08:00
|
|
|
retval = (bool) seg_contains(key, query);
|
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2006-09-11 01:36:52 +08:00
|
|
|
case RTOldContainedByStrategyNumber:
|
2001-03-22 12:01:46 +08:00
|
|
|
retval = (bool) seg_overlap(key, query);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
|
|
|
return (retval);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SEG *
|
2001-03-22 12:01:46 +08:00
|
|
|
gseg_binary_union(SEG * r1, SEG * r2, int *sizep)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
SEG *retval;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
retval = seg_union(r1, r2);
|
|
|
|
*sizep = sizeof(SEG);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (retval);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_contains(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return ((a->lower <= b->lower) && (a->upper >= b->upper));
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_contained(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return (seg_contains(b, a));
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Operator class for R-tree indexing
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_same(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return seg_cmp(a, b) == 0;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/* seg_overlap -- does a overlap b?
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_overlap(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return (
|
|
|
|
((a->upper >= b->upper) && (a->lower <= b->upper))
|
|
|
|
||
|
|
|
|
((b->upper >= a->upper) && (b->lower <= a->upper))
|
2001-10-25 13:50:21 +08:00
|
|
|
);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2005-06-27 08:48:07 +08:00
|
|
|
/* seg_overleft -- is the right edge of (a) located at or left of the right edge of (b)?
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_over_left(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2005-06-27 08:48:07 +08:00
|
|
|
return (a->upper <= b->upper);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/* seg_left -- is (a) entirely on the left of (b)?
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_left(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return (a->upper < b->lower);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/* seg_right -- is (a) entirely on the right of (b)?
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_right(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return (a->lower > b->upper);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2005-06-27 08:48:07 +08:00
|
|
|
/* seg_overright -- is the left edge of (a) located at or right of the left edge of (b)?
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_over_right(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2005-06-27 08:48:07 +08:00
|
|
|
return (a->lower >= b->lower);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SEG *
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_union(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
SEG *n;
|
|
|
|
|
|
|
|
n = (SEG *) palloc(sizeof(*n));
|
|
|
|
|
|
|
|
/* take max of upper endpoints */
|
|
|
|
if (a->upper > b->upper)
|
|
|
|
{
|
|
|
|
n->upper = a->upper;
|
|
|
|
n->u_sigd = a->u_sigd;
|
|
|
|
n->u_ext = a->u_ext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n->upper = b->upper;
|
|
|
|
n->u_sigd = b->u_sigd;
|
|
|
|
n->u_ext = b->u_ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* take min of lower endpoints */
|
|
|
|
if (a->lower < b->lower)
|
|
|
|
{
|
|
|
|
n->lower = a->lower;
|
|
|
|
n->l_sigd = a->l_sigd;
|
|
|
|
n->l_ext = a->l_ext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n->lower = b->lower;
|
|
|
|
n->l_sigd = b->l_sigd;
|
|
|
|
n->l_ext = b->l_ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (n);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SEG *
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_inter(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
SEG *n;
|
|
|
|
|
|
|
|
n = (SEG *) palloc(sizeof(*n));
|
|
|
|
|
|
|
|
/* take min of upper endpoints */
|
|
|
|
if (a->upper < b->upper)
|
|
|
|
{
|
|
|
|
n->upper = a->upper;
|
|
|
|
n->u_sigd = a->u_sigd;
|
|
|
|
n->u_ext = a->u_ext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n->upper = b->upper;
|
|
|
|
n->u_sigd = b->u_sigd;
|
|
|
|
n->u_ext = b->u_ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* take max of lower endpoints */
|
|
|
|
if (a->lower > b->lower)
|
|
|
|
{
|
|
|
|
n->lower = a->lower;
|
|
|
|
n->l_sigd = a->l_sigd;
|
|
|
|
n->l_ext = a->l_ext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n->lower = b->lower;
|
|
|
|
n->l_sigd = b->l_sigd;
|
|
|
|
n->l_ext = b->l_ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (n);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-03-22 12:01:46 +08:00
|
|
|
rt_seg_size(SEG * a, float *size)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a == (SEG *) NULL || a->upper <= a->lower)
|
|
|
|
*size = 0.0;
|
|
|
|
else
|
2004-10-22 03:28:36 +08:00
|
|
|
*size = (float) Abs(a->upper - a->lower);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
return;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float *
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_size(SEG * a)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
float *result;
|
|
|
|
|
|
|
|
result = (float *) palloc(sizeof(float));
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2004-10-22 03:28:36 +08:00
|
|
|
*result = (float) Abs(a->upper - a->lower);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (result);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 12:01:46 +08:00
|
|
|
* Miscellaneous operators
|
2000-12-12 04:40:33 +08:00
|
|
|
*****************************************************************************/
|
|
|
|
int32
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_cmp(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* First compare on lower boundary position
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->lower < b->lower)
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->lower > b->lower)
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
|
|
|
* a->lower == b->lower, so consider type of boundary.
|
|
|
|
*
|
2001-03-22 12:01:46 +08:00
|
|
|
* A '-' lower bound is < any other kind (this could only be relevant if
|
2005-10-15 10:49:52 +08:00
|
|
|
* -HUGE_VAL is used as a regular data value). A '<' lower bound is < any
|
|
|
|
* other kind except '-'. A '>' lower bound is > any other kind.
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->l_ext != b->l_ext)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->l_ext == '-')
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->l_ext == '-')
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->l_ext == '<')
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->l_ext == '<')
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->l_ext == '>')
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->l_ext == '>')
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
|
|
|
* For other boundary types, consider # of significant digits first.
|
|
|
|
*/
|
2005-10-15 10:49:52 +08:00
|
|
|
if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include (b) */
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->l_sigd > b->l_sigd) /* (a) is less blurred and is likely to be
|
|
|
|
* included in (b) */
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
|
|
|
* For same # of digits, an approximate boundary is more blurred than
|
|
|
|
* exact.
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->l_ext != b->l_ext)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->l_ext == '~') /* (a) is approximate, while (b) is exact */
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->l_ext == '~')
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
|
|
|
/* can't get here unless data is corrupt */
|
2003-07-25 01:52:50 +08:00
|
|
|
elog(ERROR, "bogus lower boundary types %d %d",
|
2000-12-12 04:40:33 +08:00
|
|
|
(int) a->l_ext, (int) b->l_ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* at this point, the lower boundaries are identical */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First compare on upper boundary position
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->upper < b->upper)
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->upper > b->upper)
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
|
|
|
* a->upper == b->upper, so consider type of boundary.
|
|
|
|
*
|
2001-03-22 12:01:46 +08:00
|
|
|
* A '-' upper bound is > any other kind (this could only be relevant if
|
2005-10-15 10:49:52 +08:00
|
|
|
* HUGE_VAL is used as a regular data value). A '<' upper bound is < any
|
|
|
|
* other kind. A '>' upper bound is > any other kind except '-'.
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->u_ext != b->u_ext)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->u_ext == '-')
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->u_ext == '-')
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->u_ext == '<')
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->u_ext == '<')
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->u_ext == '>')
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->u_ext == '>')
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* For other boundary types, consider # of significant digits first. Note
|
|
|
|
* result here is converse of the lower-boundary case.
|
2000-12-12 04:40:33 +08:00
|
|
|
*/
|
2005-10-15 10:49:52 +08:00
|
|
|
if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include (b) */
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->u_sigd > b->u_sigd) /* (a) is less blurred and is likely to be
|
|
|
|
* included in (b) */
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-12-12 04:40:33 +08:00
|
|
|
/*
|
|
|
|
* For same # of digits, an approximate boundary is more blurred than
|
|
|
|
* exact. Again, result is converse of lower-boundary case.
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->u_ext != b->u_ext)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
if (a->u_ext == '~') /* (a) is approximate, while (b) is exact */
|
2000-12-12 04:40:33 +08:00
|
|
|
return 1;
|
2001-03-22 12:01:46 +08:00
|
|
|
if (b->u_ext == '~')
|
2000-12-12 04:40:33 +08:00
|
|
|
return -1;
|
|
|
|
/* can't get here unless data is corrupt */
|
2003-07-25 01:52:50 +08:00
|
|
|
elog(ERROR, "bogus upper boundary types %d %d",
|
2000-12-12 04:40:33 +08:00
|
|
|
(int) a->u_ext, (int) b->u_ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_lt(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return seg_cmp(a, b) < 0;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_le(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return seg_cmp(a, b) <= 0;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_gt(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return seg_cmp(a, b) > 0;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_ge(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return seg_cmp(a, b) >= 0;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_different(SEG * a, SEG * b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return seg_cmp(a, b) != 0;
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 12:01:46 +08:00
|
|
|
* Auxiliary functions
|
2000-12-12 04:40:33 +08:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* The purpose of this routine is to print the floating point
|
|
|
|
* value with exact number of significant digits. Its behaviour
|
|
|
|
* is similar to %.ng except it prints 8.00 where %.ng would
|
|
|
|
* print 8
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
static int
|
|
|
|
restore(char *result, float val, int n)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
static char efmt[8] = {'%', '-', '1', '5', '.', '#', 'e', 0};
|
|
|
|
char buf[25] = {
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '\0'
|
|
|
|
};
|
|
|
|
char *p;
|
|
|
|
char *mant;
|
|
|
|
int exp;
|
|
|
|
int i,
|
|
|
|
dp,
|
|
|
|
sign;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* put a cap on the number of siugnificant digits to avoid nonsense in the
|
|
|
|
* output
|
2001-03-22 12:01:46 +08:00
|
|
|
*/
|
2004-09-14 12:21:38 +08:00
|
|
|
n = Min(n, FLT_DIG);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
/* remember the sign */
|
|
|
|
sign = (val < 0 ? 1 : 0);
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
efmt[5] = '0' + (n - 1) % 10; /* makes %-15.(n-1)e -- this format
|
|
|
|
* guarantees that the exponent is
|
|
|
|
* always present */
|
2001-03-22 12:01:46 +08:00
|
|
|
|
|
|
|
sprintf(result, efmt, val);
|
|
|
|
|
|
|
|
/* trim the spaces left by the %e */
|
|
|
|
for (p = result; *p != ' '; p++);
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* get the exponent */
|
|
|
|
mant = (char *) strtok(strdup(result), "e");
|
|
|
|
exp = atoi(strtok(NULL, "e"));
|
|
|
|
|
|
|
|
if (exp == 0)
|
|
|
|
{
|
|
|
|
/* use the supplied mantyssa with sign */
|
2004-09-14 12:21:38 +08:00
|
|
|
strcpy((char *) strchr(result, 'e'), "");
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
2001-03-22 12:01:46 +08:00
|
|
|
else
|
|
|
|
{
|
2004-10-22 03:28:36 +08:00
|
|
|
if (Abs(exp) <= 4)
|
2001-03-22 12:01:46 +08:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* remove the decimal point from the mantyssa and write the digits
|
|
|
|
* to the buf array
|
2001-03-22 12:01:46 +08:00
|
|
|
*/
|
|
|
|
for (p = result + sign, i = 10, dp = 0; *p != 'e'; p++, i++)
|
|
|
|
{
|
|
|
|
buf[i] = *p;
|
|
|
|
if (*p == '.')
|
|
|
|
{
|
|
|
|
dp = i--; /* skip the decimal point */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dp == 0)
|
|
|
|
dp = i--; /* no decimal point was found in the above
|
|
|
|
* for() loop */
|
|
|
|
|
|
|
|
if (exp > 0)
|
|
|
|
{
|
|
|
|
if (dp - 10 + exp >= n)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* the decimal point is behind the last significant digit;
|
|
|
|
* the digits in between must be converted to the exponent
|
|
|
|
* and the decimal point placed after the first digit
|
2001-03-22 12:01:46 +08:00
|
|
|
*/
|
|
|
|
exp = dp - 10 + exp - n;
|
|
|
|
buf[10 + n] = '\0';
|
|
|
|
|
|
|
|
/* insert the decimal point */
|
|
|
|
if (n > 1)
|
|
|
|
{
|
|
|
|
dp = 11;
|
|
|
|
for (i = 23; i > dp; i--)
|
|
|
|
buf[i] = buf[i - 1];
|
|
|
|
buf[dp] = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* adjust the exponent by the number of digits after the
|
|
|
|
* decimal point
|
2001-03-22 12:01:46 +08:00
|
|
|
*/
|
|
|
|
if (n > 1)
|
|
|
|
sprintf(&buf[11 + n], "e%d", exp + n - 1);
|
|
|
|
else
|
|
|
|
sprintf(&buf[11], "e%d", exp + n - 1);
|
|
|
|
|
|
|
|
if (sign)
|
|
|
|
{
|
|
|
|
buf[9] = '-';
|
|
|
|
strcpy(result, &buf[9]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(result, &buf[10]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* insert the decimal point */
|
|
|
|
dp += exp;
|
|
|
|
for (i = 23; i > dp; i--)
|
|
|
|
buf[i] = buf[i - 1];
|
|
|
|
buf[11 + n] = '\0';
|
|
|
|
buf[dp] = '.';
|
|
|
|
if (sign)
|
|
|
|
{
|
|
|
|
buf[9] = '-';
|
|
|
|
strcpy(result, &buf[9]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(result, &buf[10]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* exp <= 0 */
|
|
|
|
dp += exp - 1;
|
|
|
|
buf[10 + n] = '\0';
|
|
|
|
buf[dp] = '.';
|
|
|
|
if (sign)
|
|
|
|
{
|
|
|
|
buf[dp - 2] = '-';
|
|
|
|
strcpy(result, &buf[dp - 2]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(result, &buf[dp - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-22 03:28:36 +08:00
|
|
|
/* do nothing for Abs(exp) > 4; %e must be OK */
|
2001-03-22 12:01:46 +08:00
|
|
|
/* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
|
|
|
|
|
|
|
|
/* ... this is not done yet. */
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
2001-03-22 12:01:46 +08:00
|
|
|
return (strlen(result));
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Miscellany
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_contains_int(SEG * a, int *b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return ((a->lower <= *b) && (a->upper >= *b));
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_contains_float4(SEG * a, float4 *b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return ((a->lower <= *b) && (a->upper >= *b));
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2001-03-22 12:01:46 +08:00
|
|
|
seg_contains_float8(SEG * a, float8 *b)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
return ((a->lower <= *b) && (a->upper >= *b));
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/* find out the number of significant digits in a string representing
|
2000-12-12 04:40:33 +08:00
|
|
|
* a floating point number
|
|
|
|
*/
|
2001-03-22 12:01:46 +08:00
|
|
|
int
|
|
|
|
significant_digits(char *s)
|
2000-12-12 04:40:33 +08:00
|
|
|
{
|
2001-03-22 12:01:46 +08:00
|
|
|
char *p = s;
|
|
|
|
int n,
|
|
|
|
c,
|
|
|
|
zeroes;
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
zeroes = 1;
|
|
|
|
/* skip leading zeroes and sign */
|
|
|
|
for (c = *p; (c == '0' || c == '+' || c == '-') && c != 0; c = *(++p));
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/* skip decimal point and following zeroes */
|
|
|
|
for (c = *p; (c == '0' || c == '.') && c != 0; c = *(++p))
|
|
|
|
{
|
|
|
|
if (c != '.')
|
|
|
|
zeroes++;
|
|
|
|
}
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
/* count significant digits (n) */
|
|
|
|
for (c = *p, n = 0; c != 0; c = *(++p))
|
|
|
|
{
|
|
|
|
if (!((c >= '0' && c <= '9') || (c == '.')))
|
|
|
|
break;
|
|
|
|
if (c != '.')
|
|
|
|
n++;
|
|
|
|
}
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
if (!n)
|
|
|
|
return (zeroes);
|
2000-12-12 04:40:33 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
return (n);
|
2000-12-12 04:40:33 +08:00
|
|
|
}
|