* svec_count - Count the number of non-zero entries in the input vector * Right argument is capped at 1, then added to the left */
| 387 | * Right argument is capped at 1, then added to the left |
| 388 | */ |
| 389 | Datum svec_count(PG_FUNCTION_ARGS) |
| 390 | { |
| 391 | SvecType * svec1 = PG_GETARG_SVECTYPE_P(0); |
| 392 | SvecType * svec2 = PG_GETARG_SVECTYPE_P(1); |
| 393 | SparseData left = sdata_from_svec(svec1); |
| 394 | SparseData right = sdata_from_svec(svec2); |
| 395 | |
| 396 | if (IS_SCALAR(svec1)) { |
| 397 | /* |
| 398 | * If the left argument is {1}:{0}, this is the first call to |
| 399 | * the routine, and we need a zero vector for the beginning |
| 400 | * of the accumulation of the correct dimension. |
| 401 | */ |
| 402 | double * left_vals = (double *)(left->vals->data); |
| 403 | if (left_vals[0] == 0) |
| 404 | left = makeSparseDataFromDouble(0.,right->total_value_count); |
| 405 | } |
| 406 | double *right_vals=(double *)(right->vals->data); |
| 407 | SvecType *result; |
| 408 | double *clamped_vals; |
| 409 | SparseData right_clamped,sdata_result; |
| 410 | |
| 411 | if (left->total_value_count != right->total_value_count) |
| 412 | ereport(ERROR, |
| 413 | (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 414 | errmsg("Array dimension of inputs are not the same: dim1=%d, dim2=%d\n", |
| 415 | left->total_value_count, right->total_value_count))); |
| 416 | |
| 417 | /* Create an array of values either 1 or 0 depending on whether |
| 418 | * the right vector has a non-zero value in it |
| 419 | */ |
| 420 | clamped_vals = |
| 421 | (double *)palloc0(sizeof(double)*(right->unique_value_count)); |
| 422 | |
| 423 | for (int i=0;i<(right->unique_value_count);i++) |
| 424 | { |
| 425 | if (right_vals[i] != 0. && !IS_NVP(right_vals[i])) |
| 426 | clamped_vals[i] = 1.; |
| 427 | } |
| 428 | right_clamped = makeInplaceSparseData( |
| 429 | (char *)clamped_vals,right->index->data, |
| 430 | right->vals->len,right->index->len,FLOAT8OID, |
| 431 | right->unique_value_count, |
| 432 | right->total_value_count); |
| 433 | |
| 434 | /* Create the output SVEC */ |
| 435 | sdata_result = op_sdata_by_sdata(add,left,right_clamped); |
| 436 | result = svec_from_sparsedata(sdata_result,true); |
| 437 | |
| 438 | pfree(clamped_vals); |
| 439 | pfree(right_clamped); |
| 440 | |
| 441 | PG_RETURN_SVECTYPE_P(result); |
| 442 | } |
| 443 | |
| 444 | PG_FUNCTION_INFO_V1( svec_l2norm ); |
| 445 | /** |
nothing calls this directly
no test coverage detected