shz.core.stack.a.JArrayStack Maven / Gradle / Ivy
package shz.core.stack.a;
import shz.core.ArrayHelp;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.LongConsumer;
/**
* 元素类型为long基于动态数组的栈
*
* 24+8*n(n为元素个数)=es
*
* B=48+8*n
*/
public class JArrayStack extends ArrayStack {
private static final long serialVersionUID = -4128459839564949570L;
protected long[] es;
public JArrayStack(int capacity) {
super(capacity);
es = new long[capacity];
}
public JArrayStack() {
this(DEFAULT_CAPACITY);
}
public static JArrayStack of(int capacity) {
return new JArrayStack(capacity);
}
public static JArrayStack of() {
return new JArrayStack();
}
@Override
protected final void resize(int capacity) {
this.capacity = capacity;
es = Arrays.copyOf(es, capacity);
}
@Override
protected final void setNull(int i) {
es[i] = 0L;
}
public final void push(long e) {
beforePush();
es[size++] = e;
}
public final long pop() {
checkEmpty();
long e = es[--size];
afterPop();
return e;
}
public final long peek() {
checkEmpty();
return es[size - 1];
}
public final void reverse() {
if (size > 1) ArrayHelp.reverse(es, 0, size);
}
public final void forEach(LongConsumer action) {
Objects.requireNonNull(action);
int current = size;
while (current > 0) action.accept(es[--current]);
}
}