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