|
|
@ -0,0 +1,92 @@ |
|
|
|
#include "utils.h" |
|
|
|
#include <math.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <string.h> |
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
float vec_dist(VecData x, VecData y) { |
|
|
|
float dist = 0.0f; |
|
|
|
while (*x != '\0' && *y != '\0') { |
|
|
|
int xi = *x - '0'; |
|
|
|
int yi = *y - '0'; |
|
|
|
dist += pow(xi - yi, 2); |
|
|
|
x++; |
|
|
|
y++; |
|
|
|
} |
|
|
|
return sqrt(dist); |
|
|
|
} |
|
|
|
|
|
|
|
void fvecs_read(const char* filename, int* bound, float** vectors, int* num_vectors, int* vector_dimension) { |
|
|
|
FILE* fid = fopen(filename, "rb"); |
|
|
|
if (fid == NULL) { |
|
|
|
fprintf(stderr, "I/O error: Unable to open the file %s\n", filename); |
|
|
|
exit(EXIT_FAILURE); |
|
|
|
} |
|
|
|
|
|
|
|
// Read the vector size |
|
|
|
int d; |
|
|
|
fread(&d, sizeof(int), 1, fid); |
|
|
|
|
|
|
|
// Get the file size |
|
|
|
fseek(fid, 0, SEEK_END); |
|
|
|
long file_size = ftell(fid); |
|
|
|
fseek(fid, 0, SEEK_SET); |
|
|
|
|
|
|
|
// Get the number of vectors |
|
|
|
long vec_size = (long) d * sizeof(float); |
|
|
|
long vec_count = (file_size - sizeof(int)) / vec_size; |
|
|
|
|
|
|
|
// Apply the bounds if specified |
|
|
|
int a = 1; |
|
|
|
int b = vec_count; |
|
|
|
if (bound != NULL) { |
|
|
|
if (bound[1] == 1) { |
|
|
|
b = bound[0]; |
|
|
|
} else if (bound[1] == 2) { |
|
|
|
a = bound[0]; |
|
|
|
b = bound[1]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Check if the bounds are valid |
|
|
|
if (a < 1 || b < a || b > vec_count) { |
|
|
|
*vectors = NULL; |
|
|
|
fclose(fid); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// Compute the number of vectors to read |
|
|
|
int n = b - a + 1; |
|
|
|
|
|
|
|
// Read the vectors |
|
|
|
*vectors = malloc(n * d * sizeof(float)); |
|
|
|
float* ptr = *vectors; |
|
|
|
for (int i = 0; i < vec_count; i++) { |
|
|
|
// Read the vector size |
|
|
|
int vec_d; |
|
|
|
fread(&vec_d, sizeof(int), 1, fid); |
|
|
|
|
|
|
|
// Check if the vector size is correct |
|
|
|
if (vec_d != d) { |
|
|
|
fprintf(stderr, "Error: Vector %d has incorrect dimension %d (expected %d)\n", i + 1, vec_d, d); |
|
|
|
fclose(fid); |
|
|
|
free(*vectors); |
|
|
|
*vectors = NULL; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// Read the vector data |
|
|
|
fread(ptr, sizeof(float), d, fid); |
|
|
|
ptr += d; |
|
|
|
|
|
|
|
// Stop reading if we have read enough vectors |
|
|
|
if (i >= b - 1) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
*vector_dimension = d; |
|
|
|
*num_vectors = n; |
|
|
|
fclose(fid); |
|
|
|
} |