Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * contrib/seg/seg.c
3 : : *
4 : : ******************************************************************************
5 : : This file contains routines that can be bound to a Postgres backend and
6 : : called by the backend in the process of processing queries. The calling
7 : : format for these routines is dictated by Postgres architecture.
8 : : ******************************************************************************/
9 : :
10 : : #include "postgres.h"
11 : :
12 : : #include <float.h>
13 : : #include <math.h>
14 : :
15 : : #include "access/gist.h"
16 : : #include "access/stratnum.h"
17 : : #include "fmgr.h"
18 : :
19 : : #include "segdata.h"
20 : :
21 : :
22 : : #define DatumGetSegP(X) ((SEG *) DatumGetPointer(X))
23 : : #define PG_GETARG_SEG_P(n) DatumGetSegP(PG_GETARG_DATUM(n))
24 : :
25 : :
26 : : /*
27 : : #define GIST_DEBUG
28 : : #define GIST_QUERY_DEBUG
29 : : */
30 : :
164 tgl@sss.pgh.pa.us 31 :CBC 3 : PG_MODULE_MAGIC_EXT(
32 : : .name = "seg",
33 : : .version = PG_VERSION
34 : : );
35 : :
36 : : /*
37 : : * Auxiliary data structure for picksplit method.
38 : : */
39 : : typedef struct
40 : : {
41 : : float center;
42 : : OffsetNumber index;
43 : : SEG *data;
44 : : } gseg_picksplit_item;
45 : :
46 : : /*
47 : : ** Input/Output routines
48 : : */
6350 alvherre@alvh.no-ip. 49 : 4 : PG_FUNCTION_INFO_V1(seg_in);
50 : 3 : PG_FUNCTION_INFO_V1(seg_out);
6349 tgl@sss.pgh.pa.us 51 : 2 : PG_FUNCTION_INFO_V1(seg_size);
6350 alvherre@alvh.no-ip. 52 : 3 : PG_FUNCTION_INFO_V1(seg_lower);
53 : 3 : PG_FUNCTION_INFO_V1(seg_upper);
54 : 3 : PG_FUNCTION_INFO_V1(seg_center);
55 : :
56 : : /*
57 : : ** GiST support methods
58 : : */
3083 andres@anarazel.de 59 : 3 : PG_FUNCTION_INFO_V1(gseg_consistent);
60 : 2 : PG_FUNCTION_INFO_V1(gseg_compress);
61 : 2 : PG_FUNCTION_INFO_V1(gseg_decompress);
62 : 3 : PG_FUNCTION_INFO_V1(gseg_picksplit);
63 : 3 : PG_FUNCTION_INFO_V1(gseg_penalty);
64 : 3 : PG_FUNCTION_INFO_V1(gseg_union);
65 : 3 : PG_FUNCTION_INFO_V1(gseg_same);
66 : : static Datum gseg_leaf_consistent(Datum key, Datum query, StrategyNumber strategy);
67 : : static Datum gseg_internal_consistent(Datum key, Datum query, StrategyNumber strategy);
68 : : static Datum gseg_binary_union(Datum r1, Datum r2, int *sizep);
69 : :
70 : :
71 : : /*
72 : : ** R-tree support functions
73 : : */
74 : 3 : PG_FUNCTION_INFO_V1(seg_same);
75 : 3 : PG_FUNCTION_INFO_V1(seg_contains);
76 : 3 : PG_FUNCTION_INFO_V1(seg_contained);
77 : 3 : PG_FUNCTION_INFO_V1(seg_overlap);
78 : 3 : PG_FUNCTION_INFO_V1(seg_left);
79 : 3 : PG_FUNCTION_INFO_V1(seg_over_left);
80 : 3 : PG_FUNCTION_INFO_V1(seg_right);
81 : 3 : PG_FUNCTION_INFO_V1(seg_over_right);
82 : 2 : PG_FUNCTION_INFO_V1(seg_union);
83 : 2 : PG_FUNCTION_INFO_V1(seg_inter);
84 : : static void rt_seg_size(SEG *a, float *size);
85 : :
86 : : /*
87 : : ** Various operators
88 : : */
89 : 4 : PG_FUNCTION_INFO_V1(seg_cmp);
90 : 2 : PG_FUNCTION_INFO_V1(seg_lt);
91 : 2 : PG_FUNCTION_INFO_V1(seg_le);
92 : 2 : PG_FUNCTION_INFO_V1(seg_gt);
93 : 2 : PG_FUNCTION_INFO_V1(seg_ge);
94 : 3 : PG_FUNCTION_INFO_V1(seg_different);
95 : :
96 : : /*
97 : : ** Auxiliary functions
98 : : */
99 : : static int restore(char *result, float val, int n);
100 : :
101 : :
102 : : /*****************************************************************************
103 : : * Input/Output functions
104 : : *****************************************************************************/
105 : :
106 : : Datum
6350 alvherre@alvh.no-ip. 107 : 2825 : seg_in(PG_FUNCTION_ARGS)
108 : : {
109 : 2825 : char *str = PG_GETARG_CSTRING(0);
8934 bruce@momjian.us 110 : 2825 : SEG *result = palloc(sizeof(SEG));
111 : : yyscan_t scanner;
112 : :
262 peter@eisentraut.org 113 : 2825 : seg_scanner_init(str, &scanner);
114 : :
115 [ + + ]: 2825 : if (seg_yyparse(result, fcinfo->context, scanner) != 0)
116 : 8 : seg_yyerror(result, fcinfo->context, scanner, "bogus input");
117 : :
118 : 2816 : seg_scanner_finish(scanner);
119 : :
6350 alvherre@alvh.no-ip. 120 : 2816 : PG_RETURN_POINTER(result);
121 : : }
122 : :
123 : : Datum
124 : 209 : seg_out(PG_FUNCTION_ARGS)
125 : : {
3083 andres@anarazel.de 126 : 209 : SEG *seg = PG_GETARG_SEG_P(0);
127 : : char *result;
128 : : char *p;
129 : :
8934 bruce@momjian.us 130 : 209 : p = result = (char *) palloc(40);
131 : :
132 [ + + + + : 209 : if (seg->l_ext == '>' || seg->l_ext == '<' || seg->l_ext == '~')
+ + ]
133 : 22 : p += sprintf(p, "%c", seg->l_ext);
134 : :
135 [ + + + - ]: 209 : if (seg->lower == seg->upper && seg->l_ext == seg->u_ext)
136 : : {
137 : : /*
138 : : * indicates that this interval was built by seg_in off a single point
139 : : */
140 : 47 : p += restore(p, seg->lower, seg->l_sigd);
141 : : }
142 : : else
143 : : {
144 [ + + ]: 162 : if (seg->l_ext != '-')
145 : : {
146 : : /* print the lower boundary if exists */
147 : 155 : p += restore(p, seg->lower, seg->l_sigd);
148 : 155 : p += sprintf(p, " ");
149 : : }
150 : 162 : p += sprintf(p, "..");
151 [ + + ]: 162 : if (seg->u_ext != '-')
152 : : {
153 : : /* print the upper boundary if exists */
154 : 123 : p += sprintf(p, " ");
155 [ + + + + : 123 : if (seg->u_ext == '>' || seg->u_ext == '<' || seg->l_ext == '~')
- + ]
156 : 32 : p += sprintf(p, "%c", seg->u_ext);
157 : 123 : p += restore(p, seg->upper, seg->u_sigd);
158 : : }
159 : : }
160 : :
6350 alvherre@alvh.no-ip. 161 : 209 : PG_RETURN_CSTRING(result);
162 : : }
163 : :
164 : : Datum
165 : 143 : seg_center(PG_FUNCTION_ARGS)
166 : : {
3083 andres@anarazel.de 167 : 143 : SEG *seg = PG_GETARG_SEG_P(0);
168 : :
6350 alvherre@alvh.no-ip. 169 : 143 : PG_RETURN_FLOAT4(((float) seg->lower + (float) seg->upper) / 2.0);
170 : : }
171 : :
172 : : Datum
173 : 143 : seg_lower(PG_FUNCTION_ARGS)
174 : : {
3083 andres@anarazel.de 175 : 143 : SEG *seg = PG_GETARG_SEG_P(0);
176 : :
6350 alvherre@alvh.no-ip. 177 : 143 : PG_RETURN_FLOAT4(seg->lower);
178 : : }
179 : :
180 : : Datum
181 : 143 : seg_upper(PG_FUNCTION_ARGS)
182 : : {
3083 andres@anarazel.de 183 : 143 : SEG *seg = PG_GETARG_SEG_P(0);
184 : :
6350 alvherre@alvh.no-ip. 185 : 143 : PG_RETURN_FLOAT4(seg->upper);
186 : : }
187 : :
188 : :
189 : : /*****************************************************************************
190 : : * GiST functions
191 : : *****************************************************************************/
192 : :
193 : : /*
194 : : ** The GiST Consistent method for segments
195 : : ** Should return false if for all data items x below entry,
196 : : ** the predicate x op query == false, where op is the oper
197 : : ** corresponding to strategy in the pg_amop table.
198 : : */
199 : : Datum
3083 andres@anarazel.de 200 : 4776 : gseg_consistent(PG_FUNCTION_ARGS)
201 : : {
202 : 4776 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
203 : 4776 : Datum query = PG_GETARG_DATUM(1);
204 : 4776 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
205 : :
206 : : /* Oid subtype = PG_GETARG_OID(3); */
207 : 4776 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
208 : :
209 : : /* All cases served by this function are exact */
6354 tgl@sss.pgh.pa.us 210 : 4776 : *recheck = false;
211 : :
212 : : /*
213 : : * if entry is not leaf, use gseg_internal_consistent, else use
214 : : * gseg_leaf_consistent
215 : : */
8934 bruce@momjian.us 216 [ + + ]: 4776 : if (GIST_LEAF(entry))
3083 andres@anarazel.de 217 : 4704 : return gseg_leaf_consistent(entry->key, query, strategy);
218 : : else
219 : 72 : return gseg_internal_consistent(entry->key, query, strategy);
220 : : }
221 : :
222 : : /*
223 : : ** The GiST Union method for segments
224 : : ** returns the minimal bounding seg that encloses all the entries in entryvec
225 : : */
226 : : Datum
227 : 2316 : gseg_union(PG_FUNCTION_ARGS)
228 : : {
229 : 2316 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
230 : 2316 : int *sizep = (int *) PG_GETARG_POINTER(1);
231 : : int numranges,
232 : : i;
233 : 2316 : Datum out = 0;
234 : : Datum tmp;
235 : :
236 : : #ifdef GIST_DEBUG
237 : : fprintf(stderr, "union\n");
238 : : #endif
239 : :
7830 teodor@sigaev.ru 240 : 2316 : numranges = entryvec->n;
3083 andres@anarazel.de 241 : 2316 : tmp = entryvec->vector[0].key;
8934 bruce@momjian.us 242 : 2316 : *sizep = sizeof(SEG);
243 : :
244 [ + + ]: 4632 : for (i = 1; i < numranges; i++)
245 : : {
3083 andres@anarazel.de 246 : 2316 : out = gseg_binary_union(tmp, entryvec->vector[i].key, sizep);
8934 bruce@momjian.us 247 : 2316 : tmp = out;
248 : : }
249 : :
3083 andres@anarazel.de 250 : 2316 : PG_RETURN_DATUM(out);
251 : : }
252 : :
253 : : /*
254 : : ** GiST Compress and Decompress methods for segments
255 : : ** do not do anything.
256 : : */
257 : : Datum
3083 andres@anarazel.de 258 :UBC 0 : gseg_compress(PG_FUNCTION_ARGS)
259 : : {
260 : 0 : PG_RETURN_POINTER(PG_GETARG_POINTER(0));
261 : : }
262 : :
263 : : Datum
264 : 0 : gseg_decompress(PG_FUNCTION_ARGS)
265 : : {
266 : 0 : PG_RETURN_POINTER(PG_GETARG_POINTER(0));
267 : : }
268 : :
269 : : /*
270 : : ** The GiST Penalty method for segments
271 : : ** As in the R-tree paper, we use change in area as our penalty metric
272 : : */
273 : : Datum
3083 andres@anarazel.de 274 :CBC 5367 : gseg_penalty(PG_FUNCTION_ARGS)
275 : : {
276 : 5367 : GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
277 : 5367 : GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
278 : 5367 : float *result = (float *) PG_GETARG_POINTER(2);
279 : : SEG *ud;
280 : : float tmp1,
281 : : tmp2;
282 : :
283 : 5367 : ud = DatumGetSegP(DirectFunctionCall2(seg_union,
284 : : origentry->key,
285 : : newentry->key));
8864 tgl@sss.pgh.pa.us 286 : 5367 : rt_seg_size(ud, &tmp1);
3083 andres@anarazel.de 287 : 5367 : rt_seg_size(DatumGetSegP(origentry->key), &tmp2);
8934 bruce@momjian.us 288 : 5367 : *result = tmp1 - tmp2;
289 : :
290 : : #ifdef GIST_DEBUG
291 : : fprintf(stderr, "penalty\n");
292 : : fprintf(stderr, "\t%g\n", *result);
293 : : #endif
294 : :
3083 andres@anarazel.de 295 : 5367 : PG_RETURN_POINTER(result);
296 : : }
297 : :
298 : : /*
299 : : * Compare function for gseg_picksplit_item: sort by center.
300 : : */
301 : : static int
5379 tgl@sss.pgh.pa.us 302 : 29282 : gseg_picksplit_item_cmp(const void *a, const void *b)
303 : : {
304 : 29282 : const gseg_picksplit_item *i1 = (const gseg_picksplit_item *) a;
305 : 29282 : const gseg_picksplit_item *i2 = (const gseg_picksplit_item *) b;
306 : :
307 [ + + ]: 29282 : if (i1->center < i2->center)
308 : 12557 : return -1;
309 [ + + ]: 16725 : else if (i1->center == i2->center)
310 : 5191 : return 0;
311 : : else
312 : 11534 : return 1;
313 : : }
314 : :
315 : : /*
316 : : * The GiST PickSplit method for segments
317 : : *
318 : : * We used to use Guttman's split algorithm here, but since the data is 1-D
319 : : * it's easier and more robust to just sort the segments by center-point and
320 : : * split at the middle.
321 : : */
322 : : Datum
3083 andres@anarazel.de 323 : 17 : gseg_picksplit(PG_FUNCTION_ARGS)
324 : : {
325 : 17 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
326 : 17 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
327 : : int i;
328 : : SEG *seg,
329 : : *seg_l,
330 : : *seg_r;
331 : : gseg_picksplit_item *sort_items;
332 : : OffsetNumber *left,
333 : : *right;
334 : : OffsetNumber maxoff;
335 : : OffsetNumber firstright;
336 : :
337 : : #ifdef GIST_DEBUG
338 : : fprintf(stderr, "picksplit\n");
339 : : #endif
340 : :
341 : : /* Valid items in entryvec->vector[] are indexed 1..maxoff */
5379 tgl@sss.pgh.pa.us 342 : 17 : maxoff = entryvec->n - 1;
343 : :
344 : : /*
345 : : * Prepare the auxiliary array and sort it.
346 : : */
347 : : sort_items = (gseg_picksplit_item *)
348 : 17 : palloc(maxoff * sizeof(gseg_picksplit_item));
349 [ + + ]: 4471 : for (i = 1; i <= maxoff; i++)
350 : : {
3083 andres@anarazel.de 351 : 4454 : seg = DatumGetSegP(entryvec->vector[i].key);
352 : : /* center calculation is done this way to avoid possible overflow */
5263 bruce@momjian.us 353 : 4454 : sort_items[i - 1].center = seg->lower * 0.5f + seg->upper * 0.5f;
5379 tgl@sss.pgh.pa.us 354 : 4454 : sort_items[i - 1].index = i;
355 : 4454 : sort_items[i - 1].data = seg;
356 : : }
357 : 17 : qsort(sort_items, maxoff, sizeof(gseg_picksplit_item),
358 : : gseg_picksplit_item_cmp);
359 : :
360 : : /* sort items below "firstright" will go into the left side */
361 : 17 : firstright = maxoff / 2;
362 : :
363 : 17 : v->spl_left = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
364 : 17 : v->spl_right = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
8934 bruce@momjian.us 365 : 17 : left = v->spl_left;
366 : 17 : v->spl_nleft = 0;
367 : 17 : right = v->spl_right;
368 : 17 : v->spl_nright = 0;
369 : :
370 : : /*
371 : : * Emit segments to the left output page, and compute its bounding box.
372 : : */
3083 andres@anarazel.de 373 : 17 : seg_l = (SEG *) palloc(sizeof(SEG));
374 : 17 : memcpy(seg_l, sort_items[0].data, sizeof(SEG));
5379 tgl@sss.pgh.pa.us 375 : 17 : *left++ = sort_items[0].index;
376 : 17 : v->spl_nleft++;
377 [ + + ]: 2227 : for (i = 1; i < firstright; i++)
378 : : {
3083 andres@anarazel.de 379 : 2210 : Datum sortitem = PointerGetDatum(sort_items[i].data);
380 : :
381 : 2210 : seg_l = DatumGetSegP(DirectFunctionCall2(seg_union,
382 : : PointerGetDatum(seg_l),
383 : : sortitem));
5379 tgl@sss.pgh.pa.us 384 : 2210 : *left++ = sort_items[i].index;
385 : 2210 : v->spl_nleft++;
386 : : }
387 : :
388 : : /*
389 : : * Likewise for the right page.
390 : : */
3083 andres@anarazel.de 391 : 17 : seg_r = (SEG *) palloc(sizeof(SEG));
392 : 17 : memcpy(seg_r, sort_items[firstright].data, sizeof(SEG));
5379 tgl@sss.pgh.pa.us 393 : 17 : *right++ = sort_items[firstright].index;
394 : 17 : v->spl_nright++;
395 [ + + ]: 2227 : for (i = firstright + 1; i < maxoff; i++)
396 : : {
3083 andres@anarazel.de 397 : 2210 : Datum sortitem = PointerGetDatum(sort_items[i].data);
398 : :
399 : 2210 : seg_r = DatumGetSegP(DirectFunctionCall2(seg_union,
400 : : PointerGetDatum(seg_r),
401 : : sortitem));
5379 tgl@sss.pgh.pa.us 402 : 2210 : *right++ = sort_items[i].index;
403 : 2210 : v->spl_nright++;
404 : : }
405 : :
3083 andres@anarazel.de 406 : 17 : v->spl_ldatum = PointerGetDatum(seg_l);
407 : 17 : v->spl_rdatum = PointerGetDatum(seg_r);
408 : :
409 : 17 : PG_RETURN_POINTER(v);
410 : : }
411 : :
412 : : /*
413 : : ** Equality methods
414 : : */
415 : : Datum
416 : 2315 : gseg_same(PG_FUNCTION_ARGS)
417 : : {
418 : 2315 : bool *result = (bool *) PG_GETARG_POINTER(2);
419 : :
29 peter@eisentraut.org 420 [ + + ]:GNC 2315 : if (DatumGetBool(DirectFunctionCall2(seg_same, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1))))
2943 peter_e@gmx.net 421 :CBC 2253 : *result = true;
422 : : else
423 : 62 : *result = false;
424 : :
425 : : #ifdef GIST_DEBUG
426 : : fprintf(stderr, "same: %s\n", (*result ? "TRUE" : "FALSE"));
427 : : #endif
428 : :
3083 andres@anarazel.de 429 : 2315 : PG_RETURN_POINTER(result);
430 : : }
431 : :
432 : : /*
433 : : ** SUPPORT ROUTINES
434 : : */
435 : : static Datum
436 : 4704 : gseg_leaf_consistent(Datum key, Datum query, StrategyNumber strategy)
437 : : {
438 : : Datum retval;
439 : :
440 : : #ifdef GIST_QUERY_DEBUG
441 : : fprintf(stderr, "leaf_consistent, %d\n", strategy);
442 : : #endif
443 : :
8934 bruce@momjian.us 444 [ - - - - : 4704 : switch (strategy)
- - + -
- ]
445 : : {
8934 bruce@momjian.us 446 :UBC 0 : case RTLeftStrategyNumber:
3083 andres@anarazel.de 447 : 0 : retval = DirectFunctionCall2(seg_left, key, query);
8934 bruce@momjian.us 448 : 0 : break;
449 : 0 : case RTOverLeftStrategyNumber:
3083 andres@anarazel.de 450 : 0 : retval = DirectFunctionCall2(seg_over_left, key, query);
8934 bruce@momjian.us 451 : 0 : break;
452 : 0 : case RTOverlapStrategyNumber:
3083 andres@anarazel.de 453 : 0 : retval = DirectFunctionCall2(seg_overlap, key, query);
8934 bruce@momjian.us 454 : 0 : break;
455 : 0 : case RTOverRightStrategyNumber:
3083 andres@anarazel.de 456 : 0 : retval = DirectFunctionCall2(seg_over_right, key, query);
8934 bruce@momjian.us 457 : 0 : break;
458 : 0 : case RTRightStrategyNumber:
3083 andres@anarazel.de 459 : 0 : retval = DirectFunctionCall2(seg_right, key, query);
8934 bruce@momjian.us 460 : 0 : break;
461 : 0 : case RTSameStrategyNumber:
3083 andres@anarazel.de 462 : 0 : retval = DirectFunctionCall2(seg_same, key, query);
8934 bruce@momjian.us 463 : 0 : break;
8934 bruce@momjian.us 464 :CBC 4704 : case RTContainsStrategyNumber:
465 : : case RTOldContainsStrategyNumber:
3083 andres@anarazel.de 466 : 4704 : retval = DirectFunctionCall2(seg_contains, key, query);
8934 bruce@momjian.us 467 : 4704 : break;
8934 bruce@momjian.us 468 :UBC 0 : case RTContainedByStrategyNumber:
469 : : case RTOldContainedByStrategyNumber:
3083 andres@anarazel.de 470 : 0 : retval = DirectFunctionCall2(seg_contained, key, query);
8934 bruce@momjian.us 471 : 0 : break;
472 : 0 : default:
29 peter@eisentraut.org 473 :UNC 0 : retval = BoolGetDatum(false);
474 : : }
475 : :
3083 andres@anarazel.de 476 :CBC 4704 : PG_RETURN_DATUM(retval);
477 : : }
478 : :
479 : : static Datum
480 : 72 : gseg_internal_consistent(Datum key, Datum query, StrategyNumber strategy)
481 : : {
482 : : bool retval;
483 : :
484 : : #ifdef GIST_QUERY_DEBUG
485 : : fprintf(stderr, "internal_consistent, %d\n", strategy);
486 : : #endif
487 : :
8934 bruce@momjian.us 488 [ - - - - : 72 : switch (strategy)
- + - - ]
489 : : {
8934 bruce@momjian.us 490 :UBC 0 : case RTLeftStrategyNumber:
3083 andres@anarazel.de 491 : 0 : retval =
492 : 0 : !DatumGetBool(DirectFunctionCall2(seg_over_right, key, query));
7376 tgl@sss.pgh.pa.us 493 : 0 : break;
8934 bruce@momjian.us 494 : 0 : case RTOverLeftStrategyNumber:
3083 andres@anarazel.de 495 : 0 : retval =
496 : 0 : !DatumGetBool(DirectFunctionCall2(seg_right, key, query));
8934 bruce@momjian.us 497 : 0 : break;
498 : 0 : case RTOverlapStrategyNumber:
499 : : retval =
3083 andres@anarazel.de 500 : 0 : DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
8934 bruce@momjian.us 501 : 0 : break;
502 : 0 : case RTOverRightStrategyNumber:
3083 andres@anarazel.de 503 : 0 : retval =
504 : 0 : !DatumGetBool(DirectFunctionCall2(seg_left, key, query));
7376 tgl@sss.pgh.pa.us 505 : 0 : break;
8934 bruce@momjian.us 506 : 0 : case RTRightStrategyNumber:
3083 andres@anarazel.de 507 : 0 : retval =
508 : 0 : !DatumGetBool(DirectFunctionCall2(seg_over_left, key, query));
8934 bruce@momjian.us 509 : 0 : break;
8934 bruce@momjian.us 510 :CBC 72 : case RTSameStrategyNumber:
511 : : case RTContainsStrategyNumber:
512 : : case RTOldContainsStrategyNumber:
513 : : retval =
3083 andres@anarazel.de 514 : 72 : DatumGetBool(DirectFunctionCall2(seg_contains, key, query));
8934 bruce@momjian.us 515 : 72 : break;
8934 bruce@momjian.us 516 :UBC 0 : case RTContainedByStrategyNumber:
517 : : case RTOldContainedByStrategyNumber:
518 : : retval =
3083 andres@anarazel.de 519 : 0 : DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
8934 bruce@momjian.us 520 : 0 : break;
521 : 0 : default:
2943 peter_e@gmx.net 522 : 0 : retval = false;
523 : : }
524 : :
3083 andres@anarazel.de 525 :CBC 72 : PG_RETURN_BOOL(retval);
526 : : }
527 : :
528 : : static Datum
529 : 2316 : gseg_binary_union(Datum r1, Datum r2, int *sizep)
530 : : {
531 : : Datum retval;
532 : :
533 : 2316 : retval = DirectFunctionCall2(seg_union, r1, r2);
8934 bruce@momjian.us 534 : 2316 : *sizep = sizeof(SEG);
535 : :
2942 peter_e@gmx.net 536 : 2316 : return retval;
537 : : }
538 : :
539 : :
540 : : Datum
3083 andres@anarazel.de 541 : 4791 : seg_contains(PG_FUNCTION_ARGS)
542 : : {
543 : 4791 : SEG *a = PG_GETARG_SEG_P(0);
544 : 4791 : SEG *b = PG_GETARG_SEG_P(1);
545 : :
546 [ + + + + ]: 4791 : PG_RETURN_BOOL((a->lower <= b->lower) && (a->upper >= b->upper));
547 : : }
548 : :
549 : : Datum
550 : 14 : seg_contained(PG_FUNCTION_ARGS)
551 : : {
552 : 14 : Datum a = PG_GETARG_DATUM(0);
553 : 14 : Datum b = PG_GETARG_DATUM(1);
554 : :
555 : 14 : PG_RETURN_DATUM(DirectFunctionCall2(seg_contains, b, a));
556 : : }
557 : :
558 : : /*****************************************************************************
559 : : * Operator class for R-tree indexing
560 : : *****************************************************************************/
561 : :
562 : : Datum
563 : 2459 : seg_same(PG_FUNCTION_ARGS)
564 : : {
2046 alvherre@alvh.no-ip. 565 : 2459 : int cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
566 : : PG_GETARG_DATUM(0),
567 : : PG_GETARG_DATUM(1)));
568 : :
3083 andres@anarazel.de 569 : 2459 : PG_RETURN_BOOL(cmp == 0);
570 : : }
571 : :
572 : : /* seg_overlap -- does a overlap b?
573 : : */
574 : : Datum
575 : 15 : seg_overlap(PG_FUNCTION_ARGS)
576 : : {
577 : 15 : SEG *a = PG_GETARG_SEG_P(0);
578 : 15 : SEG *b = PG_GETARG_SEG_P(1);
579 : :
580 [ + + + + : 15 : PG_RETURN_BOOL(((a->upper >= b->upper) && (a->lower <= b->upper)) ||
+ + + + ]
581 : : ((b->upper >= a->upper) && (b->lower <= a->upper)));
582 : : }
583 : :
584 : : /* seg_over_left -- is the right edge of (a) located at or left of the right edge of (b)?
585 : : */
586 : : Datum
587 : 11 : seg_over_left(PG_FUNCTION_ARGS)
588 : : {
589 : 11 : SEG *a = PG_GETARG_SEG_P(0);
590 : 11 : SEG *b = PG_GETARG_SEG_P(1);
591 : :
592 : 11 : PG_RETURN_BOOL(a->upper <= b->upper);
593 : : }
594 : :
595 : : /* seg_left -- is (a) entirely on the left of (b)?
596 : : */
597 : : Datum
598 : 11 : seg_left(PG_FUNCTION_ARGS)
599 : : {
600 : 11 : SEG *a = PG_GETARG_SEG_P(0);
601 : 11 : SEG *b = PG_GETARG_SEG_P(1);
602 : :
603 : 11 : PG_RETURN_BOOL(a->upper < b->lower);
604 : : }
605 : :
606 : : /* seg_right -- is (a) entirely on the right of (b)?
607 : : */
608 : : Datum
609 : 11 : seg_right(PG_FUNCTION_ARGS)
610 : : {
611 : 11 : SEG *a = PG_GETARG_SEG_P(0);
612 : 11 : SEG *b = PG_GETARG_SEG_P(1);
613 : :
614 : 11 : PG_RETURN_BOOL(a->lower > b->upper);
615 : : }
616 : :
617 : : /* seg_over_right -- is the left edge of (a) located at or right of the left edge of (b)?
618 : : */
619 : : Datum
620 : 11 : seg_over_right(PG_FUNCTION_ARGS)
621 : : {
622 : 11 : SEG *a = PG_GETARG_SEG_P(0);
623 : 11 : SEG *b = PG_GETARG_SEG_P(1);
624 : :
625 : 11 : PG_RETURN_BOOL(a->lower >= b->lower);
626 : : }
627 : :
628 : : Datum
629 : 12103 : seg_union(PG_FUNCTION_ARGS)
630 : : {
631 : 12103 : SEG *a = PG_GETARG_SEG_P(0);
632 : 12103 : SEG *b = PG_GETARG_SEG_P(1);
633 : : SEG *n;
634 : :
8934 bruce@momjian.us 635 : 12103 : n = (SEG *) palloc(sizeof(*n));
636 : :
637 : : /* take max of upper endpoints */
638 [ + + ]: 12103 : if (a->upper > b->upper)
639 : : {
640 : 10394 : n->upper = a->upper;
641 : 10394 : n->u_sigd = a->u_sigd;
642 : 10394 : n->u_ext = a->u_ext;
643 : : }
644 : : else
645 : : {
646 : 1709 : n->upper = b->upper;
647 : 1709 : n->u_sigd = b->u_sigd;
648 : 1709 : n->u_ext = b->u_ext;
649 : : }
650 : :
651 : : /* take min of lower endpoints */
652 [ + + ]: 12103 : if (a->lower < b->lower)
653 : : {
654 : 11800 : n->lower = a->lower;
655 : 11800 : n->l_sigd = a->l_sigd;
656 : 11800 : n->l_ext = a->l_ext;
657 : : }
658 : : else
659 : : {
660 : 303 : n->lower = b->lower;
661 : 303 : n->l_sigd = b->l_sigd;
662 : 303 : n->l_ext = b->l_ext;
663 : : }
664 : :
3083 andres@anarazel.de 665 : 12103 : PG_RETURN_POINTER(n);
666 : : }
667 : :
668 : : Datum
3083 andres@anarazel.de 669 :UBC 0 : seg_inter(PG_FUNCTION_ARGS)
670 : : {
671 : 0 : SEG *a = PG_GETARG_SEG_P(0);
672 : 0 : SEG *b = PG_GETARG_SEG_P(1);
673 : : SEG *n;
674 : :
8934 bruce@momjian.us 675 : 0 : n = (SEG *) palloc(sizeof(*n));
676 : :
677 : : /* take min of upper endpoints */
678 [ # # ]: 0 : if (a->upper < b->upper)
679 : : {
680 : 0 : n->upper = a->upper;
681 : 0 : n->u_sigd = a->u_sigd;
682 : 0 : n->u_ext = a->u_ext;
683 : : }
684 : : else
685 : : {
686 : 0 : n->upper = b->upper;
687 : 0 : n->u_sigd = b->u_sigd;
688 : 0 : n->u_ext = b->u_ext;
689 : : }
690 : :
691 : : /* take max of lower endpoints */
692 [ # # ]: 0 : if (a->lower > b->lower)
693 : : {
694 : 0 : n->lower = a->lower;
695 : 0 : n->l_sigd = a->l_sigd;
696 : 0 : n->l_ext = a->l_ext;
697 : : }
698 : : else
699 : : {
700 : 0 : n->lower = b->lower;
701 : 0 : n->l_sigd = b->l_sigd;
702 : 0 : n->l_ext = b->l_ext;
703 : : }
704 : :
3083 andres@anarazel.de 705 : 0 : PG_RETURN_POINTER(n);
706 : : }
707 : :
708 : : static void
5931 bruce@momjian.us 709 :CBC 10734 : rt_seg_size(SEG *a, float *size)
710 : : {
8934 711 [ + - - + ]: 10734 : if (a == (SEG *) NULL || a->upper <= a->lower)
8934 bruce@momjian.us 712 :UBC 0 : *size = 0.0;
713 : : else
1064 peter@eisentraut.org 714 :CBC 10734 : *size = fabsf(a->upper - a->lower);
9035 tgl@sss.pgh.pa.us 715 : 10734 : }
716 : :
717 : : Datum
6349 tgl@sss.pgh.pa.us 718 :UBC 0 : seg_size(PG_FUNCTION_ARGS)
719 : : {
3083 andres@anarazel.de 720 : 0 : SEG *seg = PG_GETARG_SEG_P(0);
721 : :
1064 peter@eisentraut.org 722 : 0 : PG_RETURN_FLOAT4(fabsf(seg->upper - seg->lower));
723 : : }
724 : :
725 : :
726 : : /*****************************************************************************
727 : : * Miscellaneous operators
728 : : *****************************************************************************/
729 : : Datum
3083 andres@anarazel.de 730 :CBC 4433 : seg_cmp(PG_FUNCTION_ARGS)
731 : : {
732 : 4433 : SEG *a = PG_GETARG_SEG_P(0);
733 : 4433 : SEG *b = PG_GETARG_SEG_P(1);
734 : :
735 : : /*
736 : : * First compare on lower boundary position
737 : : */
8934 bruce@momjian.us 738 [ + + ]: 4433 : if (a->lower < b->lower)
3083 andres@anarazel.de 739 : 904 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 740 [ + + ]: 3529 : if (a->lower > b->lower)
3083 andres@anarazel.de 741 : 801 : PG_RETURN_INT32(1);
742 : :
743 : : /*
744 : : * a->lower == b->lower, so consider type of boundary.
745 : : *
746 : : * A '-' lower bound is < any other kind (this could only be relevant if
747 : : * -HUGE_VAL is used as a regular data value). A '<' lower bound is < any
748 : : * other kind except '-'. A '>' lower bound is > any other kind.
749 : : */
8934 bruce@momjian.us 750 [ + + ]: 2728 : if (a->l_ext != b->l_ext)
751 : : {
752 [ - + ]: 66 : if (a->l_ext == '-')
3083 andres@anarazel.de 753 :UBC 0 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 754 [ - + ]:CBC 66 : if (b->l_ext == '-')
3083 andres@anarazel.de 755 :UBC 0 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 756 [ + + ]:CBC 66 : if (a->l_ext == '<')
3083 andres@anarazel.de 757 : 26 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 758 [ + + ]: 40 : if (b->l_ext == '<')
3083 andres@anarazel.de 759 : 21 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 760 [ + + ]: 19 : if (a->l_ext == '>')
3083 andres@anarazel.de 761 : 13 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 762 [ + - ]: 6 : if (b->l_ext == '>')
3083 andres@anarazel.de 763 : 6 : PG_RETURN_INT32(-1);
764 : : }
765 : :
766 : : /*
767 : : * For other boundary types, consider # of significant digits first.
768 : : */
7266 bruce@momjian.us 769 [ + + ]: 2662 : if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include (b) */
3083 andres@anarazel.de 770 : 18 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 771 [ + + ]: 2644 : if (a->l_sigd > b->l_sigd) /* (a) is less blurred and is likely to be
772 : : * included in (b) */
3083 andres@anarazel.de 773 : 18 : PG_RETURN_INT32(1);
774 : :
775 : : /*
776 : : * For same # of digits, an approximate boundary is more blurred than
777 : : * exact.
778 : : */
8934 bruce@momjian.us 779 [ - + ]: 2626 : if (a->l_ext != b->l_ext)
780 : : {
8934 bruce@momjian.us 781 [ # # ]:UBC 0 : if (a->l_ext == '~') /* (a) is approximate, while (b) is exact */
3083 andres@anarazel.de 782 : 0 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 783 [ # # ]: 0 : if (b->l_ext == '~')
3083 andres@anarazel.de 784 : 0 : PG_RETURN_INT32(1);
785 : : /* can't get here unless data is corrupt */
8080 tgl@sss.pgh.pa.us 786 [ # # ]: 0 : elog(ERROR, "bogus lower boundary types %d %d",
787 : : (int) a->l_ext, (int) b->l_ext);
788 : : }
789 : :
790 : : /* at this point, the lower boundaries are identical */
791 : :
792 : : /*
793 : : * First compare on upper boundary position
794 : : */
8934 bruce@momjian.us 795 [ + + ]:CBC 2626 : if (a->upper < b->upper)
3083 andres@anarazel.de 796 : 163 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 797 [ + + ]: 2463 : if (a->upper > b->upper)
3083 andres@anarazel.de 798 : 126 : PG_RETURN_INT32(1);
799 : :
800 : : /*
801 : : * a->upper == b->upper, so consider type of boundary.
802 : : *
803 : : * A '-' upper bound is > any other kind (this could only be relevant if
804 : : * HUGE_VAL is used as a regular data value). A '<' upper bound is < any
805 : : * other kind. A '>' upper bound is > any other kind except '-'.
806 : : */
8934 bruce@momjian.us 807 [ + + ]: 2337 : if (a->u_ext != b->u_ext)
808 : : {
809 [ - + ]: 63 : if (a->u_ext == '-')
3083 andres@anarazel.de 810 :UBC 0 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 811 [ - + ]:CBC 63 : if (b->u_ext == '-')
3083 andres@anarazel.de 812 :UBC 0 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 813 [ + + ]:CBC 63 : if (a->u_ext == '<')
3083 andres@anarazel.de 814 : 6 : PG_RETURN_INT32(-1);
8934 bruce@momjian.us 815 [ + + ]: 57 : if (b->u_ext == '<')
3083 andres@anarazel.de 816 : 3 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 817 [ + + ]: 54 : if (a->u_ext == '>')
3083 andres@anarazel.de 818 : 29 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 819 [ + - ]: 25 : if (b->u_ext == '>')
3083 andres@anarazel.de 820 : 25 : PG_RETURN_INT32(-1);
821 : : }
822 : :
823 : : /*
824 : : * For other boundary types, consider # of significant digits first. Note
825 : : * result here is converse of the lower-boundary case.
826 : : */
7266 bruce@momjian.us 827 [ + + ]: 2274 : if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include (b) */
3083 andres@anarazel.de 828 : 9 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 829 [ + + ]: 2265 : if (a->u_sigd > b->u_sigd) /* (a) is less blurred and is likely to be
830 : : * included in (b) */
3083 andres@anarazel.de 831 : 10 : PG_RETURN_INT32(-1);
832 : :
833 : : /*
834 : : * For same # of digits, an approximate boundary is more blurred than
835 : : * exact. Again, result is converse of lower-boundary case.
836 : : */
8934 bruce@momjian.us 837 [ - + ]: 2255 : if (a->u_ext != b->u_ext)
838 : : {
8934 bruce@momjian.us 839 [ # # ]:UBC 0 : if (a->u_ext == '~') /* (a) is approximate, while (b) is exact */
3083 andres@anarazel.de 840 : 0 : PG_RETURN_INT32(1);
8934 bruce@momjian.us 841 [ # # ]: 0 : if (b->u_ext == '~')
3083 andres@anarazel.de 842 : 0 : PG_RETURN_INT32(-1);
843 : : /* can't get here unless data is corrupt */
8080 tgl@sss.pgh.pa.us 844 [ # # ]: 0 : elog(ERROR, "bogus upper boundary types %d %d",
845 : : (int) a->u_ext, (int) b->u_ext);
846 : : }
847 : :
3083 andres@anarazel.de 848 :CBC 2255 : PG_RETURN_INT32(0);
849 : : }
850 : :
851 : : Datum
3083 andres@anarazel.de 852 :UBC 0 : seg_lt(PG_FUNCTION_ARGS)
853 : : {
2046 alvherre@alvh.no-ip. 854 : 0 : int cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
855 : : PG_GETARG_DATUM(0),
856 : : PG_GETARG_DATUM(1)));
857 : :
3083 andres@anarazel.de 858 : 0 : PG_RETURN_BOOL(cmp < 0);
859 : : }
860 : :
861 : : Datum
862 : 0 : seg_le(PG_FUNCTION_ARGS)
863 : : {
2046 alvherre@alvh.no-ip. 864 : 0 : int cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
865 : : PG_GETARG_DATUM(0),
866 : : PG_GETARG_DATUM(1)));
867 : :
3083 andres@anarazel.de 868 : 0 : PG_RETURN_BOOL(cmp <= 0);
869 : : }
870 : :
871 : : Datum
872 : 0 : seg_gt(PG_FUNCTION_ARGS)
873 : : {
2046 alvherre@alvh.no-ip. 874 : 0 : int cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
875 : : PG_GETARG_DATUM(0),
876 : : PG_GETARG_DATUM(1)));
877 : :
3083 andres@anarazel.de 878 : 0 : PG_RETURN_BOOL(cmp > 0);
879 : : }
880 : :
881 : : Datum
882 : 0 : seg_ge(PG_FUNCTION_ARGS)
883 : : {
2046 alvherre@alvh.no-ip. 884 : 0 : int cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
885 : : PG_GETARG_DATUM(0),
886 : : PG_GETARG_DATUM(1)));
887 : :
3083 andres@anarazel.de 888 : 0 : PG_RETURN_BOOL(cmp >= 0);
889 : : }
890 : :
891 : :
892 : : Datum
3083 andres@anarazel.de 893 :CBC 2 : seg_different(PG_FUNCTION_ARGS)
894 : : {
2046 alvherre@alvh.no-ip. 895 : 2 : int cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
896 : : PG_GETARG_DATUM(0),
897 : : PG_GETARG_DATUM(1)));
898 : :
3083 andres@anarazel.de 899 : 2 : PG_RETURN_BOOL(cmp != 0);
900 : : }
901 : :
902 : :
903 : :
904 : : /*****************************************************************************
905 : : * Auxiliary functions
906 : : *****************************************************************************/
907 : :
908 : : /*
909 : : * The purpose of this routine is to print the given floating point
910 : : * value with exactly n significant digits. Its behaviour
911 : : * is similar to %.ng except it prints 8.00 where %.ng would
912 : : * print 8. Returns the length of the string written at "result".
913 : : *
914 : : * Caller must provide a sufficiently large result buffer; 16 bytes
915 : : * should be enough for all known float implementations.
916 : : */
917 : : static int
8934 bruce@momjian.us 918 : 325 : restore(char *result, float val, int n)
919 : : {
920 : 325 : char buf[25] = {
921 : : '0', '0', '0', '0', '0',
922 : : '0', '0', '0', '0', '0',
923 : : '0', '0', '0', '0', '0',
924 : : '0', '0', '0', '0', '0',
925 : : '0', '0', '0', '0', '\0'
926 : : };
927 : : char *p;
928 : : int exp;
929 : : int i,
930 : : dp,
931 : : sign;
932 : :
933 : : /*
934 : : * Put a cap on the number of significant digits to avoid garbage in the
935 : : * output and ensure we don't overrun the result buffer. (n should not be
936 : : * negative, but check to protect ourselves against corrupted data.)
937 : : */
990 tgl@sss.pgh.pa.us 938 [ - + ]: 325 : if (n <= 0)
990 tgl@sss.pgh.pa.us 939 :UBC 0 : n = FLT_DIG;
940 : : else
990 tgl@sss.pgh.pa.us 941 :CBC 325 : n = Min(n, FLT_DIG);
942 : :
943 : : /* remember the sign */
8934 bruce@momjian.us 944 : 325 : sign = (val < 0 ? 1 : 0);
945 : :
946 : : /* print, in %e style to start with */
3443 tgl@sss.pgh.pa.us 947 : 325 : sprintf(result, "%.*e", n - 1, val);
948 : :
949 : : /* find the exponent */
950 : 325 : p = strchr(result, 'e');
951 : :
952 : : /* punt if we have 'inf' or similar */
953 [ - + ]: 325 : if (p == NULL)
3443 tgl@sss.pgh.pa.us 954 :UBC 0 : return strlen(result);
955 : :
3443 tgl@sss.pgh.pa.us 956 :CBC 325 : exp = atoi(p + 1);
8934 bruce@momjian.us 957 [ + + ]: 325 : if (exp == 0)
958 : : {
959 : : /* just truncate off the 'e+00' */
3443 tgl@sss.pgh.pa.us 960 : 169 : *p = '\0';
961 : : }
962 : : else
963 : : {
1065 peter@eisentraut.org 964 [ + + + + ]: 156 : if (abs(exp) <= 4)
965 : : {
966 : : /*
967 : : * remove the decimal point from the mantissa and write the digits
968 : : * to the buf array
969 : : */
8934 bruce@momjian.us 970 [ + + ]: 637 : for (p = result + sign, i = 10, dp = 0; *p != 'e'; p++, i++)
971 : : {
972 : 499 : buf[i] = *p;
973 [ + + ]: 499 : if (*p == '.')
974 : : {
975 : 130 : dp = i--; /* skip the decimal point */
976 : : }
977 : : }
978 [ + + ]: 138 : if (dp == 0)
979 : 8 : dp = i--; /* no decimal point was found in the above
980 : : * for() loop */
981 : :
982 [ + + ]: 138 : if (exp > 0)
983 : : {
984 [ + + ]: 133 : if (dp - 10 + exp >= n)
985 : : {
986 : : /*
987 : : * the decimal point is behind the last significant digit;
988 : : * the digits in between must be converted to the exponent
989 : : * and the decimal point placed after the first digit
990 : : */
991 : 43 : exp = dp - 10 + exp - n;
992 : 43 : buf[10 + n] = '\0';
993 : :
994 : : /* insert the decimal point */
995 [ + + ]: 43 : if (n > 1)
996 : : {
997 : 39 : dp = 11;
998 [ + + ]: 507 : for (i = 23; i > dp; i--)
999 : 468 : buf[i] = buf[i - 1];
1000 : 39 : buf[dp] = '.';
1001 : : }
1002 : :
1003 : : /*
1004 : : * adjust the exponent by the number of digits after the
1005 : : * decimal point
1006 : : */
1007 [ + + ]: 43 : if (n > 1)
1008 : 39 : sprintf(&buf[11 + n], "e%d", exp + n - 1);
1009 : : else
1010 : 4 : sprintf(&buf[11], "e%d", exp + n - 1);
1011 : :
1012 [ - + ]: 43 : if (sign)
1013 : : {
8934 bruce@momjian.us 1014 :UBC 0 : buf[9] = '-';
1015 : 0 : strcpy(result, &buf[9]);
1016 : : }
1017 : : else
8934 bruce@momjian.us 1018 :CBC 43 : strcpy(result, &buf[10]);
1019 : : }
1020 : : else
1021 : : { /* insert the decimal point */
1022 : 90 : dp += exp;
1023 [ + + ]: 1080 : for (i = 23; i > dp; i--)
1024 : 990 : buf[i] = buf[i - 1];
1025 : 90 : buf[11 + n] = '\0';
1026 : 90 : buf[dp] = '.';
1027 [ - + ]: 90 : if (sign)
1028 : : {
8934 bruce@momjian.us 1029 :UBC 0 : buf[9] = '-';
1030 : 0 : strcpy(result, &buf[9]);
1031 : : }
1032 : : else
8934 bruce@momjian.us 1033 :CBC 90 : strcpy(result, &buf[10]);
1034 : : }
1035 : : }
1036 : : else
1037 : : { /* exp <= 0 */
1038 : 5 : dp += exp - 1;
1039 : 5 : buf[10 + n] = '\0';
1040 : 5 : buf[dp] = '.';
1041 [ - + ]: 5 : if (sign)
1042 : : {
8934 bruce@momjian.us 1043 :UBC 0 : buf[dp - 2] = '-';
1044 : 0 : strcpy(result, &buf[dp - 2]);
1045 : : }
1046 : : else
8934 bruce@momjian.us 1047 :CBC 5 : strcpy(result, &buf[dp - 1]);
1048 : : }
1049 : : }
1050 : :
1051 : : /* do nothing for abs(exp) > 4; %e must be OK */
1052 : : /* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
1053 : :
1054 : : /* ... this is not done yet. */
1055 : : }
2942 peter_e@gmx.net 1056 : 325 : return strlen(result);
1057 : : }
1058 : :
1059 : :
1060 : : /*
1061 : : ** Miscellany
1062 : : */
1063 : :
1064 : : /* find out the number of significant digits in a string representing
1065 : : * a floating point number
1066 : : */
1067 : : int
2867 1068 : 5191 : significant_digits(const char *s)
1069 : : {
1070 : 5191 : const char *p = s;
1071 : : int n,
1072 : : c,
1073 : : zeroes;
1074 : :
8934 bruce@momjian.us 1075 : 5191 : zeroes = 1;
1076 : : /* skip leading zeroes and sign */
1077 [ + + + - : 5330 : for (c = *p; (c == '0' || c == '+' || c == '-') && c != 0; c = *(++p));
+ + + - ]
1078 : :
1079 : : /* skip decimal point and following zeroes */
1080 [ + + + + : 5212 : for (c = *p; (c == '0' || c == '.') && c != 0; c = *(++p))
+ - ]
1081 : : {
1082 [ + + ]: 21 : if (c != '.')
1083 : 11 : zeroes++;
1084 : : }
1085 : :
1086 : : /* count significant digits (n) */
1087 [ + + ]: 20353 : for (c = *p, n = 0; c != 0; c = *(++p))
1088 : : {
1089 [ + + + + : 15189 : if (!((c >= '0' && c <= '9') || (c == '.')))
+ + ]
1090 : 27 : break;
1091 [ + + ]: 15162 : if (c != '.')
1092 : 10617 : n++;
1093 : : }
1094 : :
1095 [ + + ]: 5191 : if (!n)
2942 peter_e@gmx.net 1096 : 98 : return zeroes;
1097 : :
1098 : 5093 : return n;
1099 : : }
|