From 9ebe80f88bf031295cb8a2946ebe77e84dc23cdf Mon Sep 17 00:00:00 2001 From: poma Date: Wed, 10 Feb 2021 09:43:28 +0300 Subject: [PATCH] remove recursion --- contracts/TornadoTrees.sol | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/contracts/TornadoTrees.sol b/contracts/TornadoTrees.sol index b66dff8..8e34600 100644 --- a/contracts/TornadoTrees.sol +++ b/contracts/TornadoTrees.sol @@ -122,29 +122,18 @@ contract TornadoTrees is EnsResolve { } while (direction == elementExists(_tornadoTreesV1, _type, _from)); uint256 high = direction ? _from : _from + _step; uint256 low = direction ? _from - _step : _from; + uint256 mid = (high + low) / 2; - // Perform a binary search on this segment - uint256 lastIndex = binarySearch(_tornadoTreesV1, _type, high, low); - - return lastIndex + 1; - } - - function binarySearch( - ITornadoTreesV1 _tornadoTreesV1, - string memory _type, - uint256 _high, - uint256 _low - ) public view returns (uint256) { - uint256 mid = (_high + _low) / 2; - if (mid == _low) { - return mid; - } - - if (elementExists(_tornadoTreesV1, _type, mid)) { - return binarySearch(_tornadoTreesV1, _type, _high, mid); - } else { - return binarySearch(_tornadoTreesV1, _type, mid, _low); + // Perform a binary search in this segment + while (low < mid) { + if (elementExists(_tornadoTreesV1, _type, mid)) { + low = mid; + } else { + high = mid; + } + mid = (high + low) / 2; } + return mid + 1; } function elementExists(