solidity/libsolutil/Algorithms.h

129 lines
3.6 KiB
C
Raw Permalink Normal View History

2018-03-15 18:53:29 +00:00
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
2018-03-15 18:53:29 +00:00
#pragma once
#include <functional>
2021-03-15 12:50:24 +00:00
#include <list>
2018-03-15 18:53:29 +00:00
#include <set>
2019-12-11 16:31:36 +00:00
namespace solidity::util
2018-03-15 18:53:29 +00:00
{
/**
* Detector for cycles in directed graphs. It returns the first
* vertex on the path towards a cycle or a nullptr if there is
* no reachable cycle starting from a given vertex.
*/
template <typename V>
class CycleDetector
{
public:
using Visitor = std::function<void(V const&, CycleDetector&, size_t)>;
2018-03-15 18:53:29 +00:00
/// Initializes the cycle detector
/// @param _visit function that is given the current vertex
/// and is supposed to call @a run on all
/// adjacent vertices.
explicit CycleDetector(Visitor _visit):
2018-03-15 18:53:29 +00:00
m_visit(std::move(_visit))
{ }
/// Recursively perform cycle detection starting
/// (or continuing) with @param _vertex
/// @returns the first vertex on the path towards a cycle from @a _vertex
/// or nullptr if no cycle is reachable from @a _vertex.
V const* run(V const& _vertex)
{
if (m_firstCycleVertex)
return m_firstCycleVertex;
if (m_processed.count(&_vertex))
return nullptr;
else if (m_processing.count(&_vertex))
return m_firstCycleVertex = &_vertex;
m_processing.insert(&_vertex);
m_depth++;
m_visit(_vertex, *this, m_depth);
2018-03-15 18:53:29 +00:00
m_depth--;
if (m_firstCycleVertex && m_depth == 1)
m_firstCycleVertex = &_vertex;
m_processing.erase(&_vertex);
m_processed.insert(&_vertex);
return m_firstCycleVertex;
}
private:
Visitor m_visit;
2018-03-15 18:53:29 +00:00
std::set<V const*> m_processing;
std::set<V const*> m_processed;
size_t m_depth = 0;
V const* m_firstCycleVertex = nullptr;
};
2019-01-08 18:33:46 +00:00
/**
* Generic breadth first search.
*
2021-01-05 13:05:30 +00:00
* Note that V needs to be a comparable value type or a pointer.
*
2019-01-08 18:33:46 +00:00
* Example: Gather all (recursive) children in a graph starting at (and including) ``root``:
*
* Node const* root = ...;
* std::set<Node const*> allNodes = BreadthFirstSearch<Node const*>{{root}}.run([](Node const* _node, auto&& _addChild) {
2019-01-08 18:33:46 +00:00
* // Potentially process ``_node``.
* for (Node const& _child: _node->children())
2019-01-08 18:33:46 +00:00
* // Potentially filter the children to be visited.
* _addChild(&_child);
2019-01-08 18:33:46 +00:00
* }).visited;
*/
template<typename V>
struct BreadthFirstSearch
{
/// Runs the breadth first search. The verticesToTraverse member of the struct needs to be initialized.
/// @param _forEachChild is a callable of the form [...](V const& _node, auto&& _addChild) { ... }
/// that is called for each visited node and is supposed to call _addChild(childNode) for every child
/// node of _node.
template<typename ForEachChild>
BreadthFirstSearch& run(ForEachChild&& _forEachChild)
{
while (!verticesToTraverse.empty())
{
2021-01-05 13:05:30 +00:00
V v = std::move(verticesToTraverse.front());
verticesToTraverse.pop_front();
2021-01-11 12:18:59 +00:00
if (!visited.insert(v).second)
continue;
2019-01-08 18:33:46 +00:00
_forEachChild(v, [this](V _vertex) {
2021-01-11 12:18:59 +00:00
verticesToTraverse.emplace_back(std::move(_vertex));
2019-01-08 18:33:46 +00:00
});
}
return *this;
}
void abort()
{
verticesToTraverse.clear();
}
2019-01-08 18:33:46 +00:00
2021-01-05 13:05:30 +00:00
std::list<V> verticesToTraverse;
std::set<V> visited{};
2019-01-08 18:33:46 +00:00
};
2018-03-15 18:53:29 +00:00
}