Proposed patch for contrib/cube - Mailing list pgsql-hackers

From Joshua Reich
Subject Proposed patch for contrib/cube
Date
Msg-id 44BBCAEF.5060007@root.net
Whole thread Raw
Responses Re: Proposed patch for contrib/cube  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hi,

I use the cube datatype a fair bit, and one thing I have always wanted
is the ability to do this:

pg=# select cube_from_arrays('{1,2,3}'::float[], '{3,5,6}'::float[]);
   cube_from_arrays
---------------------
  (1, 2, 3),(3, 5, 6)
(1 row)

That is - build a cube by specifying 2 arrays, one for the UR
coordinate, one for LL.

I hope people find this useful, and if so, we can add it to contrib/cube.

Source is attached.

Thanks,

Joshua Reich
(jdigittl on #postgresql)

#include "postgres.h"
#include "utils/array.h"
#include "cubedata.h"            /* contrib/cube */

/*
**    CREATE OR REPLACE FUNCTION cube_from_arrays(float[], float[]) RETURNS cube
**        AS 'ordermatch'
**        LANGUAGE C;
*/

NDBOX *cube_from_arrays (ArrayType *ur, ArrayType *ll);

/*
** Taken from the intarray contrib header
*/
#define ARRPTR(x)  ( (double *) ARR_DATA_PTR(x) )
#define ARRNELEMS(x)  ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x))


/*
** Allows the construction of a cube from 2 float[]'s
*/
NDBOX *cube_from_arrays (ArrayType *ur, ArrayType *ll)
{
    int        i;
    int        dim;
    int        size;
    NDBOX    *result;
    double    *dur, *dll;

    dim = ARRNELEMS(ur);
    if (ARRNELEMS(ll) < dim)
    {
        /*
        ** If the array's are not of equal length, use the length
        ** of the shorter array.
        */
        dim = ARRNELEMS(ll);
    }

    dur = ARRPTR(ur);
    dll = ARRPTR(ll);

    size = offsetof(NDBOX, x[0]) + sizeof(double) * 2 * dim;
    result = (NDBOX *) palloc (size);
    memset (result, 0, size);
    result->size = size;
    result->dim = dim;

    for (i=0; i<dim; i++)
    {
        result->x[i] = dur[i];
        result->x[i+dim] = dll[i];
    }

    return result;
}

pgsql-hackers by date:

Previous
From: Neil Conway
Date:
Subject: Re: plPHP and plRuby
Next
From: Tom Lane
Date:
Subject: Re: Proposed patch for contrib/cube