dbTalk Databases Forums  

sum 2 arrays of general int type

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss sum 2 arrays of general int type in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Leonardo Francalanci
 
Posts: n/a

Default sum 2 arrays of general int type - 10-14-2004 , 08:33 AM






I wrote a function to sum arrays.
It works, but I had to cast the data pointer to int64 (because my arrays
are 'int8[]'):

int64* ptr1 = ARR_DATA_PTR(v1);

What if I want to write a more general function that adds values of 2
arrays of every int type? How could I do it?

Here is the function (if you find any errors tell me...):

PG_FUNCTION_INFO_V1(array_sum);

Datum
array_add(PG_FUNCTION_ARGS)
{
Oid element_type;
ArrayType *result, *v1, *v2;
char *dat1, *dat2;
int ndatabytes, ndims1, ndims2;



v1 = PG_GETARG_ARRAYTYPE_P(0);
v2 = PG_GETARG_ARRAYTYPE_P(1);
ndims1 = ARR_NDIM(v1);
ndims2 = ARR_NDIM(v2);
dat1 = ARR_DATA_PTR(v1);
dat2 = ARR_DATA_PTR(v2);
int dim1 = ARR_DIMS(v1)[0];
int dim2 = ARR_DIMS(v2)[0];
int *lbs = ARR_LBOUND(v1);
ndatabytes = ARR_SIZE(v1) - ARR_OVERHEAD(ndims1);


int nbytes = ndatabytes + ARR_OVERHEAD(ndims1);

result = (ArrayType *) palloc(nbytes);
element_type = ARR_ELEMTYPE(v1);
result->size = ARR_SIZE(v1);//nbytes;
result->ndim = ARR_NDIM(v1);
result->flags = 0;
result->elemtype = element_type;
memcpy(ARR_DIMS(result), &dim1, sizeof(int));
memcpy(ARR_LBOUND(result), lbs, sizeof(int));
int64* ptr = ARR_DATA_PTR(result);
int64* ptr1 = ARR_DATA_PTR(v1);
int64* ptr2 = ARR_DATA_PTR(v2);
int i = 0;
for (; i < dim1; i++)
{
ptr[i]=ptr1[i]+ptr2[i];
}

PG_RETURN_ARRAYTYPE_P(result);
}

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.