|
| 1 | +/* |
| 2 | +** 2024-07-04 |
| 3 | +** |
| 4 | +** Copyright 2024 the libSQL authors |
| 5 | +** |
| 6 | +** Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +** this software and associated documentation files (the "Software"), to deal in |
| 8 | +** the Software without restriction, including without limitation the rights to |
| 9 | +** use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +** the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +** subject to the following conditions: |
| 12 | +** |
| 13 | +** The above copyright notice and this permission notice shall be included in all |
| 14 | +** copies or substantial portions of the Software. |
| 15 | +** |
| 16 | +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | +** FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | +** COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | +** IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +** |
| 23 | +****************************************************************************** |
| 24 | +** |
| 25 | +** 1-bit vector format utilities. |
| 26 | +*/ |
| 27 | +#ifndef SQLITE_OMIT_VECTOR |
| 28 | +#include "sqliteInt.h" |
| 29 | + |
| 30 | +#include "vectorInt.h" |
| 31 | + |
| 32 | +#include <math.h> |
| 33 | + |
| 34 | +/************************************************************************** |
| 35 | +** Utility routines for debugging |
| 36 | +**************************************************************************/ |
| 37 | + |
| 38 | +void vector1BitDump(const Vector *pVec){ |
| 39 | + u8 *elems = pVec->data; |
| 40 | + unsigned i; |
| 41 | + |
| 42 | + assert( pVec->type == VECTOR_TYPE_1BIT ); |
| 43 | + |
| 44 | + for(i = 0; i < pVec->dims; i++){ |
| 45 | + printf("%d ", ((elems[i / 8] >> (i & 7)) & 1) ? +1 : -1); |
| 46 | + } |
| 47 | + printf("\n"); |
| 48 | +} |
| 49 | + |
| 50 | +/************************************************************************** |
| 51 | +** Utility routines for vector serialization and deserialization |
| 52 | +**************************************************************************/ |
| 53 | + |
| 54 | +size_t vector1BitSerializeToBlob( |
| 55 | + const Vector *pVector, |
| 56 | + unsigned char *pBlob, |
| 57 | + size_t nBlobSize |
| 58 | +){ |
| 59 | + float *elems = pVector->data; |
| 60 | + unsigned char *pPtr = pBlob; |
| 61 | + size_t len = 0; |
| 62 | + unsigned i; |
| 63 | + |
| 64 | + assert( pVector->type == VECTOR_TYPE_1BIT ); |
| 65 | + assert( pVector->dims <= MAX_VECTOR_SZ ); |
| 66 | + assert( nBlobSize >= (pVector->dims + 7) / 8 ); |
| 67 | + |
| 68 | + for(i = 0; i < pVector->dims; i++){ |
| 69 | + elems[i] = pPtr[i]; |
| 70 | + } |
| 71 | + return (pVector->dims + 7) / 8; |
| 72 | +} |
| 73 | + |
| 74 | +// [sum(map(int, bin(i)[2:])) for i in range(256)] |
| 75 | +static int BitsCount[256] = { |
| 76 | + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, |
| 77 | + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| 78 | + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| 79 | + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 80 | + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| 81 | + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 82 | + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 83 | + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| 84 | + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| 85 | + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 86 | + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 87 | + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| 88 | + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 89 | + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| 90 | + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| 91 | + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, |
| 92 | +}; |
| 93 | + |
| 94 | +int vector1BitDistanceHamming(const Vector *v1, const Vector *v2){ |
| 95 | + int sum = 0; |
| 96 | + u8 *e1 = v1->data; |
| 97 | + u8 *e2 = v2->data; |
| 98 | + int i; |
| 99 | + |
| 100 | + assert( v1->dims == v2->dims ); |
| 101 | + assert( v1->type == VECTOR_TYPE_1BIT ); |
| 102 | + assert( v2->type == VECTOR_TYPE_1BIT ); |
| 103 | + |
| 104 | + for(i = 0; i < v1->dims; i++){ |
| 105 | + sum += BitsCount[e1[i]&e2[i]]; |
| 106 | + } |
| 107 | + return sum; |
| 108 | +} |
| 109 | + |
| 110 | +#endif /* !defined(SQLITE_OMIT_VECTOR) */ |
0 commit comments