Skip to content

Commit

Permalink
Fix vector insertion complexity from O(N) to O(1)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyVH committed Jan 15, 2025
1 parent 8e60387 commit 836919b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions include/boost/graph/detail/adjacency_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2228,9 +2228,11 @@ inline typename Config::vertex_descriptor add_vertex(
vec_adj_list_impl< Graph, Config, Base >& g_)
{
Graph& g = static_cast< Graph& >(g_);
g.m_vertices.resize(g.m_vertices.size() + 1);
g.added_vertex(g.m_vertices.size() - 1);
return g.m_vertices.size() - 1;
const typename Config::vertex_descriptor added_descriptor
= g.m_vertices.size();
g.m_vertices.emplace_back();
g.added_vertex(added_descriptor);
return added_descriptor;
}

template < class Graph, class Config, class Base >
Expand Down
3 changes: 2 additions & 1 deletion include/boost/graph/transitive_closure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ void transitive_closure(const Graph& g, GraphTC& tc,
cg_vertex v = *i;
if (!in_a_chain[v])
{
chains.resize(chains.size() + 1);
chains.push_back(std::vector< cg_vertex >());
std::vector< cg_vertex >& chain = chains.back();

for (;;)
{
chain.push_back(v);
Expand Down
2 changes: 1 addition & 1 deletion include/boost/graph/vector_as_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void remove_edge_if(Predicate p, std::vector< EdgeList, Allocator >& g)
template < class EdgeList, class Allocator >
typename EdgeList::value_type add_vertex(std::vector< EdgeList, Allocator >& g)
{
g.resize(g.size() + 1);
g.push_back(EdgeList());
return g.size() - 1;
}

Expand Down

0 comments on commit 836919b

Please sign in to comment.