~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/btree_gist/btree_float4.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "btree_gist.h"
 
2
#include "btree_utils_num.h"
 
3
 
 
4
typedef struct float4key
 
5
{
 
6
        float4          lower;
 
7
        float4          upper;
 
8
}       float4KEY;
 
9
 
 
10
/*
 
11
** float4 ops
 
12
*/
 
13
PG_FUNCTION_INFO_V1(gbt_float4_compress);
 
14
PG_FUNCTION_INFO_V1(gbt_float4_union);
 
15
PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
 
16
PG_FUNCTION_INFO_V1(gbt_float4_consistent);
 
17
PG_FUNCTION_INFO_V1(gbt_float4_penalty);
 
18
PG_FUNCTION_INFO_V1(gbt_float4_same);
 
19
 
 
20
Datum           gbt_float4_compress(PG_FUNCTION_ARGS);
 
21
Datum           gbt_float4_union(PG_FUNCTION_ARGS);
 
22
Datum           gbt_float4_picksplit(PG_FUNCTION_ARGS);
 
23
Datum           gbt_float4_consistent(PG_FUNCTION_ARGS);
 
24
Datum           gbt_float4_penalty(PG_FUNCTION_ARGS);
 
25
Datum           gbt_float4_same(PG_FUNCTION_ARGS);
 
26
 
 
27
static bool
 
28
gbt_float4gt(const void *a, const void *b)
 
29
{
 
30
        return (*((float4 *) a) > *((float4 *) b));
 
31
}
 
32
static bool
 
33
gbt_float4ge(const void *a, const void *b)
 
34
{
 
35
        return (*((float4 *) a) >= *((float4 *) b));
 
36
}
 
37
static bool
 
38
gbt_float4eq(const void *a, const void *b)
 
39
{
 
40
        return (*((float4 *) a) == *((float4 *) b));
 
41
}
 
42
static bool
 
43
gbt_float4le(const void *a, const void *b)
 
44
{
 
45
        return (*((float4 *) a) <= *((float4 *) b));
 
46
}
 
47
static bool
 
48
gbt_float4lt(const void *a, const void *b)
 
49
{
 
50
        return (*((float4 *) a) < *((float4 *) b));
 
51
}
 
52
 
 
53
static int
 
54
gbt_float4key_cmp(const void *a, const void *b)
 
55
{
 
56
 
 
57
        if (*(float4 *) &(((Nsrt *) a)->t[0]) > *(float4 *) &(((Nsrt *) b)->t[0]))
 
58
                return 1;
 
59
        else if (*(float4 *) &(((Nsrt *) a)->t[0]) < *(float4 *) &(((Nsrt *) b)->t[0]))
 
60
                return -1;
 
61
        return 0;
 
62
 
 
63
}
 
64
 
 
65
 
 
66
static const gbtree_ninfo tinfo =
 
67
{
 
68
        gbt_t_float4,
 
69
        sizeof(float4),
 
70
        gbt_float4gt,
 
71
        gbt_float4ge,
 
72
        gbt_float4eq,
 
73
        gbt_float4le,
 
74
        gbt_float4lt,
 
75
        gbt_float4key_cmp
 
76
};
 
77
 
 
78
 
 
79
/**************************************************
 
80
 * float4 ops
 
81
 **************************************************/
 
82
 
 
83
 
 
84
Datum
 
85
gbt_float4_compress(PG_FUNCTION_ARGS)
 
86
{
 
87
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
88
        GISTENTRY  *retval = NULL;
 
89
 
 
90
        PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
 
91
}
 
92
 
 
93
 
 
94
Datum
 
95
gbt_float4_consistent(PG_FUNCTION_ARGS)
 
96
{
 
97
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
98
        float4          query = PG_GETARG_FLOAT4(1);
 
99
        float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
 
100
        GBT_NUMKEY_R key;
 
101
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 
102
 
 
103
        key.lower = (GBT_NUMKEY *) & kkk->lower;
 
104
        key.upper = (GBT_NUMKEY *) & kkk->upper;
 
105
 
 
106
        PG_RETURN_BOOL(
 
107
                                   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
 
108
                );
 
109
}
 
110
 
 
111
 
 
112
Datum
 
113
gbt_float4_union(PG_FUNCTION_ARGS)
 
114
{
 
115
        GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
 
116
        void       *out = palloc(sizeof(float4KEY));
 
117
 
 
118
        *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
 
119
        PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
 
120
}
 
121
 
 
122
 
 
123
Datum
 
124
gbt_float4_penalty(PG_FUNCTION_ARGS)
 
125
{
 
126
        float4KEY  *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
 
127
        float4KEY  *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
 
128
        float      *result = (float *) PG_GETARG_POINTER(2);
 
129
 
 
130
        float4          res;
 
131
 
 
132
        *result = 0.0;
 
133
 
 
134
        penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
 
135
 
 
136
        if (res > 0)
 
137
        {
 
138
                *result += FLT_MIN;
 
139
                *result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
 
140
                *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
 
141
        }
 
142
 
 
143
        PG_RETURN_POINTER(result);
 
144
 
 
145
}
 
146
 
 
147
Datum
 
148
gbt_float4_picksplit(PG_FUNCTION_ARGS)
 
149
{
 
150
        PG_RETURN_POINTER(gbt_num_picksplit(
 
151
                                                                (GistEntryVector *) PG_GETARG_POINTER(0),
 
152
                                                                  (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
 
153
                                                                                &tinfo
 
154
                                                                                ));
 
155
}
 
156
 
 
157
Datum
 
158
gbt_float4_same(PG_FUNCTION_ARGS)
 
159
{
 
160
        float4KEY  *b1 = (float4KEY *) PG_GETARG_POINTER(0);
 
161
        float4KEY  *b2 = (float4KEY *) PG_GETARG_POINTER(1);
 
162
        bool       *result = (bool *) PG_GETARG_POINTER(2);
 
163
 
 
164
        *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
 
165
        PG_RETURN_POINTER(result);
 
166
}