PostgreSQL Source Code  git master
_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 
18 Datum
20 {
21  /* just reverse the operands and call _int_contains */
23  PG_GETARG_DATUM(1),
24  PG_GETARG_DATUM(0));
25 }
26 
27 Datum
29 {
30  /* Force copy so we can modify the arrays in-place */
33  bool res;
34 
35  CHECKARRVALID(a);
36  CHECKARRVALID(b);
37  PREPAREARR(a);
38  PREPAREARR(b);
39  res = inner_int_contains(a, b);
40  pfree(a);
41  pfree(b);
42  PG_RETURN_BOOL(res);
43 }
44 
45 Datum
47 {
51 }
52 
53 Datum
55 {
58  int na,
59  nb;
60  int n;
61  int *da,
62  *db;
63  bool result;
64 
65  CHECKARRVALID(a);
66  CHECKARRVALID(b);
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  */
98 Datum
100 {
103  bool result;
104 
105  CHECKARRVALID(a);
106  CHECKARRVALID(b);
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 
121 Datum
123 {
126  ArrayType *result;
127 
128  CHECKARRVALID(a);
129  CHECKARRVALID(b);
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 
142 Datum
144 {
147  ArrayType *result;
148 
149  CHECKARRVALID(a);
150  CHECKARRVALID(b);
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 
178 Datum
180 {
182 }
183 
184 Datum
186 {
188  int32 count = ARRNELEMS(a);
189 
190  PG_FREE_IF_COPY(a, 0);
191  PG_RETURN_INT32(count);
192 }
193 
194 Datum
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 
203  CHECKARRVALID(a);
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)
219  ereport(ERROR,
220  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
221  errmsg("second parameter must be \"ASC\" or \"DESC\"")));
222  QSORT(a, dir);
224 }
225 
226 Datum
228 {
230 
231  CHECKARRVALID(a);
232  QSORT(a, 1);
234 }
235 
236 Datum
238 {
240 
241  CHECKARRVALID(a);
242  QSORT(a, 0);
244 }
245 
246 Datum
248 {
250 
251  CHECKARRVALID(a);
252  if (ARRNELEMS(a) < 2)
254  a = _int_unique(a);
256 }
257 
258 Datum
260 {
262  int32 result;
263 
264  CHECKARRVALID(a);
265  result = ARRNELEMS(a);
266  if (result)
267  result = intarray_match_first(a, PG_GETARG_INT32(1));
268  PG_FREE_IF_COPY(a, 0);
269  PG_RETURN_INT32(result);
270 }
271 
272 Datum
274 {
276  int32 start = PG_GETARG_INT32(1);
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 
284  CHECKARRVALID(a);
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 
322 Datum
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 
333 Datum
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 
346 Datum
348 {
350  int32 elem = PG_GETARG_INT32(1);
351  int32 c;
352  int32 *aa;
353  int32 n = 0,
354  i;
355 
356  CHECKARRVALID(a);
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 
376 Datum
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 
388 Datum
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 
403  CHECKARRVALID(a);
404  CHECKARRVALID(b);
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 }
Datum icount(PG_FUNCTION_ARGS)
Definition: _int_op.c:185
ArrayType * intarray_concat_arrays(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:368
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
Datum intset(PG_FUNCTION_ARGS)
Definition: _int_op.c:179
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:252
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define CHECKARRVALID(x)
Definition: _int.h:30
PG_MODULE_MAGIC
Definition: _int_op.c:8
#define PREPAREARR(x)
Definition: _int.h:49
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
Datum subarray(PG_FUNCTION_ARGS)
Definition: _int_op.c:273
int errcode(int sqlerrcode)
Definition: elog.c:610
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
ArrayType * inner_int_inter(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:135
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define SORT(x)
Definition: _int.h:41
signed int int32
Definition: c.h:362
Datum intarray_del_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:347
#define QSORT(a, direction)
Definition: _int.h:183
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
int32 intarray_match_first(ArrayType *a, int32 elem)
Definition: _int_tool.c:335
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum intset_union_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:377
Datum _int_inter(PG_FUNCTION_ARGS)
Definition: _int_op.c:143
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
bool inner_int_overlap(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:49
ArrayType * resize_intArrayType(ArrayType *a, int num)
Definition: _int_tool.c:249
char * c
Datum intarray_push_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:323
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
#define ARRISEMPTY(x)
Definition: _int.h:38
#define DatumGetBool(X)
Definition: postgres.h:393
Datum intset_subtract(PG_FUNCTION_ARGS)
Definition: _int_op.c:389
PG_FUNCTION_INFO_V1(_int_different)
Datum _int_contains(PG_FUNCTION_ARGS)
Definition: _int_op.c:28
bool inner_int_contains(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:14
Datum _int_union(PG_FUNCTION_ARGS)
Definition: _int_op.c:122
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:221
ArrayType * _int_unique(ArrayType *a)
Definition: _int_tool.c:310
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uintptr_t Datum
Definition: postgres.h:367
Datum sort_desc(PG_FUNCTION_ARGS)
Definition: _int_op.c:237
#define ereport(elevel,...)
Definition: elog.h:144
Datum _int_overlap(PG_FUNCTION_ARGS)
Definition: _int_op.c:99
ArrayType * inner_int_union(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:78
Datum _int_same(PG_FUNCTION_ARGS)
Definition: _int_op.c:54
ArrayType * intarray_add_elem(ArrayType *a, int32 elem)
Definition: _int_tool.c:351
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define ARRNELEMS(x)
Definition: cube.c:25
Datum sort_asc(PG_FUNCTION_ARGS)
Definition: _int_op.c:227
Datum _int_contained(PG_FUNCTION_ARGS)
Definition: _int_op.c:19
Datum intarray_push_array(PG_FUNCTION_ARGS)
Definition: _int_op.c:334
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
Definition: c.h:562
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define ARRPTR(x)
Definition: cube.c:24
Datum _int_different(PG_FUNCTION_ARGS)
Definition: _int_op.c:46
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
Datum uniq(PG_FUNCTION_ARGS)
Definition: _int_op.c:247
ArrayType * int_to_intset(int32 elem)
Definition: _int_tool.c:385