Skip to content

Commit 4f1c60f

Browse files
I think this is handy to have and i just came in need of it.
1 parent d0a3100 commit 4f1c60f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

core/src/main/java/org/neo4j/gds/core/utils/paged/HugeLongArrayStack.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public long pop() {
5656
return array.get(--size);
5757
}
5858

59+
public long peek() {
60+
if (isEmpty()) {
61+
throw new IndexOutOfBoundsException("Stack is empty.");
62+
}
63+
return array.get(size - 1);
64+
}
65+
5966
public long size() {
6067
return size;
6168
}

core/src/test/java/org/neo4j/gds/core/utils/paged/HugeLongArrayStackTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ void testPop() {
4747
assertEquals(0, q.size());
4848
}
4949

50+
@Test
51+
void testPeek() {
52+
var q = HugeLongArrayStack.newStack(10);
53+
q.push(42);
54+
assertEquals(q.peek(), 42);
55+
q.push(1337);
56+
assertEquals(q.peek(), 1337);
57+
q.pop();
58+
assertEquals(q.peek(), 42);
59+
}
60+
5061
@Test
5162
void testPopFromFullStack() {
5263
var capacity = 10;

0 commit comments

Comments
 (0)