PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
_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
17
20{
21 /* just reverse the operands and call _int_contains */
25}
26
29{
30 /* Force copy so we can modify the arrays in-place */
33 bool res;
34
40 pfree(a);
41 pfree(b);
43}
44
47{
51}
52
55{
58 int na,
59 nb;
60 int n;
61 int *da,
62 *db;
63 bool result;
64
67 na = ARRNELEMS(a);
68 nb = ARRNELEMS(b);
69 da = ARRPTR(a);
70 db = ARRPTR(b);
71
72 result = false;
73
74 if (na == nb)
75 {
76 SORT(a);
77 SORT(b);
78 result = true;
79
80 for (n = 0; n < na; n++)
81 {
82 if (da[n] != db[n])
83 {
84 result = false;
85 break;
86 }
87 }
88 }
89
90 pfree(a);
91 pfree(b);
92
93 PG_RETURN_BOOL(result);
94}
95
96/* _int_overlap -- does a overlap b?
97 */
100{
103 bool result;
104
107 if (ARRISEMPTY(a) || ARRISEMPTY(b))
108 return false;
109
110 SORT(a);
111 SORT(b);
112
113 result = inner_int_overlap(a, b);
114
115 pfree(a);
116 pfree(b);
117
118 PG_RETURN_BOOL(result);
119}
120
121Datum
123{
126 ArrayType *result;
127
130
131 SORT(a);
132 SORT(b);
133
134 result = inner_int_union(a, b);
135
136 pfree(a);
137 pfree(b);
138
139 PG_RETURN_POINTER(result);
140}
141
142Datum
144{
147 ArrayType *result;
148
151
152 SORT(a);
153 SORT(b);
154
155 result = inner_int_inter(a, b);
156
157 pfree(a);
158 pfree(b);
159
160 PG_RETURN_POINTER(result);
161}
162
163
177
178Datum
180{
182}
183
184Datum
186{
188 int32 count = ARRNELEMS(a);
189
190 PG_FREE_IF_COPY(a, 0);
191 PG_RETURN_INT32(count);
192}
193
194Datum
196{
198 text *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_PP(1) : NULL;
199 int32 dc = (dirstr) ? VARSIZE_ANY_EXHDR(dirstr) : 0;
200 char *d = (dirstr) ? VARDATA_ANY(dirstr) : NULL;
201 int dir = -1;
202
204 if (ARRNELEMS(a) < 2)
206
207 if (dirstr == NULL || (dc == 3
208 && (d[0] == 'A' || d[0] == 'a')
209 && (d[1] == 'S' || d[1] == 's')
210 && (d[2] == 'C' || d[2] == 'c')))
211 dir = 1;
212 else if (dc == 4
213 && (d[0] == 'D' || d[0] == 'd')
214 && (d[1] == 'E' || d[1] == 'e')
215 && (d[2] == 'S' || d[2] == 's')
216 && (d[3] == 'C' || d[3] == 'c'))
217 dir = 0;
218 if (dir == -1)
220 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
221 errmsg("second parameter must be \"ASC\" or \"DESC\"")));
222 QSORT(a, dir);
224}
225
226Datum
228{
230
232 QSORT(a, 1);
234}
235
236Datum
238{
240
242 QSORT(a, 0);
244}
245
246Datum
248{
250
252 if (ARRNELEMS(a) < 2)
254 a = _int_unique(a);
256}
257
258Datum
260{
262 int32 result;
263
265 result = ARRNELEMS(a);
266 if (result)
268 PG_FREE_IF_COPY(a, 0);
269 PG_RETURN_INT32(result);
270}
271
272Datum
274{
277 int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
278 int32 end = 0;
279 int32 c;
280 ArrayType *result;
281
282 start = (start > 0) ? start - 1 : start;
283
285 if (ARRISEMPTY(a))
286 {
287 PG_FREE_IF_COPY(a, 0);
289 }
290
291 c = ARRNELEMS(a);
292
293 if (start < 0)
294 start = c + start;
295
296 if (len < 0)
297 end = c + len;
298 else if (len == 0)
299 end = c;
300 else
301 end = start + len;
302
303 if (end > c)
304 end = c;
305
306 if (start < 0)
307 start = 0;
308
309 if (start >= end || end <= 0)
310 {
311 PG_FREE_IF_COPY(a, 0);
313 }
314
315 result = new_intArrayType(end - start);
316 if (end - start > 0)
317 memcpy(ARRPTR(result), ARRPTR(a) + start, (end - start) * sizeof(int32));
318 PG_FREE_IF_COPY(a, 0);
319 PG_RETURN_POINTER(result);
320}
321
322Datum
324{
326 ArrayType *result;
327
328 result = intarray_add_elem(a, PG_GETARG_INT32(1));
329 PG_FREE_IF_COPY(a, 0);
330 PG_RETURN_POINTER(result);
331}
332
333Datum
335{
338 ArrayType *result;
339
340 result = intarray_concat_arrays(a, b);
341 PG_FREE_IF_COPY(a, 0);
342 PG_FREE_IF_COPY(b, 1);
343 PG_RETURN_POINTER(result);
344}
345
346Datum
348{
350 int32 elem = PG_GETARG_INT32(1);
351 int32 c;
352 int32 *aa;
353 int32 n = 0,
354 i;
355
357 if (!ARRISEMPTY(a))
358 {
359 c = ARRNELEMS(a);
360 aa = ARRPTR(a);
361 for (i = 0; i < c; i++)
362 {
363 if (aa[i] != elem)
364 {
365 if (i > n)
366 aa[n++] = aa[i];
367 else
368 n++;
369 }
370 }
371 a = resize_intArrayType(a, n);
372 }
374}
375
376Datum
378{
380 ArrayType *result;
381
382 result = intarray_add_elem(a, PG_GETARG_INT32(1));
383 PG_FREE_IF_COPY(a, 0);
384 QSORT(result, 1);
386}
387
388Datum
390{
393 ArrayType *result;
394 int32 ca;
395 int32 cb;
396 int32 *aa,
397 *bb,
398 *r;
399 int32 n = 0,
400 i = 0,
401 k = 0;
402
405
406 QSORT(a, 1);
407 a = _int_unique(a);
408 ca = ARRNELEMS(a);
409 QSORT(b, 1);
410 b = _int_unique(b);
411 cb = ARRNELEMS(b);
412 result = new_intArrayType(ca);
413 aa = ARRPTR(a);
414 bb = ARRPTR(b);
415 r = ARRPTR(result);
416 while (i < ca)
417 {
418 if (k == cb || aa[i] < bb[k])
419 r[n++] = aa[i++];
420 else if (aa[i] == bb[k])
421 {
422 i++;
423 k++;
424 }
425 else
426 k++;
427 }
428 result = resize_intArrayType(result, n);
429 pfree(a);
430 pfree(b);
431 PG_RETURN_POINTER(result);
432}
bool inner_int_overlap(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:50
ArrayType * int_to_intset(int32 elem)
Definition: _int_tool.c:386
ArrayType * intarray_concat_arrays(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:369
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:222
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:336
#define PREPAREARR(x)
Definition: _int.h:49
ArrayType * intarray_add_elem(ArrayType *a, int32 elem)
Definition: _int_tool.c:352
ArrayType * inner_int_inter(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:136
#define QSORT(a, direction)
Definition: _int.h:183
#define CHECKARRVALID(x)
Definition: _int.h:30
ArrayType * resize_intArrayType(ArrayType *a, int num)
Definition: _int_tool.c:250
#define SORT(x)
Definition: _int.h:41
#define ARRISEMPTY(x)
Definition: _int.h:38
ArrayType * _int_unique(ArrayType *r)
Definition: _int_tool.c:311
Datum sort_desc(PG_FUNCTION_ARGS)
Definition: _int_op.c:237
Datum _int_contains(PG_FUNCTION_ARGS)
Definition: _int_op.c:28
PG_FUNCTION_INFO_V1(_int_different)
Datum icount(PG_FUNCTION_ARGS)
Definition: _int_op.c:185
Datum _int_union(PG_FUNCTION_ARGS)
Definition: _int_op.c:122
Datum uniq(PG_FUNCTION_ARGS)
Definition: _int_op.c:247
Datum intarray_push_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:323
PG_MODULE_MAGIC
Definition: _int_op.c:8
Datum _int_same(PG_FUNCTION_ARGS)
Definition: _int_op.c:54
Datum _int_inter(PG_FUNCTION_ARGS)
Definition: _int_op.c:143
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
Datum intset_union_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:377
Datum sort_asc(PG_FUNCTION_ARGS)
Definition: _int_op.c:227
Datum intset(PG_FUNCTION_ARGS)
Definition: _int_op.c:179
Datum _int_overlap(PG_FUNCTION_ARGS)
Definition: _int_op.c:99
Datum subarray(PG_FUNCTION_ARGS)
Definition: _int_op.c:273
Datum intarray_del_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:347
Datum _int_different(PG_FUNCTION_ARGS)
Definition: _int_op.c:46
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
Datum intset_subtract(PG_FUNCTION_ARGS)
Definition: _int_op.c:389
Datum intarray_push_array(PG_FUNCTION_ARGS)
Definition: _int_op.c:334
Datum _int_contained(PG_FUNCTION_ARGS)
Definition: _int_op.c:19
#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:481
#define ARRNELEMS(x)
Definition: cube.c:26
#define ARRPTR(x)
Definition: cube.c:25
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_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
return str start
int b
Definition: isn.c:69
int a
Definition: isn.c:68
int i
Definition: isn.c:72
void pfree(void *pointer)
Definition: mcxt.c:1521
const void size_t len
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
char * c
Definition: c.h:641
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317