LCOV - differential code coverage report
Current view: top level - contrib/intarray - _int_tool.c (source / functions) Coverage Total Hit UBC CBC
Current: 7a15cff1f11193467898da1c1fabf06fd2caee04 vs 84a3778c79c2d28b4dc281d03ef2ab019b16483b Lines: 96.2 % 184 177 7 177
Current Date: 2025-12-15 18:36:29 -0500 Functions: 93.8 % 16 15 1 15
Baseline: lcov-20251216-010103-baseline Branches: 64.1 % 184 118 66 118
Baseline Date: 2025-12-15 13:30:48 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 10 10 10
(360..) days: 96.0 % 174 167 7 167
Function coverage date bins:
(360..) days: 93.8 % 16 15 1 15
Branch coverage date bins:
(30,360] days: 90.0 % 10 9 1 9
(360..) days: 62.6 % 174 109 65 109

 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
 8224 bruce@momjian.us           15                 :CBC       96418 : 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                 :          96418 :     na = ARRNELEMS(a);
                                 26                 :          96418 :     nb = ARRNELEMS(b);
                                 27         [ -  + ]:          96418 :     da = ARRPTR(a);
                                 28         [ -  + ]:          96418 :     db = ARRPTR(b);
                                 29                 :                : 
                                 30                 :          96418 :     i = j = n = 0;
                                 31   [ +  +  +  + ]:         235407 :     while (i < na && j < nb)
                                 32                 :                :     {
                                 33         [ +  + ]:         228518 :         if (da[i] < db[j])
                                 34                 :         132354 :             i++;
                                 35         [ +  + ]:          96164 :         else if (da[i] == db[j])
                                 36                 :                :         {
                                 37                 :           6635 :             n++;
                                 38                 :           6635 :             i++;
                                 39                 :           6635 :             j++;
                                 40                 :                :         }
                                 41                 :                :         else
 5455 tgl@sss.pgh.pa.us          42                 :          89529 :             break;              /* db[j] is not in da */
                                 43                 :                :     }
                                 44                 :                : 
 1560 michael@paquier.xyz        45                 :          96418 :     return (n == nb);
                                 46                 :                : }
                                 47                 :                : 
                                 48                 :                : /* arguments are assumed sorted */
                                 49                 :                : bool
 8224 bruce@momjian.us           50                 :          24208 : 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                 :          24208 :     na = ARRNELEMS(a);
                                 60                 :          24208 :     nb = ARRNELEMS(b);
                                 61         [ -  + ]:          24208 :     da = ARRPTR(a);
                                 62         [ -  + ]:          24208 :     db = ARRPTR(b);
                                 63                 :                : 
                                 64                 :          24208 :     i = j = 0;
                                 65   [ +  +  +  + ]:         112869 :     while (i < na && j < nb)
                                 66                 :                :     {
                                 67         [ +  + ]:          91567 :         if (da[i] < db[j])
                                 68                 :          46032 :             i++;
                                 69         [ +  + ]:          45535 :         else if (da[i] == db[j])
 3044 peter_e@gmx.net            70                 :           2906 :             return true;
                                 71                 :                :         else
 8224 bruce@momjian.us           72                 :          42629 :             j++;
                                 73                 :                :     }
                                 74                 :                : 
 3044 peter_e@gmx.net            75                 :          21302 :     return false;
                                 76                 :                : }
                                 77                 :                : 
                                 78                 :                : ArrayType *
 8224 bruce@momjian.us           79                 :        2199480 : inner_int_union(ArrayType *a, ArrayType *b)
                                 80                 :                : {
                                 81                 :        2199480 :     ArrayType  *r = NULL;
                                 82                 :                : 
 7332 tgl@sss.pgh.pa.us          83   [ -  +  -  -  :        2199480 :     CHECKARRVALID(a);
                                              -  - ]
                                 84   [ -  +  -  -  :        2199480 :     CHECKARRVALID(b);
                                              -  - ]
                                 85                 :                : 
 5455                            86   [ +  +  +  + ]:        2199480 :     if (ARRISEMPTY(a) && ARRISEMPTY(b))
 8224 bruce@momjian.us           87                 :            175 :         return new_intArrayType(0);
 5455 tgl@sss.pgh.pa.us          88         [ +  + ]:        2199305 :     if (ARRISEMPTY(a))
 8224 bruce@momjian.us           89                 :          12762 :         r = copy_intArrayType(b);
 5455 tgl@sss.pgh.pa.us          90         [ +  + ]:        2199305 :     if (ARRISEMPTY(b))
 8224 bruce@momjian.us           91                 :           4124 :         r = copy_intArrayType(a);
                                 92                 :                : 
 7160 teodor@sigaev.ru           93         [ +  + ]:        2199305 :     if (!r)
                                 94                 :                :     {
 7013 bruce@momjian.us           95                 :        2182419 :         int         na = ARRNELEMS(a),
                                 96                 :        2182419 :                     nb = ARRNELEMS(b);
                                 97         [ -  + ]:        2182419 :         int        *da = ARRPTR(a),
                                 98         [ -  + ]:        2182419 :                    *db = ARRPTR(b);
                                 99                 :                :         int         i,
                                100                 :                :                     j,
                                101                 :                :                    *dr;
                                102                 :                : 
 8224                           103                 :        2182419 :         r = new_intArrayType(na + nb);
                                104         [ -  + ]:        2182419 :         dr = ARRPTR(r);
                                105                 :                : 
                                106                 :                :         /* union */
                                107                 :        2182419 :         i = j = 0;
 7013                           108   [ +  +  +  + ]:       78975567 :         while (i < na && j < nb)
                                109                 :                :         {
                                110         [ +  + ]:       76793148 :             if (da[i] == db[j])
                                111                 :                :             {
 7160 teodor@sigaev.ru          112                 :        4876021 :                 *dr++ = da[i++];
                                113                 :        4876021 :                 j++;
                                114                 :                :             }
 7013 bruce@momjian.us          115         [ +  + ]:       71917127 :             else if (da[i] < db[j])
 8224                           116                 :       60366017 :                 *dr++ = da[i++];
                                117                 :                :             else
                                118                 :       11551110 :                 *dr++ = db[j++];
                                119                 :                :         }
                                120                 :                : 
                                121         [ +  + ]:        7524641 :         while (i < na)
                                122                 :        5342222 :             *dr++ = da[i++];
                                123         [ +  + ]:        4140487 :         while (j < nb)
                                124                 :        1958068 :             *dr++ = db[j++];
                                125                 :                : 
 7013                           126         [ -  + ]:        2182419 :         r = resize_intArrayType(r, dr - ARRPTR(r));
                                127                 :                :     }
                                128                 :                : 
 8224                           129         [ +  + ]:        2199305 :     if (ARRNELEMS(r) > 1)
                                130                 :        2199300 :         r = _int_unique(r);
                                131                 :                : 
                                132                 :        2199305 :     return r;
                                133                 :                : }
                                134                 :                : 
                                135                 :                : ArrayType *
                                136                 :        1779489 : 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                 :                : 
 5455 tgl@sss.pgh.pa.us         148   [ +  +  +  + ]:        1779489 :     if (ARRISEMPTY(a) || ARRISEMPTY(b))
 8224 bruce@momjian.us          149                 :          16491 :         return new_intArrayType(0);
                                150                 :                : 
                                151                 :        1762998 :     na = ARRNELEMS(a);
                                152                 :        1762998 :     nb = ARRNELEMS(b);
                                153         [ -  + ]:        1762998 :     da = ARRPTR(a);
                                154         [ -  + ]:        1762998 :     db = ARRPTR(b);
 7726 tgl@sss.pgh.pa.us         155                 :        1762998 :     r = new_intArrayType(Min(na, nb));
 8224 bruce@momjian.us          156         [ -  + ]:        1762998 :     dr = ARRPTR(r);
                                157                 :                : 
 5052 tgl@sss.pgh.pa.us         158                 :        1762998 :     i = j = k = 0;
 8224 bruce@momjian.us          159   [ +  +  +  + ]:       21684472 :     while (i < na && j < nb)
                                160                 :                :     {
                                161         [ +  + ]:       19921474 :         if (da[i] < db[j])
                                162                 :        9228822 :             i++;
                                163         [ +  + ]:       10692652 :         else if (da[i] == db[j])
                                164                 :                :         {
 5052 tgl@sss.pgh.pa.us         165   [ +  +  +  - ]:        1639309 :             if (k == 0 || dr[k - 1] != db[j])
                                166                 :        1639309 :                 dr[k++] = db[j];
 8224 bruce@momjian.us          167                 :        1639309 :             i++;
                                168                 :        1639309 :             j++;
                                169                 :                :         }
                                170                 :                :         else
                                171                 :        9053343 :             j++;
                                172                 :                :     }
                                173                 :                : 
 5052 tgl@sss.pgh.pa.us         174         [ +  + ]:        1762998 :     if (k == 0)
                                175                 :                :     {
 8224 bruce@momjian.us          176                 :        1395599 :         pfree(r);
                                177                 :        1395599 :         return new_intArrayType(0);
                                178                 :                :     }
                                179                 :                :     else
 5052 tgl@sss.pgh.pa.us         180                 :         367399 :         return resize_intArrayType(r, k);
                                181                 :                : }
                                182                 :                : 
                                183                 :                : void
 8224 bruce@momjian.us          184                 :        4270148 : rt__int_size(ArrayType *a, float *size)
                                185                 :                : {
                                186                 :        4270148 :     *size = (float) ARRNELEMS(a);
                                187                 :        4270148 : }
                                188                 :                : 
                                189                 :                : /* comparison function for isort() and _int_unique() */
                                190                 :                : static inline int
 3929 tgl@sss.pgh.pa.us         191                 :      320164913 : isort_cmp(const void *a, const void *b, void *arg)
                                192                 :                : {
                                193                 :      320164913 :     int32       aval = *((const int32 *) a);
                                194                 :      320164913 :     int32       bval = *((const int32 *) b);
                                195                 :                : 
  301 john.naylor@postgres      196         [ +  + ]:      320164913 :     if (*((bool *) arg))
                                197                 :                :     {
                                198                 :                :         /* compare for ascending order */
                                199         [ +  + ]:      320164907 :         if (aval < bval)
                                200                 :      137508929 :             return -1;
                                201         [ +  + ]:      182655978 :         if (aval > bval)
                                202                 :      181597339 :             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                 :                :     }
 3929 tgl@sss.pgh.pa.us         211                 :        1058639 :     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 *
 8224 bruce@momjian.us          224                 :        5456175 : new_intArrayType(int num)
                                225                 :                : {
                                226                 :                :     ArrayType  *r;
                                227                 :                :     int         nbytes;
                                228                 :                : 
                                229                 :                :     /* if no elements, return a zero-dimensional array */
 2717 tgl@sss.pgh.pa.us         230         [ +  + ]:        5456175 :     if (num <= 0)
                                231                 :                :     {
 2580 rhodiumtoad@postgres      232         [ -  + ]:        1412265 :         Assert(num == 0);
 2717 tgl@sss.pgh.pa.us         233                 :        1412265 :         r = construct_empty_array(INT4OID);
                                234                 :        1412265 :         return r;
                                235                 :                :     }
                                236                 :                : 
                                237                 :        4043910 :     nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
                                238                 :                : 
 8224 bruce@momjian.us          239                 :        4043910 :     r = (ArrayType *) palloc0(nbytes);
                                240                 :                : 
 6867 tgl@sss.pgh.pa.us         241                 :        4043910 :     SET_VARSIZE(r, nbytes);
 5455                           242                 :        4043910 :     ARR_NDIM(r) = 1;
 7334                           243                 :        4043910 :     r->dataoffset = 0;           /* marker for no null bitmap */
 8224 bruce@momjian.us          244                 :        4043910 :     ARR_ELEMTYPE(r) = INT4OID;
 5455 tgl@sss.pgh.pa.us         245                 :        4043910 :     ARR_DIMS(r)[0] = num;
                                246                 :        4043910 :     ARR_LBOUND(r)[0] = 1;
                                247                 :                : 
 8224 bruce@momjian.us          248                 :        4043910 :     return r;
                                249                 :                : }
                                250                 :                : 
                                251                 :                : ArrayType *
                                252                 :        5094504 : resize_intArrayType(ArrayType *a, int num)
                                253                 :                : {
                                254                 :                :     int         nbytes;
                                255                 :                :     int         i;
                                256                 :                : 
                                257                 :                :     /* if no elements, return a zero-dimensional array */
 2717 tgl@sss.pgh.pa.us         258         [ +  + ]:        5094504 :     if (num <= 0)
                                259                 :                :     {
 2580 rhodiumtoad@postgres      260         [ -  + ]:            234 :         Assert(num == 0);
 2324 tgl@sss.pgh.pa.us         261                 :            234 :         a = construct_empty_array(INT4OID);
 4483 bruce@momjian.us          262                 :            234 :         return a;
                                263                 :                :     }
                                264                 :                : 
 8224                           265         [ +  + ]:        5094270 :     if (num == ARRNELEMS(a))
                                266                 :        3955043 :         return a;
                                267                 :                : 
 2717 tgl@sss.pgh.pa.us         268         [ -  + ]:        1139227 :     nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
                                269                 :                : 
 8224 bruce@momjian.us          270                 :        1139227 :     a = (ArrayType *) repalloc(a, nbytes);
                                271                 :                : 
 6867 tgl@sss.pgh.pa.us         272                 :        1139227 :     SET_VARSIZE(a, nbytes);
                                273                 :                :     /* usually the array should be 1-D already, but just in case ... */
 5455                           274         [ +  + ]:        2278454 :     for (i = 0; i < ARR_NDIM(a); i++)
                                275                 :                :     {
                                276                 :        1139227 :         ARR_DIMS(a)[i] = num;
                                277                 :        1139227 :         num = 1;
                                278                 :                :     }
 8224 bruce@momjian.us          279                 :        1139227 :     return a;
                                280                 :                : }
                                281                 :                : 
                                282                 :                : ArrayType *
                                283                 :          18088 : copy_intArrayType(ArrayType *a)
                                284                 :                : {
                                285                 :                :     ArrayType  *r;
 5455 tgl@sss.pgh.pa.us         286                 :          18088 :     int         n = ARRNELEMS(a);
                                287                 :                : 
                                288                 :          18088 :     r = new_intArrayType(n);
 4922 peter_e@gmx.net           289   [ -  +  -  + ]:          18088 :     memcpy(ARRPTR(r), ARRPTR(a), n * sizeof(int32));
 8224 bruce@momjian.us          290                 :          18088 :     return r;
                                291                 :                : }
                                292                 :                : 
                                293                 :                : /* num for compressed key */
                                294                 :                : int
                                295                 :          26011 : internal_size(int *a, int len)
                                296                 :                : {
                                297                 :                :     int         i;
 2580 rhodiumtoad@postgres      298                 :          26011 :     int64       size = 0;
                                299                 :                : 
 8224 bruce@momjian.us          300         [ +  + ]:        6077359 :     for (i = 0; i < len; i += 2)
                                301                 :                :     {
 3100 tgl@sss.pgh.pa.us         302   [ +  +  +  - ]:        6051348 :         if (!i || a[i] != a[i - 1]) /* do not count repeated range */
 2400                           303                 :        6051348 :             size += (int64) (a[i + 1]) - (int64) (a[i]) + 1;
                                304                 :                :     }
                                305                 :                : 
                                306   [ +  -  -  + ]:          26011 :     if (size > (int64) INT_MAX || size < (int64) INT_MIN)
 2580 rhodiumtoad@postgres      307                 :UBC           0 :         return -1;              /* overflow */
 2580 rhodiumtoad@postgres      308                 :CBC       26011 :     return (int) size;
                                309                 :                : }
                                310                 :                : 
                                311                 :                : /* unique-ify elements of r in-place ... r must be sorted already */
                                312                 :                : ArrayType *
 8224 bruce@momjian.us          313                 :        2541866 : _int_unique(ArrayType *r)
                                314                 :                : {
                                315                 :        2541866 :     int         num = ARRNELEMS(r);
  301 john.naylor@postgres      316                 :        2541866 :     bool        ascending = true;
                                317                 :                : 
 2231 tmunro@postgresql.or      318         [ -  + ]:        2541866 :     num = qunique_arg(ARRPTR(r), num, sizeof(int), isort_cmp,
                                319                 :                :                       &ascending);
                                320                 :                : 
                                321                 :        2541866 :     return resize_intArrayType(r, num);
                                322                 :                : }
                                323                 :                : 
                                324                 :                : void
 2087 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 */
 8224 bruce@momjian.us          330         [ #  # ]:              0 :     for (i = 0; i < len; i++)
                                331                 :                :     {
 2087 akorotkov@postgresql      332                 :              0 :         HASH(sign, *a, siglen);
 8224 bruce@momjian.us          333                 :              0 :         a++;
                                334                 :                :     }
                                335                 :              0 : }
                                336                 :                : 
                                337                 :                : int32
 8224 bruce@momjian.us          338                 :CBC           1 : intarray_match_first(ArrayType *a, int32 elem)
                                339                 :                : {
                                340                 :                :     int32      *aa,
                                341                 :                :                 c,
                                342                 :                :                 i;
                                343                 :                : 
 7332 tgl@sss.pgh.pa.us         344   [ -  +  -  -  :              1 :     CHECKARRVALID(a);
                                              -  - ]
                                345                 :              1 :     c = ARRNELEMS(a);
 8224 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);
 8224 bruce@momjian.us          350                 :UBC           0 :     return 0;
                                351                 :                : }
                                352                 :                : 
                                353                 :                : ArrayType *
 8224 bruce@momjian.us          354                 :CBC           4 : intarray_add_elem(ArrayType *a, int32 elem)
                                355                 :                : {
                                356                 :                :     ArrayType  *result;
                                357                 :                :     int32      *r;
                                358                 :                :     int32       c;
                                359                 :                : 
 7332 tgl@sss.pgh.pa.us         360   [ -  +  -  -  :              4 :     CHECKARRVALID(a);
                                              -  - ]
 5455                           361                 :              4 :     c = ARRNELEMS(a);
 8224 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;
 5455 tgl@sss.pgh.pa.us         374                 :              1 :     int32       ac = ARRNELEMS(a);
                                375                 :              1 :     int32       bc = ARRNELEMS(b);
                                376                 :                : 
 7332                           377   [ -  +  -  -  :              1 :     CHECKARRVALID(a);
                                              -  - ]
                                378   [ -  +  -  -  :              1 :     CHECKARRVALID(b);
                                              -  - ]
 8224 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 *
 1181 pg@bowt.ie                388                 :              1 : int_to_intset(int32 elem)
                                389                 :                : {
                                390                 :                :     ArrayType  *result;
                                391                 :                :     int32      *aa;
                                392                 :                : 
 8224 bruce@momjian.us          393                 :              1 :     result = new_intArrayType(1);
                                394         [ -  + ]:              1 :     aa = ARRPTR(result);
 1181 pg@bowt.ie                395                 :              1 :     aa[0] = elem;
 8224 bruce@momjian.us          396                 :              1 :     return result;
                                397                 :                : }
        

Generated by: LCOV version 2.4-beta