PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
complex.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "libpq/pqformat.h"
Include dependency graph for complex.c:

Go to the source code of this file.

Data Structures

struct  Complex
 

Macros

#define Mag(c)   ((c)->x*(c)->x + (c)->y*(c)->y)
 

Typedefs

typedef struct Complex Complex
 

Functions

 PG_FUNCTION_INFO_V1 (complex_in)
 
Datum complex_in (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_out)
 
Datum complex_out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_recv)
 
Datum complex_recv (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_send)
 
Datum complex_send (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_add)
 
Datum complex_add (PG_FUNCTION_ARGS)
 
static int complex_abs_cmp_internal (Complex *a, Complex *b)
 
 PG_FUNCTION_INFO_V1 (complex_abs_lt)
 
Datum complex_abs_lt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_abs_le)
 
Datum complex_abs_le (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_abs_eq)
 
Datum complex_abs_eq (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_abs_ge)
 
Datum complex_abs_ge (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_abs_gt)
 
Datum complex_abs_gt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (complex_abs_cmp)
 
Datum complex_abs_cmp (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ Mag

#define Mag (   c)    ((c)->x*(c)->x + (c)->y*(c)->y)

Definition at line 129 of file complex.c.

Typedef Documentation

◆ Complex

typedef struct Complex Complex

Function Documentation

◆ complex_abs_cmp()

Datum complex_abs_cmp ( PG_FUNCTION_ARGS  )

Definition at line 203 of file complex.c.

204 {
207 
209 }
static int complex_abs_cmp_internal(Complex *a, Complex *b)
Definition: complex.c:132
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int b
Definition: isn.c:69
int a
Definition: isn.c:68

References a, b, complex_abs_cmp_internal(), PG_GETARG_POINTER, and PG_RETURN_INT32.

◆ complex_abs_cmp_internal()

static int complex_abs_cmp_internal ( Complex a,
Complex b 
)
static

Definition at line 132 of file complex.c.

133 {
134  double amag = Mag(a),
135  bmag = Mag(b);
136 
137  if (amag < bmag)
138  return -1;
139  if (amag > bmag)
140  return 1;
141  return 0;
142 }
#define Mag(c)
Definition: complex.c:129

References a, b, and Mag.

Referenced by complex_abs_cmp(), complex_abs_eq(), complex_abs_ge(), complex_abs_gt(), complex_abs_le(), and complex_abs_lt().

◆ complex_abs_eq()

Datum complex_abs_eq ( PG_FUNCTION_ARGS  )

Definition at line 170 of file complex.c.

171 {
174 
176 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References a, b, complex_abs_cmp_internal(), PG_GETARG_POINTER, and PG_RETURN_BOOL.

◆ complex_abs_ge()

Datum complex_abs_ge ( PG_FUNCTION_ARGS  )

Definition at line 181 of file complex.c.

182 {
185 
187 }

References a, b, complex_abs_cmp_internal(), PG_GETARG_POINTER, and PG_RETURN_BOOL.

◆ complex_abs_gt()

Datum complex_abs_gt ( PG_FUNCTION_ARGS  )

Definition at line 192 of file complex.c.

193 {
196 
198 }

References a, b, complex_abs_cmp_internal(), PG_GETARG_POINTER, and PG_RETURN_BOOL.

◆ complex_abs_le()

Datum complex_abs_le ( PG_FUNCTION_ARGS  )

Definition at line 159 of file complex.c.

160 {
163 
165 }

References a, b, complex_abs_cmp_internal(), PG_GETARG_POINTER, and PG_RETURN_BOOL.

◆ complex_abs_lt()

Datum complex_abs_lt ( PG_FUNCTION_ARGS  )

Definition at line 148 of file complex.c.

149 {
152 
154 }

References a, b, complex_abs_cmp_internal(), PG_GETARG_POINTER, and PG_RETURN_BOOL.

◆ complex_add()

Datum complex_add ( PG_FUNCTION_ARGS  )

Definition at line 105 of file complex.c.

106 {
109  Complex *result;
110 
111  result = (Complex *) palloc(sizeof(Complex));
112  result->x = a->x + b->x;
113  result->y = a->y + b->y;
114  PG_RETURN_POINTER(result);
115 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
void * palloc(Size size)
Definition: mcxt.c:1317
double y
Definition: complex.c:20
double x
Definition: complex.c:19

References a, b, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, Complex::x, and Complex::y.

◆ complex_in()

Datum complex_in ( PG_FUNCTION_ARGS  )

Definition at line 31 of file complex.c.

32 {
33  char *str = PG_GETARG_CSTRING(0);
34  double x,
35  y;
36  Complex *result;
37 
38  if (sscanf(str, " ( %lf , %lf )", &x, &y) != 2)
39  ereport(ERROR,
40  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
41  errmsg("invalid input syntax for type %s: \"%s\"",
42  "complex", str)));
43 
44  result = (Complex *) palloc(sizeof(Complex));
45  result->x = x;
46  result->y = y;
47  PG_RETURN_POINTER(result);
48 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
const char * str
int y
Definition: isn.c:71
int x
Definition: isn.c:70

References ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_CSTRING, PG_RETURN_POINTER, str, x, Complex::x, y, and Complex::y.

◆ complex_out()

Datum complex_out ( PG_FUNCTION_ARGS  )

Definition at line 53 of file complex.c.

54 {
55  Complex *complex = (Complex *) PG_GETARG_POINTER(0);
56  char *result;
57 
58  result = psprintf("(%g,%g)", complex->x, complex->y);
59  PG_RETURN_CSTRING(result);
60 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43

References PG_GETARG_POINTER, PG_RETURN_CSTRING, psprintf(), Complex::x, and Complex::y.

◆ complex_recv()

Datum complex_recv ( PG_FUNCTION_ARGS  )

Definition at line 71 of file complex.c.

72 {
74  Complex *result;
75 
76  result = (Complex *) palloc(sizeof(Complex));
77  result->x = pq_getmsgfloat8(buf);
78  result->y = pq_getmsgfloat8(buf);
79  PG_RETURN_POINTER(result);
80 }
static char * buf
Definition: pg_test_fsync.c:72
float8 pq_getmsgfloat8(StringInfo msg)
Definition: pqformat.c:488
StringInfoData * StringInfo
Definition: stringinfo.h:54

References buf, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, pq_getmsgfloat8(), Complex::x, and Complex::y.

◆ complex_send()

Datum complex_send ( PG_FUNCTION_ARGS  )

Definition at line 85 of file complex.c.

86 {
87  Complex *complex = (Complex *) PG_GETARG_POINTER(0);
89 
91  pq_sendfloat8(&buf, complex->x);
92  pq_sendfloat8(&buf, complex->y);
94 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
void pq_sendfloat8(StringInfo buf, float8 f)
Definition: pqformat.c:276

References buf, PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendfloat8(), Complex::x, and Complex::y.

◆ PG_FUNCTION_INFO_V1() [1/11]

PG_FUNCTION_INFO_V1 ( complex_abs_cmp  )

◆ PG_FUNCTION_INFO_V1() [2/11]

PG_FUNCTION_INFO_V1 ( complex_abs_eq  )

◆ PG_FUNCTION_INFO_V1() [3/11]

PG_FUNCTION_INFO_V1 ( complex_abs_ge  )

◆ PG_FUNCTION_INFO_V1() [4/11]

PG_FUNCTION_INFO_V1 ( complex_abs_gt  )

◆ PG_FUNCTION_INFO_V1() [5/11]

PG_FUNCTION_INFO_V1 ( complex_abs_le  )

◆ PG_FUNCTION_INFO_V1() [6/11]

PG_FUNCTION_INFO_V1 ( complex_abs_lt  )

◆ PG_FUNCTION_INFO_V1() [7/11]

PG_FUNCTION_INFO_V1 ( complex_add  )

◆ PG_FUNCTION_INFO_V1() [8/11]

PG_FUNCTION_INFO_V1 ( complex_in  )

◆ PG_FUNCTION_INFO_V1() [9/11]

PG_FUNCTION_INFO_V1 ( complex_out  )

◆ PG_FUNCTION_INFO_V1() [10/11]

PG_FUNCTION_INFO_V1 ( complex_recv  )

◆ PG_FUNCTION_INFO_V1() [11/11]

PG_FUNCTION_INFO_V1 ( complex_send  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 15 of file complex.c.