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