@@ -88,7 +88,7 @@ void HNSWIndex::add(int id, std::span<const float> vec) {
8888 node->id = id;
8989 node->level = node_level;
9090 node->vector = node_vec;
91- node->neighbors .resize (node_level + 1 );
91+ node->neighbors .resize (static_cast < size_t >( node_level + 1 ) );
9292
9393 entry_point_ = id;
9494 max_level_ = node_level;
@@ -98,7 +98,7 @@ void HNSWIndex::add(int id, std::span<const float> vec) {
9898 }
9999
100100 // Build neighbor lists for new node
101- std::vector<std::vector<int >> new_node_neighbors (node_level + 1 );
101+ std::vector<std::vector<int >> new_node_neighbors (static_cast < size_t >( node_level + 1 ) );
102102
103103 // Search for nearest neighbors at each layer
104104 std::vector<int > entry_points = {entry_point_};
@@ -120,7 +120,7 @@ void HNSWIndex::add(int id, std::span<const float> vec) {
120120 int M = (lc == 0 ) ? params_.max_M0 : params_.max_M ;
121121 auto neighbors = select_neighbors_heuristic (node_vec, candidates, M);
122122
123- new_node_neighbors[lc ] = neighbors;
123+ new_node_neighbors[static_cast < size_t >(lc) ] = neighbors;
124124
125125 // Update entry point for next layer
126126 if (!candidates.empty ()) {
@@ -141,7 +141,7 @@ void HNSWIndex::add(int id, std::span<const float> vec) {
141141
142142 // Add bidirectional links
143143 for (int lc = node_level; lc >= 0 ; --lc) {
144- for (int neighbor_id : new_node_neighbors[lc ]) {
144+ for (int neighbor_id : new_node_neighbors[static_cast < size_t >(lc) ]) {
145145 auto neighbor_it = id_to_index_.find (neighbor_id);
146146 if (neighbor_it == id_to_index_.end ())
147147 continue ;
@@ -152,14 +152,15 @@ void HNSWIndex::add(int id, std::span<const float> vec) {
152152 if (lc >= static_cast <int >(nodes_[neighbor_idx]->neighbors .size ()))
153153 continue ;
154154
155- nodes_[neighbor_idx]->neighbors [lc ].push_back (id);
155+ nodes_[neighbor_idx]->neighbors [static_cast < size_t >(lc) ].push_back (id);
156156
157157 // Prune if necessary
158158 int max_conn = (lc == 0 ) ? params_.max_M0 : params_.max_M ;
159- if (static_cast <int >(nodes_[neighbor_idx]->neighbors [lc].size ()) > max_conn) {
159+ if (static_cast <int >(nodes_[neighbor_idx]->neighbors [static_cast <size_t >(lc)].size ()) >
160+ max_conn) {
160161 auto & neighbor_vec = nodes_[neighbor_idx]->vector ;
161162 std::vector<SearchResult> neighbor_candidates;
162- for (int conn_id : nodes_[neighbor_idx]->neighbors [lc ]) {
163+ for (int conn_id : nodes_[neighbor_idx]->neighbors [static_cast < size_t >(lc) ]) {
163164 auto conn_it = id_to_index_.find (conn_id);
164165 if (conn_it == id_to_index_.end ())
165166 continue ;
@@ -170,7 +171,7 @@ void HNSWIndex::add(int id, std::span<const float> vec) {
170171
171172 auto pruned =
172173 select_neighbors_heuristic (neighbor_vec, neighbor_candidates, max_conn);
173- nodes_[neighbor_idx]->neighbors [lc ] = pruned;
174+ nodes_[neighbor_idx]->neighbors [static_cast < size_t >(lc) ] = pruned;
174175 }
175176 }
176177 }
@@ -219,7 +220,7 @@ std::vector<HNSWIndex::SearchResult> HNSWIndex::search_layer(std::span<const flo
219220 continue ;
220221 }
221222
222- for (int neighbor_id : nodes_[current_idx]->neighbors [layer]) {
223+ for (int neighbor_id : nodes_[current_idx]->neighbors [static_cast < size_t >( layer) ]) {
223224 if (visited.find (neighbor_id) != visited.end ()) {
224225 continue ;
225226 }
@@ -254,9 +255,9 @@ std::vector<HNSWIndex::SearchResult> HNSWIndex::search_layer(std::span<const flo
254255 return result_vec;
255256}
256257
257- std::vector<int > HNSWIndex::select_neighbors_heuristic (std::span< const float > base_vec,
258- const std::vector<SearchResult>& candidates,
259- int M) const {
258+ std::vector<int > HNSWIndex::select_neighbors_heuristic (
259+ [[maybe_unused]] std::span< const float > base_vec, const std::vector<SearchResult>& candidates,
260+ int M) const {
260261 if (static_cast <int >(candidates.size ()) <= M) {
261262 std::vector<int > result;
262263 result.reserve (candidates.size ());
@@ -271,8 +272,8 @@ std::vector<int> HNSWIndex::select_neighbors_heuristic(std::span<const float> ba
271272 std::sort (sorted.begin (), sorted.end ());
272273
273274 std::vector<int > result;
274- result.reserve (M );
275- for (int i = 0 ; i < M && i < static_cast < int >( sorted.size () ); ++i) {
275+ result.reserve (static_cast < size_t >(M) );
276+ for (size_t i = 0 ; i < static_cast < size_t >(M) && i < sorted.size (); ++i) {
276277 result.push_back (sorted[i].id );
277278 }
278279
@@ -308,7 +309,7 @@ std::vector<HNSWIndex::SearchResult> HNSWIndex::search(std::span<const float> qu
308309
309310 // Return top k
310311 if (static_cast <int >(results.size ()) > k) {
311- results.resize (k );
312+ results.resize (static_cast < size_t >(k) );
312313 }
313314
314315 return results;
0 commit comments