Skip to content

Commit 553003d

Browse files
extract
1 parent 64505e9 commit 553003d

File tree

2 files changed

+64
-35
lines changed

2 files changed

+64
-35
lines changed

algo/src/main/java/org/neo4j/gds/triangle/intersect/GraphIntersect.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.jetbrains.annotations.Nullable;
2323
import org.neo4j.gds.api.AdjacencyCursor;
24+
import org.neo4j.gds.api.AdjacencyCursorUtils;
2425
import org.neo4j.gds.api.IntersectionConsumer;
2526
import org.neo4j.gds.api.RelationshipIntersect;
2627

@@ -67,7 +68,7 @@ private void triangles(
6768
CURSOR neighborsOfa,
6869
IntersectionConsumer consumer
6970
) {
70-
long b = next(neighborsOfa);
71+
long b = AdjacencyCursorUtils.next(neighborsOfa);
7172
while (b != NOT_FOUND && b < a) {
7273
var degreeOfb = degree(b);
7374
if (degreeFilter.test(degreeOfb)) {
@@ -88,23 +89,23 @@ private void triangles(
8889
); //find all triangles involving the edge (a-b)
8990
}
9091

91-
b = next(neighborsOfa);
92+
b = AdjacencyCursorUtils.next(neighborsOfa);
9293
}
9394

9495
}
9596

9697
private void triangles(long a, long b, CURSOR neighborsOfa, CURSOR neighborsOfb, IntersectionConsumer consumer) {
97-
long c = next(neighborsOfb);
98-
long currentOfa = next(neighborsOfa);
98+
long c = AdjacencyCursorUtils.next(neighborsOfb);
99+
long currentOfa = AdjacencyCursorUtils.next(neighborsOfa);
99100
while (c != NOT_FOUND && currentOfa != NOT_FOUND && c < b) {
100101
var degreeOfc = degree(c);
101102
if (degreeFilter.test(degreeOfc)) {
102-
currentOfa = advance(neighborsOfa, currentOfa, c);
103+
currentOfa = AdjacencyCursorUtils.advance(neighborsOfa, currentOfa, c);
103104
//now print all triangles a-b-c (taking into consideration the parallel edges of c)
104105
checkForAndEmitTriangle(consumer, a, b, currentOfa, c);
105106

106107
}
107-
c = next(neighborsOfb);
108+
c = AdjacencyCursorUtils.next(neighborsOfb);
108109
}
109110
}
110111

@@ -123,35 +124,6 @@ private void checkForAndEmitTriangle(
123124
}
124125
}
125126

126-
private long advance(CURSOR adjacencyList, long start, long target) {
127-
long current = start;
128-
while (current != NOT_FOUND && current < target) {
129-
current = next(adjacencyList);
130-
}
131-
return current;
132-
}
133-
private long next(CURSOR adjacencyList) {
134-
135-
if (!adjacencyList.hasNextVLong()) {
136-
return NOT_FOUND;
137-
}
138-
var value = adjacencyList.nextVLong();
139-
140-
while (peek(adjacencyList) == value) {
141-
adjacencyList.nextVLong();
142-
}
143-
144-
return value;
145-
}
146-
147-
private long peek(CURSOR adjacencyList) {
148-
149-
if (!adjacencyList.hasNextVLong()) {
150-
return NOT_FOUND;
151-
}
152-
153-
return adjacencyList.peekVLong();
154-
}
155127

156128
protected abstract CURSOR cursorForNode(@Nullable CURSOR reuse, long node, int degree);
157129

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.api;
21+
22+
import static org.neo4j.gds.api.AdjacencyCursor.NOT_FOUND;
23+
24+
public final class AdjacencyCursorUtils {
25+
26+
private AdjacencyCursorUtils() {}
27+
28+
public static <CURSOR extends AdjacencyCursor> long advance(CURSOR adjacencyList, long start, long target) {
29+
long current = start;
30+
while (current != NOT_FOUND && current < target) {
31+
current = next(adjacencyList);
32+
}
33+
return current;
34+
}
35+
36+
public static <CURSOR extends AdjacencyCursor> long next(CURSOR adjacencyList) {
37+
38+
if (!adjacencyList.hasNextVLong()) {
39+
return NOT_FOUND;
40+
}
41+
var value = adjacencyList.nextVLong();
42+
43+
while (peek(adjacencyList) == value) {
44+
adjacencyList.nextVLong();
45+
}
46+
47+
return value;
48+
}
49+
50+
public static <CURSOR extends AdjacencyCursor> long peek(CURSOR adjacencyList) {
51+
52+
if (!adjacencyList.hasNextVLong()) {
53+
return NOT_FOUND;
54+
}
55+
return adjacencyList.peekVLong();
56+
}
57+
}

0 commit comments

Comments
 (0)