-
Notifications
You must be signed in to change notification settings - Fork 3
/
_03_01_MultiStack.groovy
36 lines (32 loc) · 1.06 KB
/
_03_01_MultiStack.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Ch03_StacksAndQueues
/** Simulate multiple stacks with one array */
class MultiStack<T> {
private T[] values
private int[] sizes
private int capacity
def MultiStack(int numberOfStacks, int capacity) {
this.values = new T[numberOfStacks * capacity]
this.sizes = new int[numberOfStacks]
this.capacity = capacity
}
void push(int id, T value) {
sizes[id]++
values[getLastIndex(id)] = value
}
T peek(int id) { values[getLastIndex(id)] }
T pop(int id) {
def index = getLastIndex(id)
def val = values[index]
values[index] = null
sizes[id]--
val
}
boolean isEmpty(int id) { getSize(id) == 0 }
private getLastIndex(int id) {
def offset = validateId(id) * capacity
offset + getSize(id) - 1
}
private getSize(int id) { assertValid(0..capacity, sizes[validateId(id)]) }
private validateId(int id) { assertValid(0..<sizes.size(), id) }
private assertValid(IntRange range, int value) { assert range.contains(value); value }
}