From 4d9184ef04a3163a4740b2d179398682c9f5140e Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 25 Sep 2018 16:29:46 +0200 Subject: Expression breaker. --- libdevcore/CommonData.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'libdevcore') diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index e410af5c..98136b44 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -25,11 +25,14 @@ #include +#include + #include #include #include #include #include +#include namespace dev { @@ -229,6 +232,36 @@ bool contains(T const& _t, V const& _v) return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v); } + +/// Function that iterates over a vector, calling a function on each of its +/// elements. If that function returns a vector, the element is replaced by +/// the returned vector. During the iteration, the original vector is only valid +/// on the current element and after that. The actual replacement takes +/// place at the end, but already visited elements might be invalidated. +/// If nothing is replaced, no copy is performed. +template +void iterateReplacing(std::vector& _vector, std::function>(T&)> _f) +{ + bool useModified = false; + std::vector modifiedVector; + for (size_t i = 0; i < _vector.size(); ++i) + { + if (boost::optional> r = _f(_vector[i])) + { + if (!useModified) + { + std::move(_vector.begin(), _vector.begin() + i, back_inserter(modifiedVector)); + useModified = true; + } + modifiedVector += std::move(*r); + } + else if (useModified) + modifiedVector.emplace_back(std::move(_vector[i])); + } + if (useModified) + _vector = std::move(modifiedVector); +} + /// @returns true iff @a _str passess the hex address checksum test. /// @param _strict if false, hex strings with only uppercase or only lowercase letters /// are considered valid. -- cgit