mirror of
https://github.com/pgvector/pgvector.git
synced 2026-12-05 16:58:38 +00:00
Hardened VectorArray functions [skip ci]
This commit is contained in:
1 parent
354cfe066d
commit
568fea3ab6
3 files changed
+28
-14
No files matched your search
+1
-4
@@ -68,10 +68,7 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
||||
}
|
||||
|
||||
if (samples->length < targsamples)
|
||||
{
|
||||
VectorArraySet(samples, samples->length, DatumGetPointer(value));
|
||||
samples->length++;
|
||||
}
|
||||
VectorArrayAdd(samples, DatumGetPointer(value));
|
||||
else
|
||||
{
|
||||
if (buildstate->rowstoskip < 0)
|
||||
|
||||
+24
-3
@@ -324,22 +324,43 @@ static inline Pointer
|
||||
VectorArrayGet(VectorArray arr, int offset)
|
||||
{
|
||||
/* Safety check */
|
||||
if (offset < 0 || offset >= arr->maxlen)
|
||||
if (offset < 0 || offset >= arr->length)
|
||||
elog(ERROR, "index out of bounds");
|
||||
|
||||
return ((char *) arr->items) + (offset * arr->itemsize);
|
||||
}
|
||||
|
||||
static inline void
|
||||
VectorArraySet(VectorArray arr, int offset, Pointer val)
|
||||
VectorArraySetUncheckedLength(VectorArray arr, int offset, Pointer val)
|
||||
{
|
||||
Size size = VARSIZE_ANY(val);
|
||||
|
||||
/* Safety check */
|
||||
if (offset < 0 || offset >= arr->maxlen)
|
||||
elog(ERROR, "index out of bounds");
|
||||
|
||||
/* Safety check */
|
||||
if (size > arr->itemsize)
|
||||
elog(ERROR, "item too large");
|
||||
|
||||
memcpy(VectorArrayGet(arr, offset), val, size);
|
||||
memcpy(((char *) arr->items) + (offset * arr->itemsize), val, size);
|
||||
}
|
||||
|
||||
static inline void
|
||||
VectorArraySet(VectorArray arr, int offset, Pointer val)
|
||||
{
|
||||
/* Safety check */
|
||||
if (offset >= arr->length)
|
||||
elog(ERROR, "index out of bounds");
|
||||
|
||||
VectorArraySetUncheckedLength(arr, offset, val);
|
||||
}
|
||||
|
||||
static inline void
|
||||
VectorArrayAdd(VectorArray arr, Pointer val)
|
||||
{
|
||||
VectorArraySetUncheckedLength(arr, arr->length, val);
|
||||
arr->length++;
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
|
||||
+3
-7
@@ -33,8 +33,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, float *low
|
||||
collation = index->rd_indcollation[0];
|
||||
|
||||
/* Choose an initial center uniformly at random */
|
||||
VectorArraySet(centers, 0, VectorArrayGet(samples, RandomInt() % samples->length));
|
||||
centers->length++;
|
||||
VectorArrayAdd(centers, VectorArrayGet(samples, RandomInt() % samples->length));
|
||||
|
||||
for (int i = 0; i < numSamples; i++)
|
||||
weight[i] = FLT_MAX;
|
||||
@@ -83,8 +82,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, float *low
|
||||
break;
|
||||
}
|
||||
|
||||
VectorArraySet(centers, i + 1, VectorArrayGet(samples, j));
|
||||
centers->length++;
|
||||
VectorArrayAdd(centers, VectorArrayGet(samples, j));
|
||||
}
|
||||
|
||||
pfree(weight);
|
||||
@@ -117,14 +115,12 @@ RandomCenters(Relation index, VectorArray centers, int dimensions, const Ivfflat
|
||||
/* Fill with random data */
|
||||
while (centers->length < centers->maxlen)
|
||||
{
|
||||
Pointer center = VectorArrayGet(centers, centers->length);
|
||||
Pointer center = VectorArrayGet(centers, centers->length++);
|
||||
|
||||
for (int i = 0; i < dimensions; i++)
|
||||
x[i] = (float) RandomDouble();
|
||||
|
||||
typeInfo->updateCenter(center, dimensions, x);
|
||||
|
||||
centers->length++;
|
||||
}
|
||||
|
||||
if (normprocinfo != NULL)
|
||||
|
||||
Reference in new issue
Block a user