diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2018-04-05 18:04:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-05 18:04:54 +0800 |
commit | 36d6c27e6826f173b491a7a536a3755609edaa29 (patch) | |
tree | 23e7cd9d859a3beb94d5d5cdaaa644f8c41ecf20 /libdevcore/Algorithms.h | |
parent | c6da5c1650964f9dadd4b483d42585223e086b74 (diff) | |
parent | 8fdbd19a05c976908172d0f776a0f96837449683 (diff) | |
download | dexon-solidity-36d6c27e6826f173b491a7a536a3755609edaa29.tar dexon-solidity-36d6c27e6826f173b491a7a536a3755609edaa29.tar.gz dexon-solidity-36d6c27e6826f173b491a7a536a3755609edaa29.tar.bz2 dexon-solidity-36d6c27e6826f173b491a7a536a3755609edaa29.tar.lz dexon-solidity-36d6c27e6826f173b491a7a536a3755609edaa29.tar.xz dexon-solidity-36d6c27e6826f173b491a7a536a3755609edaa29.tar.zst dexon-solidity-36d6c27e6826f173b491a7a536a3755609edaa29.zip |
Merge pull request #3745 from ethereum/fixRecursion
Fix invalid recursion errors for structs
Diffstat (limited to 'libdevcore/Algorithms.h')
-rw-r--r-- | libdevcore/Algorithms.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/libdevcore/Algorithms.h b/libdevcore/Algorithms.h new file mode 100644 index 00000000..b2540668 --- /dev/null +++ b/libdevcore/Algorithms.h @@ -0,0 +1,76 @@ +/* + 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/>. +*/ +#pragma once + + +#include <functional> +#include <set> + +namespace dev +{ + +/** + * 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: + /// 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(std::function<void(V const&, CycleDetector&)> _visit): + 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--; + if (m_firstCycleVertex && m_depth == 1) + m_firstCycleVertex = &_vertex; + + m_processing.erase(&_vertex); + m_processed.insert(&_vertex); + return m_firstCycleVertex; + } + +private: + std::function<void(V const&, CycleDetector&)> m_visit; + std::set<V const*> m_processing; + std::set<V const*> m_processed; + size_t m_depth = 0; + V const* m_firstCycleVertex = nullptr; +}; + +} |