PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
_int_tool.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "_int.h"
Include dependency graph for _int_tool.c:

Go to the source code of this file.

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)
 
bool isort (int32 *a, int len)
 
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 (BITVEC sign, int *a, int len)
 
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 n)
 
int compASC (const void *a, const void *b)
 
int compDESC (const void *a, const void *b)
 

Function Documentation

ArrayType* _int_unique ( ArrayType r)

Definition at line 294 of file _int_tool.c.

References ARRNELEMS, ARRPTR, and resize_intArrayType().

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

295 {
296  int *tmp,
297  *dr,
298  *data;
299  int num = ARRNELEMS(r);
300 
301  if (num < 2)
302  return r;
303 
304  data = tmp = dr = ARRPTR(r);
305  while (tmp - data < num)
306  {
307  if (*tmp != *dr)
308  *(++dr) = *tmp++;
309  else
310  tmp++;
311  }
312  return resize_intArrayType(r, dr + 1 - ARRPTR(r));
313 }
ArrayType * resize_intArrayType(ArrayType *a, int num)
Definition: _int_tool.c:238
#define ARRNELEMS(x)
Definition: cube.c:27
#define ARRPTR(x)
Definition: cube.c:26
int compASC ( const void *  a,
const void *  b 
)

Definition at line 391 of file _int_tool.c.

392 {
393  if (*(const int32 *) a == *(const int32 *) b)
394  return 0;
395  return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
396 }
signed int int32
Definition: c.h:256
int compDESC ( const void *  a,
const void *  b 
)

Definition at line 399 of file _int_tool.c.

400 {
401  if (*(const int32 *) a == *(const int32 *) b)
402  return 0;
403  return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
404 }
signed int int32
Definition: c.h:256
ArrayType* copy_intArrayType ( ArrayType a)

Definition at line 266 of file _int_tool.c.

References ARRNELEMS, ARRPTR, and new_intArrayType().

Referenced by g_int_picksplit(), and inner_int_union().

267 {
268  ArrayType *r;
269  int n = ARRNELEMS(a);
270 
271  r = new_intArrayType(n);
272  memcpy(ARRPTR(r), ARRPTR(a), n * sizeof(int32));
273  return r;
274 }
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:220
signed int int32
Definition: c.h:256
#define ARRNELEMS(x)
Definition: cube.c:27
#define ARRPTR(x)
Definition: cube.c:26
void gensign ( BITVEC  sign,
int *  a,
int  len 
)

Definition at line 316 of file _int_tool.c.

References HASH, and i.

317 {
318  int i;
319 
320  /* we assume that the sign vector is previously zeroed */
321  for (i = 0; i < len; i++)
322  {
323  HASH(sign, *a);
324  a++;
325  }
326 }
#define HASH(sign, val)
Definition: hstore_gist.c:38
char sign
Definition: informix.c:693
int i
bool inner_int_contains ( ArrayType a,
ArrayType b 
)

Definition at line 13 of file _int_tool.c.

References ARRNELEMS, ARRPTR, FALSE, i, and TRUE.

Referenced by _int_contains(), and g_int_consistent().

14 {
15  int na,
16  nb;
17  int i,
18  j,
19  n;
20  int *da,
21  *db;
22 
23  na = ARRNELEMS(a);
24  nb = ARRNELEMS(b);
25  da = ARRPTR(a);
26  db = ARRPTR(b);
27 
28  i = j = n = 0;
29  while (i < na && j < nb)
30  {
31  if (da[i] < db[j])
32  i++;
33  else if (da[i] == db[j])
34  {
35  n++;
36  i++;
37  j++;
38  }
39  else
40  break; /* db[j] is not in da */
41  }
42 
43  return (n == nb) ? TRUE : FALSE;
44 }
#define FALSE
Definition: c.h:221
#define ARRNELEMS(x)
Definition: cube.c:27
int i
#define TRUE
Definition: c.h:217
#define ARRPTR(x)
Definition: cube.c:26
ArrayType* inner_int_inter ( ArrayType a,
ArrayType b 
)

Definition at line 134 of file _int_tool.c.

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

Referenced by _int_inter(), and g_int_picksplit().

135 {
136  ArrayType *r;
137  int na,
138  nb;
139  int *da,
140  *db,
141  *dr;
142  int i,
143  j,
144  k;
145 
146  if (ARRISEMPTY(a) || ARRISEMPTY(b))
147  return new_intArrayType(0);
148 
149  na = ARRNELEMS(a);
150  nb = ARRNELEMS(b);
151  da = ARRPTR(a);
152  db = ARRPTR(b);
153  r = new_intArrayType(Min(na, nb));
154  dr = ARRPTR(r);
155 
156  i = j = k = 0;
157  while (i < na && j < nb)
158  {
159  if (da[i] < db[j])
160  i++;
161  else if (da[i] == db[j])
162  {
163  if (k == 0 || dr[k - 1] != db[j])
164  dr[k++] = db[j];
165  i++;
166  j++;
167  }
168  else
169  j++;
170  }
171 
172  if (k == 0)
173  {
174  pfree(r);
175  return new_intArrayType(0);
176  }
177  else
178  return resize_intArrayType(r, k);
179 }
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:220
#define Min(x, y)
Definition: c.h:806
ArrayType * resize_intArrayType(ArrayType *a, int num)
Definition: _int_tool.c:238
void pfree(void *pointer)
Definition: mcxt.c:950
#define ARRISEMPTY(x)
Definition: _int.h:26
#define ARRNELEMS(x)
Definition: cube.c:27
int i
#define ARRPTR(x)
Definition: cube.c:26
bool inner_int_overlap ( ArrayType a,
ArrayType b 
)

Definition at line 48 of file _int_tool.c.

References ARRNELEMS, ARRPTR, FALSE, i, and TRUE.

Referenced by _int_overlap(), and g_int_consistent().

49 {
50  int na,
51  nb;
52  int i,
53  j;
54  int *da,
55  *db;
56 
57  na = ARRNELEMS(a);
58  nb = ARRNELEMS(b);
59  da = ARRPTR(a);
60  db = ARRPTR(b);
61 
62  i = j = 0;
63  while (i < na && j < nb)
64  {
65  if (da[i] < db[j])
66  i++;
67  else if (da[i] == db[j])
68  return TRUE;
69  else
70  j++;
71  }
72 
73  return FALSE;
74 }
#define FALSE
Definition: c.h:221
#define ARRNELEMS(x)
Definition: cube.c:27
int i
#define TRUE
Definition: c.h:217
#define ARRPTR(x)
Definition: cube.c:26
ArrayType* inner_int_union ( ArrayType a,
ArrayType b 
)

Definition at line 77 of file _int_tool.c.

References _int_unique(), ARRISEMPTY, ARRNELEMS, ARRPTR, CHECKARRVALID, copy_intArrayType(), i, new_intArrayType(), NULL, and resize_intArrayType().

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

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

Definition at line 379 of file _int_tool.c.

References ARRPTR, new_intArrayType(), and result.

Referenced by intset().

380 {
381  ArrayType *result;
382  int32 *aa;
383 
384  result = new_intArrayType(1);
385  aa = ARRPTR(result);
386  aa[0] = n;
387  return result;
388 }
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:220
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
#define ARRPTR(x)
Definition: cube.c:26
ArrayType* intarray_add_elem ( ArrayType a,
int32  elem 
)

Definition at line 345 of file _int_tool.c.

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

Referenced by intarray_push_elem(), and intset_union_elem().

346 {
347  ArrayType *result;
348  int32 *r;
349  int32 c;
350 
351  CHECKARRVALID(a);
352  c = ARRNELEMS(a);
353  result = new_intArrayType(c + 1);
354  r = ARRPTR(result);
355  if (c > 0)
356  memcpy(r, ARRPTR(a), c * sizeof(int32));
357  r[c] = elem;
358  return result;
359 }
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:220
#define CHECKARRVALID(x)
Definition: _int.h:18
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
char * c
#define ARRNELEMS(x)
Definition: cube.c:27
#define ARRPTR(x)
Definition: cube.c:26
ArrayType* intarray_concat_arrays ( ArrayType a,
ArrayType b 
)

Definition at line 362 of file _int_tool.c.

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

Referenced by intarray_push_array().

363 {
364  ArrayType *result;
365  int32 ac = ARRNELEMS(a);
366  int32 bc = ARRNELEMS(b);
367 
368  CHECKARRVALID(a);
369  CHECKARRVALID(b);
370  result = new_intArrayType(ac + bc);
371  if (ac)
372  memcpy(ARRPTR(result), ARRPTR(a), ac * sizeof(int32));
373  if (bc)
374  memcpy(ARRPTR(result) + ac, ARRPTR(b), bc * sizeof(int32));
375  return result;
376 }
ArrayType * new_intArrayType(int num)
Definition: _int_tool.c:220
#define CHECKARRVALID(x)
Definition: _int.h:18
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
#define ARRNELEMS(x)
Definition: cube.c:27
#define ARRPTR(x)
Definition: cube.c:26
int32 intarray_match_first ( ArrayType a,
int32  elem 
)

Definition at line 329 of file _int_tool.c.

References ARRNELEMS, ARRPTR, CHECKARRVALID, and i.

Referenced by idx().

330 {
331  int32 *aa,
332  c,
333  i;
334 
335  CHECKARRVALID(a);
336  c = ARRNELEMS(a);
337  aa = ARRPTR(a);
338  for (i = 0; i < c; i++)
339  if (aa[i] == elem)
340  return (i + 1);
341  return 0;
342 }
#define CHECKARRVALID(x)
Definition: _int.h:18
signed int int32
Definition: c.h:256
char * c
#define ARRNELEMS(x)
Definition: cube.c:27
int i
#define ARRPTR(x)
Definition: cube.c:26
int internal_size ( int *  a,
int  len 
)

Definition at line 278 of file _int_tool.c.

References i.

Referenced by g_int_decompress().

279 {
280  int i,
281  size = 0;
282 
283  for (i = 0; i < len; i += 2)
284  {
285  if (!i || a[i] != a[i - 1]) /* do not count repeated range */
286  size += a[i + 1] - a[i] + 1;
287  }
288 
289  return size;
290 }
int i
bool isort ( int32 a,
int  len 
)

Definition at line 210 of file _int_tool.c.

References isort_cmp(), and qsort_arg().

211 {
212  bool r = false;
213 
214  qsort_arg(a, len, sizeof(int32), isort_cmp, (void *) &r);
215  return r;
216 }
signed int int32
Definition: c.h:256
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
static int isort_cmp(const void *a, const void *b, void *arg)
Definition: _int_tool.c:189
static int isort_cmp ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 189 of file _int_tool.c.

Referenced by isort().

190 {
191  int32 aval = *((const int32 *) a);
192  int32 bval = *((const int32 *) b);
193 
194  if (aval < bval)
195  return -1;
196  if (aval > bval)
197  return 1;
198 
199  /*
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.
203  */
204  *((bool *) arg) = true;
205  return 0;
206 }
signed int int32
Definition: c.h:256
void * arg
ArrayType* new_intArrayType ( int  num)

Definition at line 220 of file _int_tool.c.

References ARR_DIMS, ARR_ELEMTYPE, ARR_LBOUND, ARR_NDIM, ARR_OVERHEAD_NONULLS, ArrayType::dataoffset, INT4OID, 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().

221 {
222  ArrayType *r;
223  int nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
224 
225  r = (ArrayType *) palloc0(nbytes);
226 
227  SET_VARSIZE(r, nbytes);
228  ARR_NDIM(r) = 1;
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;
233 
234  return r;
235 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
int32 dataoffset
Definition: array.h:80
#define INT4OID
Definition: pg_type.h:316
#define ARR_LBOUND(a)
Definition: array.h:277
#define ARR_DIMS(a)
Definition: array.h:275
void * palloc0(Size size)
Definition: mcxt.c:878
#define ARR_NDIM(a)
Definition: array.h:271
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define ARR_ELEMTYPE(a)
Definition: array.h:273
ArrayType* resize_intArrayType ( ArrayType a,
int  num 
)

Definition at line 238 of file _int_tool.c.

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

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

239 {
240  int nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
241  int i;
242 
243  /* if no elements, return a zero-dimensional array */
244  if (num == 0)
245  {
246  ARR_NDIM(a) = 0;
247  return a;
248  }
249 
250  if (num == ARRNELEMS(a))
251  return a;
252 
253  a = (ArrayType *) repalloc(a, nbytes);
254 
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++)
258  {
259  ARR_DIMS(a)[i] = num;
260  num = 1;
261  }
262  return a;
263 }
#define ARR_DATA_OFFSET(a)
Definition: array.h:297
#define ARR_DIMS(a)
Definition: array.h:275
#define ARR_NDIM(a)
Definition: array.h:271
#define ARRNELEMS(x)
Definition: cube.c:27
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
void rt__int_size ( ArrayType a,
float *  size 
)

Definition at line 182 of file _int_tool.c.

References ARRNELEMS.

Referenced by g_int_penalty(), and g_int_picksplit().

183 {
184  *size = (float) ARRNELEMS(a);
185 }
#define ARRNELEMS(x)
Definition: cube.c:27