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

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

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

* 24+[8+E(类型字节)]*n(n为元素个数)=es *

* B=56+8*n+(E*n+对齐填充) */ @SuppressWarnings("unchecked") public class LArrayQueue extends ArrayQueue { protected E[] es; protected LArrayQueue(int capacity) { super(capacity); es = (E[]) new Object[capacity]; } public static LArrayQueue of(int capacity) { return new LArrayQueue<>(capacity); } public static LArrayQueue of() { return of(DEFAULT_CAPACITY); } @Override protected final E get(int i) { return es[i]; } @Override protected final void resize(int capacity) { E[] temp = (E[]) new Object[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] = null; } public final void offer(E e) { beforeOffer(); es[tail] = e; afterOffer(); } public final E poll() { E e = es[head]; afterPoll(); return e; } public final E head() { return es[head]; } public final E tail() { return es[tail == 0 ? capacity - 1 : tail - 1]; } public final List toList() { if (size == 0) return Collections.emptyList(); List result = new ArrayList<>(size); int current = head; while (current != tail) { result.add(es[current]); current = (current + 1) % capacity; } return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy