#include "postgres.h"#include <float.h>#include "access/gist.h"#include "access/skey.h"#include "segdata.h"
Go to the source code of this file.
| bool gseg_consistent | ( | GISTENTRY * | entry, | |
| SEG * | query, | |||
| StrategyNumber | strategy, | |||
| Oid | subtype, | |||
| bool * | recheck | |||
| ) |
Definition at line 213 of file seg.c.
References DatumGetPointer, GIST_LEAF, gseg_internal_consistent(), gseg_leaf_consistent(), and GISTENTRY::key.
{
/* All cases served by this function are exact */
*recheck = false;
/*
* if entry is not leaf, use gseg_internal_consistent, else use
* gseg_leaf_consistent
*/
if (GIST_LEAF(entry))
return (gseg_leaf_consistent((SEG *) DatumGetPointer(entry->key), query, strategy));
else
return (gseg_internal_consistent((SEG *) DatumGetPointer(entry->key), query, strategy));
}
| bool gseg_internal_consistent | ( | SEG * | key, | |
| SEG * | query, | |||
| StrategyNumber | strategy | |||
| ) |
Definition at line 476 of file seg.c.
References RTContainedByStrategyNumber, RTContainsStrategyNumber, RTLeftStrategyNumber, RTOldContainedByStrategyNumber, RTOldContainsStrategyNumber, RTOverlapStrategyNumber, RTOverLeftStrategyNumber, RTOverRightStrategyNumber, RTRightStrategyNumber, RTSameStrategyNumber, seg_contains(), seg_left(), seg_over_left(), seg_over_right(), seg_overlap(), and seg_right().
Referenced by gseg_consistent().
{
bool retval;
#ifdef GIST_QUERY_DEBUG
fprintf(stderr, "internal_consistent, %d\n", strategy);
#endif
switch (strategy)
{
case RTLeftStrategyNumber:
retval = (bool) !seg_over_right(key, query);
break;
case RTOverLeftStrategyNumber:
retval = (bool) !seg_right(key, query);
break;
case RTOverlapStrategyNumber:
retval = (bool) seg_overlap(key, query);
break;
case RTOverRightStrategyNumber:
retval = (bool) !seg_left(key, query);
break;
case RTRightStrategyNumber:
retval = (bool) !seg_over_left(key, query);
break;
case RTSameStrategyNumber:
case RTContainsStrategyNumber:
case RTOldContainsStrategyNumber:
retval = (bool) seg_contains(key, query);
break;
case RTContainedByStrategyNumber:
case RTOldContainedByStrategyNumber:
retval = (bool) seg_overlap(key, query);
break;
default:
retval = FALSE;
}
return (retval);
}
| bool gseg_leaf_consistent | ( | SEG * | key, | |
| SEG * | query, | |||
| StrategyNumber | strategy | |||
| ) |
Definition at line 431 of file seg.c.
References RTContainedByStrategyNumber, RTContainsStrategyNumber, RTLeftStrategyNumber, RTOldContainedByStrategyNumber, RTOldContainsStrategyNumber, RTOverlapStrategyNumber, RTOverLeftStrategyNumber, RTOverRightStrategyNumber, RTRightStrategyNumber, RTSameStrategyNumber, seg_contained(), seg_contains(), seg_left(), seg_over_left(), seg_over_right(), seg_overlap(), seg_right(), and seg_same().
Referenced by gseg_consistent().
{
bool retval;
#ifdef GIST_QUERY_DEBUG
fprintf(stderr, "leaf_consistent, %d\n", strategy);
#endif
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:
case RTOldContainsStrategyNumber:
retval = (bool) seg_contains(key, query);
break;
case RTContainedByStrategyNumber:
case RTOldContainedByStrategyNumber:
retval = (bool) seg_contained(key, query);
break;
default:
retval = FALSE;
}
return (retval);
}
Definition at line 284 of file seg.c.
References DatumGetPointer, GISTENTRY::key, rt_seg_size(), and seg_union().
{
SEG *ud;
float tmp1,
tmp2;
ud = seg_union((SEG *) DatumGetPointer(origentry->key),
(SEG *) DatumGetPointer(newentry->key));
rt_seg_size(ud, &tmp1);
rt_seg_size((SEG *) DatumGetPointer(origentry->key), &tmp2);
*result = tmp1 - tmp2;
#ifdef GIST_DEBUG
fprintf(stderr, "penalty\n");
fprintf(stderr, "\t%g\n", *result);
#endif
return (result);
}
| GIST_SPLITVEC * gseg_picksplit | ( | GistEntryVector * | entryvec, | |
| GIST_SPLITVEC * | v | |||
| ) |
Definition at line 329 of file seg.c.
References gseg_picksplit_item::center, gseg_picksplit_item::data, DatumGetPointer, gseg_picksplit_item_cmp(), i, gseg_picksplit_item::index, GISTENTRY::key, SEG::lower, GistEntryVector::n, palloc(), PointerGetDatum, qsort, seg_union(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, SEG::upper, and GistEntryVector::vector.
{
int i;
SEG *datum_l,
*datum_r,
*seg;
gseg_picksplit_item *sort_items;
OffsetNumber *left,
*right;
OffsetNumber maxoff;
OffsetNumber firstright;
#ifdef GIST_DEBUG
fprintf(stderr, "picksplit\n");
#endif
/* Valid items in entryvec->vector[] are indexed 1..maxoff */
maxoff = entryvec->n - 1;
/*
* Prepare the auxiliary array and sort it.
*/
sort_items = (gseg_picksplit_item *)
palloc(maxoff * sizeof(gseg_picksplit_item));
for (i = 1; i <= maxoff; i++)
{
seg = (SEG *) DatumGetPointer(entryvec->vector[i].key);
/* center calculation is done this way to avoid possible overflow */
sort_items[i - 1].center = seg->lower * 0.5f + seg->upper * 0.5f;
sort_items[i - 1].index = i;
sort_items[i - 1].data = seg;
}
qsort(sort_items, maxoff, sizeof(gseg_picksplit_item),
gseg_picksplit_item_cmp);
/* sort items below "firstright" will go into the left side */
firstright = maxoff / 2;
v->spl_left = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
v->spl_right = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
left = v->spl_left;
v->spl_nleft = 0;
right = v->spl_right;
v->spl_nright = 0;
/*
* Emit segments to the left output page, and compute its bounding box.
*/
datum_l = (SEG *) palloc(sizeof(SEG));
memcpy(datum_l, sort_items[0].data, sizeof(SEG));
*left++ = sort_items[0].index;
v->spl_nleft++;
for (i = 1; i < firstright; i++)
{
datum_l = seg_union(datum_l, sort_items[i].data);
*left++ = sort_items[i].index;
v->spl_nleft++;
}
/*
* Likewise for the right page.
*/
datum_r = (SEG *) palloc(sizeof(SEG));
memcpy(datum_r, sort_items[firstright].data, sizeof(SEG));
*right++ = sort_items[firstright].index;
v->spl_nright++;
for (i = firstright + 1; i < maxoff; i++)
{
datum_r = seg_union(datum_r, sort_items[i].data);
*right++ = sort_items[i].index;
v->spl_nright++;
}
v->spl_ldatum = PointerGetDatum(datum_l);
v->spl_rdatum = PointerGetDatum(datum_r);
return v;
}
| static int gseg_picksplit_item_cmp | ( | const void * | a, | |
| const void * | b | |||
| ) | [static] |
Definition at line 308 of file seg.c.
References gseg_picksplit_item::center.
Referenced by gseg_picksplit().
{
const gseg_picksplit_item *i1 = (const gseg_picksplit_item *) a;
const gseg_picksplit_item *i2 = (const gseg_picksplit_item *) b;
if (i1->center < i2->center)
return -1;
else if (i1->center == i2->center)
return 0;
else
return 1;
}
Definition at line 413 of file seg.c.
References seg_same().
{
if (seg_same(b1, b2))
*result = TRUE;
else
*result = FALSE;
#ifdef GIST_DEBUG
fprintf(stderr, "same: %s\n", (*result ? "TRUE" : "FALSE"));
#endif
return (result);
}
| SEG * gseg_union | ( | GistEntryVector * | entryvec, | |
| int * | sizep | |||
| ) |
Definition at line 237 of file seg.c.
References DatumGetPointer, gseg_binary_union(), i, GISTENTRY::key, GistEntryVector::n, NULL, and GistEntryVector::vector.
{
int numranges,
i;
SEG *out = (SEG *) NULL;
SEG *tmp;
#ifdef GIST_DEBUG
fprintf(stderr, "union\n");
#endif
numranges = entryvec->n;
tmp = (SEG *) DatumGetPointer(entryvec->vector[0].key);
*sizep = sizeof(SEG);
for (i = 1; i < numranges; i++)
{
out = gseg_binary_union(tmp, (SEG *)
DatumGetPointer(entryvec->vector[i].key),
sizep);
tmp = out;
}
return (out);
}
| PG_FUNCTION_INFO_V1 | ( | seg_upper | ) |
| PG_FUNCTION_INFO_V1 | ( | seg_in | ) |
| PG_FUNCTION_INFO_V1 | ( | seg_lower | ) |
| PG_FUNCTION_INFO_V1 | ( | seg_center | ) |
| PG_FUNCTION_INFO_V1 | ( | seg_out | ) |
| PG_FUNCTION_INFO_V1 | ( | seg_size | ) |
| static int restore | ( | char * | s, | |
| float | val, | |||
| int | n | |||
| ) | [static] |
Definition at line 858 of file seg.c.
References Abs, buf, i, Min, NULL, pstrdup(), and sign.
Referenced by seg_out().
{
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;
int exp;
int i,
dp,
sign;
/*
* put a cap on the number of siugnificant digits to avoid nonsense in the
* output
*/
n = Min(n, FLT_DIG);
/* remember the sign */
sign = (val < 0 ? 1 : 0);
efmt[5] = '0' + (n - 1) % 10; /* makes %-15.(n-1)e -- this format
* guarantees that the exponent is
* always present */
sprintf(result, efmt, val);
/* trim the spaces left by the %e */
for (p = result; *p != ' '; p++);
*p = '\0';
/* get the exponent */
strtok(pstrdup(result), "e");
exp = atoi(strtok(NULL, "e"));
if (exp == 0)
{
/* use the supplied mantyssa with sign */
strcpy((char *) strchr(result, 'e'), "");
}
else
{
if (Abs(exp) <= 4)
{
/*
* remove the decimal point from the mantyssa and write the digits
* to the buf array
*/
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)
{
/*
* 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
*/
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] = '.';
}
/*
* adjust the exponent by the number of digits after the
* decimal point
*/
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]);
}
}
/* do nothing for Abs(exp) > 4; %e must be OK */
/* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
/* ... this is not done yet. */
}
return (strlen(result));
}
| void rt_seg_size | ( | SEG * | a, | |
| float * | sz | |||
| ) |
Definition at line 675 of file seg.c.
References Abs, SEG::lower, NULL, and SEG::upper.
Referenced by gseg_penalty().
| Datum seg_center | ( | PG_FUNCTION_ARGS | ) |
Definition at line 178 of file seg.c.
References SEG::lower, PG_GETARG_POINTER, PG_RETURN_FLOAT4, and SEG::upper.
{
SEG *seg = (SEG *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT4(((float) seg->lower + (float) seg->upper) / 2.0);
}
Definition at line 698 of file seg.c.
References elog, ERROR, SEG::l_ext, SEG::l_sigd, SEG::lower, SEG::u_ext, SEG::u_sigd, and SEG::upper.
Referenced by seg_different(), seg_ge(), seg_gt(), seg_le(), seg_lt(), and seg_same().
{
/*
* First compare on lower boundary position
*/
if (a->lower < b->lower)
return -1;
if (a->lower > b->lower)
return 1;
/*
* a->lower == b->lower, so consider type of boundary.
*
* A '-' lower bound is < any other kind (this could only be relevant if
* -HUGE_VAL is used as a regular data value). A '<' lower bound is < any
* other kind except '-'. A '>' lower bound is > any other kind.
*/
if (a->l_ext != b->l_ext)
{
if (a->l_ext == '-')
return -1;
if (b->l_ext == '-')
return 1;
if (a->l_ext == '<')
return -1;
if (b->l_ext == '<')
return 1;
if (a->l_ext == '>')
return 1;
if (b->l_ext == '>')
return -1;
}
/*
* For other boundary types, consider # of significant digits first.
*/
if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include (b) */
return -1;
if (a->l_sigd > b->l_sigd) /* (a) is less blurred and is likely to be
* included in (b) */
return 1;
/*
* For same # of digits, an approximate boundary is more blurred than
* exact.
*/
if (a->l_ext != b->l_ext)
{
if (a->l_ext == '~') /* (a) is approximate, while (b) is exact */
return -1;
if (b->l_ext == '~')
return 1;
/* can't get here unless data is corrupt */
elog(ERROR, "bogus lower boundary types %d %d",
(int) a->l_ext, (int) b->l_ext);
}
/* at this point, the lower boundaries are identical */
/*
* First compare on upper boundary position
*/
if (a->upper < b->upper)
return -1;
if (a->upper > b->upper)
return 1;
/*
* a->upper == b->upper, so consider type of boundary.
*
* A '-' upper bound is > any other kind (this could only be relevant if
* HUGE_VAL is used as a regular data value). A '<' upper bound is < any
* other kind. A '>' upper bound is > any other kind except '-'.
*/
if (a->u_ext != b->u_ext)
{
if (a->u_ext == '-')
return 1;
if (b->u_ext == '-')
return -1;
if (a->u_ext == '<')
return -1;
if (b->u_ext == '<')
return 1;
if (a->u_ext == '>')
return 1;
if (b->u_ext == '>')
return -1;
}
/*
* For other boundary types, consider # of significant digits first. Note
* result here is converse of the lower-boundary case.
*/
if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include (b) */
return 1;
if (a->u_sigd > b->u_sigd) /* (a) is less blurred and is likely to be
* included in (b) */
return -1;
/*
* For same # of digits, an approximate boundary is more blurred than
* exact. Again, result is converse of lower-boundary case.
*/
if (a->u_ext != b->u_ext)
{
if (a->u_ext == '~') /* (a) is approximate, while (b) is exact */
return 1;
if (b->u_ext == '~')
return -1;
/* can't get here unless data is corrupt */
elog(ERROR, "bogus upper boundary types %d %d",
(int) a->u_ext, (int) b->u_ext);
}
return 0;
}
Definition at line 537 of file seg.c.
References seg_contains().
Referenced by gseg_leaf_consistent().
{
return (seg_contains(b, a));
}
Definition at line 531 of file seg.c.
References SEG::lower, and SEG::upper.
Referenced by gseg_internal_consistent(), gseg_leaf_consistent(), and seg_contained().
Definition at line 1011 of file seg.c.
References SEG::lower, and SEG::upper.
Definition at line 1017 of file seg.c.
References SEG::lower, and SEG::upper.
Definition at line 1005 of file seg.c.
References SEG::lower, and SEG::upper.
| Datum seg_in | ( | PG_FUNCTION_ARGS | ) |
Definition at line 121 of file seg.c.
References palloc(), PG_GETARG_CSTRING, PG_RETURN_POINTER, seg_scanner_finish(), seg_scanner_init(), seg_yyerror(), and seg_yyparse().
{
char *str = PG_GETARG_CSTRING(0);
SEG *result = palloc(sizeof(SEG));
seg_scanner_init(str);
if (seg_yyparse(result) != 0)
seg_yyerror("bogus input");
seg_scanner_finish();
PG_RETURN_POINTER(result);
}
Definition at line 637 of file seg.c.
References SEG::l_ext, SEG::l_sigd, SEG::lower, palloc(), SEG::u_ext, SEG::u_sigd, and SEG::upper.
{
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);
}
Definition at line 575 of file seg.c.
References SEG::lower, and SEG::upper.
Referenced by gseg_internal_consistent(), and gseg_leaf_consistent().
| Datum seg_lower | ( | PG_FUNCTION_ARGS | ) |
Definition at line 186 of file seg.c.
References SEG::lower, PG_GETARG_POINTER, and PG_RETURN_FLOAT4.
{
SEG *seg = (SEG *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT4(seg->lower);
}
| Datum seg_out | ( | PG_FUNCTION_ARGS | ) |
Definition at line 137 of file seg.c.
References SEG::l_ext, SEG::l_sigd, SEG::lower, palloc(), PG_GETARG_POINTER, PG_RETURN_CSTRING, restore(), SEG::u_ext, SEG::u_sigd, and SEG::upper.
{
SEG *seg = (SEG *) PG_GETARG_POINTER(0);
char *result;
char *p;
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)
{
/*
* indicates that this interval was built by seg_in off a single point
*/
p += restore(p, seg->lower, seg->l_sigd);
}
else
{
if (seg->l_ext != '-')
{
/* print the lower boundary if exists */
p += restore(p, seg->lower, seg->l_sigd);
p += sprintf(p, " ");
}
p += sprintf(p, "..");
if (seg->u_ext != '-')
{
/* print the upper boundary 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);
}
}
PG_RETURN_CSTRING(result);
}
Definition at line 567 of file seg.c.
References SEG::upper.
Referenced by gseg_internal_consistent(), and gseg_leaf_consistent().
Definition at line 591 of file seg.c.
References SEG::lower.
Referenced by gseg_internal_consistent(), and gseg_leaf_consistent().
Definition at line 555 of file seg.c.
References SEG::lower, and SEG::upper.
Referenced by gseg_internal_consistent(), and gseg_leaf_consistent().
Definition at line 583 of file seg.c.
References SEG::lower, and SEG::upper.
Referenced by gseg_internal_consistent(), and gseg_leaf_consistent().
Definition at line 547 of file seg.c.
References seg_cmp().
Referenced by gseg_leaf_consistent(), and gseg_same().
{
return seg_cmp(a, b) == 0;
}
| void seg_scanner_finish | ( | void | ) |
Referenced by seg_in().
| void seg_scanner_init | ( | const char * | str | ) |
Referenced by seg_in().
| Datum seg_size | ( | PG_FUNCTION_ARGS | ) |
Definition at line 686 of file seg.c.
References Abs, SEG::lower, PG_GETARG_POINTER, PG_RETURN_FLOAT4, and SEG::upper.
{
SEG *seg = (SEG *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT4((float) Abs(seg->upper - seg->lower));
}
Definition at line 598 of file seg.c.
References SEG::l_ext, SEG::l_sigd, SEG::lower, palloc(), SEG::u_ext, SEG::u_sigd, and SEG::upper.
Referenced by gseg_binary_union(), gseg_penalty(), and gseg_picksplit().
{
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);
}
| Datum seg_upper | ( | PG_FUNCTION_ARGS | ) |
Definition at line 194 of file seg.c.
References PG_GETARG_POINTER, PG_RETURN_FLOAT4, and SEG::upper.
{
SEG *seg = (SEG *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT4(seg->upper);
}
| void seg_yyerror | ( | const char * | message | ) |
Referenced by seg_in().
| int seg_yyparse | ( | ) |
Referenced by seg_in().
| int significant_digits | ( | char * | s | ) |
Definition at line 1026 of file seg.c.
{
char *p = s;
int n,
c,
zeroes;
zeroes = 1;
/* skip leading zeroes and sign */
for (c = *p; (c == '0' || c == '+' || c == '-') && c != 0; c = *(++p));
/* skip decimal point and following zeroes */
for (c = *p; (c == '0' || c == '.') && c != 0; c = *(++p))
{
if (c != '.')
zeroes++;
}
/* count significant digits (n) */
for (c = *p, n = 0; c != 0; c = *(++p))
{
if (!((c >= '0' && c <= '9') || (c == '.')))
break;
if (c != '.')
n++;
}
if (!n)
return (zeroes);
return (n);
}
1.7.1