<学习记录>Queue源码学习
2017-03-13 11:32
323 查看
Queue实现方式(ArrayDeque和LinkedList)
ArrayDeque:循环数组elements保存元素
transient Object[] elements;
transient int head;
transient int tail;
构造
调整数组长度:
添加
移除
获取元素
ArrayDeque:循环数组elements保存元素
transient Object[] elements;
transient int head;
transient int tail;
构造
public ArrayDeque() { elements = new Object[16]; } public ArrayDeque(int numElements) { allocateElements(numElements); }
调整数组长度:
private void allocateElements(int numElements) { int initialCapacity = MIN_INITIAL_CAPACITY; // Find the best power of two to hold elements. // Tests "<=" because arrays aren't kept full. 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 = new Object[initialCapacity]; }解析:根据特定参数设置初始数组的大小。中间移位和或操作是将initialCapacity左边最高位的1复制到右边,再加1变成2的幂次方。
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]; System.arraycopy(elements, p, a, 0, r); System.arraycopy(elements, 0, a, r, p); elements = a; head = 0; tail = n; }解析:这个数组长度扩展,当元素个数增长到等于数组大小时(head==tail),此时将数组大小扩充一倍,再使用arraycopy将元素复制到新的数组。arraycopy(src,srcPos,dest,destPos,length),src指原数组,srcPos指数组复制的开始位置,dest指目的数组,destPos指目的数组的起始位置,length指要复制的长度。
添加
public void addFirst(E e) { if (e == null) throw new NullPointerException(); elements[head = (head - 1) & (elements.length - 1)] = e; if (head == tail) doubleCapacity(); }
public void addLast(E e) { if (e == null) throw new NullPointerException(); elements[tail] = e; if ( (tail = (tail + 1) & (elements.length - 1)) == head) doubleCapacity(); }
public boolean offerFirst(E e) { addFirst(e); return true; }
public boolean offerLast(E e) { addLast(e); return true; }添加是将新的元素添加到队头或者队尾,add和offer的功能实现是一样的。因为已经初始化了数组大小,出头元素时head往后移动,如尾元素时tail往后移动,当移动到数组最后时,会将head或者tail循环到数组的前面。所以
head = (head - 1) & (elements.length - 1)的功能相当于
head = (head - 1) %(element.length - 1)由于tail指向的是队尾的后一位,所以先入队尾,再
(tail = (tail + 1) & (elements.length - 1)功能相当于
tail = (tail + 1) % (elements.length - 1)当tail==head时,说明元素已满,所以要扩充数组大小,并使用arraycopy拷贝数组。
移除
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 pollFirst() { int h = head; @SuppressWarnings("unchecked") E result = (E) 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); @SuppressWarnings("unchecked") E result = (E) elements[t]; if (result == null) return null; elements[t] = null; tail = t; return result; }解析:poll和remove都是移除元素操作,实现方法一样。出队头元素,将头元素为null,将head移动到下一个位置;
head = (h + 1) & (elements.length - 1);出队尾元素,将最后一个元素为null,tail指向前一个位置。
t = (tail - 1) & (elements.length - 1);
获取元素
public E getFirst() { @SuppressWarnings("unchecked") E result = (E) elements[head]; if (result == null) throw new NoSuchElementException(); return result; }
public E getLast() { @SuppressWarnings("unchecked") E result = (E) elements[(tail - 1) & (elements.length - 1)]; if (result == null) throw new NoSuchElementException(); return result; }
public E peekFirst() { // elements[head] is null if deque empty return (E) elements[head]; }
public E peekLast() { return (E) elements[(tail - 1) & (elements.length - 1)]; }解析:peek和get的方法实现一样。主要是获取头元素和尾元素的数组下标。
相关文章推荐
- Delphi-IOCP学习笔记<九>======性能测试和IOCP源码下载<5000个连接数>
- <<精通Spring2.x>>笔记:第2章学习源码
- [记录]学习<<QT学习之路2>>第二天
- 20100611 学习记录:Sections must only appear once per config file. See the help topic <location> for exceptions.
- HTML学习记录<一>:<abbr>及<acronym>
- 记录下再次学习<深入浅出MFC>
- cmake 学习之路 记录中间所遇到过的所有错误与问题的解决 <三>
- [记录]学习<<QT学习之路2>>第四天
- Android学习记录<五>
- android学习记录<二>
- silverlight学习<三> 记录今天silverlight调用WCF服务出现的各种状况
- Android学习记录<三>
- linux简单指令学习历史记录<不断累加中>
- 14-9-11 C/C++课程设计--图书馆管理系---<time.h>中时间数据类型的学习记录
- <Android API Guide学习记录> -- Activities
- 在别的地方看的<<给程序员介绍一些C++开源库>>,记录给大家共同学习
- muduo网络库学习之BlockinngQueue<T>类、ThreadPool 类、Singleton类封装中的知识点
- [记录]学习<<QT学习之路2>>第五天
- muduo 6 网络库学习之BlockinngQueue<T>类、ThreadPool 类、Singleton类封装中的知识点
- Android学习记录<四>