1、Queue声明创建对象。(Queue为泛型对象。)

public class Queue<T> :IEnumerable<T>,System.Collections.ICollection,IReadOnlyCollection<T>

本质为Array对象存储数据。

Queue<string> qy = new Queue<string>();
private T[] _array
public Queue()
{
_array = Array.Empty<T>();
}
Queue<int> qy1 = new Queue<int>();
public Queue(int capacity)
{
if (capacity < )
throw new ArgumentOutOfRangeException(nameof(capacity), capacity, SR.ArgumentOutOfRange_NeedNonNegNum);
_array = new T[capacity];
}
Queue<int> qy2 = new Queue<int>(new List<int>());

public Queue(IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection)); _array = EnumerableHelpers.ToArray(collection, out _size);
if (_size != _array.Length) _tail = _size;
}

2、队列的空间是实际存储值的两倍,如果小于两倍则每次增场4的长度。

 if (_size == _array.Length)
{
int newcapacity = (int)((long)_array.Length * (long)GrowFactor / );
if (newcapacity < _array.Length + MinimumGrow)
{
newcapacity = _array.Length + MinimumGrow;
}
SetCapacity(newcapacity);
}

3、Queue为先进先出。

进队列在数组末尾赋值

        public void Enqueue(T item)
{
if (_size == _array.Length)
{
int newcapacity = (int)((long)_array.Length * (long)GrowFactor / );
if (newcapacity < _array.Length + MinimumGrow)
{
newcapacity = _array.Length + MinimumGrow;
}
SetCapacity(newcapacity);
} _array[_tail] = item;
MoveNext(ref _tail);
_size++;
_version++;
}

出队列则从头部取值

        public T Dequeue()
{
int head = _head;
T[] array = _array; if (_size == )
{
ThrowForEmptyQueue();
} T removed = array[head];
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
array[head] = default!;
}
MoveNext(ref _head);
_size--;
_version++;
return removed;
}

4、TryDequeue 与 Dequeue()区别在于 TryDequeue 判断size为0时,返回false 并out 泛型的默认值

        public bool TryDequeue([MaybeNullWhen(false)] out T result)
{
int head = _head;
T[] array = _array; if (_size == )
{
result = default!;
return false;
} result = array[head];
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
array[head] = default!;
}
MoveNext(ref _head);
_size--;
_version++;
return true;
}

注:MaybeNullWhenAttribute(Boolean) 返回值条件。 如果方法返回此值,则关联的参数可能为 null

5、Peek 仅仅时查看一下下一个要返回的值,不从数组移除

            if (_size == )
{
result = default!;
return false;
} result = _array[_head];
return true;

随是拙见,但为原创,转载注明出处,敬谢

Queue 常用的方法大约就这些。先写道这里,碎觉。

以下为公众号,一起学起来~

DotNet 源码学习——QUEUE的更多相关文章

  1. DotNet源码学习-HASHSET(初探)

    命名空间:System.Collections.Generic 先看一下官方说明:类提供了高级的设置操作.集是不包含重复元素的集合,其元素无特定顺序. HashSet <T>对象的容量是对 ...

  2. Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结

    2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...

  3. Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析

    经过前面几篇文章的铺垫,今天我们终于要看看AQS的庐山真面目了,建议第一次看AbstractQueuedSynchronizer 类源码的朋友可以先看下我前面几篇文章: <Java并发包源码学习 ...

  4. Java并发包源码学习之AQS框架(一)概述

    AQS其实就是java.util.concurrent.locks.AbstractQueuedSynchronizer这个类. 阅读Java的并发包源码你会发现这个类是整个java.util.con ...

  5. igmpproxy源码学习——igmpProxyInit()

    igmpproxy源码学习--igmpProxyInit()函数具体解释.igmpproxy初始化 在执行igmpproxy的主程序igmpproxyRun()之前须要对igmpproxy进行一些配置 ...

  6. [Android FrameWork 6.0源码学习] View的重绘过程之WindowManager的addView方法

    博客首页:http://www.cnblogs.com/kezhuang/p/关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下<[Andr ...

  7. threadpool源码学习

    threadpool源码学习 __all__ = [ 'makeRequests', 'NoResultsPending', 'NoWorkersAvailable', 'ThreadPool', ' ...

  8. RocketMQ 源码学习笔记————Producer 是怎么将消息发送至 Broker 的?

    目录 RocketMQ 源码学习笔记----Producer 是怎么将消息发送至 Broker 的? 前言 项目结构 rocketmq-client 模块 DefaultMQProducerTest ...

  9. RocketMQ 源码学习笔记 Producer 是怎么将消息发送至 Broker 的?

    目录 RocketMQ 源码学习笔记 Producer 是怎么将消息发送至 Broker 的? 前言 项目结构 rocketmq-client 模块 DefaultMQProducerTest Roc ...

随机推荐

  1. FastDF step by step

    step one 肯定是安装一个FastDF服务了 step two FasDFS配置节点 step third 码代码

  2. WEB Node-JS 服务器搭建

    一.创建express 1.创建一个单独文件 2.打开命令面板,进入该文件 3.npm config set registry = https://registry.npm.taobao.org(设置 ...

  3. ios---二维码的扫描

    二维码扫描 使用ios的AVFoundation框架实现二维码扫描 第一步:设置相机访问权限:在Info.plist添加Privacy - Camera Usage Description权限 第二步 ...

  4. VirtualBox 虚拟机 从入门到入坑

                                                                                                        ...

  5. (转)Gamma分布,Beta分布,Multinomial多项式分布,Dirichlet狄利克雷分布

    1. Gamma函数 首先我们可以看一下Gamma函数的定义: Gamma的重要性质包括下面几条: 1. 递推公式: 2. 对于正整数n, 有 因此可以说Gamma函数是阶乘的推广. 3.  4.  ...

  6. Leetcode 题目整理 climbing stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. 安装Eclipse activity插件 报异常 Cannot complete the install because one or more required items could not be

    下载插件:Activiti Designer 5.17 2)安装过程中错误处理 a.错误: Cannot complete the install because one or more requir ...

  8. 【Nginx入门系列】第一章 手把手带你搭建Nginx服务器

    1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境,搭建前请先按如下语句配置好环境. GCC 安装nginx需要先将官网下载的源码进行编译 ...

  9. 六、Django学习之基于下划线的跨表查询

    六.Django学习之基于下划线的跨表查询 一对一 正向查询的例子为 已知用户名,查询用户的电话号码.反向查询例子反之. 正向查询 其中下划线前的表示表名,无下划线的表示的是Author表 resul ...

  10. IntelliJ IDEA 2020 的Debug功能也太好用了,真香!

    写在前边 作为一个有点强迫症的程序员来说,所有的应用软件.开发工具都必须要升级到最高版本,否则就会很难受到坐立不安.日思夜想.茶饭不思.至于什么时候得的这种病我也记不清了,哈哈哈 IntelliJ I ...