mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-11 15:16:54 +08:00
DRY code for sorting vector arrays [skip ci]
This commit is contained in:
@@ -143,6 +143,22 @@ CompareBitVectors(const void *a, const void *b)
|
|||||||
return DirectFunctionCall2(bitcmp, VarBitPGetDatum((VarBit *) a), VarBitPGetDatum((VarBit *) b));
|
return DirectFunctionCall2(bitcmp, VarBitPGetDatum((VarBit *) a), VarBitPGetDatum((VarBit *) b));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sort vector array
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
SortVectorArray(VectorArray arr, IvfflatType type)
|
||||||
|
{
|
||||||
|
if (type == IVFFLAT_TYPE_VECTOR)
|
||||||
|
qsort(arr->items, arr->length, arr->itemsize, CompareVectors);
|
||||||
|
else if (type == IVFFLAT_TYPE_HALFVEC)
|
||||||
|
qsort(arr->items, arr->length, arr->itemsize, CompareHalfVectors);
|
||||||
|
else if (type == IVFFLAT_TYPE_BIT)
|
||||||
|
qsort(arr->items, arr->length, arr->itemsize, CompareBitVectors);
|
||||||
|
else
|
||||||
|
elog(ERROR, "Unsupported type");
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Quick approach if we have little data
|
* Quick approach if we have little data
|
||||||
*/
|
*/
|
||||||
@@ -157,14 +173,7 @@ QuickCenters(Relation index, VectorArray samples, VectorArray centers, IvfflatTy
|
|||||||
/* Copy existing vectors while avoiding duplicates */
|
/* Copy existing vectors while avoiding duplicates */
|
||||||
if (samples->length > 0)
|
if (samples->length > 0)
|
||||||
{
|
{
|
||||||
if (type == IVFFLAT_TYPE_VECTOR)
|
SortVectorArray(samples, type);
|
||||||
qsort(samples->items, samples->length, samples->itemsize, CompareVectors);
|
|
||||||
else if (type == IVFFLAT_TYPE_HALFVEC)
|
|
||||||
qsort(samples->items, samples->length, samples->itemsize, CompareHalfVectors);
|
|
||||||
else if (type == IVFFLAT_TYPE_BIT)
|
|
||||||
qsort(samples->items, samples->length, samples->itemsize, CompareBitVectors);
|
|
||||||
else
|
|
||||||
elog(ERROR, "Unsupported type");
|
|
||||||
|
|
||||||
for (int i = 0; i < samples->length; i++)
|
for (int i = 0; i < samples->length; i++)
|
||||||
{
|
{
|
||||||
@@ -708,13 +717,7 @@ CheckCenters(Relation index, VectorArray centers, IvfflatType type)
|
|||||||
if (type != IVFFLAT_TYPE_BIT)
|
if (type != IVFFLAT_TYPE_BIT)
|
||||||
{
|
{
|
||||||
/* Ensure no duplicate centers */
|
/* Ensure no duplicate centers */
|
||||||
/* Fine to sort in-place */
|
SortVectorArray(centers, type);
|
||||||
if (type == IVFFLAT_TYPE_VECTOR)
|
|
||||||
qsort(centers->items, centers->length, centers->itemsize, CompareVectors);
|
|
||||||
else if (type == IVFFLAT_TYPE_HALFVEC)
|
|
||||||
qsort(centers->items, centers->length, centers->itemsize, CompareHalfVectors);
|
|
||||||
else
|
|
||||||
elog(ERROR, "Unsupported type");
|
|
||||||
|
|
||||||
for (int i = 1; i < centers->length; i++)
|
for (int i = 1; i < centers->length; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user