1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include <stdio.h>
/* do not include libpq-fe.h for backend-loaded functions*/
/* #include "libpq-fe.h" */
#include "postgres.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/mcxt.h"
typedef struct Complex {
double x;
double y;
} Complex;
/*****************************************************************************
* Input/Output functions
*****************************************************************************/
Complex *
complex_in(char *str)
{
double x, y;
Complex *result;
if (sscanf(str, " ( %lf , %lf )", &x, &y) != 2) {
elog(WARN, "complex_in: error in parsing \"%s\"", str);
return NULL;
}
result = (Complex *)palloc(sizeof(Complex));
result->x = x;
result->y = y;
return (result);
}
/*
* You might have noticed a slight inconsistency between the following
* declaration and the SQL definition:
* CREATE FUNCTION complex_out(opaque) RETURNS opaque ...
* The reason is that the argument pass into complex_out is really just a
* pointer. POSTGRES thinks all output functions are:
* char *out_func(char *);
*/
char *
complex_out(Complex *complex)
{
char *result;
if (complex == NULL)
return(NULL);
result = (char *) palloc(60);
sprintf(result, "(%lg,%lg)", complex->x, complex->y);
return(result);
}
/*****************************************************************************
* New Operators
*****************************************************************************/
Complex *
complex_add(Complex *a, Complex *b)
{
Complex *result;
result = (Complex *)palloc(sizeof(Complex));
result->x = a->x + b->x;
result->y = a->y + b->y;
return (result);
}
/*****************************************************************************
* Operator class for defining B-tree index
*****************************************************************************/
#define Mag(c) ((c)->x*(c)->x + (c)->y*(c)->y)
bool
complex_abs_lt(Complex *a, Complex *b)
{
double amag = Mag(a), bmag = Mag(b);
return (amag<bmag);
}
bool
complex_abs_le(Complex *a, Complex *b)
{
double amag = Mag(a), bmag = Mag(b);
return (amag<=bmag);
}
bool
complex_abs_eq(Complex *a, Complex *b)
{
double amag = Mag(a), bmag = Mag(b);
return (amag==bmag);
}
bool
complex_abs_ge(Complex *a, Complex *b)
{
double amag = Mag(a), bmag = Mag(b);
return (amag>=bmag);
}
bool
complex_abs_gt(Complex *a, Complex *b)
{
double amag = Mag(a), bmag = Mag(b);
return (amag>bmag);
}
int4
complex_abs_cmp(Complex *a, Complex *b)
{
double amag = Mag(a), bmag = Mag(b);
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
/*****************************************************************************
* test code
*****************************************************************************/
/*
* You should always test your code separately. Trust me, using POSTGRES to
* debug your C function will be very painful and unproductive. In case of
* POSTGRES crashing, it is impossible to tell whether the bug is in your
* code or POSTGRES's.
*/
void
test_main()
{
Complex *a;
Complex *b;
a = complex_in("(4.01, 3.77 )");
printf("a = %s\n", complex_out(a));
b = complex_in("(1.0,2.0)");
printf("b = %s\n", complex_out(b));
printf("a + b = %s\n", complex_out(complex_add(a,b)));
printf("a < b = %d\n", complex_abs_lt(a,b));
printf("a <= b = %d\n", complex_abs_le(a,b));
printf("a = b = %d\n", complex_abs_eq(a,b));
printf("a >= b = %d\n", complex_abs_ge(a,b));
printf("a > b = %d\n", complex_abs_gt(a,b));
}
|