算法:基于 RingBuffer 的 Deque 实现
2013-12-16 12:08
483 查看
背景
前两篇文章介绍了 Queue 的实现,很多类库都引入了 Deque,Deque 可以两头添加和删除,然后在 Deque 之上构建 Queue 和 Stack。Deque
代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataStuctureStudy.Deques { public class Deque<T> { private T[] _items = new T[0]; private int _size = 0; private int _head = -1; private int _tail = -1; private void AllocateNewArray() { int newLength = (_size == 0) ? 4 : _size * 2; T[] newArray = new T[newLength]; if (_size > 0) { // copy contents... // if the array has no wrapping, just copy the valid range // else copy from head to end of the array and then from 0 to the tail // if tail is less than head we've wrapped var newIndex = 0; if (_tail < _head) { // copy the _items[head].._items[end] -> newArray[0]..newArray for (int index = _head; index < _items.Length; index++) { newArray[newIndex] = _items[index]; newIndex++; } // copy _items[0].._items[tail] -> newArray[N+1].. for (int index = 0; index <= _tail; index++) { newArray[newIndex] = _items[index]; newIndex++; } } else { // copy the _items[head].._items[tail] -> newArray[0]..newArray for (int index = _head; index <= _tail; index++) { newArray[newIndex] = _items[index]; newIndex++; } } _head = 0; _tail = _size - 1; // compensate for the extra bump } else { // nothing in the array _head = -1; _tail = -1; } _items = newArray; } public void EnqueueFirst(T item) { // if the array needs to grow if (_items.Length == _size) { AllocateNewArray(); } // since we know the array isn't full and _head is greater than 0 // we know the slot in front of head is open if (_head > 0) { _head--; } else { // otherwise we need to wrap around to the end of the array _head = _items.Length - 1; if (_tail == -1) { _tail = _head; } } _items[_head] = item; _size++; } public void EnqueueLast(T item) { // if the array needs to grow if (_items.Length == _size) { AllocateNewArray(); } // now we have a properly sized array and can focus on wrapping issues. // if _tail is at the end of the array we need to wrap around if (_tail == _items.Length - 1) { _tail = 0; } else { _tail++; if (_head == -1) { _head = _tail; } } _items[_tail] = item; _size++; } public T DequeueFirst() { if (_size == 0) { throw new InvalidOperationException("The deque is empty"); } T value = _items[_head]; if (_head == _items.Length - 1) { // if the head is at the last index in the array - wrap around. _head = 0; } else { // move to the next slot _head++; } _size--; return value; } public T DequeueLast() { if (_size == 0) { throw new InvalidOperationException("The deque is empty"); } T value = _items[_tail]; if (_tail == 0) { // if the tai is at the first index in the array - wrap around. _tail = _items.Length - 1; } else { // move to the previous slot _tail--; } _size--; return value; } public T PeekFirst() { if (_size == 0) { throw new InvalidOperationException("The deque is empty"); } return _items[_head]; } public T PeekLast() { if (_size == 0) { throw new InvalidOperationException("The deque is empty"); } return _items[_tail]; } public int Count { get { return _size; } } } }
相关文章推荐
- 算法:基于 RingBuffer 的 Queue 实现《续》
- 算法:基于 RingBuffer 的 Queue 实现
- mahout demo——本质上是基于Hadoop的分步式算法实现,比如多节点的数据合并,数据排序,网路通信的效率,节点宕机重算,数据分步式存储
- Java Collections Framework之Deque(LinkedList实现)源码分析(基于JDK1.6)(已补充)
- [置顶] Hadoop伪分布安装详解+MapReduce运行原理+基于MapReduce的KNN算法实现
- Python实现的人工神经网络算法示例【基于反向传播算法】
- linux网络编程--Circular Buffer(Ring Buffer) 环形缓冲区的设计与实现
- 图像处理常用算法GPU实现四:基于中值滤波的二值图像平滑
- 基于块的全搜索运动估计算法实现matlab代码
- 【课程论文】基于模糊控制的路径规划算法的仿真实现
- 十三种基于直方图的图像全局二值化算法原理、实现、代码及效果。
- 朴素贝叶斯方法的应用实例----基于newsgroup文档集的贝叶斯算法实现
- 数据挖掘-基于贝叶斯算法及KNN算法的newsgroup18828文本分类器的JAVA实现(上)
- 基于硬件的ORB特征提取与实现(第一部分)——特征点算法仔细分析
- GrabCut in One Cut(基于图割算法grabcut的一次快速图像分割的OpenCV实现)----目前效果最好的图割
- 基于Spark的移动用户主要活动地点的挖掘算法实现以及JavaEE技术整合
- 图论算法——基于的java实现(dijkstra,bfs,dfs,floyd)
- 基于边界的模板匹配的原理及算法实现
- 24、USART 使用 ringbuffer 实现无阻塞的读写
- 基于直方图的图像全局二值化算法原理、实现--灰度平局值值法