Skip to content

Commit b98102b

Browse files
committed
Created a function to read the groundtruth
1 parent 75fe92b commit b98102b

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

main2.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,57 @@ void saveGroundtruthToFile(const std::vector<std::vector<float>>& distances, con
9797
file.close();
9898
}
9999

100+
/**
101+
* @brief Read the groundtruth distances from a binary file.
102+
*
103+
* This function reads the groundtruth distances from a binary file. The file format consists of the following:
104+
* - The number of query vectors (4 bytes)
105+
* - For each query vector:
106+
* - The number of distances (4 bytes)
107+
* - The computed distances (4 bytes each)
108+
*
109+
* @param filename The name of the input file to read the distances from
110+
*
111+
* @return A 2D vector containing the read distances for each query vector
112+
*/
113+
std::vector<std::vector<float>> readGroundtruthFromFile(const std::string& filename) {
114+
std::ifstream file(filename, std::ios::binary);
115+
116+
if (!file.is_open()) {
117+
std::cerr << "Error opening file: " << filename << std::endl;
118+
return {};
119+
}
120+
121+
// Read the number of query vectors from the file
122+
uint32_t num_queries;
123+
file.read(reinterpret_cast<char*>(&num_queries), sizeof(num_queries));
124+
125+
std::vector<std::vector<float>> distances(num_queries);
126+
127+
// Read the distances for each query vector
128+
for (auto& query_distances : distances) {
129+
uint32_t num_distances;
130+
file.read(reinterpret_cast<char*>(&num_distances), sizeof(num_distances));
131+
query_distances.resize(num_distances);
132+
file.read(reinterpret_cast<char*>(query_distances.data()), num_distances * sizeof(float));
133+
}
134+
135+
file.close();
136+
return distances;
137+
}
100138

101139
int main(int argc, char* argv[]) {
102140

103141
std::vector<BaseDataVector<float>> base_vectors = ReadFilteredBaseVectorFile("data/Dummy/dummy-data.bin");
104142
std::vector<QueryDataVector<float>> query_vectors = ReadFilteredQueryVectorFile("data/Dummy/dummy-queries.bin");
105143

106144
// Compute the distance vector, and save the computed distances to a file
107-
std::vector<std::vector<float>> distances = computeGroundtruth(base_vectors, query_vectors, 100);
145+
std::vector<std::vector<float>> distances = computeGroundtruth(base_vectors, query_vectors, 1000);
108146
saveGroundtruthToFile(distances, "data/Dummy/dummy-groundtruth.bin");
109-
147+
148+
// Example usage of readGroundtruthFromFile
149+
std::vector<std::vector<float>> read_distances = readGroundtruthFromFile("data/Dummy/dummy-groundtruth.bin");
150+
110151
return 0;
111152

112153
}

0 commit comments

Comments
 (0)