mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-01 02:02:10 +08:00
Added subvector function
This commit is contained in:
31
src/vector.c
31
src/vector.c
@@ -877,6 +877,37 @@ quantize_binary(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_VARBIT_P(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a subvector
|
||||
*/
|
||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(subvector);
|
||||
Datum
|
||||
subvector(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Vector *a = PG_GETARG_VECTOR_P(0);
|
||||
int32 start = PG_GETARG_INT32(1);
|
||||
int32 count = PG_GETARG_INT32(2);
|
||||
int32 end = start + count;
|
||||
float *ax = a->x;
|
||||
Vector *result;
|
||||
int dim;
|
||||
|
||||
/* Indexing starts at 1, like substring */
|
||||
if (start < 1)
|
||||
start = 1;
|
||||
|
||||
if (end > a->dim)
|
||||
end = a->dim + 1;
|
||||
|
||||
dim = end - start;
|
||||
CheckDim(dim);
|
||||
result = InitVector(dim);
|
||||
|
||||
for (int i = 0; i < dim; i++)
|
||||
result->x[i] = ax[start - 1 + i];
|
||||
|
||||
PG_RETURN_POINTER(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal helper to compare vectors
|
||||
|
||||
Reference in New Issue
Block a user