From 2c65bce7f049bc5bcb0b608baa120e9056e99ba5 Mon Sep 17 00:00:00 2001 From: Ethan Date: Thu, 29 Jun 2023 20:48:12 +0800 Subject: [PATCH] feat: add vl --- inc/hnsw.h | 13 +++++++++++++ src/hnsw.c | 28 ++++++++++++++++++++++++++++ src/test.c | 4 ++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/inc/hnsw.h b/inc/hnsw.h index 43a0439..400adb4 100644 --- a/inc/hnsw.h +++ b/inc/hnsw.h @@ -21,9 +21,22 @@ typedef struct HNSWContext HNSWGraph *graph; // graph of HNSW } HNSWContext; + +typedef struct { + unsigned int* visited; + unsigned int size; + unsigned int mark; +} VisitedList; + // help functions // public functions HNSWContext *init_hnsw_context(const char *file_path, size_t dim, size_t len); void insert_vec(HNSWContext *ctx, VecData vec); void approximate_knn(HNSWContext *ctx, VecData *results); + +VisitedList* visited_list_new(int size); +void visited_list_reset(VisitedList* vl); +unsigned int visited_list_get_visit_mark(VisitedList* vl); +unsigned int* visited_list_get_visited(VisitedList* vl); +void visited_list_free(VisitedList* vl); diff --git a/src/hnsw.c b/src/hnsw.c index 5fe4992..98d3995 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -1,2 +1,30 @@ #include "hnsw.h" + +VisitedList* visited_list_new(int size) { + VisitedList* vl = malloc(sizeof(VisitedList)); + vl->size = size; + vl->mark = 1; + vl->visited = calloc(size, sizeof(unsigned int)); + return vl; +} + +void visited_list_reset(VisitedList* vl) { + if (++vl->mark == 0) { + vl->mark = 1; + memset(vl->visited, 0, sizeof(unsigned int) * vl->size); + } +} + +unsigned int visited_list_get_visit_mark(VisitedList* vl) { + return vl->mark; +} + +unsigned int* visited_list_get_visited(VisitedList* vl) { + return vl->visited; +} + +void visited_list_free(VisitedList* vl) { + free(vl->visited); + free(vl); +} \ No newline at end of file diff --git a/src/test.c b/src/test.c index 5a56dc9..46a1ae5 100644 --- a/src/test.c +++ b/src/test.c @@ -8,8 +8,8 @@ int main() { int num = 0; VecData* vecs = fvecs_read("../dataset/siftsmall_base.fvecs", NULL, &num); printf("num: %d\n", num); - - + printf("id of vector 1: %d\n", vecs[0].id); + printf("test distance: %f\n", vec_dist(vecs[0], vecs[1])); // Free memory for (int i = 0; i < num; i++) {