Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve][Java] Make EdgesCollection and VerticesCollection support foreach loop #270

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ on:
branches:
- main
paths:
- 'cpp/include/**'
- 'java/**'
- '.github/workflows/java.yml'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
import com.alibaba.graphar.stdcxx.StdString;
import com.alibaba.graphar.types.AdjListType;
import com.alibaba.graphar.util.Result;
import java.util.Iterator;

/** EdgesCollection is designed for reading a collection of edges. */
public interface EdgesCollection extends CXXPointer {
public interface EdgesCollection extends CXXPointer, Iterable<Edge> {
/**
* Construct the collection for a range of edges.
*
Expand Down Expand Up @@ -129,4 +130,24 @@ static EdgesCollection create(

/** Get the number of edges in the collection. */
long size();

/** Implement Iterable interface to support for-each loop. */
default Iterator<Edge> iterator() {
return new Iterator<Edge>() {
EdgeIter current = begin();
EdgeIter end = end();

@Override
public boolean hasNext() {
return !current.isEnd();
}

@Override
public Edge next() {
Edge ret = current.get();
current.inc();
return ret;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
import com.alibaba.fastffi.FFITypeAlias;
import com.alibaba.graphar.graphinfo.VertexInfo;
import com.alibaba.graphar.stdcxx.StdString;
import java.util.Iterator;

/** VerticesCollection is designed for reading a collection of vertices. */
@FFIGen
@FFITypeAlias(GAR_VERTICES_COLLECTION)
@CXXHead(GAR_GRAPH_H)
public interface VerticesCollection extends CXXPointer {
public interface VerticesCollection extends CXXPointer, Iterable<Vertex> {

/** The iterator pointing to the first vertex. */
@CXXValue
Expand All @@ -48,6 +49,26 @@ public interface VerticesCollection extends CXXPointer {

long size();

/** Implement Iterable interface to support for-each loop. */
default Iterator<Vertex> iterator() {
return new Iterator<Vertex>() {
VertexIter current = begin();
VertexIter end = end();

@Override
public boolean hasNext() {
return !current.eq(end);
}

@Override
public Vertex next() {
Vertex ret = current.get();
current.inc();
return ret;
}
};
}

@FFIFactory
interface Factory {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ public void test1() {
AdjListType.ordered_by_source,
0,
1);
EdgeIter end = edges.end();
EdgeIter it = edges.begin();
long count = 0L;
for (EdgeIter it = edges.begin(); !it.eq(end); it.inc()) {
for (Edge edge : edges) {
// access data through iterator directly
System.out.print("src=" + it.source() + ", dst=" + it.destination() + " ");
// access data through edge
Edge edge = it.get();
Assert.assertEquals(edge.source(), it.source());
Assert.assertEquals(edge.destination(), it.destination());
StdString creationDate = StdString.create("creationDate");
System.out.println("creationDate=" + edge.property(creationDate, creationDate).value());
count++;
it.inc();
}
System.out.println("edge_count=" + count);
Assert.assertEquals(count, edges.size());
Expand All @@ -69,7 +69,7 @@ public void test1() {
4);
EdgeIter end1 = edges1.end();
long count1 = 0;
for (EdgeIter it = edges1.begin(); !it.eq(end1); it.inc()) {
for (Edge edge : edges1) {
count1++;
}
System.out.println("edge_count=" + count1);
Expand All @@ -83,10 +83,8 @@ public void test1() {
edgeLabel.toJavaString(),
dstLabel.toJavaString(),
AdjListType.ordered_by_source);
EdgeIter end2 = edges2.end();
long count2 = 0;
for (EdgeIter it = edges2.begin(); !it.eq(end2); it.inc()) {
Edge edge = it.get();
for (Edge edge : edges2) {
System.out.println("src=" + edge.source() + ", dst=" + edge.destination());
count2++;
}
Expand All @@ -103,9 +101,8 @@ public void test1() {
AdjListType.unordered_by_source,
5,
5);
EdgeIter end3 = edges3.end();
long count3 = 0;
for (EdgeIter it = edges3.begin(); !it.eq(end3); it.inc()) {
for (Edge edge : edges3) {
count3++;
}
System.out.println("edge_count=" + count3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void test1() {
GrapharStaticFunctions.INSTANCE.constructVerticesCollection(graphInfo, label);
Assert.assertFalse(maybeVerticesCollection.hasError());
VerticesCollection vertices = maybeVerticesCollection.value();
for (VertexIter it = vertices.begin(); !it.eq(vertices.end()); it.inc()) {
VertexIter it = vertices.begin();
for (Vertex vertex : vertices) {
// access data through iterator directly
VertexIterGen itGen = (VertexIterGen) it;
System.out.println(
Expand All @@ -50,7 +51,6 @@ public void test1() {
+ ", firstName="
+ it.property(property, property).value().toJavaString());
// access data through vertex
Vertex vertex = it.get();
System.out.println(
vertex.id()
+ ", id="
Expand All @@ -66,6 +66,7 @@ public void test1() {
Assert.assertEquals(
it.<StdString>property(property, property).value().toJavaString(),
vertex.<StdString>property(property, property).value().toJavaString());
it.inc();
}
}
}