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 = int>(12);
public Queue(int capacity)
{
    if (capacity < 0)
        throw new ArgumentOutOfRangeException(nameof(capacity),capacity,SR.ArgumentOutOfRange_NeedNonNegNum);
    _array =  T[capacity];
}
Queue<int> qy2 = int>(new List<int>());

public Queue(IEnumerable<T> collection)
{
    if (collection == null)
        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 / 1004      if (newcapacity < _array.Length + MinimumGrow)
5      {
6          newcapacity = _array.Length + MinimumGrow;
7      }
8      SetCapacity(newcapacity);
9 }

 

3、Queue为先进先出。

 

进队列在数组末尾赋值

 

        void Enqueue(T item)
        {
             _array.Length)
            {
                );
                 MinimumGrow)
                {
                    newcapacity = _array.Length + MinimumGrow;
                }
                SetCapacity(newcapacity);
            }

            _array[_tail] = item;
            MoveNext(ref _tail);
            _size++;
            _version++;
        }

 

出队列则从头部取值

 

 

         T Dequeue()
        {
            int head = _head;
            T[] array = _array;

            if (_size == )
            {
                ThrowForEmptyQueue();
            }

            T removed = array[head];
            if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
            {
                array[head] = default!;
            }
            MoveNext( _head);
            _size--;
            return removed;
        }    

 

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

 

 

        bool TryDequeue([MaybeNullWhen(false)] out T result)
        {
            )
            {
                result = ;
                return false;
            }

            result =true;
        }    

 

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

 

 

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

 

             _array[_head];
            true;            

 

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

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

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

 

相关文章

引言 本文从Linux小白的视角, 在CentOS 7.x服务器上搭建一个...
引言: 多线程编程/异步编程非常复杂,有很多概念和工具需要...
一. 宏观概念 ASP.NET Core Middleware是在应用程序处理管道...
背景 在.Net和C#中运行异步代码相当简单,因为我们有时候需要...
HTTP基本认证 在HTTP中,HTTP基本认证(Basic Authenticatio...
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续...