Browse Source

feat: add vl

master
Ethan 1 year ago
parent
commit
2c65bce7f0
3 changed files with 43 additions and 2 deletions
  1. +13
    -0
      inc/hnsw.h
  2. +28
    -0
      src/hnsw.c
  3. +2
    -2
      src/test.c

+ 13
- 0
inc/hnsw.h View File

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

+ 28
- 0
src/hnsw.c View File

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

+ 2
- 2
src/test.c View File

@ -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++) {

Loading…
Cancel
Save