Skip to content

Commit 6be0be1

Browse files
committed
Add predicate support in spatial index interface
1 parent c0039c8 commit 6be0be1

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/main/java/org/gephi/graph/api/SpatialIndex.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.gephi.graph.api;
1717

18+
import java.util.function.Predicate;
19+
1820
/**
1921
* Query the (quadtree-based) index based on the given rectangle area.
2022
* <p>
@@ -42,6 +44,15 @@ public interface SpatialIndex {
4244
*/
4345
NodeIterable getNodesInArea(Rect2D rect);
4446

47+
/**
48+
* Returns the nodes in the given area, filtered by the given predicate.
49+
*
50+
* @param rect area to query
51+
* @param predicate filter predicate
52+
* @return nodes in the area
53+
*/
54+
NodeIterable getNodesInArea(Rect2D rect, Predicate<? super Node> predicate);
55+
4556
/**
4657
* Returns the nodes in the given area using a faster, but approximate method.
4758
* <p>
@@ -53,6 +64,19 @@ public interface SpatialIndex {
5364
*/
5465
NodeIterable getApproximateNodesInArea(Rect2D rect);
5566

67+
/**
68+
* Returns the nodes in the given area using a faster, but approximate method,
69+
* filtered by the given predicate.
70+
* <p>
71+
* All nodes in the provided area are guaranteed to be returned, but some nodes
72+
* outside the area may also be returned.
73+
*
74+
* @param rect area to query
75+
* @param predicate filter predicate
76+
* @return nodes in the area
77+
*/
78+
NodeIterable getApproximateNodesInArea(Rect2D rect, Predicate<? super Node> predicate);
79+
5680
/**
5781
* Returns the edges in the given area. Edges may be returned twice.
5882
*
@@ -61,6 +85,16 @@ public interface SpatialIndex {
6185
*/
6286
EdgeIterable getEdgesInArea(Rect2D rect);
6387

88+
/**
89+
* Returns the edges in the given area, filtered by the given predicate. Edges
90+
* may be returned twice.
91+
*
92+
* @param rect area to query
93+
* @param predicate filter predicate
94+
* @return edges in the area
95+
*/
96+
EdgeIterable getEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate);
97+
6498
/**
6599
* Returns the edges in the given area using a faster, but approximate method.
66100
* <p>
@@ -72,6 +106,19 @@ public interface SpatialIndex {
72106
*/
73107
EdgeIterable getApproximateEdgesInArea(Rect2D rect);
74108

109+
/**
110+
* Returns the edges in the given area using a faster, but approximate method,
111+
* filtered by the given predicate.
112+
* <p>
113+
* All edges in the provided area are guaranteed to be returned, but some edges
114+
* outside the area may also be returned. Edges may also be returned twice.
115+
*
116+
* @param rect area to query
117+
* @param predicate filter predicate
118+
* @return edges in the area
119+
*/
120+
EdgeIterable getApproximateEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate);
121+
75122
/**
76123
* Returns the bounding rectangle that contains all nodes in the graph. The
77124
* boundaries are calculated based on each node's position and size.

src/main/java/org/gephi/graph/impl/GraphViewDecorator.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Spliterator;
2222
import java.util.ConcurrentModificationException;
2323
import java.util.function.Consumer;
24+
import java.util.function.Predicate;
2425
import org.gephi.graph.api.DirectedSubgraph;
2526
import org.gephi.graph.api.Edge;
2627
import org.gephi.graph.api.EdgeIterable;
@@ -874,6 +875,14 @@ public NodeIterable getNodesInArea(Rect2D rect) {
874875
return graphStore.spatialIndex.getNodesInArea(rect, view::containsNode);
875876
}
876877

878+
@Override
879+
public NodeIterable getNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
880+
if (graphStore.spatialIndex == null) {
881+
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
882+
}
883+
return graphStore.spatialIndex.getNodesInArea(rect, (node) -> view.containsNode(node) && predicate.test(node));
884+
}
885+
877886
@Override
878887
public NodeIterable getApproximateNodesInArea(Rect2D rect) {
879888
if (graphStore.spatialIndex == null) {
@@ -882,6 +891,15 @@ public NodeIterable getApproximateNodesInArea(Rect2D rect) {
882891
return graphStore.spatialIndex.getApproximateNodesInArea(rect, view::containsNode);
883892
}
884893

894+
@Override
895+
public NodeIterable getApproximateNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
896+
if (graphStore.spatialIndex == null) {
897+
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
898+
}
899+
return graphStore.spatialIndex
900+
.getApproximateNodesInArea(rect, (node) -> view.containsNode(node) && predicate.test(node));
901+
}
902+
885903
@Override
886904
public EdgeIterable getEdgesInArea(Rect2D rect) {
887905
if (graphStore.spatialIndex == null) {
@@ -890,6 +908,14 @@ public EdgeIterable getEdgesInArea(Rect2D rect) {
890908
return graphStore.spatialIndex.getEdgesInArea(rect, view::containsEdge);
891909
}
892910

911+
@Override
912+
public EdgeIterable getEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
913+
if (graphStore.spatialIndex == null) {
914+
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
915+
}
916+
return graphStore.spatialIndex.getEdgesInArea(rect, (edge) -> view.containsEdge(edge) && predicate.test(edge));
917+
}
918+
893919
@Override
894920
public EdgeIterable getApproximateEdgesInArea(Rect2D rect) {
895921
if (graphStore.spatialIndex == null) {
@@ -898,6 +924,15 @@ public EdgeIterable getApproximateEdgesInArea(Rect2D rect) {
898924
return graphStore.spatialIndex.getApproximateEdgesInArea(rect, view::containsEdge);
899925
}
900926

927+
@Override
928+
public EdgeIterable getApproximateEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
929+
if (graphStore.spatialIndex == null) {
930+
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
931+
}
932+
return graphStore.spatialIndex
933+
.getApproximateEdgesInArea(rect, (edge) -> view.containsEdge(edge) && predicate.test(edge));
934+
}
935+
901936
@Override
902937
public Rect2D getBoundaries() {
903938
if (graphStore.spatialIndex == null) {

src/main/java/org/gephi/graph/impl/SpatialIndexImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,22 @@ public void spatialIndexReadUnlock() {
5353
nodesTree.readUnlock();
5454
}
5555

56+
@Override
5657
public NodeIterable getNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
5758
return nodesTree.getNodes(rect, false, predicate);
5859
}
5960

61+
@Override
6062
public NodeIterable getApproximateNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
6163
return nodesTree.getNodes(rect, true, predicate);
6264
}
6365

66+
@Override
6467
public EdgeIterable getEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
6568
return nodesTree.getEdges(rect, false, predicate);
6669
}
6770

71+
@Override
6872
public EdgeIterable getApproximateEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
6973
return nodesTree.getEdges(rect, true, predicate);
7074
}

0 commit comments

Comments
 (0)