PostgreSQL Source Code git master
Loading...
Searching...
No Matches
_int_op.c
Go to the documentation of this file.
1/*
2 * contrib/intarray/_int_op.c
3 */
4#include "postgres.h"
5
6#include "_int.h"
7
9 .name = "intarray",
10 .version = PG_VERSION
11);
12
20
23{
24 /* just reverse the operands and call _int_contains */
28}
29
32{
33 /* Force copy so we can modify the arrays in-place */
36 bool res;
37
42 res = inner_int_contains(a, b);
43 pfree(a);
44 pfree(b);
45 PG_RETURN_BOOL(res);
46}
47
55
58{
61 int na,
62 nb;
63 int n;
64 int *da,
65 *db;
66 bool result;
67
70 na = ARRNELEMS(a);
71 nb = ARRNELEMS(b);
72 da = ARRPTR(a);
73 db = ARRPTR(b);
74
75 result = false;
76
77 if (na == nb)
78 {
79 SORT(a);
80 SORT(b);
81 result = true;
82
83 for (n = 0; n < na; n++)
84 {
85 if (da[n] != db[n])
86 {
87 result = false;
88 break;
89 }
90 }
91 }
92
93 pfree(a);
94 pfree(b);
95
97}
98
99/*
100 * _int_overlap -- does a overlap b?
101 */
102Datum
104{
107 bool result;
108
111 if (ARRISEMPTY(a) || ARRISEMPTY(b))
112 PG_RETURN_BOOL(false);
113
114 SORT(a);
115 SORT(b);
116
118
119 pfree(a);
120 pfree(b);
121
123}
124
125Datum
145
146Datum
166
167
181
182Datum
187
188Datum
190{
192 int32 count = ARRNELEMS(a);
193
194 PG_FREE_IF_COPY(a, 0);
195 PG_RETURN_INT32(count);
196}
197
198Datum
200{
202 text *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_PP(1) : NULL;
204 char *d = (dirstr) ? VARDATA_ANY(dirstr) : NULL;
205 int dir = -1;
206
208 if (ARRNELEMS(a) < 2)
210
211 if (dirstr == NULL || (dc == 3
212 && (d[0] == 'A' || d[0] == 'a')
213 && (d[1] == 'S' || d[1] == 's')
214 && (d[2] == 'C' || d[2] == 'c')))
215 dir = 1;
216 else if (dc == 4
217 && (d[0] == 'D' || d[0] == 'd')
218 && (d[1] == 'E' || d[1] == 'e')
219 && (d[2] == 'S' || d[2] == 's')
220 && (d[3] == 'C' || d[3] == 'c'))
221 dir = 0;
222 if (dir == -1)
225 errmsg("second parameter must be \"ASC\" or \"DESC\"")));
226 QSORT(a, dir);
228}
229
230Datum
239
240Datum
249
250Datum
261
262Datum
275
276Datum
278{
281 int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
282 int32 end = 0;
283 int32 c;
285
286 start = (start > 0) ? start - 1 : start;
287
289 if (ARRISEMPTY(a))
290 {
291 PG_FREE_IF_COPY(a, 0);
293 }
294
295 c = ARRNELEMS(a);
296
297 if (start < 0)
298 start = c + start;
299
300 if (len < 0)
301 end = c + len;
302 else if (len == 0)
303 end = c;
304 else
305 end = start + len;
306
307 if (end > c)
308 end = c;
309
310 if (start < 0)
311 start = 0;
312
313 if (start >= end || end <= 0)
314 {
315 PG_FREE_IF_COPY(a, 0);
317 }
318
320 if (end - start > 0)
321 memcpy(ARRPTR(result), ARRPTR(a) + start, (end - start) * sizeof(int32));
322 PG_FREE_IF_COPY(a, 0);
324}
325
326Datum
336
337Datum
349
350Datum
352{
354 int32 elem = PG_GETARG_INT32(1);
355 int32 c;
356 int32 *aa;
357 int32 n = 0,
358 i;
359
361 if (!ARRISEMPTY(a))
362 {
363 c = ARRNELEMS(a);
364 aa = ARRPTR(a);
365 for (i = 0; i < c; i++)
366 {
367 if (aa[i] != elem)
368 {
369 if (i > n)
370 aa[n++] = aa[i];
371 else
372 n++;
373 }
374 }
375 a = resize_intArrayType(a, n);
376 }
378}
379
380Datum
391
392Datum
394{
398 int32 ca;
399 int32 cb;
400 int32 *aa,
401 *bb,
402 *r;
403 int32 n = 0,
404 i = 0,
405 k = 0;
406
409
410 QSORT(a, 1);
411 a = _int_unique(a);
412 ca = ARRNELEMS(a);
413 QSORT(b, 1);
414 b = _int_unique(b);
415 cb = ARRNELEMS(b);
417 aa = ARRPTR(a);
418 bb = ARRPTR(b);
419 r = ARRPTR(result);
420 while (i < ca)
421 {
422 if (k == cb || aa[i] < bb[k])
423 r[n++] = aa[i++];
424 else if (aa[i] == bb[k])
425 {
426 i++;
427 k++;
428 }
429 else
430 k++;
431 }
433 pfree(a);
434 pfree(b);
436}
bool inner_int_overlap(ArrayType *a, ArrayType *b)
Definition _int_tool.c:50
ArrayType * int_to_intset(int32 elem)
Definition _int_tool.c:388
ArrayType * intarray_concat_arrays(ArrayType *a, ArrayType *b)
Definition _int_tool.c:371
ArrayType * new_intArrayType(int num)
Definition _int_tool.c:224
ArrayType * inner_int_union(ArrayType *a, ArrayType *b)
Definition _int_tool.c:79
bool inner_int_contains(ArrayType *a, ArrayType *b)
Definition _int_tool.c:15
int32 intarray_match_first(ArrayType *a, int32 elem)
Definition _int_tool.c:338
#define PREPAREARR(x)
Definition _int.h:49
ArrayType * intarray_add_elem(ArrayType *a, int32 elem)
Definition _int_tool.c:354
ArrayType * inner_int_inter(ArrayType *a, ArrayType *b)
Definition _int_tool.c:136
#define QSORT(a, direction)
Definition _int.h:180
#define CHECKARRVALID(x)
Definition _int.h:30
ArrayType * resize_intArrayType(ArrayType *a, int num)
Definition _int_tool.c:252
#define SORT(x)
Definition _int.h:41
#define ARRISEMPTY(x)
Definition _int.h:38
ArrayType * _int_unique(ArrayType *r)
Definition _int_tool.c:313
Datum sort_desc(PG_FUNCTION_ARGS)
Definition _int_op.c:241
Datum _int_contains(PG_FUNCTION_ARGS)
Definition _int_op.c:31
Datum icount(PG_FUNCTION_ARGS)
Definition _int_op.c:189
Datum _int_union(PG_FUNCTION_ARGS)
Definition _int_op.c:126
Datum uniq(PG_FUNCTION_ARGS)
Definition _int_op.c:251
Datum intarray_push_elem(PG_FUNCTION_ARGS)
Definition _int_op.c:327
Datum _int_same(PG_FUNCTION_ARGS)
Definition _int_op.c:57
Datum _int_inter(PG_FUNCTION_ARGS)
Definition _int_op.c:147
Datum sort(PG_FUNCTION_ARGS)
Definition _int_op.c:199
Datum intset_union_elem(PG_FUNCTION_ARGS)
Definition _int_op.c:381
Datum sort_asc(PG_FUNCTION_ARGS)
Definition _int_op.c:231
Datum intset(PG_FUNCTION_ARGS)
Definition _int_op.c:183
Datum _int_overlap(PG_FUNCTION_ARGS)
Definition _int_op.c:103
Datum subarray(PG_FUNCTION_ARGS)
Definition _int_op.c:277
Datum intarray_del_elem(PG_FUNCTION_ARGS)
Definition _int_op.c:351
Datum _int_different(PG_FUNCTION_ARGS)
Definition _int_op.c:49
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:263
Datum intset_subtract(PG_FUNCTION_ARGS)
Definition _int_op.c:393
Datum intarray_push_array(PG_FUNCTION_ARGS)
Definition _int_op.c:338
Datum _int_contained(PG_FUNCTION_ARGS)
Definition _int_op.c:22
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition array.h:264
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
int32_t int32
Definition c.h:620
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define ARRNELEMS(x)
Definition cube.c:29
#define ARRPTR(x)
Definition cube.c:28
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#define PG_FREE_IF_COPY(ptr, n)
Definition fmgr.h:260
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:690
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
return str start
int b
Definition isn.c:74
int a
Definition isn.c:73
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1619
static char * errmsg
const void size_t len
static bool DatumGetBool(Datum X)
Definition postgres.h:100
uint64_t Datum
Definition postgres.h:70
#define PointerGetDatum(X)
Definition postgres.h:354
char * c
static int fb(int x)
Definition c.h:776
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
const char * name