PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
_int_tool.c File Reference
#include "postgres.h"
#include <limits.h>
#include "_int.h"
#include "catalog/pg_type.h"
#include "common/int.h"
#include "lib/qunique.h"
#include "lib/sort_template.h"
Include dependency graph for _int_tool.c:

Go to the source code of this file.

Macros

#define ST_SORT   isort
 
#define ST_ELEMENT_TYPE   int32
 
#define ST_COMPARE(a, b, ascending)   isort_cmp(a, b, ascending)
 
#define ST_COMPARE_ARG_TYPE   void
 
#define ST_SCOPE
 
#define ST_DEFINE
 

Functions

bool inner_int_contains (ArrayType *a, ArrayType *b)
 
bool inner_int_overlap (ArrayType *a, ArrayType *b)
 
ArrayTypeinner_int_union (ArrayType *a, ArrayType *b)
 
ArrayTypeinner_int_inter (ArrayType *a, ArrayType *b)
 
void rt__int_size (ArrayType *a, float *size)
 
static int isort_cmp (const void *a, const void *b, void *arg)
 
ArrayTypenew_intArrayType (int num)
 
ArrayTyperesize_intArrayType (ArrayType *a, int num)
 
ArrayTypecopy_intArrayType (ArrayType *a)
 
int internal_size (int *a, int len)
 
ArrayType_int_unique (ArrayType *r)
 
void gensign (BITVECP sign, int *a, int len, int siglen)
 
int32 intarray_match_first (ArrayType *a, int32 elem)
 
ArrayTypeintarray_add_elem (ArrayType *a, int32 elem)
 
ArrayTypeintarray_concat_arrays (ArrayType *a, ArrayType *b)
 
ArrayTypeint_to_intset (int32 elem)
 

Macro Definition Documentation

◆ ST_COMPARE

#define ST_COMPARE (   a,
  b,
  ascending 
)    isort_cmp(a, b, ascending)

Definition at line 216 of file _int_tool.c.

◆ ST_COMPARE_ARG_TYPE

#define ST_COMPARE_ARG_TYPE   void

Definition at line 217 of file _int_tool.c.

◆ ST_DEFINE

#define ST_DEFINE

Definition at line 219 of file _int_tool.c.

◆ ST_ELEMENT_TYPE

#define ST_ELEMENT_TYPE   int32

Definition at line 215 of file _int_tool.c.

◆ ST_SCOPE

#define ST_SCOPE

Definition at line 218 of file _int_tool.c.

◆ ST_SORT

#define ST_SORT   isort

Definition at line 214 of file _int_tool.c.

Function Documentation

◆ _int_unique()

ArrayType * _int_unique ( ArrayType r)

Definition at line 313 of file _int_tool.c.

314{
315 int num = ARRNELEMS(r);
316 bool ascending = true;
317
318 num = qunique_arg(ARRPTR(r), num, sizeof(int), isort_cmp,
319 &ascending);
320
321 return resize_intArrayType(r, num);
322}
ArrayType * resize_intArrayType(ArrayType *a, int num)
Definition: _int_tool.c:252
static int isort_cmp(const void *a, const void *b, void *arg)
Definition: _int_tool.c:191
#define ARRNELEMS(x)
Definition: cube.c:29
#define ARRPTR(x)
Definition: cube.c:28
static size_t qunique_arg(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *, void *), void *arg)
Definition: qunique.h:46

References ARRNELEMS, ARRPTR, isort_cmp(), qunique_arg(), and resize_intArrayType().

Referenced by g_int_union(), inner_int_union(), intset_subtract(), intset_union_elem(), and uniq().

◆ copy_intArrayType()

ArrayType * copy_intArrayType ( ArrayType a)

Definition at line 283 of file _int_tool.c.

284{
285 ArrayType *r;
286 int n = ARRNELEMS(a);
287
288 r = new_intArrayType(n);
289 memcpy(ARRPTR(r), ARRPTR(a), n * sizeof(int32));
290 return r;
291}
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:224
int32_t int32
Definition: c.h:498
int a
Definition: isn.c:73

References a, ARRNELEMS, ARRPTR, and new_intArrayType().

Referenced by g_int_picksplit(), and inner_int_union().

◆ gensign()

void gensign ( BITVECP  sign,
int *  a,
int  len,
int  siglen 
)

Definition at line 325 of file _int_tool.c.

326{
327 int i;
328
329 /* we assume that the sign vector is previously zeroed */
330 for (i = 0; i < len; i++)
331 {
332 HASH(sign, *a, siglen);
333 a++;
334 }
335}
#define HASH(sign, val, siglen)
Definition: hstore_gist.c:46
char sign
Definition: informix.c:693
int i
Definition: isn.c:77
const void size_t len

References a, HASH, i, len, and sign.

◆ inner_int_contains()

bool inner_int_contains ( ArrayType a,
ArrayType b 
)

Definition at line 15 of file _int_tool.c.

16{
17 int na,
18 nb;
19 int i,
20 j,
21 n;
22 int *da,
23 *db;
24
25 na = ARRNELEMS(a);
26 nb = ARRNELEMS(b);
27 da = ARRPTR(a);
28 db = ARRPTR(b);
29
30 i = j = n = 0;
31 while (i < na && j < nb)
32 {
33 if (da[i] < db[j])
34 i++;
35 else if (da[i] == db[j])
36 {
37 n++;
38 i++;
39 j++;
40 }
41 else
42 break; /* db[j] is not in da */
43 }
44
45 return (n == nb);
46}
int b
Definition: isn.c:74
int j
Definition: isn.c:78

References a, ARRNELEMS, ARRPTR, b, i, and j.

Referenced by _int_contains(), and g_int_consistent().

◆ inner_int_inter()

ArrayType * inner_int_inter ( ArrayType a,
ArrayType b 
)

Definition at line 136 of file _int_tool.c.

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 if (ARRISEMPTY(a) || ARRISEMPTY(b))
149 return new_intArrayType(0);
150
151 na = ARRNELEMS(a);
152 nb = ARRNELEMS(b);
153 da = ARRPTR(a);
154 db = ARRPTR(b);
155 r = new_intArrayType(Min(na, nb));
156 dr = ARRPTR(r);
157
158 i = j = k = 0;
159 while (i < na && j < nb)
160 {
161 if (da[i] < db[j])
162 i++;
163 else if (da[i] == db[j])
164 {
165 if (k == 0 || dr[k - 1] != db[j])
166 dr[k++] = db[j];
167 i++;
168 j++;
169 }
170 else
171 j++;
172 }
173
174 if (k == 0)
175 {
176 pfree(r);
177 return new_intArrayType(0);
178 }
179 else
180 return resize_intArrayType(r, k);
181}
#define ARRISEMPTY(x)
Definition: _int.h:38
#define Min(x, y)
Definition: c.h:975
void pfree(void *pointer)
Definition: mcxt.c:2146

References a, ARRISEMPTY, ARRNELEMS, ARRPTR, b, i, j, Min, new_intArrayType(), pfree(), and resize_intArrayType().

Referenced by _int_inter(), and g_int_picksplit().

◆ inner_int_overlap()

bool inner_int_overlap ( ArrayType a,
ArrayType b 
)

Definition at line 50 of file _int_tool.c.

51{
52 int na,
53 nb;
54 int i,
55 j;
56 int *da,
57 *db;
58
59 na = ARRNELEMS(a);
60 nb = ARRNELEMS(b);
61 da = ARRPTR(a);
62 db = ARRPTR(b);
63
64 i = j = 0;
65 while (i < na && j < nb)
66 {
67 if (da[i] < db[j])
68 i++;
69 else if (da[i] == db[j])
70 return true;
71 else
72 j++;
73 }
74
75 return false;
76}

References a, ARRNELEMS, ARRPTR, b, i, and j.

Referenced by _int_overlap(), and g_int_consistent().

◆ inner_int_union()

ArrayType * inner_int_union ( ArrayType a,
ArrayType b 
)

Definition at line 79 of file _int_tool.c.

80{
81 ArrayType *r = NULL;
82
85
86 if (ARRISEMPTY(a) && ARRISEMPTY(b))
87 return new_intArrayType(0);
88 if (ARRISEMPTY(a))
90 if (ARRISEMPTY(b))
92
93 if (!r)
94 {
95 int na = ARRNELEMS(a),
96 nb = ARRNELEMS(b);
97 int *da = ARRPTR(a),
98 *db = ARRPTR(b);
99 int i,
100 j,
101 *dr;
102
103 r = new_intArrayType(na + nb);
104 dr = ARRPTR(r);
105
106 /* union */
107 i = j = 0;
108 while (i < na && j < nb)
109 {
110 if (da[i] == db[j])
111 {
112 *dr++ = da[i++];
113 j++;
114 }
115 else if (da[i] < db[j])
116 *dr++ = da[i++];
117 else
118 *dr++ = db[j++];
119 }
120
121 while (i < na)
122 *dr++ = da[i++];
123 while (j < nb)
124 *dr++ = db[j++];
125
126 r = resize_intArrayType(r, dr - ARRPTR(r));
127 }
128
129 if (ARRNELEMS(r) > 1)
130 r = _int_unique(r);
131
132 return r;
133}
#define CHECKARRVALID(x)
Definition: _int.h:30
ArrayType * copy_intArrayType(ArrayType *a)
Definition: _int_tool.c:283
ArrayType * _int_unique(ArrayType *r)
Definition: _int_tool.c:313

References _int_unique(), a, ARRISEMPTY, ARRNELEMS, ARRPTR, b, CHECKARRVALID, copy_intArrayType(), i, j, new_intArrayType(), and resize_intArrayType().

Referenced by _int_union(), g_int_penalty(), and g_int_picksplit().

◆ int_to_intset()

ArrayType * int_to_intset ( int32  elem)

Definition at line 388 of file _int_tool.c.

389{
390 ArrayType *result;
391 int32 *aa;
392
393 result = new_intArrayType(1);
394 aa = ARRPTR(result);
395 aa[0] = elem;
396 return result;
397}

References ARRPTR, and new_intArrayType().

Referenced by intset().

◆ intarray_add_elem()

ArrayType * intarray_add_elem ( ArrayType a,
int32  elem 
)

Definition at line 354 of file _int_tool.c.

355{
356 ArrayType *result;
357 int32 *r;
358 int32 c;
359
361 c = ARRNELEMS(a);
362 result = new_intArrayType(c + 1);
363 r = ARRPTR(result);
364 if (c > 0)
365 memcpy(r, ARRPTR(a), c * sizeof(int32));
366 r[c] = elem;
367 return result;
368}
char * c

References a, ARRNELEMS, ARRPTR, CHECKARRVALID, and new_intArrayType().

Referenced by intarray_push_elem(), and intset_union_elem().

◆ intarray_concat_arrays()

ArrayType * intarray_concat_arrays ( ArrayType a,
ArrayType b 
)

Definition at line 371 of file _int_tool.c.

372{
373 ArrayType *result;
374 int32 ac = ARRNELEMS(a);
375 int32 bc = ARRNELEMS(b);
376
379 result = new_intArrayType(ac + bc);
380 if (ac)
381 memcpy(ARRPTR(result), ARRPTR(a), ac * sizeof(int32));
382 if (bc)
383 memcpy(ARRPTR(result) + ac, ARRPTR(b), bc * sizeof(int32));
384 return result;
385}

References a, ARRNELEMS, ARRPTR, b, CHECKARRVALID, and new_intArrayType().

Referenced by intarray_push_array().

◆ intarray_match_first()

int32 intarray_match_first ( ArrayType a,
int32  elem 
)

Definition at line 338 of file _int_tool.c.

339{
340 int32 *aa,
341 c,
342 i;
343
345 c = ARRNELEMS(a);
346 aa = ARRPTR(a);
347 for (i = 0; i < c; i++)
348 if (aa[i] == elem)
349 return (i + 1);
350 return 0;
351}

References a, ARRNELEMS, ARRPTR, CHECKARRVALID, and i.

Referenced by idx().

◆ internal_size()

int internal_size ( int *  a,
int  len 
)

Definition at line 295 of file _int_tool.c.

296{
297 int i;
298 int64 size = 0;
299
300 for (i = 0; i < len; i += 2)
301 {
302 if (!i || a[i] != a[i - 1]) /* do not count repeated range */
303 size += (int64) (a[i + 1]) - (int64) (a[i]) + 1;
304 }
305
306 if (size > (int64) INT_MAX || size < (int64) INT_MIN)
307 return -1; /* overflow */
308 return (int) size;
309}
int64_t int64
Definition: c.h:499

References a, i, and len.

Referenced by g_int_compress(), and g_int_decompress().

◆ isort_cmp()

static int isort_cmp ( const void *  a,
const void *  b,
void *  arg 
)
inlinestatic

Definition at line 191 of file _int_tool.c.

192{
193 int32 aval = *((const int32 *) a);
194 int32 bval = *((const int32 *) b);
195
196 if (*((bool *) arg))
197 {
198 /* compare for ascending order */
199 if (aval < bval)
200 return -1;
201 if (aval > bval)
202 return 1;
203 }
204 else
205 {
206 if (aval > bval)
207 return -1;
208 if (aval < bval)
209 return 1;
210 }
211 return 0;
212}
void * arg

References a, arg, and b.

Referenced by _int_unique().

◆ new_intArrayType()

ArrayType * new_intArrayType ( int  num)

Definition at line 224 of file _int_tool.c.

225{
226 ArrayType *r;
227 int nbytes;
228
229 /* if no elements, return a zero-dimensional array */
230 if (num <= 0)
231 {
232 Assert(num == 0);
233 r = construct_empty_array(INT4OID);
234 return r;
235 }
236
237 nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
238
239 r = (ArrayType *) palloc0(nbytes);
240
241 SET_VARSIZE(r, nbytes);
242 ARR_NDIM(r) = 1;
243 r->dataoffset = 0; /* marker for no null bitmap */
244 ARR_ELEMTYPE(r) = INT4OID;
245 ARR_DIMS(r)[0] = num;
246 ARR_LBOUND(r)[0] = 1;
247
248 return r;
249}
#define ARR_NDIM(a)
Definition: array.h:290
#define ARR_ELEMTYPE(a)
Definition: array.h:292
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:310
#define ARR_DIMS(a)
Definition: array.h:294
#define ARR_LBOUND(a)
Definition: array.h:296
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3580
Assert(PointerIsAligned(start, uint64))
void * palloc0(Size size)
Definition: mcxt.c:1969
int32 dataoffset
Definition: array.h:96
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References ARR_DIMS, ARR_ELEMTYPE, ARR_LBOUND, ARR_NDIM, ARR_OVERHEAD_NONULLS, Assert(), construct_empty_array(), ArrayType::dataoffset, palloc0(), and SET_VARSIZE.

Referenced by copy_intArrayType(), g_int_decompress(), g_int_union(), inner_int_inter(), inner_int_union(), int_to_intset(), intarray_add_elem(), intarray_concat_arrays(), intset_subtract(), and subarray().

◆ resize_intArrayType()

ArrayType * resize_intArrayType ( ArrayType a,
int  num 
)

Definition at line 252 of file _int_tool.c.

253{
254 int nbytes;
255 int i;
256
257 /* if no elements, return a zero-dimensional array */
258 if (num <= 0)
259 {
260 Assert(num == 0);
261 a = construct_empty_array(INT4OID);
262 return a;
263 }
264
265 if (num == ARRNELEMS(a))
266 return a;
267
268 nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
269
270 a = (ArrayType *) repalloc(a, nbytes);
271
272 SET_VARSIZE(a, nbytes);
273 /* usually the array should be 1-D already, but just in case ... */
274 for (i = 0; i < ARR_NDIM(a); i++)
275 {
276 ARR_DIMS(a)[i] = num;
277 num = 1;
278 }
279 return a;
280}
#define ARR_DATA_OFFSET(a)
Definition: array.h:316
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:2166

References a, ARR_DATA_OFFSET, ARR_DIMS, ARR_NDIM, ARRNELEMS, Assert(), construct_empty_array(), i, repalloc(), and SET_VARSIZE.

Referenced by _int_unique(), g_int_compress(), inner_int_inter(), inner_int_union(), intarray_del_elem(), and intset_subtract().

◆ rt__int_size()

void rt__int_size ( ArrayType a,
float *  size 
)

Definition at line 184 of file _int_tool.c.

185{
186 *size = (float) ARRNELEMS(a);
187}

References a, and ARRNELEMS.

Referenced by g_int_penalty(), and g_int_picksplit().