com.jtransc.game.util.StackPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jtransc-media-game Show documentation
Show all versions of jtransc-media-game Show documentation
JVM AOT compiler currently generating Javascript and Haxe, with initial focus on Kotlin and games.
package com.jtransc.game.util;
import java.util.ArrayList;
public class StackPool {
public interface Generator {
T generate(int index);
}
final int capacity;
final Generator generator;
private ArrayList values = new ArrayList();
private int index = 0;
public StackPool(int capacity, Generator generator) {
this.capacity = capacity;
this.generator = generator;
ensure(capacity);
}
private void ensure(int count) {
while (values.size() < count) values.add(generator.generate(values.size()));
}
public int getLength() {
return index;
}
public T push() {
ensure(index);
return values.get(index++);
}
public T pop() {
return values.get(--index);
}
public void clear() {
index = 0;
}
}