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 {
50  _int_same,
53  )
54  ));
55 }
56 
57 Datum
59 {
62  int na,
63  nb;
64  int n;
65  int *da,
66  *db;
67  bool result;
68 
69  CHECKARRVALID(a);
70  CHECKARRVALID(b);
71  na = ARRNELEMS(a);
72  nb = ARRNELEMS(b);
73  da = ARRPTR(a);
74  db = ARRPTR(b);
75 
76  result = false;
77 
78  if (na == nb)
79  {
80  SORT(a);
81  SORT(b);
82  result = true;
83 
84  for (n = 0; n < na; n++)
85  {
86  if (da[n] != db[n])
87  {
88  result = false;
89  break;
90  }
91  }
92  }
93 
94  pfree(a);
95  pfree(b);
96 
97  PG_RETURN_BOOL(result);
98 }
99 
100 /* _int_overlap -- does a overlap b?
101  */
102 Datum
104 {
107  bool result;
108 
109  CHECKARRVALID(a);
110  CHECKARRVALID(b);
111  if (ARRISEMPTY(a) || ARRISEMPTY(b))
112  return false;
113 
114  SORT(a);
115  SORT(b);
116 
117  result = inner_int_overlap(a, b);
118 
119  pfree(a);
120  pfree(b);
121 
122  PG_RETURN_BOOL(result);
123 }
124 
125 Datum
127 {
130  ArrayType *result;
131 
132  CHECKARRVALID(a);
133  CHECKARRVALID(b);
134 
135  SORT(a);
136  SORT(b);
137 
138  result = inner_int_union(a, b);
139 
140  pfree(a);
141  pfree(b);
142 
143  PG_RETURN_POINTER(result);
144 }
145 
146 Datum
148 {
151  ArrayType *result;
152 
153  CHECKARRVALID(a);
154  CHECKARRVALID(b);
155 
156  SORT(a);
157  SORT(b);
158 
159  result = inner_int_inter(a, b);
160 
161  pfree(a);
162  pfree(b);
163 
164  PG_RETURN_POINTER(result);
165 }
166 
167 
181 
182 Datum
184 {
186 }
187 
188 Datum
190 {
192  int32 count = ARRNELEMS(a);
193 
194  PG_FREE_IF_COPY(a, 0);
195  PG_RETURN_INT32(count);
196 }
197 
198 Datum
200 {
202  text *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_PP(1) : NULL;
203  int32 dc = (dirstr) ? VARSIZE_ANY_EXHDR(dirstr) : 0;
204  char *d = (dirstr) ? VARDATA_ANY(dirstr) : NULL;
205  int dir = -1;
206 
207  CHECKARRVALID(a);
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)
223  ereport(ERROR,
224  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
225  errmsg("second parameter must be \"ASC\" or \"DESC\"")));
226  QSORT(a, dir);
228 }
229 
230 Datum
232 {
234 
235  CHECKARRVALID(a);
236  QSORT(a, 1);
238 }
239 
240 Datum
242 {
244 
245  CHECKARRVALID(a);
246  QSORT(a, 0);
248 }
249 
250 Datum
252 {
254 
255  CHECKARRVALID(a);
256  if (ARRNELEMS(a) < 2)
258  a = _int_unique(a);
260 }
261 
262 Datum
264 {
266  int32 result;
267 
268  CHECKARRVALID(a);
269  result = ARRNELEMS(a);
270  if (result)
271  result = intarray_match_first(a, PG_GETARG_INT32(1));
272  PG_FREE_IF_COPY(a, 0);
273  PG_RETURN_INT32(result);
274 }
275 
276 Datum
278 {
280  int32 start = PG_GETARG_INT32(1);
281  int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
282  int32 end = 0;
283  int32 c;
284  ArrayType *result;
285 
286  start = (start > 0) ? start - 1 : start;
287 
288  CHECKARRVALID(a);
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 
319  result = new_intArrayType(end - start);
320  if (end - start > 0)
321  memcpy(ARRPTR(result), ARRPTR(a) + start, (end - start) * sizeof(int32));
322  PG_FREE_IF_COPY(a, 0);
323  PG_RETURN_POINTER(result);
324 }
325 
326 Datum
328 {
330  ArrayType *result;
331 
332  result = intarray_add_elem(a, PG_GETARG_INT32(1));
333  PG_FREE_IF_COPY(a, 0);
334  PG_RETURN_POINTER(result);
335 }
336 
337 Datum
339 {
342  ArrayType *result;
343 
344  result = intarray_concat_arrays(a, b);
345  PG_FREE_IF_COPY(a, 0);
346  PG_FREE_IF_COPY(b, 1);
347  PG_RETURN_POINTER(result);
348 }
349 
350 Datum
352 {
354  int32 elem = PG_GETARG_INT32(1);
355  int32 c;
356  int32 *aa;
357  int32 n = 0,
358  i;
359 
360  CHECKARRVALID(a);
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 
380 Datum
382 {
384  ArrayType *result;
385 
386  result = intarray_add_elem(a, PG_GETARG_INT32(1));
387  PG_FREE_IF_COPY(a, 0);
388  QSORT(result, 1);
390 }
391 
392 Datum
394 {
397  ArrayType *result;
398  int32 ca;
399  int32 cb;
400  int32 *aa,
401  *bb,
402  *r;
403  int32 n = 0,
404  i = 0,
405  k = 0;
406 
407  CHECKARRVALID(a);
408  CHECKARRVALID(b);
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);
416  result = new_intArrayType(ca);
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  }
432  result = resize_intArrayType(result, n);
433  pfree(a);
434  pfree(b);
435  PG_RETURN_POINTER(result);
436 }
Datum icount(PG_FUNCTION_ARGS)
Definition: _int_op.c:189
ArrayType * intarray_concat_arrays(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:368
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
Datum intset(PG_FUNCTION_ARGS)
Definition: _int_op.c:183
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#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:263
#define CHECKARRVALID(x)
Definition: _int.h:18
PG_MODULE_MAGIC
Definition: _int_op.c:8
#define PREPAREARR(x)
Definition: _int.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
Datum subarray(PG_FUNCTION_ARGS)
Definition: _int_op.c:277
int errcode(int sqlerrcode)
Definition: elog.c:608
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:263
ArrayType * inner_int_inter(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:135
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define SORT(x)
Definition: _int.h:29
signed int int32
Definition: c.h:347
Datum intarray_del_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:351
#define QSORT(a, direction)
Definition: _int.h:162
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:381
Datum _int_inter(PG_FUNCTION_ARGS)
Definition: _int_op.c:147
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:327
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:199
#define ARRISEMPTY(x)
Definition: _int.h:26
#define DatumGetBool(X)
Definition: postgres.h:393
Datum intset_subtract(PG_FUNCTION_ARGS)
Definition: _int_op.c:393
PG_FUNCTION_INFO_V1(_int_different)
Datum _int_contains(PG_FUNCTION_ARGS)
Definition: _int_op.c:28
#define ereport(elevel, rest)
Definition: elog.h:141
bool inner_int_contains(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:14
Datum _int_union(PG_FUNCTION_ARGS)
Definition: _int_op.c:126
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:349
uintptr_t Datum
Definition: postgres.h:367
Datum sort_desc(PG_FUNCTION_ARGS)
Definition: _int_op.c:241
Datum _int_overlap(PG_FUNCTION_ARGS)
Definition: _int_op.c:103
ArrayType * inner_int_union(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:78
Datum _int_same(PG_FUNCTION_ARGS)
Definition: _int_op.c:58
ArrayType * intarray_add_elem(ArrayType *a, int32 elem)
Definition: _int_tool.c:351
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define ARRNELEMS(x)
Definition: cube.c:25
Datum sort_asc(PG_FUNCTION_ARGS)
Definition: _int_op.c:231
Datum _int_contained(PG_FUNCTION_ARGS)
Definition: _int_op.c:19
Datum intarray_push_array(PG_FUNCTION_ARGS)
Definition: _int_op.c:338
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
Definition: c.h:556
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
#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:617
Datum uniq(PG_FUNCTION_ARGS)
Definition: _int_op.c:251
ArrayType * int_to_intset(int32 elem)
Definition: _int_tool.c:385