PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
lwgeom_btree.c
Go to the documentation of this file.
1/**********************************************************************
2 *
3 * PostGIS - Spatial Types for PostgreSQL
4 * http://postgis.net
5 *
6 * PostGIS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * PostGIS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18 *
19 **********************************************************************
20 *
21 * Copyright (C) 2010 Olivier Courtin <olivier.courtin@oslandia.com>
22 * Copyright (C) 2010 Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
23 * Copyright (C) 2009-2011 Paul Ramsey <pramsey@cleverelephant.ca>
24 *
25 **********************************************************************/
26
27
28#include "postgres.h"
29#include "fmgr.h"
30#include "access/hash.h"
31#include "utils/geo_decls.h"
32#include "utils/sortsupport.h" /* SortSupport */
33
34#include "../postgis_config.h"
35#include "liblwgeom.h"
36#include "lwgeom_pg.h"
37
38#include <math.h>
39#include <float.h>
40#include <string.h>
41#include <stdio.h>
42
43Datum lwgeom_lt(PG_FUNCTION_ARGS);
44Datum lwgeom_le(PG_FUNCTION_ARGS);
45Datum lwgeom_eq(PG_FUNCTION_ARGS);
46Datum lwgeom_ge(PG_FUNCTION_ARGS);
47Datum lwgeom_gt(PG_FUNCTION_ARGS);
48Datum lwgeom_cmp(PG_FUNCTION_ARGS);
49
51Datum lwgeom_lt(PG_FUNCTION_ARGS)
52{
53 GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
54 GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
55 int cmp = gserialized_cmp(g1, g2);
56 PG_FREE_IF_COPY(g1, 0);
57 PG_FREE_IF_COPY(g2, 1);
58 if (cmp < 0)
59 PG_RETURN_BOOL(true);
60 else
61 PG_RETURN_BOOL(false);
62}
63
65Datum lwgeom_le(PG_FUNCTION_ARGS)
66{
67 GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
68 GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
69 int cmp = gserialized_cmp(g1, g2);
70 PG_FREE_IF_COPY(g1, 0);
71 PG_FREE_IF_COPY(g2, 1);
72 if (cmp <= 0)
73 PG_RETURN_BOOL(true);
74 else
75 PG_RETURN_BOOL(false);
76}
77
79Datum lwgeom_eq(PG_FUNCTION_ARGS)
80{
81 GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
82 GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
83 int cmp = gserialized_cmp(g1, g2);
84 PG_FREE_IF_COPY(g1, 0);
85 PG_FREE_IF_COPY(g2, 1);
86 if (cmp == 0)
87 PG_RETURN_BOOL(true);
88 else
89 PG_RETURN_BOOL(false);
90}
91
93Datum lwgeom_ge(PG_FUNCTION_ARGS)
94{
95 GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
96 GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
97 int cmp = gserialized_cmp(g1, g2);
98 PG_FREE_IF_COPY(g1, 0);
99 PG_FREE_IF_COPY(g2, 1);
100 if (cmp >= 0)
101 PG_RETURN_BOOL(true);
102 else
103 PG_RETURN_BOOL(false);
104}
105
107Datum lwgeom_gt(PG_FUNCTION_ARGS)
108{
109 GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
110 GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
111 int cmp = gserialized_cmp(g1, g2);
112 PG_FREE_IF_COPY(g1, 0);
113 PG_FREE_IF_COPY(g2, 1);
114 if (cmp > 0)
115 PG_RETURN_BOOL(true);
116 else
117 PG_RETURN_BOOL(false);
118}
119
121Datum lwgeom_cmp(PG_FUNCTION_ARGS)
122{
123 GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
124 GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
125 int ret = gserialized_cmp(g1, g2);
126 PG_FREE_IF_COPY(g1, 0);
127 PG_FREE_IF_COPY(g2, 1);
128 PG_RETURN_INT32(ret);
129}
130
132Datum lwgeom_hash(PG_FUNCTION_ARGS)
133{
134 GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
135
136 int32_t hval = gserialized_hash(g1);
137 PG_FREE_IF_COPY(g1, 0);
138 PG_RETURN_INT32(hval);
139}
140
141static int
142lwgeom_cmp_abbrev(Datum x, Datum y, SortSupport ssup)
143{
144 /* Empty is a special case */
145 if (x == 0 || y == 0 || x == y)
146 return 0; /* 0 means "ask bigger comparator" and not equality*/
147 else if (x > y)
148 return 1;
149 else
150 return -1;
151}
152
153static int
154lwgeom_cmp_full(Datum x, Datum y, SortSupport ssup)
155{
156 GSERIALIZED *g1 = (GSERIALIZED *)PG_DETOAST_DATUM(x);
157 GSERIALIZED *g2 = (GSERIALIZED *)PG_DETOAST_DATUM(y);
158 int ret = gserialized_cmp(g1, g2);
161 return ret;
162}
163
164static bool
165lwgeom_abbrev_abort(int memtupcount, SortSupport ssup)
166{
167 return LW_FALSE;
168}
169
170static Datum
171lwgeom_abbrev_convert(Datum original, SortSupport ssup)
172{
173 GSERIALIZED *g = (GSERIALIZED *)PG_DETOAST_DATUM(original);
174 uint64_t hash = gserialized_get_sortable_hash(g);
175 POSTGIS_FREE_IF_COPY_P(g, original);
176 return hash;
177}
178
179/*
180 * Sort support strategy routine
181 */
183Datum lwgeom_sortsupport(PG_FUNCTION_ARGS)
184{
185 SortSupport ssup = (SortSupport)PG_GETARG_POINTER(0);
186
187 ssup->comparator = lwgeom_cmp_full;
188 ssup->ssup_extra = NULL;
189 /* Enable sortsupport only on 64 bit Datum */
190 if (ssup->abbreviate && sizeof(Datum) == 8)
191 {
192 ssup->comparator = lwgeom_cmp_abbrev;
193 ssup->abbrev_converter = lwgeom_abbrev_convert;
194 ssup->abbrev_abort = lwgeom_abbrev_abort;
195 ssup->abbrev_full_comparator = lwgeom_cmp_full;
196 }
197
198 PG_RETURN_VOID();
199}
uint64_t gserialized_get_sortable_hash(const GSERIALIZED *g)
Return a sortable key based on gserialized.
int32_t gserialized_hash(const GSERIALIZED *g)
Returns a hash code for the srid/type/geometry information in the GSERIALIZED.
int gserialized_cmp(const GSERIALIZED *g1, const GSERIALIZED *g2)
Return -1 if g1 is "less than" g2, 1 if g1 is "greater than" g2 and 0 if g1 and g2 are the "same".
#define LW_FALSE
Definition liblwgeom.h:108
This library is the generic geometry handling section of PostGIS.
Datum lwgeom_sortsupport(PG_FUNCTION_ARGS)
Datum lwgeom_lt(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(lwgeom_lt)
Datum lwgeom_cmp(PG_FUNCTION_ARGS)
Datum lwgeom_ge(PG_FUNCTION_ARGS)
Datum lwgeom_gt(PG_FUNCTION_ARGS)
static bool lwgeom_abbrev_abort(int memtupcount, SortSupport ssup)
static int lwgeom_cmp_abbrev(Datum x, Datum y, SortSupport ssup)
Datum lwgeom_eq(PG_FUNCTION_ARGS)
Datum lwgeom_le(PG_FUNCTION_ARGS)
static Datum lwgeom_abbrev_convert(Datum original, SortSupport ssup)
static int lwgeom_cmp_full(Datum x, Datum y, SortSupport ssup)
Datum lwgeom_hash(PG_FUNCTION_ARGS)
#define POSTGIS_FREE_IF_COPY_P(ptrsrc, ptrori)
Definition lwinline.h:235