While doing a project I wrote this line, basically it decided whether or not to merge the current node based on how many children there are.
int succNodes = Arrays.stream(children).mapToInt(PRQuadNode::count).sum();
if (succNodes <= bucketingParam) { /* do something */ }
The problem is that succNodes
will often be significantly larger than bucketingParam
. And there's no point to continue counting if I have already found a large enough sum. What would be the best way to enable the stream to stop early, if I knew I was going to fail the check succNodes <= bucketingParam?
Note: In this case children is always of size 4.
Note 2: PRQuadNode::count is a recursive method, it is not tail recursive.