PostGIS  2.4.9dev-r@@SVN_REVISION@@
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 "utils/geo_decls.h"
31 
32 #include "../postgis_config.h"
33 #include "liblwgeom.h"
34 #include "lwgeom_pg.h"
35 
36 #include <math.h>
37 #include <float.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <errno.h>
41 
42 Datum lwgeom_lt(PG_FUNCTION_ARGS);
43 Datum lwgeom_le(PG_FUNCTION_ARGS);
44 Datum lwgeom_eq(PG_FUNCTION_ARGS);
45 Datum lwgeom_ge(PG_FUNCTION_ARGS);
46 Datum lwgeom_gt(PG_FUNCTION_ARGS);
47 Datum lwgeom_cmp(PG_FUNCTION_ARGS);
48 
50 Datum lwgeom_lt(PG_FUNCTION_ARGS)
51 {
52  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
53  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
54  int cmp = gserialized_cmp(g1, g2);
55  PG_FREE_IF_COPY(g1, 0);
56  PG_FREE_IF_COPY(g2, 1);
57  if (cmp < 0)
58  PG_RETURN_BOOL(TRUE);
59  else
60  PG_RETURN_BOOL(FALSE);
61 }
62 
64 Datum lwgeom_le(PG_FUNCTION_ARGS)
65 {
66  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
67  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
68  int cmp = gserialized_cmp(g1, g2);
69  PG_FREE_IF_COPY(g1, 0);
70  PG_FREE_IF_COPY(g2, 1);
71  if (cmp <= 0)
72  PG_RETURN_BOOL(TRUE);
73  else
74  PG_RETURN_BOOL(FALSE);
75 }
76 
78 Datum lwgeom_eq(PG_FUNCTION_ARGS)
79 {
80  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
81  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
82  int cmp = gserialized_cmp(g1, g2);
83  PG_FREE_IF_COPY(g1, 0);
84  PG_FREE_IF_COPY(g2, 1);
85  if (cmp == 0)
86  PG_RETURN_BOOL(TRUE);
87  else
88  PG_RETURN_BOOL(FALSE);
89 }
90 
92 Datum lwgeom_ge(PG_FUNCTION_ARGS)
93 {
94  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
95  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
96  int cmp = gserialized_cmp(g1, g2);
97  PG_FREE_IF_COPY(g1, 0);
98  PG_FREE_IF_COPY(g2, 1);
99  if (cmp >= 0)
100  PG_RETURN_BOOL(TRUE);
101  else
102  PG_RETURN_BOOL(FALSE);
103 }
104 
106 Datum lwgeom_gt(PG_FUNCTION_ARGS)
107 {
108  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
109  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
110  int cmp = gserialized_cmp(g1, g2);
111  PG_FREE_IF_COPY(g1, 0);
112  PG_FREE_IF_COPY(g2, 1);
113  if (cmp > 0)
114  PG_RETURN_BOOL(TRUE);
115  else
116  PG_RETURN_BOOL(FALSE);
117 }
118 
120 Datum lwgeom_cmp(PG_FUNCTION_ARGS)
121 {
122  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
123  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
124  int ret = gserialized_cmp(g1, g2);
125  PG_FREE_IF_COPY(g1, 0);
126  PG_FREE_IF_COPY(g2, 1);
127  PG_RETURN_INT32(ret);
128 }
129 
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"...
Definition: g_serialized.c:294
Datum lwgeom_eq(PG_FUNCTION_ARGS)
Definition: lwgeom_btree.c:78
Datum lwgeom_le(PG_FUNCTION_ARGS)
Definition: lwgeom_btree.c:64
Datum lwgeom_lt(PG_FUNCTION_ARGS)
Definition: lwgeom_btree.c:50
Datum lwgeom_cmp(PG_FUNCTION_ARGS)
Definition: lwgeom_btree.c:120
Datum lwgeom_gt(PG_FUNCTION_ARGS)
Definition: lwgeom_btree.c:106
#define FALSE
Definition: dbfopen.c:168
PG_FUNCTION_INFO_V1(lwgeom_lt)
#define TRUE
Definition: dbfopen.c:169
Datum lwgeom_ge(PG_FUNCTION_ARGS)
Definition: lwgeom_btree.c:92
This library is the generic geometry handling section of PostGIS.