-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.h
62 lines (56 loc) · 1.85 KB
/
io.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Example code for IO, read binary data vectors and save KNNs to path.
*
*/
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include "assert.h"
using std::cout;
using std::endl;
using std::string;
using std::vector;
/// @brief Save knng in binary format (uint32_t) with name "output.bin"
/// @param knn a (N * 100) shape 2-D vector
/// @param path target save path, the output knng should be named as
/// "output.bin" for evaluation
void SaveKNN(const std::vector<std::vector<uint32_t>> &knns,
const std::string &path = "output.bin") {
std::ofstream ofs(path, std::ios::out | std::ios::binary);
const int K = 100;
const uint32_t N = knns.size();
assert(knns.front().size() == K);
for (unsigned i = 0; i < N; ++i) {
auto const &knn = knns[i];
ofs.write(reinterpret_cast<char const *>(&knn[0]), K * sizeof(uint32_t));
}
ofs.close();
}
/// @brief Reading binary data vectors. Raw data store as a (N x dim)
/// @param file_path file path of binary data
/// @param data returned 2D data vectors
void ReadBin(const std::string &file_path,
const int num_dimensions,
std::vector<std::vector<float>> &data) {
std::cout << "Reading Data: " << file_path << std::endl;
std::ifstream ifs;
ifs.open(file_path, std::ios::binary);
assert(ifs.is_open());
uint32_t N; // num of points
ifs.read((char *)&N, sizeof(uint32_t));
data.resize(N);
std::cout << "# of points: " << N << std::endl;
std::vector<float> buff(num_dimensions);
int counter = 0;
while (ifs.read((char *)buff.data(), num_dimensions * sizeof(float))) {
std::vector<float> row(num_dimensions);
for (int d = 0; d < num_dimensions; d++) {
row[d] = static_cast<float>(buff[d]);
}
data[counter++] = std::move(row);
}
ifs.close();
std::cout << "Finish Reading Data" << endl;
}