remove recursion

This commit is contained in:
poma 2021-02-10 09:43:28 +03:00
parent 5c42b1e208
commit 9ebe80f88b
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657

@ -122,29 +122,18 @@ contract TornadoTrees is EnsResolve {
} while (direction == elementExists(_tornadoTreesV1, _type, _from)); } while (direction == elementExists(_tornadoTreesV1, _type, _from));
uint256 high = direction ? _from : _from + _step; uint256 high = direction ? _from : _from + _step;
uint256 low = direction ? _from - _step : _from; uint256 low = direction ? _from - _step : _from;
uint256 mid = (high + low) / 2;
// Perform a binary search on this segment // Perform a binary search in this segment
uint256 lastIndex = binarySearch(_tornadoTreesV1, _type, high, low); while (low < mid) {
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)) { if (elementExists(_tornadoTreesV1, _type, mid)) {
return binarySearch(_tornadoTreesV1, _type, _high, mid); low = mid;
} else { } else {
return binarySearch(_tornadoTreesV1, _type, mid, _low); high = mid;
} }
mid = (high + low) / 2;
}
return mid + 1;
} }
function elementExists( function elementExists(