selfdev/bruteforce/test/test_demo_sift1M.cpp
float * fvecs_read (const char *fname,
                    size_t *d_out, size_t *n_out)
{
    FILE *f = fopen(fname, "r");
    if(!f) {
        fprintf(stderr, "could not open %s\n", fname);
        perror("");
        abort();
    }
    int d;
    fread(&d, 1, sizeof(int), f);
    assert((d > 0 && d < 1000000) || !"unreasonable dimension");
    fseek(f, 0, SEEK_SET);
    struct stat st;
    fstat(fileno(f), &st);
    size_t sz = st.st_size;
    assert(sz % ((d + 1) * 4) == 0 || !"weird file size");
    size_t n = sz / ((d + 1) * 4);

    *d_out = d; *n_out = n;
    float *x = new float[n * (d + 1)];
    size_t nr = fread(x, sizeof(float), n * (d + 1), f);
    assert(nr == n * (d + 1) || !"could not read whole file");

    // shift array to remove row headers
    for(size_t i = 0; i < n; i++)
        memmove(x + i * d, x + 1 + i * (d + 1), d * sizeof(*x));

    fclose(f);
    return x;
}
/*****************************************************
 * I/O functions for fvecs and ivecs
 *****************************************************/
I/O functions for fvecs and ivecs
I / O functions for fvecs and ivecs
float * fvecs_read (const char *fname,
                    size_t *d_out, size_t *n_out)
{
    FILE *f = fopen(fname, "r");
    if(!f) {
        fprintf(stderr, "could not open %s\n", fname);
        perror("");
        abort();
    }
    int d;
    fread(&d, 1, sizeof(int), f);
    assert((d > 0 && d < 1000000) || !"unreasonable dimension");
    fseek(f, 0, SEEK_SET);
    struct stat st;
    fstat(fileno(f), &st);
    size_t sz = st.st_size;
    assert(sz % ((d + 1) * 4) == 0 || !"weird file size");
    size_t n = sz / ((d + 1) * 4);
    *d_out = d; *n_out = n;
    float *x = new float[n * (d + 1)];
    size_t nr = fread(x, sizeof(float), n * (d + 1), f);
    assert(nr == n * (d + 1) || !"could not read whole file");
    for(size_t i = 0; i < n; i++)
        memmove(x + i * d, x + 1 + i * (d + 1), d * sizeof(*x));
    fclose(f);
    return x;
}
float * fvecs_read ( const char * fname , size_t * d_out , size_t * n_out ) { FILE * f = fopen ( fname , " " ) ; if ( ! f ) { fprintf ( stderr , " \n " , fname ) ; perror ( " " ) ; abort ( ) ; } int d ; fread ( & d , 1 , sizeof ( int ) , f ) ; assert ( ( d > 0 && d < 1000000 ) || ! " " ) ; fseek ( f , 0 , SEEK_SET ) ; struct stat st ; fstat ( fileno ( f ) , & st ) ; size_t sz = st . st_size ; assert ( sz % ( ( d + 1 ) * 4 ) == 0 || ! " " ) ; size_t n = sz / ( ( d + 1 ) * 4 ) ; * d_out = d ; * n_out = n ; float * x = new float [ n * ( d + 1 ) ] ; size_t nr = fread ( x , sizeof ( float ) , n * ( d + 1 ) , f ) ; assert ( nr == n * ( d + 1 ) || ! " " ) ; for ( size_t i = 0 ; i < n ; i ++ ) memmove ( x + i * d , x + 1 + i * ( d + 1 ) , d * sizeof ( * x ) ) ; fclose ( f ) ; return x ; }
I/O functions for fvecs and ivecs
I / O functions for fvecs and ivecs
// shift array to remove row headers