讀ArrayDeque源碼


//一個雙端隊列 比stack和LinkedList效率高
//先看構造函數

public ArrayDeque() {
elements = (E[]) new Object[16];
}

public ArrayDeque(int numElements) {
allocateElements(numElements);
}

private void allocateElements(int numElements) {
int initialCapacity = MIN_INITIAL_CAPACITY;

if (numElements >= initialCapacity) {
initialCapacity = numElements;
initialCapacity |= (initialCapacity >>> 1);
initialCapacity |= (initialCapacity >>> 2);
initialCapacity |= (initialCapacity >>> 4);
initialCapacity |= (initialCapacity >>> 8);
initialCapacity |= (initialCapacity >>> 16);
initialCapacity++;

if (initialCapacity < 0) // Too many elements, must back off
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
}
elements = (E[]) new Object[initialCapacity];
}


public ArrayDeque(Collection<? extends E> c) {
allocateElements(c.size());
addAll(c);
}

public boolean add(E e) {
addLast(e);
return true;
}

//在隊尾添加元素
public void addLast(E e) {
if (e == null)
throw new NullPointerException();
elements[tail] = e;

if ( (tail = (tail + 1) & (elements.length - 1)) == head)
//擴容
doubleCapacity();
}


private void doubleCapacity() {
assert head == tail;
int p = head;
int n = elements.length;
int r = n - p; // number of elements to the right of p
int newCapacity = n << 1;
if (newCapacity < 0)
throw new IllegalStateException("Sorry, deque too big");
Object[] a = new Object[newCapacity];
//把head之前的元素複製到a數組
System.arraycopy(elements, p, a, 0, r);
//複製head之後的元素到a數組
System.arraycopy(elements, 0, a, r, p);
elements = (E[])a;
head = 0;
tail = n;
}

//在隊首添加元素
public void addFirst(E e) {
if (e == null)
throw new NullPointerException();
elements[head = (head - 1) & (elements.length - 1)] = e;
if (head == tail)
doubleCapacity();
}

public boolean offerFirst(E e) {
addFirst(e);
return true;
}


//壓入隊列的頭部
public void push(E e) {
addFirst(e);
}

public boolean offer(E e) {
return offerLast(e);
}

//獲取第一個
public E getFirst() {
E x = elements[head];
if (x == null)
throw new NoSuchElementException();
return x;
}

//獲取最後一個元素
public E getLast() {
E x = elements[(tail - 1) & (elements.length - 1)];
if (x == null)
throw new NoSuchElementException();
return x;
}

public E peekFirst() {
return elements[head];
}

public E peekLast() {
return elements[(tail - 1) & (elements.length - 1)];
}


//從開頭獲取元素並移除
public E pollFirst() {
int h = head;
E result = elements[h]; // Element is null if deque empty
if (result == null)
return null;
elements[h] = null; // Must null out slot
head = (h + 1) & (elements.length - 1);
return result;
}

//從隊尾獲取元素並移除
public E pollLast() {
int t = (tail - 1) & (elements.length - 1);
E result = elements[t];
if (result == null)
return null;
elements[t] = null;
tail = t;
return result;
}

public E removeFirst() {
E x = pollFirst();
if (x == null)
throw new NoSuchElementException();
return x;
}

public E removeLast() {
E x = pollLast();
if (x == null)
throw new NoSuchElementException();
return x;
}

public E remove() {
return removeFirst();
}

//從隊列頭部遍歷刪除元素第一次出現的位置
public boolean removeFirstOccurrence(Object o) {
if (o == null)
return false;
int mask = elements.length - 1;
int i = head;
E x;
while ( (x = elements[i]) != null) {
if (o.equals(x)) {
delete(i);
return true;
}
i = (i + 1) & mask;
}
return false;
}

private boolean delete(int i) {
checkInvariants();
final E[] elements = this.elements;
final int mask = elements.length - 1;
final int h = head;
final int t = tail;
final int front = (i - h) & mask;
final int back = (t - i) & mask;

// Invariant: head <= i < tail mod circularity
if (front >= ((t - h) & mask))
throw new ConcurrentModificationException();

// Optimize for least element motion
if (front < back) {
if (h <= i) {
System.arraycopy(elements, h, elements, h + 1, front);
} else { // Wrap around
System.arraycopy(elements, 0, elements, 1, i);
elements[0] = elements[mask];
System.arraycopy(elements, h, elements, h + 1, mask - h);
}
elements[h] = null;
head = (h + 1) & mask;
return false;
} else {
if (i < t) { // Copy the null tail as well
System.arraycopy(elements, i + 1, elements, i, back);
tail = t - 1;
} else { // Wrap around
System.arraycopy(elements, i + 1, elements, i, mask - i);
elements[mask] = elements[0];
System.arraycopy(elements, 1, elements, 0, t);
tail = (t - 1) & mask;
}
return true;
}
}

//從後遍歷刪除元素
public boolean removeLastOccurrence(Object o) {
if (o == null)
return false;
int mask = elements.length - 1;
int i = (tail - 1) & mask;
E x;
while ( (x = elements[i]) != null) {
if (o.equals(x)) {
delete(i);
return true;
}
i = (i - 1) & mask;
}
return false;
}

public E element() {
return getFirst();
}

public E pop() {
return removeFirst();
}

//返回隊列長度
public int size() {
return (tail - head) & (elements.length - 1);
}

public boolean isEmpty() {
return head == tail;
}

//是否包含某個元素
public boolean contains(Object o) {
if (o == null)
return false;
int mask = elements.length - 1;
int i = head;
E x;
while ( (x = elements[i]) != null) {
if (o.equals(x))
return true;
i = (i + 1) & mask;
}
return false;
}

//清空隊列
public void clear() {
int h = head;
int t = tail;
if (h != t) { // clear all cells
head = tail = 0;
int i = h;
int mask = elements.length - 1;
do {
elements[i] = null;
i = (i + 1) & mask;
} while (i != t);
}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章