2 * contrib/btree_gist/btree_float4.c
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
9 typedef struct float4key
18 PG_FUNCTION_INFO_V1(gbt_float4_compress);
19 PG_FUNCTION_INFO_V1(gbt_float4_fetch);
20 PG_FUNCTION_INFO_V1(gbt_float4_union);
21 PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
22 PG_FUNCTION_INFO_V1(gbt_float4_consistent);
23 PG_FUNCTION_INFO_V1(gbt_float4_distance);
24 PG_FUNCTION_INFO_V1(gbt_float4_penalty);
25 PG_FUNCTION_INFO_V1(gbt_float4_same);
28 gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
30 return (*((const float4 *) a) > *((const float4 *) b));
33 gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
35 return (*((const float4 *) a) >= *((const float4 *) b));
38 gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
40 return (*((const float4 *) a) == *((const float4 *) b));
43 gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
45 return (*((const float4 *) a) <= *((const float4 *) b));
48 gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
50 return (*((const float4 *) a) < *((const float4 *) b));
54 gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
57 float4KEY *ib = (float4KEY *) (((const Nsrt *) b)->t);
59 if (ia->lower == ib->lower)
61 if (ia->upper == ib->upper)
64 return (ia->upper > ib->upper) ? 1 : -1;
67 return (ia->lower > ib->lower) ? 1 : -1;
71 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 return GET_FLOAT_DISTANCE(float4, a, b);
77 static const gbtree_ninfo tinfo =
81 8, /* sizeof(gbtreekey8) */
92 PG_FUNCTION_INFO_V1(float4_dist);
94 float4_dist(PG_FUNCTION_ARGS)
96 float4 a = PG_GETARG_FLOAT4(0);
97 float4 b = PG_GETARG_FLOAT4(1);
101 CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
103 PG_RETURN_FLOAT4(Abs(r));
107 /**************************************************
109 **************************************************/
113 gbt_float4_compress(PG_FUNCTION_ARGS)
115 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
117 PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
121 gbt_float4_fetch(PG_FUNCTION_ARGS)
123 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
125 PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
129 gbt_float4_consistent(PG_FUNCTION_ARGS)
131 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 float4 query = PG_GETARG_FLOAT4(1);
133 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
135 /* Oid subtype = PG_GETARG_OID(3); */
136 bool *recheck = (bool *) PG_GETARG_POINTER(4);
137 float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
140 /* All cases served by this function are exact */
143 key.lower = (GBT_NUMKEY *) &kkk->lower;
144 key.upper = (GBT_NUMKEY *) &kkk->upper;
147 gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
153 gbt_float4_distance(PG_FUNCTION_ARGS)
155 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
156 float4 query = PG_GETARG_FLOAT4(1);
158 /* Oid subtype = PG_GETARG_OID(3); */
159 float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
162 key.lower = (GBT_NUMKEY *) &kkk->lower;
163 key.upper = (GBT_NUMKEY *) &kkk->upper;
166 gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
172 gbt_float4_union(PG_FUNCTION_ARGS)
174 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
175 void *out = palloc(sizeof(float4KEY));
177 *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
178 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
183 gbt_float4_penalty(PG_FUNCTION_ARGS)
185 float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
186 float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
187 float *result = (float *) PG_GETARG_POINTER(2);
189 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
191 PG_RETURN_POINTER(result);
196 gbt_float4_picksplit(PG_FUNCTION_ARGS)
198 PG_RETURN_POINTER(gbt_num_picksplit(
199 (GistEntryVector *) PG_GETARG_POINTER(0),
200 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
201 &tinfo, fcinfo->flinfo
206 gbt_float4_same(PG_FUNCTION_ARGS)
208 float4KEY *b1 = (float4KEY *) PG_GETARG_POINTER(0);
209 float4KEY *b2 = (float4KEY *) PG_GETARG_POINTER(1);
210 bool *result = (bool *) PG_GETARG_POINTER(2);
212 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
213 PG_RETURN_POINTER(result);