Skip to content

Commit b6d04a4

Browse files
committed
Add some documentation for BreadthFirstSearch.
1 parent d859f44 commit b6d04a4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

libdevcore/Algorithms.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,28 @@ class CycleDetector
7575
V const* m_firstCycleVertex = nullptr;
7676
};
7777

78+
/**
79+
* Generic breadth first search.
80+
*
81+
* Example: Gather all nodes in the graph starting at ``root``:
82+
*
83+
* std::set<Node> allNodes = BreadthFirstSearch<Node>{{root}}.run([](Node const& _node, auto&& _addChild) {
84+
* // Potentially process ``_node``.
85+
* for(Node const& _child: _node.children())
86+
* // Potentially filter the children to be visited.
87+
* _addChild(_child);
88+
* }).visited;
89+
*
90+
* Note that the order of the traversal is *non-deterministic* (the children are stored in a std::set of pointers).
91+
*/
7892
template<typename V>
7993
struct BreadthFirstSearch
8094
{
95+
/// Runs the breadth first search. The verticesToTraverse member of the struct needs to be initialized.
96+
/// @param _forEachChild should be a generic lambda expression (or an object of a class type with an
97+
/// analog templated call operator) of the form [...](Node const& _node, auto&& _addChild) { ... }
98+
/// that is called for each visited node and is supposd to call _addChild(childNode) for every
99+
/// child node of _node.
81100
template<typename ForEachChild>
82101
BreadthFirstSearch& run(ForEachChild&& _forEachChild)
83102
{

0 commit comments

Comments
 (0)