From eeeda20eb0acef8659a2142cb1328b17cb063310 Mon Sep 17 00:00:00 2001 From: Li Shulian Date: Sun, 18 Jan 2026 17:23:06 +0800 Subject: [PATCH] improve get_neighbors performance --- include/graaflib/graph.h | 2 +- include/graaflib/graph.tpp | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/graaflib/graph.h b/include/graaflib/graph.h index 66359666..1a1e4f78 100644 --- a/include/graaflib/graph.h +++ b/include/graaflib/graph.h @@ -169,7 +169,7 @@ class graph { * @param vertex_id The ID of the vertex * @return vertices_t - A list of neighboring vertices */ - [[nodiscard]] vertices_t get_neighbors(vertex_id_t vertex_id) const; + [[nodiscard]] const vertices_t& get_neighbors(vertex_id_t vertex_id) const; /** * Add a vertex to the graph diff --git a/include/graaflib/graph.tpp b/include/graaflib/graph.tpp index 04194a58..4401cb8b 100644 --- a/include/graaflib/graph.tpp +++ b/include/graaflib/graph.tpp @@ -118,12 +118,14 @@ graph::get_edge( return get_edge(vertex_id_lhs, vertex_id_rhs); } +static const std::unordered_set empty_list; + template -typename graph::vertices_t +const typename graph::vertices_t& graph::get_neighbors( vertex_id_t vertex_id) const { if (!adjacency_list_.contains(vertex_id)) { - return {}; + return empty_list; } return adjacency_list_.at(vertex_id); }