Added quantize_binary function

This commit is contained in:
Andrew Kane
2024-03-25 16:39:55 -07:00
parent 709fc75ce0
commit 833f379ebe
9 changed files with 56 additions and 8 deletions

View File

@@ -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));
}

View File

@@ -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
*/

View File

@@ -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);