All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.core.queue.a.JArrayQueue Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
package shz.core.queue.a;

import shz.core.constant.ArrayConstant;

import java.util.Objects;
import java.util.function.LongConsumer;

/**
 * 元素类型为long基于循环动态数组的队列
 * 

* 24+8*n(n为元素个数)=es *

* B=56+8*n */ public class JArrayQueue extends ArrayQueue { private static final long serialVersionUID = 3250673406272799783L; protected long[] es; public JArrayQueue(int capacity) { super(capacity); es = new long[this.capacity]; } public JArrayQueue() { this(DEFAULT_CAPACITY); } public static JArrayQueue of(int capacity) { return new JArrayQueue(capacity); } public static JArrayQueue of() { return new JArrayQueue(); } @Override protected final void resize(int capacity) { long[] temp = new long[capacity]; for (int i = 0; i < size; ++i) temp[i] = es[(i + head) % this.capacity]; this.capacity = capacity; head = 0; tail = size; es = temp; } @Override protected final void setNull(int i) { es[i] = 0L; } public final void offer(long e) { beforeOffer(); es[tail] = e; afterOffer(); } public final long poll() { checkEmpty(); long e = es[head]; afterPoll(); return e; } public final long head() { checkEmpty(); return es[head]; } public final long tail() { checkEmpty(); return es[tail == 0 ? capacity - 1 : tail - 1]; } public final long get(int idx) { if (idx < 0 || idx >= size) throw new IndexOutOfBoundsException(); return es[(head + idx) % capacity]; } public final long[] toArray() { if (size == 0) return ArrayConstant.EMPTY_LONG_ARRAY; long[] array = new long[size]; int idx = 0; int current = head; while (current != tail) { array[idx++] = es[current]; current = (current + 1) % capacity; } return array; } public final void forEach(LongConsumer action) { Objects.requireNonNull(action); int current = head; while (current != tail) { action.accept(es[current]); current = (current + 1) % capacity; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy