mirror of
https://github.com/pgvector/pgvector.git
synced 2026-06-30 09:41:15 +08:00
Added quantize_binary function
This commit is contained in:
@@ -80,14 +80,7 @@ GetScanValue(IndexScanDesc scan)
|
||||
int dimensions = GetDimensions(scan->indexRelation);
|
||||
|
||||
if (typid == BITOID || typid == VARBITOID)
|
||||
{
|
||||
int len = VARBITTOTALLEN(dimensions);
|
||||
VarBit *v = (VarBit *) palloc0(len);
|
||||
|
||||
SET_VARSIZE(v, len);
|
||||
VARBITLEN(v) = dimensions;
|
||||
value = PointerGetDatum(v);
|
||||
}
|
||||
value = PointerGetDatum(InitBitVector(dimensions));
|
||||
else
|
||||
value = PointerGetDatum(InitVector(dimensions));
|
||||
}
|
||||
|
||||
36
src/vector.c
36
src/vector.c
@@ -1163,6 +1163,42 @@ vector_avg(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_POINTER(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate and initialize a new bit vector
|
||||
*/
|
||||
VarBit *
|
||||
InitBitVector(int dim)
|
||||
{
|
||||
VarBit *result;
|
||||
int size;
|
||||
|
||||
size = VARBITTOTALLEN(dim);
|
||||
result = (VarBit *) palloc0(size);
|
||||
SET_VARSIZE(result, size);
|
||||
VARBITLEN(result) = dim;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quantize a vector
|
||||
*/
|
||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(quantize_binary);
|
||||
Datum
|
||||
quantize_binary(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Vector *a = PG_GETARG_VECTOR_P(0);
|
||||
float *ax = a->x;
|
||||
VarBit *result = InitBitVector(a->dim);
|
||||
unsigned char *rx = VARBITS(result);
|
||||
|
||||
/* TODO Improve */
|
||||
for (int i = 0; i < a->dim; i++)
|
||||
rx[i / 8] |= (ax[i] > 0) << (7 - (i % 8));
|
||||
|
||||
PG_RETURN_VARBIT_P(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure same number of bits
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
|
||||
#include "utils/varbit.h"
|
||||
|
||||
#define VECTOR_MAX_DIM 16000
|
||||
|
||||
#define VECTOR_SIZE(_dim) (offsetof(Vector, x) + sizeof(float)*(_dim))
|
||||
@@ -17,6 +19,7 @@ typedef struct Vector
|
||||
} Vector;
|
||||
|
||||
Vector *InitVector(int dim);
|
||||
VarBit *InitBitVector(int dim);
|
||||
void PrintVector(char *msg, Vector * vector);
|
||||
int vector_cmp_internal(Vector * a, Vector * b);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user