Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * contrib/intarray/_int_tool.c
3 : : */
4 : : #include "postgres.h"
5 : :
6 : : #include <limits.h>
7 : :
8 : : #include "_int.h"
9 : : #include "catalog/pg_type.h"
10 : : #include "common/int.h"
11 : : #include "lib/qunique.h"
12 : :
13 : : /* arguments are assumed sorted & unique-ified */
14 : : bool
8364 bruce@momjian.us 15 :CBC 94837 : inner_int_contains(ArrayType *a, ArrayType *b)
16 : : {
17 : : int na,
18 : : nb;
19 : : int i,
20 : : j,
21 : : n;
22 : : int *da,
23 : : *db;
24 : :
25 : 94837 : na = ARRNELEMS(a);
26 : 94837 : nb = ARRNELEMS(b);
27 [ - + ]: 94837 : da = ARRPTR(a);
28 [ - + ]: 94837 : db = ARRPTR(b);
29 : :
30 : 94837 : i = j = n = 0;
31 [ + + + + ]: 233763 : while (i < na && j < nb)
32 : : {
33 [ + + ]: 226871 : if (da[i] < db[j])
34 : 132098 : i++;
35 [ + + ]: 94773 : else if (da[i] == db[j])
36 : : {
37 : 6828 : n++;
38 : 6828 : i++;
39 : 6828 : j++;
40 : : }
41 : : else
5595 tgl@sss.pgh.pa.us 42 : 87945 : break; /* db[j] is not in da */
43 : : }
44 : :
1700 michael@paquier.xyz 45 : 94837 : return (n == nb);
46 : : }
47 : :
48 : : /* arguments are assumed sorted */
49 : : bool
8364 bruce@momjian.us 50 : 23321 : inner_int_overlap(ArrayType *a, ArrayType *b)
51 : : {
52 : : int na,
53 : : nb;
54 : : int i,
55 : : j;
56 : : int *da,
57 : : *db;
58 : :
59 : 23321 : na = ARRNELEMS(a);
60 : 23321 : nb = ARRNELEMS(b);
61 [ - + ]: 23321 : da = ARRPTR(a);
62 [ - + ]: 23321 : db = ARRPTR(b);
63 : :
64 : 23321 : i = j = 0;
65 [ + + + + ]: 109463 : while (i < na && j < nb)
66 : : {
67 [ + + ]: 89064 : if (da[i] < db[j])
68 : 45290 : i++;
69 [ + + ]: 43774 : else if (da[i] == db[j])
3184 peter_e@gmx.net 70 : 2922 : return true;
71 : : else
8364 bruce@momjian.us 72 : 40852 : j++;
73 : : }
74 : :
3184 peter_e@gmx.net 75 : 20399 : return false;
76 : : }
77 : :
78 : : ArrayType *
8364 bruce@momjian.us 79 : 2189795 : inner_int_union(ArrayType *a, ArrayType *b)
80 : : {
81 : 2189795 : ArrayType *r = NULL;
82 : :
7472 tgl@sss.pgh.pa.us 83 [ - + - - : 2189795 : CHECKARRVALID(a);
- - ]
84 [ - + - - : 2189795 : CHECKARRVALID(b);
- - ]
85 : :
5595 86 [ + + + + ]: 2189795 : if (ARRISEMPTY(a) && ARRISEMPTY(b))
8364 bruce@momjian.us 87 : 169 : return new_intArrayType(0);
5595 tgl@sss.pgh.pa.us 88 [ + + ]: 2189626 : if (ARRISEMPTY(a))
8364 bruce@momjian.us 89 : 12133 : r = copy_intArrayType(b);
5595 tgl@sss.pgh.pa.us 90 [ + + ]: 2189626 : if (ARRISEMPTY(b))
8364 bruce@momjian.us 91 : 4403 : r = copy_intArrayType(a);
92 : :
7300 teodor@sigaev.ru 93 [ + + ]: 2189626 : if (!r)
94 : : {
7153 bruce@momjian.us 95 : 2173090 : int na = ARRNELEMS(a),
96 : 2173090 : nb = ARRNELEMS(b);
97 [ - + ]: 2173090 : int *da = ARRPTR(a),
98 [ - + ]: 2173090 : *db = ARRPTR(b);
99 : : int i,
100 : : j,
101 : : *dr;
102 : :
8364 103 : 2173090 : r = new_intArrayType(na + nb);
104 [ - + ]: 2173090 : dr = ARRPTR(r);
105 : :
106 : : /* union */
107 : 2173090 : i = j = 0;
7153 108 [ + + + + ]: 82883221 : while (i < na && j < nb)
109 : : {
110 [ + + ]: 80710131 : if (da[i] == db[j])
111 : : {
7300 teodor@sigaev.ru 112 : 5285082 : *dr++ = da[i++];
113 : 5285082 : j++;
114 : : }
7153 bruce@momjian.us 115 [ + + ]: 75425049 : else if (da[i] < db[j])
8364 116 : 63578160 : *dr++ = da[i++];
117 : : else
118 : 11846889 : *dr++ = db[j++];
119 : : }
120 : :
121 [ + + ]: 7607355 : while (i < na)
122 : 5434265 : *dr++ = da[i++];
123 [ + + ]: 4130356 : while (j < nb)
124 : 1957266 : *dr++ = db[j++];
125 : :
7153 126 [ - + ]: 2173090 : r = resize_intArrayType(r, dr - ARRPTR(r));
127 : : }
128 : :
8364 129 [ + + ]: 2189626 : if (ARRNELEMS(r) > 1)
130 : 2189622 : r = _int_unique(r);
131 : :
132 : 2189626 : return r;
133 : : }
134 : :
135 : : ArrayType *
136 : 1745655 : inner_int_inter(ArrayType *a, ArrayType *b)
137 : : {
138 : : ArrayType *r;
139 : : int na,
140 : : nb;
141 : : int *da,
142 : : *db,
143 : : *dr;
144 : : int i,
145 : : j,
146 : : k;
147 : :
5595 tgl@sss.pgh.pa.us 148 [ + + + + ]: 1745655 : if (ARRISEMPTY(a) || ARRISEMPTY(b))
8364 bruce@momjian.us 149 : 16145 : return new_intArrayType(0);
150 : :
151 : 1729510 : na = ARRNELEMS(a);
152 : 1729510 : nb = ARRNELEMS(b);
153 [ - + ]: 1729510 : da = ARRPTR(a);
154 [ - + ]: 1729510 : db = ARRPTR(b);
7866 tgl@sss.pgh.pa.us 155 : 1729510 : r = new_intArrayType(Min(na, nb));
8364 bruce@momjian.us 156 [ - + ]: 1729510 : dr = ARRPTR(r);
157 : :
5192 tgl@sss.pgh.pa.us 158 : 1729510 : i = j = k = 0;
8364 bruce@momjian.us 159 [ + + + + ]: 21910456 : while (i < na && j < nb)
160 : : {
161 [ + + ]: 20180946 : if (da[i] < db[j])
162 : 9234177 : i++;
163 [ + + ]: 10946769 : else if (da[i] == db[j])
164 : : {
5192 tgl@sss.pgh.pa.us 165 [ + + + - ]: 1849943 : if (k == 0 || dr[k - 1] != db[j])
166 : 1849943 : dr[k++] = db[j];
8364 bruce@momjian.us 167 : 1849943 : i++;
168 : 1849943 : j++;
169 : : }
170 : : else
171 : 9096826 : j++;
172 : : }
173 : :
5192 tgl@sss.pgh.pa.us 174 [ + + ]: 1729510 : if (k == 0)
175 : : {
8364 bruce@momjian.us 176 : 1368597 : pfree(r);
177 : 1368597 : return new_intArrayType(0);
178 : : }
179 : : else
5192 tgl@sss.pgh.pa.us 180 : 360913 : return resize_intArrayType(r, k);
181 : : }
182 : :
183 : : void
8364 bruce@momjian.us 184 : 4250942 : rt__int_size(ArrayType *a, float *size)
185 : : {
186 : 4250942 : *size = (float) ARRNELEMS(a);
187 : 4250942 : }
188 : :
189 : : /* comparison function for isort() and _int_unique() */
190 : : static inline int
4069 tgl@sss.pgh.pa.us 191 : 338198327 : isort_cmp(const void *a, const void *b, void *arg)
192 : : {
193 : 338198327 : int32 aval = *((const int32 *) a);
194 : 338198327 : int32 bval = *((const int32 *) b);
195 : :
441 john.naylor@postgres 196 [ + + ]: 338198327 : if (*((bool *) arg))
197 : : {
198 : : /* compare for ascending order */
199 [ + + ]: 338198321 : if (aval < bval)
200 : 145646700 : return -1;
201 [ + + ]: 192551621 : if (aval > bval)
202 : 191451327 : return 1;
203 : : }
204 : : else
205 : : {
206 [ + + ]: 6 : if (aval > bval)
207 : 4 : return -1;
208 [ + - ]: 2 : if (aval < bval)
209 : 2 : return 1;
210 : : }
4069 tgl@sss.pgh.pa.us 211 : 1100294 : return 0;
212 : : }
213 : :
214 : : #define ST_SORT isort
215 : : #define ST_ELEMENT_TYPE int32
216 : : #define ST_COMPARE(a, b, ascending) isort_cmp(a, b, ascending)
217 : : #define ST_COMPARE_ARG_TYPE void
218 : : #define ST_SCOPE
219 : : #define ST_DEFINE
220 : : #include "lib/sort_template.h"
221 : :
222 : : /* Create a new int array with room for "num" elements */
223 : : ArrayType *
8364 bruce@momjian.us 224 : 5389746 : new_intArrayType(int num)
225 : : {
226 : : ArrayType *r;
227 : : int nbytes;
228 : :
229 : : /* if no elements, return a zero-dimensional array */
2857 tgl@sss.pgh.pa.us 230 [ + + ]: 5389746 : if (num <= 0)
231 : : {
2720 rhodiumtoad@postgres 232 [ - + ]: 1384911 : Assert(num == 0);
2857 tgl@sss.pgh.pa.us 233 : 1384911 : r = construct_empty_array(INT4OID);
234 : 1384911 : return r;
235 : : }
236 : :
237 : 4004835 : nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
238 : :
8364 bruce@momjian.us 239 : 4004835 : r = (ArrayType *) palloc0(nbytes);
240 : :
7007 tgl@sss.pgh.pa.us 241 : 4004835 : SET_VARSIZE(r, nbytes);
5595 242 : 4004835 : ARR_NDIM(r) = 1;
7474 243 : 4004835 : r->dataoffset = 0; /* marker for no null bitmap */
8364 bruce@momjian.us 244 : 4004835 : ARR_ELEMTYPE(r) = INT4OID;
5595 tgl@sss.pgh.pa.us 245 : 4004835 : ARR_DIMS(r)[0] = num;
246 : 4004835 : ARR_LBOUND(r)[0] = 1;
247 : :
8364 bruce@momjian.us 248 : 4004835 : return r;
249 : : }
250 : :
251 : : ArrayType *
252 : 5067991 : resize_intArrayType(ArrayType *a, int num)
253 : : {
254 : : int nbytes;
255 : : int i;
256 : :
257 : : /* if no elements, return a zero-dimensional array */
2857 tgl@sss.pgh.pa.us 258 [ + + ]: 5067991 : if (num <= 0)
259 : : {
2720 rhodiumtoad@postgres 260 [ - + ]: 234 : Assert(num == 0);
2464 tgl@sss.pgh.pa.us 261 : 234 : a = construct_empty_array(INT4OID);
4623 bruce@momjian.us 262 : 234 : return a;
263 : : }
264 : :
8364 265 [ + + ]: 5067757 : if (num == ARRNELEMS(a))
266 : 3922045 : return a;
267 : :
2857 tgl@sss.pgh.pa.us 268 [ - + ]: 1145712 : nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
269 : :
8364 bruce@momjian.us 270 : 1145712 : a = (ArrayType *) repalloc(a, nbytes);
271 : :
7007 tgl@sss.pgh.pa.us 272 : 1145712 : SET_VARSIZE(a, nbytes);
273 : : /* usually the array should be 1-D already, but just in case ... */
5595 274 [ + + ]: 2291424 : for (i = 0; i < ARR_NDIM(a); i++)
275 : : {
276 : 1145712 : ARR_DIMS(a)[i] = num;
277 : 1145712 : num = 1;
278 : : }
8364 bruce@momjian.us 279 : 1145712 : return a;
280 : : }
281 : :
282 : : ArrayType *
283 : 17806 : copy_intArrayType(ArrayType *a)
284 : : {
285 : : ArrayType *r;
5595 tgl@sss.pgh.pa.us 286 : 17806 : int n = ARRNELEMS(a);
287 : :
288 : 17806 : r = new_intArrayType(n);
5062 peter_e@gmx.net 289 [ - + - + ]: 17806 : memcpy(ARRPTR(r), ARRPTR(a), n * sizeof(int32));
8364 bruce@momjian.us 290 : 17806 : return r;
291 : : }
292 : :
293 : : /* num for compressed key */
294 : : int
295 : 28795 : internal_size(int *a, int len)
296 : : {
297 : : int i;
2720 rhodiumtoad@postgres 298 : 28795 : int64 size = 0;
299 : :
8364 bruce@momjian.us 300 [ + + ]: 6891607 : for (i = 0; i < len; i += 2)
301 : : {
3240 tgl@sss.pgh.pa.us 302 [ + + + - ]: 6862812 : if (!i || a[i] != a[i - 1]) /* do not count repeated range */
2540 303 : 6862812 : size += (int64) (a[i + 1]) - (int64) (a[i]) + 1;
304 : : }
305 : :
306 [ + - - + ]: 28795 : if (size > (int64) INT_MAX || size < (int64) INT_MIN)
2720 rhodiumtoad@postgres 307 :UBC 0 : return -1; /* overflow */
2720 rhodiumtoad@postgres 308 :CBC 28795 : return (int) size;
309 : : }
310 : :
311 : : /* unique-ify elements of r in-place ... r must be sorted already */
312 : : ArrayType *
8364 bruce@momjian.us 313 : 2531356 : _int_unique(ArrayType *r)
314 : : {
315 : 2531356 : int num = ARRNELEMS(r);
441 john.naylor@postgres 316 : 2531356 : bool ascending = true;
317 : :
2371 tmunro@postgresql.or 318 [ - + ]: 2531356 : num = qunique_arg(ARRPTR(r), num, sizeof(int), isort_cmp,
319 : : &ascending);
320 : :
321 : 2531356 : return resize_intArrayType(r, num);
322 : : }
323 : :
324 : : void
2227 akorotkov@postgresql 325 :UBC 0 : gensign(BITVECP sign, int *a, int len, int siglen)
326 : : {
327 : : int i;
328 : :
329 : : /* we assume that the sign vector is previously zeroed */
8364 bruce@momjian.us 330 [ # # ]: 0 : for (i = 0; i < len; i++)
331 : : {
2227 akorotkov@postgresql 332 : 0 : HASH(sign, *a, siglen);
8364 bruce@momjian.us 333 : 0 : a++;
334 : : }
335 : 0 : }
336 : :
337 : : int32
8364 bruce@momjian.us 338 :CBC 1 : intarray_match_first(ArrayType *a, int32 elem)
339 : : {
340 : : int32 *aa,
341 : : c,
342 : : i;
343 : :
7472 tgl@sss.pgh.pa.us 344 [ - + - - : 1 : CHECKARRVALID(a);
- - ]
345 : 1 : c = ARRNELEMS(a);
8364 bruce@momjian.us 346 [ - + ]: 1 : aa = ARRPTR(a);
347 [ + - ]: 2 : for (i = 0; i < c; i++)
348 [ + + ]: 2 : if (aa[i] == elem)
349 : 1 : return (i + 1);
8364 bruce@momjian.us 350 :UBC 0 : return 0;
351 : : }
352 : :
353 : : ArrayType *
8364 bruce@momjian.us 354 :CBC 4 : intarray_add_elem(ArrayType *a, int32 elem)
355 : : {
356 : : ArrayType *result;
357 : : int32 *r;
358 : : int32 c;
359 : :
7472 tgl@sss.pgh.pa.us 360 [ - + - - : 4 : CHECKARRVALID(a);
- - ]
5595 361 : 4 : c = ARRNELEMS(a);
8364 bruce@momjian.us 362 : 4 : result = new_intArrayType(c + 1);
363 [ - + ]: 4 : r = ARRPTR(result);
364 [ + - ]: 4 : if (c > 0)
365 [ - + ]: 4 : memcpy(r, ARRPTR(a), c * sizeof(int32));
366 : 4 : r[c] = elem;
367 : 4 : return result;
368 : : }
369 : :
370 : : ArrayType *
371 : 1 : intarray_concat_arrays(ArrayType *a, ArrayType *b)
372 : : {
373 : : ArrayType *result;
5595 tgl@sss.pgh.pa.us 374 : 1 : int32 ac = ARRNELEMS(a);
375 : 1 : int32 bc = ARRNELEMS(b);
376 : :
7472 377 [ - + - - : 1 : CHECKARRVALID(a);
- - ]
378 [ - + - - : 1 : CHECKARRVALID(b);
- - ]
8364 bruce@momjian.us 379 : 1 : result = new_intArrayType(ac + bc);
380 [ + - ]: 1 : if (ac)
381 [ - + - + ]: 1 : memcpy(ARRPTR(result), ARRPTR(a), ac * sizeof(int32));
382 [ + - ]: 1 : if (bc)
383 [ - + - + ]: 1 : memcpy(ARRPTR(result) + ac, ARRPTR(b), bc * sizeof(int32));
384 : 1 : return result;
385 : : }
386 : :
387 : : ArrayType *
1321 pg@bowt.ie 388 : 1 : int_to_intset(int32 elem)
389 : : {
390 : : ArrayType *result;
391 : : int32 *aa;
392 : :
8364 bruce@momjian.us 393 : 1 : result = new_intArrayType(1);
394 [ - + ]: 1 : aa = ARRPTR(result);
1321 pg@bowt.ie 395 : 1 : aa[0] = elem;
8364 bruce@momjian.us 396 : 1 : return result;
397 : : }
|