PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
_int_op.c
Go to the documentation of this file.
1 /*
2  * contrib/intarray/_int_op.c
3  */
4 #include "postgres.h"
5 
6 
7 #include "_int.h"
8 
10 
18 
19 Datum
21 {
22  /* just reverse the operands and call _int_contains */
24  PG_GETARG_DATUM(1),
25  PG_GETARG_DATUM(0));
26 }
27 
28 Datum
30 {
31  /* Force copy so we can modify the arrays in-place */
34  bool res;
35 
36  CHECKARRVALID(a);
37  CHECKARRVALID(b);
38  PREPAREARR(a);
39  PREPAREARR(b);
40  res = inner_int_contains(a, b);
41  pfree(a);
42  pfree(b);
43  PG_RETURN_BOOL(res);
44 }
45 
46 Datum
48 {
51  _int_same,
54  )
55  ));
56 }
57 
58 Datum
60 {
63  int na,
64  nb;
65  int n;
66  int *da,
67  *db;
68  bool result;
69 
70  CHECKARRVALID(a);
71  CHECKARRVALID(b);
72  na = ARRNELEMS(a);
73  nb = ARRNELEMS(b);
74  da = ARRPTR(a);
75  db = ARRPTR(b);
76 
77  result = FALSE;
78 
79  if (na == nb)
80  {
81  SORT(a);
82  SORT(b);
83  result = TRUE;
84 
85  for (n = 0; n < na; n++)
86  {
87  if (da[n] != db[n])
88  {
89  result = FALSE;
90  break;
91  }
92  }
93  }
94 
95  pfree(a);
96  pfree(b);
97 
98  PG_RETURN_BOOL(result);
99 }
100 
101 /* _int_overlap -- does a overlap b?
102  */
103 Datum
105 {
108  bool result;
109 
110  CHECKARRVALID(a);
111  CHECKARRVALID(b);
112  if (ARRISEMPTY(a) || ARRISEMPTY(b))
113  return FALSE;
114 
115  SORT(a);
116  SORT(b);
117 
118  result = inner_int_overlap(a, b);
119 
120  pfree(a);
121  pfree(b);
122 
123  PG_RETURN_BOOL(result);
124 }
125 
126 Datum
128 {
131  ArrayType *result;
132 
133  CHECKARRVALID(a);
134  CHECKARRVALID(b);
135 
136  SORT(a);
137  SORT(b);
138 
139  result = inner_int_union(a, b);
140 
141  pfree(a);
142  pfree(b);
143 
144  PG_RETURN_POINTER(result);
145 }
146 
147 Datum
149 {
152  ArrayType *result;
153 
154  CHECKARRVALID(a);
155  CHECKARRVALID(b);
156 
157  SORT(a);
158  SORT(b);
159 
160  result = inner_int_inter(a, b);
161 
162  pfree(a);
163  pfree(b);
164 
165  PG_RETURN_POINTER(result);
166 }
167 
168 
182 
183 Datum
185 {
187 }
188 
189 Datum
191 {
193  int32 count = ARRNELEMS(a);
194 
195  PG_FREE_IF_COPY(a, 0);
196  PG_RETURN_INT32(count);
197 }
198 
199 Datum
201 {
203  text *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_P(1) : NULL;
204  int32 dc = (dirstr) ? VARSIZE(dirstr) - VARHDRSZ : 0;
205  char *d = (dirstr) ? VARDATA(dirstr) : NULL;
206  int dir = -1;
207 
208  CHECKARRVALID(a);
209  if (ARRNELEMS(a) < 2)
211 
212  if (dirstr == NULL || (dc == 3
213  && (d[0] == 'A' || d[0] == 'a')
214  && (d[1] == 'S' || d[1] == 's')
215  && (d[2] == 'C' || d[2] == 'c')))
216  dir = 1;
217  else if (dc == 4
218  && (d[0] == 'D' || d[0] == 'd')
219  && (d[1] == 'E' || d[1] == 'e')
220  && (d[2] == 'S' || d[2] == 's')
221  && (d[3] == 'C' || d[3] == 'c'))
222  dir = 0;
223  if (dir == -1)
224  ereport(ERROR,
225  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
226  errmsg("second parameter must be \"ASC\" or \"DESC\"")));
227  QSORT(a, dir);
229 }
230 
231 Datum
233 {
235 
236  CHECKARRVALID(a);
237  QSORT(a, 1);
239 }
240 
241 Datum
243 {
245 
246  CHECKARRVALID(a);
247  QSORT(a, 0);
249 }
250 
251 Datum
253 {
255 
256  CHECKARRVALID(a);
257  if (ARRNELEMS(a) < 2)
259  a = _int_unique(a);
261 }
262 
263 Datum
265 {
267  int32 result;
268 
269  CHECKARRVALID(a);
270  result = ARRNELEMS(a);
271  if (result)
272  result = intarray_match_first(a, PG_GETARG_INT32(1));
273  PG_FREE_IF_COPY(a, 0);
274  PG_RETURN_INT32(result);
275 }
276 
277 Datum
279 {
281  int32 start = PG_GETARG_INT32(1);
282  int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
283  int32 end = 0;
284  int32 c;
285  ArrayType *result;
286 
287  start = (start > 0) ? start - 1 : start;
288 
289  CHECKARRVALID(a);
290  if (ARRISEMPTY(a))
291  {
292  PG_FREE_IF_COPY(a, 0);
294  }
295 
296  c = ARRNELEMS(a);
297 
298  if (start < 0)
299  start = c + start;
300 
301  if (len < 0)
302  end = c + len;
303  else if (len == 0)
304  end = c;
305  else
306  end = start + len;
307 
308  if (end > c)
309  end = c;
310 
311  if (start < 0)
312  start = 0;
313 
314  if (start >= end || end <= 0)
315  {
316  PG_FREE_IF_COPY(a, 0);
318  }
319 
320  result = new_intArrayType(end - start);
321  if (end - start > 0)
322  memcpy(ARRPTR(result), ARRPTR(a) + start, (end - start) * sizeof(int32));
323  PG_FREE_IF_COPY(a, 0);
324  PG_RETURN_POINTER(result);
325 }
326 
327 Datum
329 {
331  ArrayType *result;
332 
333  result = intarray_add_elem(a, PG_GETARG_INT32(1));
334  PG_FREE_IF_COPY(a, 0);
335  PG_RETURN_POINTER(result);
336 }
337 
338 Datum
340 {
343  ArrayType *result;
344 
345  result = intarray_concat_arrays(a, b);
346  PG_FREE_IF_COPY(a, 0);
347  PG_FREE_IF_COPY(b, 1);
348  PG_RETURN_POINTER(result);
349 }
350 
351 Datum
353 {
355  int32 elem = PG_GETARG_INT32(1);
356  int32 c;
357  int32 *aa;
358  int32 n = 0,
359  i;
360 
361  CHECKARRVALID(a);
362  if (!ARRISEMPTY(a))
363  {
364  c = ARRNELEMS(a);
365  aa = ARRPTR(a);
366  for (i = 0; i < c; i++)
367  {
368  if (aa[i] != elem)
369  {
370  if (i > n)
371  aa[n++] = aa[i];
372  else
373  n++;
374  }
375  }
376  a = resize_intArrayType(a, n);
377  }
379 }
380 
381 Datum
383 {
385  ArrayType *result;
386 
387  result = intarray_add_elem(a, PG_GETARG_INT32(1));
388  PG_FREE_IF_COPY(a, 0);
389  QSORT(result, 1);
391 }
392 
393 Datum
395 {
398  ArrayType *result;
399  int32 ca;
400  int32 cb;
401  int32 *aa,
402  *bb,
403  *r;
404  int32 n = 0,
405  i = 0,
406  k = 0;
407 
408  CHECKARRVALID(a);
409  CHECKARRVALID(b);
410 
411  QSORT(a, 1);
412  a = _int_unique(a);
413  ca = ARRNELEMS(a);
414  QSORT(b, 1);
415  b = _int_unique(b);
416  cb = ARRNELEMS(b);
417  result = new_intArrayType(ca);
418  aa = ARRPTR(a);
419  bb = ARRPTR(b);
420  r = ARRPTR(result);
421  while (i < ca)
422  {
423  if (k == cb || aa[i] < bb[k])
424  r[n++] = aa[i++];
425  else if (aa[i] == bb[k])
426  {
427  i++;
428  k++;
429  }
430  else
431  k++;
432  }
433  result = resize_intArrayType(result, n);
434  pfree(a);
435  pfree(b);
436  PG_RETURN_POINTER(result);
437 }
Datum icount(PG_FUNCTION_ARGS)
Definition: _int_op.c:190
ArrayType * intarray_concat_arrays(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:362
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Datum intset(PG_FUNCTION_ARGS)
Definition: _int_op.c:184
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define VARDATA(PTR)
Definition: postgres.h:305
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:245
#define VARSIZE(PTR)
Definition: postgres.h:306
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define VARHDRSZ
Definition: c.h:441
#define CHECKARRVALID(x)
Definition: _int.h:18
PG_MODULE_MAGIC
Definition: _int_op.c:9
#define PREPAREARR(x)
Definition: _int.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
Datum subarray(PG_FUNCTION_ARGS)
Definition: _int_op.c:278
int errcode(int sqlerrcode)
Definition: elog.c:575
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
ArrayType * inner_int_inter(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:134
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define SORT(x)
Definition: _int.h:29
signed int int32
Definition: c.h:253
Datum intarray_del_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:352
#define QSORT(a, direction)
Definition: _int.h:168
int32 intarray_match_first(ArrayType *a, int32 elem)
Definition: _int_tool.c:329
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
Datum intset_union_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:382
Datum _int_inter(PG_FUNCTION_ARGS)
Definition: _int_op.c:148
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:218
bool inner_int_overlap(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:48
ArrayType * resize_intArrayType(ArrayType *a, int num)
Definition: _int_tool.c:238
char * c
Datum intarray_push_elem(PG_FUNCTION_ARGS)
Definition: _int_op.c:328
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:200
#define ARRISEMPTY(x)
Definition: _int.h:26
#define DatumGetBool(X)
Definition: postgres.h:401
Datum intset_subtract(PG_FUNCTION_ARGS)
Definition: _int_op.c:394
PG_FUNCTION_INFO_V1(_int_different)
Datum _int_contains(PG_FUNCTION_ARGS)
Definition: _int_op.c:29
#define ereport(elevel, rest)
Definition: elog.h:122
bool inner_int_contains(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:13
Datum _int_union(PG_FUNCTION_ARGS)
Definition: _int_op.c:127
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:220
ArrayType * _int_unique(ArrayType *a)
Definition: _int_tool.c:294
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
Datum sort_desc(PG_FUNCTION_ARGS)
Definition: _int_op.c:242
Datum _int_overlap(PG_FUNCTION_ARGS)
Definition: _int_op.c:104
ArrayType * inner_int_union(ArrayType *a, ArrayType *b)
Definition: _int_tool.c:77
Datum _int_same(PG_FUNCTION_ARGS)
Definition: _int_op.c:59
#define NULL
Definition: c.h:226
ArrayType * intarray_add_elem(ArrayType *a, int32 elem)
Definition: _int_tool.c:345
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define ARRNELEMS(x)
Definition: cube.c:27
Datum sort_asc(PG_FUNCTION_ARGS)
Definition: _int_op.c:232
Datum _int_contained(PG_FUNCTION_ARGS)
Definition: _int_op.c:20
Datum intarray_push_array(PG_FUNCTION_ARGS)
Definition: _int_op.c:339
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
int i
Definition: c.h:435
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
#define TRUE
Definition: c.h:214
#define ARRPTR(x)
Definition: cube.c:26
Datum _int_different(PG_FUNCTION_ARGS)
Definition: _int_op.c:47
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum uniq(PG_FUNCTION_ARGS)
Definition: _int_op.c:252
ArrayType * int_to_intset(int32 elem)
Definition: _int_tool.c:379