DotNet 源码学习——QUEUE

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>(12);
public Queue(int capacity)
{
    if (capacity < 0)
        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的长度。

 

 

 

1 if (_size == _array.Length)
2 {
3      int newcapacity = (int)((long)_array.Length * (long)GrowFactor / 100);
4      if (newcapacity < _array.Length + MinimumGrow)
5      {
6          newcapacity = _array.Length + MinimumGrow;
7      }
8      SetCapacity(newcapacity);
9 }

 

3、Queue为先进先出。

 

进队列在数组末尾赋值

 

        public void Enqueue(T item)
        {
            if (_size == _array.Length)
            {
                int newcapacity = (int)((long)_array.Length * (long)GrowFactor / 100);
                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 == 0)
            {
                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 == 0)
            {
                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 == 0)
            {
                result = default!;
                return false;
            }

            result = _array[_head];
            return true;            

 

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

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码dll: ComSvcConfig mscorlib PresentationBuildTasks PresentationCore PresentationFramework PresentationFramework.Aero PresentationFramework.Classic PresentationFramework.Luna PresentationFramework.Royale SMDiagnostics SMSvcHost svcutil System System.Activities System.Activities.Core.Presentation System.Activities.DurableInstancing System.Activities.Presentation System.AddIn System.AddIn.Contract System.ComponentModel.DataAnnotations System.Configuration System.Core System.Data System.Data.DataSetExtensions System.Data.Entity System.Data.Entity.Design System.Data.Linq System.Data.Services System.Data.Services.Client System.Data.Services.Design System.Data.SqlXml System.Drawing System.IdentityModel System.IdentityModel.Selectors System.IO.Log System.Management System.Messaging System.Net System.Numerics System.Runtime.Caching System.Runtime.DurableInstancing System.Runtime.Remoting System.Runtime.Serialization System.Security System.ServiceModel System.ServiceModel.Activation System.ServiceModel.Activities System.ServiceModel.Channels System.ServiceModel.Discovery System.ServiceModel.Internals System.ServiceModel.Routing System.ServiceModel.WasHosting System.ServiceModel.Web System.Transactions System.Web System.Web.ApplicationServices System.Web.DynamicData System.Web.Entity System.Web.Entity.Design System.Web.Extensions System.Web.Mobile System.Web.Routing System.Web.Services System.Windows.Forms System.Workflow.Activities System.Workflow.ComponentModel System.Workflow.Runtime System.WorkflowServices System.Xaml.Hosting System.Xml System.Xml.Linq UIAutomationClient UIAutomationClientsideProviders WindowsBase WsatConfig WsatUI XamlBuildTask

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值