@@ -97,16 +97,57 @@ void saveGroundtruthToFile(const std::vector<std::vector<float>>& distances, con
97
97
file.close ();
98
98
}
99
99
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
+ }
100
138
101
139
int main (int argc, char * argv[]) {
102
140
103
141
std::vector<BaseDataVector<float >> base_vectors = ReadFilteredBaseVectorFile (" data/Dummy/dummy-data.bin" );
104
142
std::vector<QueryDataVector<float >> query_vectors = ReadFilteredQueryVectorFile (" data/Dummy/dummy-queries.bin" );
105
143
106
144
// 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 );
108
146
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
+
110
151
return 0 ;
111
152
112
153
}
0 commit comments