2 * contrib/intarray/_int_tool.c
6 #include "catalog/pg_type.h"
11 /* arguments are assumed sorted & unique-ified */
13 inner_int_contains(ArrayType *a, ArrayType *b)
29 while (i < na && j < nb)
33 else if (da[i] == db[j])
40 break; /* db[j] is not in da */
43 return (n == nb) ? TRUE : FALSE;
46 /* arguments are assumed sorted */
48 inner_int_overlap(ArrayType *a, ArrayType *b)
63 while (i < na && j < nb)
67 else if (da[i] == db[j])
77 inner_int_union(ArrayType *a, ArrayType *b)
84 if (ARRISEMPTY(a) && ARRISEMPTY(b))
85 return new_intArrayType(0);
87 r = copy_intArrayType(b);
89 r = copy_intArrayType(a);
93 int na = ARRNELEMS(a),
101 r = new_intArrayType(na + nb);
106 while (i < na && j < nb)
113 else if (da[i] < db[j])
124 r = resize_intArrayType(r, dr - ARRPTR(r));
127 if (ARRNELEMS(r) > 1)
134 inner_int_inter(ArrayType *a, ArrayType *b)
146 if (ARRISEMPTY(a) || ARRISEMPTY(b))
147 return new_intArrayType(0);
153 r = new_intArrayType(Min(na, nb));
157 while (i < na && j < nb)
161 else if (da[i] == db[j])
163 if (k == 0 || dr[k - 1] != db[j])
175 return new_intArrayType(0);
178 return resize_intArrayType(r, k);
182 rt__int_size(ArrayType *a, float *size)
184 *size = (float) ARRNELEMS(a);
187 /* qsort_arg comparison function for isort() */
189 isort_cmp(const void *a, const void *b, void *arg)
191 int32 aval = *((const int32 *) a);
192 int32 bval = *((const int32 *) b);
200 * Report if we have any duplicates. If there are equal keys, qsort must
201 * compare them at some point, else it wouldn't know whether one should go
202 * before or after the other.
204 *((bool *) arg) = true;
208 /* Sort the given data (len >= 2). Return true if any duplicates found */
210 isort(int32 *a, int len)
214 qsort_arg(a, len, sizeof(int32), isort_cmp, (void *) &r);
218 /* Create a new int array with room for "num" elements */
220 new_intArrayType(int num)
223 int nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
225 r = (ArrayType *) palloc0(nbytes);
227 SET_VARSIZE(r, nbytes);
229 r->dataoffset = 0; /* marker for no null bitmap */
230 ARR_ELEMTYPE(r) = INT4OID;
231 ARR_DIMS(r)[0] = num;
232 ARR_LBOUND(r)[0] = 1;
238 resize_intArrayType(ArrayType *a, int num)
240 int nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
243 /* if no elements, return a zero-dimensional array */
250 if (num == ARRNELEMS(a))
253 a = (ArrayType *) repalloc(a, nbytes);
255 SET_VARSIZE(a, nbytes);
256 /* usually the array should be 1-D already, but just in case ... */
257 for (i = 0; i < ARR_NDIM(a); i++)
259 ARR_DIMS(a)[i] = num;
266 copy_intArrayType(ArrayType *a)
269 int n = ARRNELEMS(a);
271 r = new_intArrayType(n);
272 memcpy(ARRPTR(r), ARRPTR(a), n * sizeof(int32));
276 /* num for compressed key */
278 internal_size(int *a, int len)
283 for (i = 0; i < len; i += 2)
285 if (!i || a[i] != a[i - 1]) /* do not count repeated range */
286 size += a[i + 1] - a[i] + 1;
292 /* unique-ify elements of r in-place ... r must be sorted already */
294 _int_unique(ArrayType *r)
299 int num = ARRNELEMS(r);
304 data = tmp = dr = ARRPTR(r);
305 while (tmp - data < num)
312 return resize_intArrayType(r, dr + 1 - ARRPTR(r));
316 gensign(BITVEC sign, int *a, int len)
320 /* we assume that the sign vector is previously zeroed */
321 for (i = 0; i < len; i++)
329 intarray_match_first(ArrayType *a, int32 elem)
338 for (i = 0; i < c; i++)
345 intarray_add_elem(ArrayType *a, int32 elem)
353 result = new_intArrayType(c + 1);
356 memcpy(r, ARRPTR(a), c * sizeof(int32));
362 intarray_concat_arrays(ArrayType *a, ArrayType *b)
365 int32 ac = ARRNELEMS(a);
366 int32 bc = ARRNELEMS(b);
370 result = new_intArrayType(ac + bc);
372 memcpy(ARRPTR(result), ARRPTR(a), ac * sizeof(int32));
374 memcpy(ARRPTR(result) + ac, ARRPTR(b), bc * sizeof(int32));
379 int_to_intset(int32 n)
384 result = new_intArrayType(1);
391 compASC(const void *a, const void *b)
393 if (*(const int32 *) a == *(const int32 *) b)
395 return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
399 compDESC(const void *a, const void *b)
401 if (*(const int32 *) a == *(const int32 *) b)
403 return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;