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
15 187250 : 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 187250 : na = ARRNELEMS(a);
26 187250 : nb = ARRNELEMS(b);
27 187250 : da = ARRPTR(a);
28 187250 : db = ARRPTR(b);
29 :
30 187250 : i = j = n = 0;
31 467322 : while (i < na && j < nb)
32 : {
33 453580 : if (da[i] < db[j])
34 266480 : i++;
35 187100 : else if (da[i] == db[j])
36 : {
37 13592 : n++;
38 13592 : i++;
39 13592 : j++;
40 : }
41 : else
42 173508 : break; /* db[j] is not in da */
43 : }
44 :
45 187250 : return (n == nb);
46 : }
47 :
48 : /* arguments are assumed sorted */
49 : bool
50 44840 : 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 44840 : na = ARRNELEMS(a);
60 44840 : nb = ARRNELEMS(b);
61 44840 : da = ARRPTR(a);
62 44840 : db = ARRPTR(b);
63 :
64 44840 : i = j = 0;
65 212176 : while (i < na && j < nb)
66 : {
67 173156 : if (da[i] < db[j])
68 89180 : i++;
69 83976 : else if (da[i] == db[j])
70 5820 : return true;
71 : else
72 78156 : j++;
73 : }
74 :
75 39020 : return false;
76 : }
77 :
78 : ArrayType *
79 4519844 : inner_int_union(ArrayType *a, ArrayType *b)
80 : {
81 4519844 : ArrayType *r = NULL;
82 :
83 4519844 : CHECKARRVALID(a);
84 4519844 : CHECKARRVALID(b);
85 :
86 4519844 : if (ARRISEMPTY(a) && ARRISEMPTY(b))
87 232 : return new_intArrayType(0);
88 4519612 : if (ARRISEMPTY(a))
89 27300 : r = copy_intArrayType(b);
90 4519612 : if (ARRISEMPTY(b))
91 8628 : r = copy_intArrayType(a);
92 :
93 4519612 : if (!r)
94 : {
95 4483684 : int na = ARRNELEMS(a),
96 4483684 : nb = ARRNELEMS(b);
97 4483684 : int *da = ARRPTR(a),
98 4483684 : *db = ARRPTR(b);
99 : int i,
100 : j,
101 : *dr;
102 :
103 4483684 : r = new_intArrayType(na + nb);
104 4483684 : dr = ARRPTR(r);
105 :
106 : /* union */
107 4483684 : i = j = 0;
108 168552464 : while (i < na && j < nb)
109 : {
110 164068780 : if (da[i] == db[j])
111 : {
112 10695840 : *dr++ = da[i++];
113 10695840 : j++;
114 : }
115 153372940 : else if (da[i] < db[j])
116 129233884 : *dr++ = da[i++];
117 : else
118 24139056 : *dr++ = db[j++];
119 : }
120 :
121 15726416 : while (i < na)
122 11242732 : *dr++ = da[i++];
123 8659734 : while (j < nb)
124 4176050 : *dr++ = db[j++];
125 :
126 4483684 : r = resize_intArrayType(r, dr - ARRPTR(r));
127 : }
128 :
129 4519612 : if (ARRNELEMS(r) > 1)
130 4519592 : r = _int_unique(r);
131 :
132 4519612 : return r;
133 : }
134 :
135 : ArrayType *
136 3624098 : 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 :
148 3624098 : if (ARRISEMPTY(a) || ARRISEMPTY(b))
149 34958 : return new_intArrayType(0);
150 :
151 3589140 : na = ARRNELEMS(a);
152 3589140 : nb = ARRNELEMS(b);
153 3589140 : da = ARRPTR(a);
154 3589140 : db = ARRPTR(b);
155 3589140 : r = new_intArrayType(Min(na, nb));
156 3589140 : dr = ARRPTR(r);
157 :
158 3589140 : i = j = k = 0;
159 44981150 : while (i < na && j < nb)
160 : {
161 41392010 : if (da[i] < db[j])
162 19030094 : i++;
163 22361916 : else if (da[i] == db[j])
164 : {
165 3712046 : if (k == 0 || dr[k - 1] != db[j])
166 3712046 : dr[k++] = db[j];
167 3712046 : i++;
168 3712046 : j++;
169 : }
170 : else
171 18649870 : j++;
172 : }
173 :
174 3589140 : if (k == 0)
175 : {
176 2853566 : pfree(r);
177 2853566 : return new_intArrayType(0);
178 : }
179 : else
180 735574 : return resize_intArrayType(r, k);
181 : }
182 :
183 : void
184 8774048 : rt__int_size(ArrayType *a, float *size)
185 : {
186 8774048 : *size = (float) ARRNELEMS(a);
187 8774048 : }
188 :
189 : /* comparison function for isort() and _int_unique() */
190 : static inline int
191 696524292 : isort_cmp(const void *a, const void *b, void *arg)
192 : {
193 696524292 : int32 aval = *((const int32 *) a);
194 696524292 : int32 bval = *((const int32 *) b);
195 :
196 696524292 : if (*((bool *) arg))
197 : {
198 : /* compare for ascending order */
199 696524280 : if (aval < bval)
200 301401886 : return -1;
201 395122394 : if (aval > bval)
202 392864932 : return 1;
203 : }
204 : else
205 : {
206 12 : if (aval > bval)
207 8 : return -1;
208 4 : if (aval < bval)
209 4 : return 1;
210 : }
211 2257462 : 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 *
224 11170460 : new_intArrayType(int num)
225 : {
226 : ArrayType *r;
227 : int nbytes;
228 :
229 : /* if no elements, return a zero-dimensional array */
230 11170460 : if (num <= 0)
231 : {
232 : Assert(num == 0);
233 2888756 : r = construct_empty_array(INT4OID);
234 2888756 : return r;
235 : }
236 :
237 8281704 : nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
238 :
239 8281704 : r = (ArrayType *) palloc0(nbytes);
240 :
241 8281704 : SET_VARSIZE(r, nbytes);
242 8281704 : ARR_NDIM(r) = 1;
243 8281704 : r->dataoffset = 0; /* marker for no null bitmap */
244 8281704 : ARR_ELEMTYPE(r) = INT4OID;
245 8281704 : ARR_DIMS(r)[0] = num;
246 8281704 : ARR_LBOUND(r)[0] = 1;
247 :
248 8281704 : return r;
249 : }
250 :
251 : ArrayType *
252 10425226 : resize_intArrayType(ArrayType *a, int num)
253 : {
254 : int nbytes;
255 : int i;
256 :
257 : /* if no elements, return a zero-dimensional array */
258 10425226 : if (num <= 0)
259 : {
260 : Assert(num == 0);
261 468 : a = construct_empty_array(INT4OID);
262 468 : return a;
263 : }
264 :
265 10424758 : if (num == ARRNELEMS(a))
266 8102038 : return a;
267 :
268 2322720 : nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
269 :
270 2322720 : a = (ArrayType *) repalloc(a, nbytes);
271 :
272 2322720 : SET_VARSIZE(a, nbytes);
273 : /* usually the array should be 1-D already, but just in case ... */
274 4645440 : for (i = 0; i < ARR_NDIM(a); i++)
275 : {
276 2322720 : ARR_DIMS(a)[i] = num;
277 2322720 : num = 1;
278 : }
279 2322720 : return a;
280 : }
281 :
282 : ArrayType *
283 38508 : copy_intArrayType(ArrayType *a)
284 : {
285 : ArrayType *r;
286 38508 : int n = ARRNELEMS(a);
287 :
288 38508 : r = new_intArrayType(n);
289 38508 : memcpy(ARRPTR(r), ARRPTR(a), n * sizeof(int32));
290 38508 : return r;
291 : }
292 :
293 : /* num for compressed key */
294 : int
295 57690 : internal_size(int *a, int len)
296 : {
297 : int i;
298 57690 : int64 size = 0;
299 :
300 13801522 : for (i = 0; i < len; i += 2)
301 : {
302 13743832 : if (!i || a[i] != a[i - 1]) /* do not count repeated range */
303 13743832 : size += (int64) (a[i + 1]) - (int64) (a[i]) + 1;
304 : }
305 :
306 57690 : if (size > (int64) INT_MAX || size < (int64) INT_MIN)
307 0 : return -1; /* overflow */
308 57690 : return (int) size;
309 : }
310 :
311 : /* unique-ify elements of r in-place ... r must be sorted already */
312 : ArrayType *
313 5200236 : _int_unique(ArrayType *r)
314 : {
315 5200236 : int num = ARRNELEMS(r);
316 5200236 : bool ascending = true;
317 :
318 5200236 : num = qunique_arg(ARRPTR(r), num, sizeof(int), isort_cmp,
319 : &ascending);
320 :
321 5200236 : return resize_intArrayType(r, num);
322 : }
323 :
324 : void
325 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 */
330 0 : for (i = 0; i < len; i++)
331 : {
332 0 : HASH(sign, *a, siglen);
333 0 : a++;
334 : }
335 0 : }
336 :
337 : int32
338 2 : intarray_match_first(ArrayType *a, int32 elem)
339 : {
340 : int32 *aa,
341 : c,
342 : i;
343 :
344 2 : CHECKARRVALID(a);
345 2 : c = ARRNELEMS(a);
346 2 : aa = ARRPTR(a);
347 4 : for (i = 0; i < c; i++)
348 4 : if (aa[i] == elem)
349 2 : return (i + 1);
350 0 : return 0;
351 : }
352 :
353 : ArrayType *
354 8 : intarray_add_elem(ArrayType *a, int32 elem)
355 : {
356 : ArrayType *result;
357 : int32 *r;
358 : int32 c;
359 :
360 8 : CHECKARRVALID(a);
361 8 : c = ARRNELEMS(a);
362 8 : result = new_intArrayType(c + 1);
363 8 : r = ARRPTR(result);
364 8 : if (c > 0)
365 8 : memcpy(r, ARRPTR(a), c * sizeof(int32));
366 8 : r[c] = elem;
367 8 : return result;
368 : }
369 :
370 : ArrayType *
371 2 : intarray_concat_arrays(ArrayType *a, ArrayType *b)
372 : {
373 : ArrayType *result;
374 2 : int32 ac = ARRNELEMS(a);
375 2 : int32 bc = ARRNELEMS(b);
376 :
377 2 : CHECKARRVALID(a);
378 2 : CHECKARRVALID(b);
379 2 : result = new_intArrayType(ac + bc);
380 2 : if (ac)
381 2 : memcpy(ARRPTR(result), ARRPTR(a), ac * sizeof(int32));
382 2 : if (bc)
383 2 : memcpy(ARRPTR(result) + ac, ARRPTR(b), bc * sizeof(int32));
384 2 : return result;
385 : }
386 :
387 : ArrayType *
388 2 : int_to_intset(int32 elem)
389 : {
390 : ArrayType *result;
391 : int32 *aa;
392 :
393 2 : result = new_intArrayType(1);
394 2 : aa = ARRPTR(result);
395 2 : aa[0] = elem;
396 2 : return result;
397 : }
|