Skip to content

Commit

Permalink
Modified: use return value to control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmie committed Jul 19, 2022
1 parent 1199c46 commit 1b52464
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Graph/Example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ int main()
out_path.clear();
graph.RemoveEdge("C", "D");
bool not_exist = graph.AddEdge("C", "D") == graph.INFINITE;
graph.GetOutPath("C", [&out_path](auto&& new_path) {out_path.push_back(new_path); });
graph.GetOutPath("C", [&out_path](auto&& new_path) {out_path.push_back(new_path); return true; });
auto in_path = graph.GetInPath("B");
in_path.clear();
graph.GetInPath("B", [&in_path](auto&& new_path) {in_path.push_back(new_path); });
graph.GetInPath("B", [&in_path](auto&& new_path) {in_path.push_back(new_path); return true; });
auto weight = graph.AddEdge("C", "E");
not_exist = graph.AddEdge("C", "T") == graph.INFINITE;
auto result = graph.GetBestPath("A", "E");
Expand Down
8 changes: 6 additions & 2 deletions Graph/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ class Graph
}

void GetOutPath(const Vertex& vertex, std::invocable<PathType> auto&& callback)
requires std::is_same_v<std::invoke_result_t<decltype(callback), PathType>, bool>
{
for (auto&& element : matrix[vertex])
if (element.second != INFINITE)
callback(element);
if (!callback(element))
break;
}

auto GetInPath(const Vertex& vertex)
Expand All @@ -78,10 +80,12 @@ class Graph
}

void GetInPath(const Vertex& vertex, std::invocable<PathType> auto&& callback)
requires std::is_same_v<std::invoke_result_t<decltype(callback), PathType>, bool>
{
for (auto iterator = matrix.begin(); iterator != matrix.end(); ++iterator)
if (iterator->second.contains(vertex) && iterator->second[vertex] != INFINITE)
callback(PathType{ iterator->first, iterator->second[vertex] });
if (!callback(PathType{ iterator->first, iterator->second[vertex] }))
break;
}

void SetBestPathAlgorithm(std::invocable<const Vertex&, const Vertex&, Compare> auto&& function)
Expand Down

0 comments on commit 1b52464

Please sign in to comment.