Skip to content

Commit

Permalink
perfect forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
bobluppes committed Jul 30, 2023
1 parent c48a691 commit debf065
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/graaflib/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class graph {
* @param vertex The vertex to be added
* @return vertices_id_t - The ID of the new vertex
*/
vertex_id_t add_vertex(VERTEX_T vertex);
vertex_id_t add_vertex(auto&& vertex);

/**
* Remove a vertex from the graph and update all its neighbors
Expand All @@ -196,7 +196,7 @@ class graph {
* @throws out_of_range - If either of the vertex does not exist in graph
*/
void add_edge(vertex_id_t vertex_id_lhs, vertex_id_t vertex_id_rhs,
EDGE_T edge);
auto&& edge);

/**
* Remove the edge between two vertices
Expand Down
9 changes: 5 additions & 4 deletions include/graaflib/graph.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ graph<VERTEX_T, EDGE_T, GRAPH_SPEC_V>::get_neighbors(
}

template <typename VERTEX_T, typename EDGE_T, graph_spec GRAPH_SPEC_V>
vertex_id_t graph<VERTEX_T, EDGE_T, GRAPH_SPEC_V>::add_vertex(VERTEX_T vertex) {
vertex_id_t graph<VERTEX_T, EDGE_T, GRAPH_SPEC_V>::add_vertex(auto&& vertex) {
// TODO: check overflow
const auto vertex_id{vertex_id_supplier_++};
vertices_.emplace(vertex_id, std::move(vertex));
vertices_.emplace(vertex_id, std::forward<VERTEX_T>(vertex));
return vertex_id;
}

Expand All @@ -121,15 +121,16 @@ void graph<VERTEX_T, EDGE_T, GRAPH_SPEC_V>::remove_vertex(
template <typename VERTEX_T, typename EDGE_T, graph_spec GRAPH_SPEC_V>
void graph<VERTEX_T, EDGE_T, GRAPH_SPEC_V>::add_edge(vertex_id_t vertex_id_lhs,
vertex_id_t vertex_id_rhs,
EDGE_T edge) {
auto&& edge) {
if (!has_vertex(vertex_id_lhs) || !has_vertex(vertex_id_rhs)) {
// TODO(bluppes): replace with std::format once Clang supports it
throw std::out_of_range{
"Vertices with ID [" + std::to_string(vertex_id_lhs) + "] and [" +
std::to_string(vertex_id_rhs) + "] not found in graph."};
}
do_add_edge(vertex_id_lhs, vertex_id_rhs,
std::make_shared<typename edge_t::element_type>(edge));
std::make_shared<typename edge_t::element_type>(
std::forward<EDGE_T>(edge)));
}

template <typename VERTEX_T, typename EDGE_T, graph_spec GRAPH_SPEC_V>
Expand Down

0 comments on commit debf065

Please sign in to comment.