com.metamx.collections.spatial.search.GutmanSearchStrategy Maven / Gradle / Ivy
package com.metamx.collections.spatial.search;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.metamx.collections.spatial.ImmutableNode;
import com.metamx.collections.spatial.ImmutablePoint;
import it.uniroma3.mat.extendedset.intset.ImmutableConciseSet;
/**
*/
public class GutmanSearchStrategy implements SearchStrategy
{
@Override
public Iterable search(ImmutableNode node, Bound bound)
{
if (bound.getLimit() > 0) {
return Iterables.transform(
breadthFirstSearch(node, bound),
new Function()
{
@Override
public ImmutableConciseSet apply(ImmutableNode immutableNode)
{
return immutableNode.getImmutableConciseSet();
}
}
);
}
return Iterables.transform(
depthFirstSearch(node, bound),
new Function()
{
@Override
public ImmutableConciseSet apply(ImmutablePoint immutablePoint)
{
return immutablePoint.getImmutableConciseSet();
}
}
);
}
public Iterable depthFirstSearch(ImmutableNode node, final Bound bound)
{
if (node.isLeaf()) {
return bound.filter(
Iterables.transform(
node.getChildren(),
new Function()
{
@Override
public ImmutablePoint apply(ImmutableNode tNode)
{
return new ImmutablePoint(tNode);
}
}
)
);
} else {
return Iterables.concat(
Iterables.transform(
Iterables.filter(
node.getChildren(),
new Predicate()
{
@Override
public boolean apply(ImmutableNode child)
{
return bound.overlaps(child);
}
}
),
new Function>()
{
@Override
public Iterable apply(ImmutableNode child)
{
return depthFirstSearch(child, bound);
}
}
)
);
}
}
public Iterable breadthFirstSearch(
ImmutableNode node,
final Bound bound
)
{
if (node.isLeaf()) {
return Iterables.filter(
node.getChildren(),
new Predicate()
{
@Override
public boolean apply(ImmutableNode immutableNode)
{
return bound.contains(immutableNode.getMinCoordinates());
}
}
);
}
return breadthFirstSearch(node.getChildren(), bound, 0);
}
public Iterable breadthFirstSearch(
Iterable nodes,
final Bound bound,
int total
)
{
Iterable points = Iterables.concat(
Iterables.transform(
Iterables.filter(
nodes,
new Predicate()
{
@Override
public boolean apply(ImmutableNode immutableNode)
{
return immutableNode.isLeaf();
}
}
),
new Function>()
{
@Override
public Iterable apply(ImmutableNode immutableNode)
{
return Iterables.filter(
immutableNode.getChildren(),
new Predicate()
{
@Override
public boolean apply(ImmutableNode immutableNode)
{
return bound.contains(immutableNode.getMinCoordinates());
}
}
);
}
}
)
);
Iterable overlappingNodes = Iterables.filter(
nodes,
new Predicate()
{
@Override
public boolean apply(ImmutableNode immutableNode)
{
return !immutableNode.isLeaf() && bound.overlaps(immutableNode);
}
}
);
int totalPoints = Iterables.size(points);
int totalOverlap = Iterables.size(overlappingNodes);
if (totalOverlap == 0 || (totalPoints + totalOverlap + total) >= bound.getLimit()) {
return Iterables.concat(
points,
overlappingNodes
);
} else {
return Iterables.concat(
points,
breadthFirstSearch(
Iterables.concat(
Iterables.transform(
overlappingNodes,
new Function>()
{
@Override
public Iterable apply(ImmutableNode immutableNode)
{
return immutableNode.getChildren();
}
}
)
),
bound,
totalPoints
)
);
}
}
}