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