2008-05-17 05:28:26 +04:00
|
|
|
/*
|
2010-09-21 00:08:53 +04:00
|
|
|
* contrib/seg/seg.c
|
2008-05-17 05:28:26 +04:00
|
|
|
*
|
|
|
|
******************************************************************************
|
2000-12-11 23:40:33 +03: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"
|
2015-05-15 23:03:16 +03:00
|
|
|
#include "access/stratnum.h"
|
|
|
|
#include "fmgr.h"
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
#include "segdata.h"
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
#define DatumGetSegP(X) ((SEG *) DatumGetPointer(X))
|
|
|
|
#define PG_GETARG_SEG_P(n) DatumGetSegP(PG_GETARG_POINTER(n))
|
|
|
|
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/*
|
2000-12-11 23:40:33 +03:00
|
|
|
#define GIST_DEBUG
|
2001-03-22 07:01:46 +03:00
|
|
|
#define GIST_QUERY_DEBUG
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
|
|
|
|
2006-05-31 02:12:16 +04:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2010-12-16 05:14:24 +03:00
|
|
|
/*
|
|
|
|
* Auxiliary data structure for picksplit method.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
float center;
|
|
|
|
OffsetNumber index;
|
|
|
|
SEG *data;
|
|
|
|
} gseg_picksplit_item;
|
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
|
|
|
** Input/Output routines
|
|
|
|
*/
|
2008-04-19 01:11:35 +04:00
|
|
|
PG_FUNCTION_INFO_V1(seg_in);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_out);
|
2008-04-20 02:55:03 +04:00
|
|
|
PG_FUNCTION_INFO_V1(seg_size);
|
2008-04-19 01:11:35 +04:00
|
|
|
PG_FUNCTION_INFO_V1(seg_lower);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_upper);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_center);
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/*
|
2000-12-11 23:40:33 +03:00
|
|
|
** GiST support methods
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_FUNCTION_INFO_V1(gseg_consistent);
|
|
|
|
PG_FUNCTION_INFO_V1(gseg_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(gseg_decompress);
|
|
|
|
PG_FUNCTION_INFO_V1(gseg_picksplit);
|
|
|
|
PG_FUNCTION_INFO_V1(gseg_penalty);
|
|
|
|
PG_FUNCTION_INFO_V1(gseg_union);
|
|
|
|
PG_FUNCTION_INFO_V1(gseg_same);
|
|
|
|
static Datum gseg_leaf_consistent(Datum key, Datum query, StrategyNumber strategy);
|
|
|
|
static Datum gseg_internal_consistent(Datum key, Datum query, StrategyNumber strategy);
|
|
|
|
static Datum gseg_binary_union(Datum r1, Datum r2, int *sizep);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-11-07 20:36:47 +03:00
|
|
|
** R-tree support functions
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_FUNCTION_INFO_V1(seg_same);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_contains);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_contained);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_overlap);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_left);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_over_left);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_right);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_over_right);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_union);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_inter);
|
|
|
|
static void rt_seg_size(SEG *a, float *size);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Various operators
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_FUNCTION_INFO_V1(seg_cmp);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_lt);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_le);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_gt);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_ge);
|
|
|
|
PG_FUNCTION_INFO_V1(seg_different);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
|
|
|
/*
|
2017-03-29 23:16:30 +03:00
|
|
|
** Auxiliary functions
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
static int restore(char *s, float val, int n);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Input/Output functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-04-19 01:11:35 +04:00
|
|
|
Datum
|
|
|
|
seg_in(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2008-04-19 01:11:35 +04:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
2001-03-22 07:01:46 +03:00
|
|
|
SEG *result = palloc(sizeof(SEG));
|
|
|
|
|
2003-09-14 06:18:49 +04:00
|
|
|
seg_scanner_init(str);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
|
|
|
if (seg_yyparse(result) != 0)
|
2013-07-29 18:42:37 +04:00
|
|
|
seg_yyerror(result, "bogus input");
|
2003-09-14 06:18:49 +04:00
|
|
|
|
|
|
|
seg_scanner_finish();
|
|
|
|
|
2008-04-19 01:11:35 +04:00
|
|
|
PG_RETURN_POINTER(result);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2008-04-19 01:11:35 +04:00
|
|
|
Datum
|
|
|
|
seg_out(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *seg = PG_GETARG_SEG_P(0);
|
2001-03-22 07:01:46 +03:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* indicates that this interval was built by seg_in off a single point
|
2001-03-22 07:01:46 +03:00
|
|
|
*/
|
|
|
|
p += restore(p, seg->lower, seg->l_sigd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (seg->l_ext != '-')
|
|
|
|
{
|
2008-04-18 22:43:09 +04:00
|
|
|
/* print the lower boundary if exists */
|
2001-03-22 07:01:46 +03:00
|
|
|
p += restore(p, seg->lower, seg->l_sigd);
|
|
|
|
p += sprintf(p, " ");
|
|
|
|
}
|
|
|
|
p += sprintf(p, "..");
|
|
|
|
if (seg->u_ext != '-')
|
|
|
|
{
|
2008-04-18 22:43:09 +04:00
|
|
|
/* print the upper boundary if exists */
|
2001-03-22 07:01:46 +03:00
|
|
|
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-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2008-04-19 01:11:35 +04:00
|
|
|
PG_RETURN_CSTRING(result);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2008-04-19 01:11:35 +04:00
|
|
|
Datum
|
|
|
|
seg_center(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *seg = PG_GETARG_SEG_P(0);
|
2008-04-19 01:11:35 +04:00
|
|
|
|
|
|
|
PG_RETURN_FLOAT4(((float) seg->lower + (float) seg->upper) / 2.0);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2008-04-19 01:11:35 +04:00
|
|
|
Datum
|
|
|
|
seg_lower(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *seg = PG_GETARG_SEG_P(0);
|
2008-04-19 01:11:35 +04:00
|
|
|
|
|
|
|
PG_RETURN_FLOAT4(seg->lower);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2008-04-19 01:11:35 +04:00
|
|
|
Datum
|
|
|
|
seg_upper(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *seg = PG_GETARG_SEG_P(0);
|
2008-04-19 01:11:35 +04:00
|
|
|
|
|
|
|
PG_RETURN_FLOAT4(seg->upper);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 07:01:46 +03:00
|
|
|
* GiST functions
|
2000-12-11 23:40:33 +03:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Consistent method for segments
|
|
|
|
** Should return false if for all data items x below entry,
|
2017-08-16 07:22:32 +03:00
|
|
|
** the predicate x op query == false, where op is the oper
|
2000-12-11 23:40:33 +03:00
|
|
|
** corresponding to strategy in the pg_amop table.
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
gseg_consistent(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
Datum query = PG_GETARG_DATUM(1);
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
|
|
|
|
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
|
|
|
|
2008-04-14 21:05:34 +04:00
|
|
|
/* All cases served by this function are exact */
|
|
|
|
*recheck = false;
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/*
|
2005-06-27 04:48:07 +04:00
|
|
|
* if entry is not leaf, use gseg_internal_consistent, else use
|
2001-03-22 07:01:46 +03:00
|
|
|
* gseg_leaf_consistent
|
|
|
|
*/
|
|
|
|
if (GIST_LEAF(entry))
|
2017-03-29 23:16:30 +03:00
|
|
|
return gseg_leaf_consistent(entry->key, query, strategy);
|
2001-03-22 07:01:46 +03:00
|
|
|
else
|
2017-03-29 23:16:30 +03:00
|
|
|
return gseg_internal_consistent(entry->key, query, strategy);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Union method for segments
|
|
|
|
** returns the minimal bounding seg that encloses all the entries in entryvec
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
gseg_union(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
int *sizep = (int *) PG_GETARG_POINTER(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
int numranges,
|
|
|
|
i;
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum out = 0;
|
|
|
|
Datum tmp;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 07:01:46 +03:00
|
|
|
fprintf(stderr, "union\n");
|
2000-12-11 23:40:33 +03:00
|
|
|
#endif
|
|
|
|
|
2004-03-30 19:45:33 +04:00
|
|
|
numranges = entryvec->n;
|
2017-03-29 23:16:30 +03:00
|
|
|
tmp = entryvec->vector[0].key;
|
2001-03-22 07:01:46 +03:00
|
|
|
*sizep = sizeof(SEG);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
for (i = 1; i < numranges; i++)
|
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
out = gseg_binary_union(tmp, entryvec->vector[i].key, sizep);
|
2001-03-22 07:01:46 +03:00
|
|
|
tmp = out;
|
|
|
|
}
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_DATUM(out);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GiST Compress and Decompress methods for segments
|
|
|
|
** do not do anything.
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
gseg_compress(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
gseg_decompress(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Penalty method for segments
|
|
|
|
** As in the R-tree paper, we use change in area as our penalty metric
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
gseg_penalty(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
|
|
|
|
float *result = (float *) PG_GETARG_POINTER(2);
|
2001-05-31 22:16:55 +04:00
|
|
|
SEG *ud;
|
2001-03-22 07:01:46 +03:00
|
|
|
float tmp1,
|
|
|
|
tmp2;
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
ud = DatumGetSegP(DirectFunctionCall2(seg_union,
|
|
|
|
origentry->key,
|
|
|
|
newentry->key));
|
2001-05-31 22:16:55 +04:00
|
|
|
rt_seg_size(ud, &tmp1);
|
2017-03-29 23:16:30 +03:00
|
|
|
rt_seg_size(DatumGetSegP(origentry->key), &tmp2);
|
2001-03-22 07:01:46 +03:00
|
|
|
*result = tmp1 - tmp2;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 07:01:46 +03:00
|
|
|
fprintf(stderr, "penalty\n");
|
|
|
|
fprintf(stderr, "\t%g\n", *result);
|
2000-12-11 23:40:33 +03:00
|
|
|
#endif
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_POINTER(result);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2010-12-16 05:14:24 +03:00
|
|
|
/*
|
|
|
|
* Compare function for gseg_picksplit_item: sort by center.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gseg_picksplit_item_cmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const gseg_picksplit_item *i1 = (const gseg_picksplit_item *) a;
|
|
|
|
const gseg_picksplit_item *i2 = (const gseg_picksplit_item *) b;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2010-12-16 05:14:24 +03:00
|
|
|
if (i1->center < i2->center)
|
|
|
|
return -1;
|
|
|
|
else if (i1->center == i2->center)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
/*
|
2010-12-16 05:14:24 +03:00
|
|
|
* The GiST PickSplit method for segments
|
|
|
|
*
|
|
|
|
* We used to use Guttman's split algorithm here, but since the data is 1-D
|
|
|
|
* it's easier and more robust to just sort the segments by center-point and
|
|
|
|
* split at the middle.
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
gseg_picksplit(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
2010-12-16 05:14:24 +03:00
|
|
|
int i;
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *seg,
|
|
|
|
*seg_l,
|
|
|
|
*seg_r;
|
2010-12-16 05:14:24 +03:00
|
|
|
gseg_picksplit_item *sort_items;
|
2001-03-22 07:01:46 +03:00
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
OffsetNumber maxoff;
|
2010-12-16 05:14:24 +03:00
|
|
|
OffsetNumber firstright;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 07:01:46 +03:00
|
|
|
fprintf(stderr, "picksplit\n");
|
2000-12-11 23:40:33 +03:00
|
|
|
#endif
|
|
|
|
|
2010-12-16 05:14:24 +03:00
|
|
|
/* Valid items in entryvec->vector[] are indexed 1..maxoff */
|
|
|
|
maxoff = entryvec->n - 1;
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2010-12-16 05:14:24 +03:00
|
|
|
/*
|
|
|
|
* Prepare the auxiliary array and sort it.
|
|
|
|
*/
|
|
|
|
sort_items = (gseg_picksplit_item *)
|
|
|
|
palloc(maxoff * sizeof(gseg_picksplit_item));
|
|
|
|
for (i = 1; i <= maxoff; i++)
|
2001-03-22 07:01:46 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
seg = DatumGetSegP(entryvec->vector[i].key);
|
2010-12-16 05:14:24 +03:00
|
|
|
/* center calculation is done this way to avoid possible overflow */
|
2011-04-10 19:42:00 +04:00
|
|
|
sort_items[i - 1].center = seg->lower * 0.5f + seg->upper * 0.5f;
|
2010-12-16 05:14:24 +03:00
|
|
|
sort_items[i - 1].index = i;
|
|
|
|
sort_items[i - 1].data = seg;
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
2010-12-16 05:14:24 +03:00
|
|
|
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;
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2010-12-16 05:14:24 +03:00
|
|
|
v->spl_left = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
|
2001-03-22 07:01:46 +03:00
|
|
|
left = v->spl_left;
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
right = v->spl_right;
|
|
|
|
v->spl_nright = 0;
|
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
2010-12-16 05:14:24 +03:00
|
|
|
* Emit segments to the left output page, and compute its bounding box.
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
seg_l = (SEG *) palloc(sizeof(SEG));
|
|
|
|
memcpy(seg_l, sort_items[0].data, sizeof(SEG));
|
2010-12-16 05:14:24 +03:00
|
|
|
*left++ = sort_items[0].index;
|
|
|
|
v->spl_nleft++;
|
|
|
|
for (i = 1; i < firstright; i++)
|
2001-03-22 07:01:46 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum sortitem = PointerGetDatum(sort_items[i].data);
|
|
|
|
|
|
|
|
seg_l = DatumGetSegP(DirectFunctionCall2(seg_union,
|
|
|
|
PointerGetDatum(seg_l),
|
|
|
|
sortitem));
|
2010-12-16 05:14:24 +03:00
|
|
|
*left++ = sort_items[i].index;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2010-12-16 05:14:24 +03:00
|
|
|
/*
|
|
|
|
* Likewise for the right page.
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
seg_r = (SEG *) palloc(sizeof(SEG));
|
|
|
|
memcpy(seg_r, sort_items[firstright].data, sizeof(SEG));
|
2010-12-16 05:14:24 +03:00
|
|
|
*right++ = sort_items[firstright].index;
|
|
|
|
v->spl_nright++;
|
|
|
|
for (i = firstright + 1; i < maxoff; i++)
|
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum sortitem = PointerGetDatum(sort_items[i].data);
|
|
|
|
|
|
|
|
seg_r = DatumGetSegP(DirectFunctionCall2(seg_union,
|
|
|
|
PointerGetDatum(seg_r),
|
|
|
|
sortitem));
|
2010-12-16 05:14:24 +03:00
|
|
|
*right++ = sort_items[i].index;
|
|
|
|
v->spl_nright++;
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
v->spl_ldatum = PointerGetDatum(seg_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(seg_r);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_POINTER(v);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Equality methods
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
gseg_same(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
|
|
|
|
|
|
|
if (DirectFunctionCall2(seg_same, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)))
|
2017-08-16 07:22:32 +03:00
|
|
|
*result = true;
|
2001-03-22 07:01:46 +03:00
|
|
|
else
|
2017-08-16 07:22:32 +03:00
|
|
|
*result = false;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 07:01:46 +03:00
|
|
|
fprintf(stderr, "same: %s\n", (*result ? "TRUE" : "FALSE"));
|
2000-12-11 23:40:33 +03:00
|
|
|
#endif
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_POINTER(result);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/*
|
2000-12-11 23:40:33 +03:00
|
|
|
** SUPPORT ROUTINES
|
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
static Datum
|
|
|
|
gseg_leaf_consistent(Datum key, Datum query, StrategyNumber strategy)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum retval;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
#ifdef GIST_QUERY_DEBUG
|
2001-03-22 07:01:46 +03:00
|
|
|
fprintf(stderr, "leaf_consistent, %d\n", strategy);
|
2000-12-11 23:40:33 +03:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTLeftStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_left, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTOverLeftStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_over_left, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTOverlapStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_overlap, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTOverRightStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_over_right, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTRightStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_right, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_same, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTContainsStrategyNumber:
|
2006-09-10 21:36:52 +04:00
|
|
|
case RTOldContainsStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_contains, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2006-09-10 21:36:52 +04:00
|
|
|
case RTOldContainedByStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_contained, key, query);
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-16 07:22:32 +03:00
|
|
|
retval = false;
|
2001-03-22 07:01:46 +03:00
|
|
|
}
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_DATUM(retval);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
static Datum
|
|
|
|
gseg_internal_consistent(Datum key, Datum query, StrategyNumber strategy)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
bool retval;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
#ifdef GIST_QUERY_DEBUG
|
2001-03-22 07:01:46 +03:00
|
|
|
fprintf(stderr, "internal_consistent, %d\n", strategy);
|
2000-12-11 23:40:33 +03:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTLeftStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval =
|
|
|
|
!DatumGetBool(DirectFunctionCall2(seg_over_right, key, query));
|
2005-06-27 04:48:07 +04:00
|
|
|
break;
|
2001-03-22 07:01:46 +03:00
|
|
|
case RTOverLeftStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval =
|
|
|
|
!DatumGetBool(DirectFunctionCall2(seg_right, key, query));
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTOverlapStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval =
|
|
|
|
DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTOverRightStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval =
|
|
|
|
!DatumGetBool(DirectFunctionCall2(seg_left, key, query));
|
2005-06-27 04:48:07 +04:00
|
|
|
break;
|
2001-03-22 07:01:46 +03:00
|
|
|
case RTRightStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval =
|
|
|
|
!DatumGetBool(DirectFunctionCall2(seg_over_left, key, query));
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
case RTContainsStrategyNumber:
|
2006-09-10 21:36:52 +04:00
|
|
|
case RTOldContainsStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval =
|
|
|
|
DatumGetBool(DirectFunctionCall2(seg_contains, key, query));
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2006-09-10 21:36:52 +04:00
|
|
|
case RTOldContainedByStrategyNumber:
|
2017-03-29 23:16:30 +03:00
|
|
|
retval =
|
|
|
|
DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
|
2001-03-22 07:01:46 +03:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-16 07:22:32 +03:00
|
|
|
retval = false;
|
2001-03-22 07:01:46 +03:00
|
|
|
}
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(retval);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
static Datum
|
|
|
|
gseg_binary_union(Datum r1, Datum r2, int *sizep)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum retval;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
retval = DirectFunctionCall2(seg_union, r1, r2);
|
2001-03-22 07:01:46 +03:00
|
|
|
*sizep = sizeof(SEG);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2017-08-17 19:39:20 +03:00
|
|
|
return retval;
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_contains(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL((a->lower <= b->lower) && (a->upper >= b->upper));
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_contained(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum a = PG_GETARG_DATUM(0);
|
|
|
|
Datum b = PG_GETARG_DATUM(1);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(seg_contains, b, a));
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Operator class for R-tree indexing
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_same(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
int cmp = DatumGetInt32(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 22:35:54 +03:00
|
|
|
DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)));
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(cmp == 0);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/* seg_overlap -- does a overlap b?
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_overlap(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(((a->upper >= b->upper) && (a->lower <= b->upper)) ||
|
|
|
|
((b->upper >= a->upper) && (b->lower <= a->upper)));
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
/* seg_over_left -- is the right edge of (a) located at or left of the right edge of (b)?
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_over_left(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(a->upper <= b->upper);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/* seg_left -- is (a) entirely on the left of (b)?
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_left(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(a->upper < b->lower);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/* seg_right -- is (a) entirely on the right of (b)?
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_right(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(a->lower > b->upper);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
/* seg_over_right -- is the left edge of (a) located at or right of the left edge of (b)?
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_over_right(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_BOOL(a->lower >= b->lower);
|
|
|
|
}
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_union(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
2001-03-22 07:01:46 +03: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;
|
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_POINTER(n);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_inter(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
2001-03-22 07:01:46 +03: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;
|
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_POINTER(n);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
static void
|
2009-06-11 18:49:15 +04:00
|
|
|
rt_seg_size(SEG *a, float *size)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a == (SEG *) NULL || a->upper <= a->lower)
|
|
|
|
*size = 0.0;
|
|
|
|
else
|
2004-10-21 23:28:36 +04:00
|
|
|
*size = (float) Abs(a->upper - a->lower);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
|
|
|
return;
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2008-04-20 02:55:03 +04:00
|
|
|
Datum
|
|
|
|
seg_size(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *seg = PG_GETARG_SEG_P(0);
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2008-04-20 02:55:03 +04:00
|
|
|
PG_RETURN_FLOAT4((float) Abs(seg->upper - seg->lower));
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 07:01:46 +03:00
|
|
|
* Miscellaneous operators
|
2000-12-11 23:40:33 +03:00
|
|
|
*****************************************************************************/
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_cmp(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
SEG *a = PG_GETARG_SEG_P(0);
|
|
|
|
SEG *b = PG_GETARG_SEG_P(1);
|
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
|
|
|
* First compare on lower boundary position
|
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->lower < b->lower)
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->lower > b->lower)
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
|
|
|
* a->lower == b->lower, so consider type of boundary.
|
|
|
|
*
|
2001-03-22 07:01:46 +03:00
|
|
|
* A '-' lower bound is < any other kind (this could only be relevant if
|
2005-10-15 06:49:52 +04: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-11 23:40:33 +03:00
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->l_ext != b->l_ext)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->l_ext == '-')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->l_ext == '-')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->l_ext == '<')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->l_ext == '<')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->l_ext == '>')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->l_ext == '>')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
|
|
|
* For other boundary types, consider # of significant digits first.
|
|
|
|
*/
|
2005-10-15 06:49:52 +04:00
|
|
|
if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include (b) */
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->l_sigd > b->l_sigd) /* (a) is less blurred and is likely to be
|
|
|
|
* included in (b) */
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
|
|
|
* For same # of digits, an approximate boundary is more blurred than
|
|
|
|
* exact.
|
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->l_ext != b->l_ext)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->l_ext == '~') /* (a) is approximate, while (b) is exact */
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->l_ext == '~')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2000-12-11 23:40:33 +03:00
|
|
|
/* can't get here unless data is corrupt */
|
2003-07-24 21:52:50 +04:00
|
|
|
elog(ERROR, "bogus lower boundary types %d %d",
|
2000-12-11 23:40:33 +03: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 07:01:46 +03:00
|
|
|
if (a->upper < b->upper)
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->upper > b->upper)
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
|
|
|
* a->upper == b->upper, so consider type of boundary.
|
|
|
|
*
|
2001-03-22 07:01:46 +03:00
|
|
|
* A '-' upper bound is > any other kind (this could only be relevant if
|
2005-10-15 06:49:52 +04: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-11 23:40:33 +03:00
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->u_ext != b->u_ext)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->u_ext == '-')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->u_ext == '-')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->u_ext == '<')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->u_ext == '<')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->u_ext == '>')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->u_ext == '>')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* For other boundary types, consider # of significant digits first. Note
|
|
|
|
* result here is converse of the lower-boundary case.
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2005-10-15 06:49:52 +04:00
|
|
|
if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include (b) */
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->u_sigd > b->u_sigd) /* (a) is less blurred and is likely to be
|
|
|
|
* included in (b) */
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*
|
|
|
|
* For same # of digits, an approximate boundary is more blurred than
|
|
|
|
* exact. Again, result is converse of lower-boundary case.
|
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->u_ext != b->u_ext)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
if (a->u_ext == '~') /* (a) is approximate, while (b) is exact */
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (b->u_ext == '~')
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(-1);
|
2000-12-11 23:40:33 +03:00
|
|
|
/* can't get here unless data is corrupt */
|
2003-07-24 21:52:50 +04:00
|
|
|
elog(ERROR, "bogus upper boundary types %d %d",
|
2000-12-11 23:40:33 +03:00
|
|
|
(int) a->u_ext, (int) b->u_ext);
|
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
PG_RETURN_INT32(0);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_lt(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
int cmp = DatumGetInt32(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 22:35:54 +03:00
|
|
|
DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)));
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(cmp < 0);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_le(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
int cmp = DatumGetInt32(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 22:35:54 +03:00
|
|
|
DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)));
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(cmp <= 0);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_gt(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
int cmp = DatumGetInt32(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 22:35:54 +03:00
|
|
|
DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)));
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(cmp > 0);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
Datum
|
|
|
|
seg_ge(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
int cmp = DatumGetInt32(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 22:35:54 +03:00
|
|
|
DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)));
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(cmp >= 0);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
Datum
|
|
|
|
seg_different(PG_FUNCTION_ARGS)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2017-03-29 23:16:30 +03:00
|
|
|
int cmp = DatumGetInt32(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 22:35:54 +03:00
|
|
|
DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)));
|
2017-03-29 23:16:30 +03:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(cmp != 0);
|
2016-04-22 18:54:23 +03:00
|
|
|
}
|
2000-12-11 23:40:33 +03:00
|
|
|
|
|
|
|
|
2016-04-28 18:46:07 +03:00
|
|
|
|
2000-12-11 23:40:33 +03:00
|
|
|
/*****************************************************************************
|
2001-03-22 07:01:46 +03:00
|
|
|
* Auxiliary functions
|
2000-12-11 23:40:33 +03:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2016-04-04 00:36:53 +03:00
|
|
|
/*
|
|
|
|
* The purpose of this routine is to print the given floating point
|
|
|
|
* value with exactly n significant digits. Its behaviour
|
2000-12-11 23:40:33 +03:00
|
|
|
* is similar to %.ng except it prints 8.00 where %.ng would
|
2016-04-04 00:36:53 +03:00
|
|
|
* print 8. Returns the length of the string written at "result".
|
|
|
|
*
|
|
|
|
* Caller must provide a sufficiently large result buffer; 16 bytes
|
|
|
|
* should be enough for all known float implementations.
|
2000-12-11 23:40:33 +03:00
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
static int
|
|
|
|
restore(char *result, float val, int n)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
/*
|
2016-04-04 00:36:53 +03:00
|
|
|
* Put a cap on the number of significant digits to avoid garbage in the
|
|
|
|
* output and ensure we don't overrun the result buffer.
|
2001-03-22 07:01:46 +03:00
|
|
|
*/
|
2004-09-14 08:21:38 +04:00
|
|
|
n = Min(n, FLT_DIG);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
|
|
|
/* remember the sign */
|
|
|
|
sign = (val < 0 ? 1 : 0);
|
|
|
|
|
2016-04-04 00:36:53 +03:00
|
|
|
/* print, in %e style to start with */
|
|
|
|
sprintf(result, "%.*e", n - 1, val);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2016-04-04 00:36:53 +03:00
|
|
|
/* find the exponent */
|
|
|
|
p = strchr(result, 'e');
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2016-04-04 00:36:53 +03:00
|
|
|
/* punt if we have 'inf' or similar */
|
|
|
|
if (p == NULL)
|
|
|
|
return strlen(result);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2016-04-04 00:36:53 +03:00
|
|
|
exp = atoi(p + 1);
|
2001-03-22 07:01:46 +03:00
|
|
|
if (exp == 0)
|
|
|
|
{
|
2016-04-04 00:36:53 +03:00
|
|
|
/* just truncate off the 'e+00' */
|
|
|
|
*p = '\0';
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
2001-03-22 07:01:46 +03:00
|
|
|
else
|
|
|
|
{
|
2004-10-21 23:28:36 +04:00
|
|
|
if (Abs(exp) <= 4)
|
2001-03-22 07:01:46 +03:00
|
|
|
{
|
|
|
|
/*
|
2017-02-06 12:33:58 +03:00
|
|
|
* remove the decimal point from the mantissa and write the digits
|
2005-10-15 06:49:52 +04:00
|
|
|
* to the buf array
|
2001-03-22 07:01:46 +03: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 06:49:52 +04: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 07:01:46 +03: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 06:49:52 +04:00
|
|
|
* adjust the exponent by the number of digits after the
|
|
|
|
* decimal point
|
2001-03-22 07:01:46 +03: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-21 23:28:36 +04:00
|
|
|
/* do nothing for Abs(exp) > 4; %e must be OK */
|
2001-03-22 07:01:46 +03:00
|
|
|
/* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
|
|
|
|
|
|
|
|
/* ... this is not done yet. */
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
2017-08-17 19:39:20 +03:00
|
|
|
return strlen(result);
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Miscellany
|
|
|
|
*/
|
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/* find out the number of significant digits in a string representing
|
2000-12-11 23:40:33 +03:00
|
|
|
* a floating point number
|
|
|
|
*/
|
2001-03-22 07:01:46 +03:00
|
|
|
int
|
|
|
|
significant_digits(char *s)
|
2000-12-11 23:40:33 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
char *p = s;
|
|
|
|
int n,
|
|
|
|
c,
|
|
|
|
zeroes;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
zeroes = 1;
|
|
|
|
/* skip leading zeroes and sign */
|
|
|
|
for (c = *p; (c == '0' || c == '+' || c == '-') && c != 0; c = *(++p));
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
/* skip decimal point and following zeroes */
|
|
|
|
for (c = *p; (c == '0' || c == '.') && c != 0; c = *(++p))
|
|
|
|
{
|
|
|
|
if (c != '.')
|
|
|
|
zeroes++;
|
|
|
|
}
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2001-03-22 07:01:46 +03: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-11 23:40:33 +03:00
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
if (!n)
|
2017-08-17 19:39:20 +03:00
|
|
|
return zeroes;
|
2000-12-11 23:40:33 +03:00
|
|
|
|
2017-08-17 19:39:20 +03:00
|
|
|
return n;
|
2000-12-11 23:40:33 +03:00
|
|
|
}
|