Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions | Variables

_int_op.c File Reference

#include "postgres.h"
#include "_int.h"
Include dependency graph for _int_op.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (_int_different)
 PG_FUNCTION_INFO_V1 (_int_same)
 PG_FUNCTION_INFO_V1 (_int_contains)
 PG_FUNCTION_INFO_V1 (_int_contained)
 PG_FUNCTION_INFO_V1 (_int_overlap)
 PG_FUNCTION_INFO_V1 (_int_union)
 PG_FUNCTION_INFO_V1 (_int_inter)
Datum _int_different (PG_FUNCTION_ARGS)
Datum _int_same (PG_FUNCTION_ARGS)
Datum _int_contains (PG_FUNCTION_ARGS)
Datum _int_contained (PG_FUNCTION_ARGS)
Datum _int_overlap (PG_FUNCTION_ARGS)
Datum _int_union (PG_FUNCTION_ARGS)
Datum _int_inter (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (intset)
 PG_FUNCTION_INFO_V1 (icount)
 PG_FUNCTION_INFO_V1 (sort)
 PG_FUNCTION_INFO_V1 (sort_asc)
 PG_FUNCTION_INFO_V1 (sort_desc)
 PG_FUNCTION_INFO_V1 (uniq)
 PG_FUNCTION_INFO_V1 (idx)
 PG_FUNCTION_INFO_V1 (subarray)
 PG_FUNCTION_INFO_V1 (intarray_push_elem)
 PG_FUNCTION_INFO_V1 (intarray_push_array)
 PG_FUNCTION_INFO_V1 (intarray_del_elem)
 PG_FUNCTION_INFO_V1 (intset_union_elem)
 PG_FUNCTION_INFO_V1 (intset_subtract)
Datum intset (PG_FUNCTION_ARGS)
Datum icount (PG_FUNCTION_ARGS)
Datum sort (PG_FUNCTION_ARGS)
Datum sort_asc (PG_FUNCTION_ARGS)
Datum sort_desc (PG_FUNCTION_ARGS)
Datum uniq (PG_FUNCTION_ARGS)
Datum idx (PG_FUNCTION_ARGS)
Datum subarray (PG_FUNCTION_ARGS)
Datum intarray_push_elem (PG_FUNCTION_ARGS)
Datum intarray_push_array (PG_FUNCTION_ARGS)
Datum intarray_del_elem (PG_FUNCTION_ARGS)
Datum intset_union_elem (PG_FUNCTION_ARGS)
Datum intset_subtract (PG_FUNCTION_ARGS)

Variables

 PG_MODULE_MAGIC

Function Documentation

Datum _int_contained ( PG_FUNCTION_ARGS   ) 

Definition at line 29 of file _int_op.c.

References _int_contains(), DirectFunctionCall2, and PG_GETARG_DATUM.

{
    /* just reverse the operands and call _int_contains */
    return DirectFunctionCall2(_int_contains,
                               PG_GETARG_DATUM(1),
                               PG_GETARG_DATUM(0));
}

Datum _int_contains ( PG_FUNCTION_ARGS   ) 

Definition at line 38 of file _int_op.c.

References CHECKARRVALID, inner_int_contains(), pfree(), PG_GETARG_ARRAYTYPE_P_COPY, PG_RETURN_BOOL, and PREPAREARR.

Referenced by _int_contained().

{
    /* Force copy so we can modify the arrays in-place */
    ArrayType  *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
    ArrayType  *b = PG_GETARG_ARRAYTYPE_P_COPY(1);
    bool        res;

    CHECKARRVALID(a);
    CHECKARRVALID(b);
    PREPAREARR(a);
    PREPAREARR(b);
    res = inner_int_contains(a, b);
    pfree(a);
    pfree(b);
    PG_RETURN_BOOL(res);
}

Datum _int_different ( PG_FUNCTION_ARGS   ) 
Datum _int_inter ( PG_FUNCTION_ARGS   ) 
Datum _int_overlap ( PG_FUNCTION_ARGS   ) 

Definition at line 113 of file _int_op.c.

References ARRISEMPTY, CHECKARRVALID, inner_int_overlap(), pfree(), PG_GETARG_ARRAYTYPE_P_COPY, PG_RETURN_BOOL, and SORT.

{
    ArrayType  *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
    ArrayType  *b = PG_GETARG_ARRAYTYPE_P_COPY(1);
    bool        result;

    CHECKARRVALID(a);
    CHECKARRVALID(b);
    if (ARRISEMPTY(a) || ARRISEMPTY(b))
        return FALSE;

    SORT(a);
    SORT(b);

    result = inner_int_overlap(a, b);

    pfree(a);
    pfree(b);

    PG_RETURN_BOOL(result);
}

Datum _int_same ( PG_FUNCTION_ARGS   ) 

Definition at line 68 of file _int_op.c.

References ARRNELEMS, ARRPTR, CHECKARRVALID, pfree(), PG_GETARG_ARRAYTYPE_P_COPY, PG_RETURN_BOOL, and SORT.

Referenced by _int_different().

{
    ArrayType  *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
    ArrayType  *b = PG_GETARG_ARRAYTYPE_P_COPY(1);
    int         na,
                nb;
    int         n;
    int        *da,
               *db;
    bool        result;

    CHECKARRVALID(a);
    CHECKARRVALID(b);
    na = ARRNELEMS(a);
    nb = ARRNELEMS(b);
    da = ARRPTR(a);
    db = ARRPTR(b);

    result = FALSE;

    if (na == nb)
    {
        SORT(a);
        SORT(b);
        result = TRUE;

        for (n = 0; n < na; n++)
        {
            if (da[n] != db[n])
            {
                result = FALSE;
                break;
            }
        }
    }

    pfree(a);
    pfree(b);

    PG_RETURN_BOOL(result);
}

Datum _int_union ( PG_FUNCTION_ARGS   ) 
Datum icount ( PG_FUNCTION_ARGS   ) 

Definition at line 212 of file _int_op.c.

References ARRNELEMS, PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

Datum idx ( PG_FUNCTION_ARGS   ) 
Datum intarray_del_elem ( PG_FUNCTION_ARGS   ) 

Definition at line 374 of file _int_op.c.

References ARRISEMPTY, ARRNELEMS, ARRPTR, CHECKARRVALID, i, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT32, PG_RETURN_POINTER, and resize_intArrayType().

{
    ArrayType  *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
    int32       elem = PG_GETARG_INT32(1);
    int32       c;
    int32      *aa;
    int32       n = 0,
                i;

    CHECKARRVALID(a);
    if (!ARRISEMPTY(a))
    {
        c = ARRNELEMS(a);
        aa = ARRPTR(a);
        for (i = 0; i < c; i++)
        {
            if (aa[i] != elem)
            {
                if (i > n)
                    aa[n++] = aa[i];
                else
                    n++;
            }
        }
        a = resize_intArrayType(a, n);
    }
    PG_RETURN_POINTER(a);
}

Datum intarray_push_array ( PG_FUNCTION_ARGS   ) 
Datum intarray_push_elem ( PG_FUNCTION_ARGS   ) 
Datum intset ( PG_FUNCTION_ARGS   ) 
Datum intset_subtract ( PG_FUNCTION_ARGS   ) 

Definition at line 416 of file _int_op.c.

References _int_unique(), ARRNELEMS, ARRPTR, CHECKARRVALID, i, new_intArrayType(), pfree(), PG_GETARG_ARRAYTYPE_P_COPY, PG_RETURN_POINTER, QSORT, and resize_intArrayType().

{
    ArrayType  *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
    ArrayType  *b = PG_GETARG_ARRAYTYPE_P_COPY(1);
    ArrayType  *result;
    int32       ca;
    int32       cb;
    int32      *aa,
               *bb,
               *r;
    int32       n = 0,
                i = 0,
                k = 0;

    CHECKARRVALID(a);
    CHECKARRVALID(b);

    QSORT(a, 1);
    a = _int_unique(a);
    ca = ARRNELEMS(a);
    QSORT(b, 1);
    b = _int_unique(b);
    cb = ARRNELEMS(b);
    result = new_intArrayType(ca);
    aa = ARRPTR(a);
    bb = ARRPTR(b);
    r = ARRPTR(result);
    while (i < ca)
    {
        if (k == cb || aa[i] < bb[k])
            r[n++] = aa[i++];
        else if (aa[i] == bb[k])
        {
            i++;
            k++;
        }
        else
            k++;
    }
    result = resize_intArrayType(result, n);
    pfree(a);
    pfree(b);
    PG_RETURN_POINTER(result);
}

Datum intset_union_elem ( PG_FUNCTION_ARGS   ) 
PG_FUNCTION_INFO_V1 ( intset_union_elem   ) 
PG_FUNCTION_INFO_V1 ( intarray_del_elem   ) 
PG_FUNCTION_INFO_V1 ( intarray_push_array   ) 
PG_FUNCTION_INFO_V1 ( intarray_push_elem   ) 
PG_FUNCTION_INFO_V1 ( subarray   ) 
PG_FUNCTION_INFO_V1 ( intset_subtract   ) 
PG_FUNCTION_INFO_V1 ( _int_different   ) 
PG_FUNCTION_INFO_V1 ( icount   ) 
PG_FUNCTION_INFO_V1 ( _int_contained   ) 
PG_FUNCTION_INFO_V1 ( sort_asc   ) 
PG_FUNCTION_INFO_V1 ( _int_overlap   ) 
PG_FUNCTION_INFO_V1 ( intset   ) 
PG_FUNCTION_INFO_V1 ( _int_inter   ) 
PG_FUNCTION_INFO_V1 ( _int_contains   ) 
PG_FUNCTION_INFO_V1 ( _int_union   ) 
PG_FUNCTION_INFO_V1 ( idx   ) 
PG_FUNCTION_INFO_V1 ( uniq   ) 
PG_FUNCTION_INFO_V1 ( sort_desc   ) 
PG_FUNCTION_INFO_V1 ( _int_same   ) 
PG_FUNCTION_INFO_V1 ( sort   ) 
Datum sort ( PG_FUNCTION_ARGS   ) 

Definition at line 222 of file _int_op.c.

References ARRNELEMS, CHECKARRVALID, ereport, errcode(), errmsg(), ERROR, NULL, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_TEXT_P, PG_RETURN_POINTER, QSORT, VARDATA, VARHDRSZ, and VARSIZE.

{
    ArrayType  *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
    text       *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_P(1) : NULL;
    int32       dc = (dirstr) ? VARSIZE(dirstr) - VARHDRSZ : 0;
    char       *d = (dirstr) ? VARDATA(dirstr) : NULL;
    int         dir = -1;

    CHECKARRVALID(a);
    if (ARRNELEMS(a) < 2)
        PG_RETURN_POINTER(a);

    if (dirstr == NULL || (dc == 3
                           && (d[0] == 'A' || d[0] == 'a')
                           && (d[1] == 'S' || d[1] == 's')
                           && (d[2] == 'C' || d[2] == 'c')))
        dir = 1;
    else if (dc == 4
             && (d[0] == 'D' || d[0] == 'd')
             && (d[1] == 'E' || d[1] == 'e')
             && (d[2] == 'S' || d[2] == 's')
             && (d[3] == 'C' || d[3] == 'c'))
        dir = 0;
    if (dir == -1)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("second parameter must be \"ASC\" or \"DESC\"")));
    QSORT(a, dir);
    PG_RETURN_POINTER(a);
}

Datum sort_asc ( PG_FUNCTION_ARGS   ) 
Datum sort_desc ( PG_FUNCTION_ARGS   ) 
Datum subarray ( PG_FUNCTION_ARGS   ) 

Definition at line 300 of file _int_op.c.

References ARRISEMPTY, ARRNELEMS, ARRPTR, CHECKARRVALID, end, new_intArrayType(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_INT32, and PG_RETURN_POINTER.

{
    ArrayType  *a = PG_GETARG_ARRAYTYPE_P(0);
    int32       start = PG_GETARG_INT32(1);
    int32       len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
    int32       end = 0;
    int32       c;
    ArrayType  *result;

    start = (start > 0) ? start - 1 : start;

    CHECKARRVALID(a);
    if (ARRISEMPTY(a))
    {
        PG_FREE_IF_COPY(a, 0);
        PG_RETURN_POINTER(new_intArrayType(0));
    }

    c = ARRNELEMS(a);

    if (start < 0)
        start = c + start;

    if (len < 0)
        end = c + len;
    else if (len == 0)
        end = c;
    else
        end = start + len;

    if (end > c)
        end = c;

    if (start < 0)
        start = 0;

    if (start >= end || end <= 0)
    {
        PG_FREE_IF_COPY(a, 0);
        PG_RETURN_POINTER(new_intArrayType(0));
    }

    result = new_intArrayType(end - start);
    if (end - start > 0)
        memcpy(ARRPTR(result), ARRPTR(a) + start, (end - start) * sizeof(int32));
    PG_FREE_IF_COPY(a, 0);
    PG_RETURN_POINTER(result);
}

Datum uniq ( PG_FUNCTION_ARGS   ) 

Variable Documentation

Definition at line 10 of file _int_op.c.