Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Deque.cs
Go to the documentation of this file.
2
4
5[DebuggerDisplay("Count = {_size}")]
6internal sealed class Deque<T>
7{
8 private T[] _array = Array.Empty<T>();
9
10 private int _head;
11
12 private int _tail;
13
14 private int _size;
15
16 public int Count => _size;
17
18 public bool IsEmpty => _size == 0;
19
20 public void EnqueueTail(T item)
21 {
22 if (_size == _array.Length)
23 {
24 Grow();
25 }
26 _array[_tail] = item;
27 if (++_tail == _array.Length)
28 {
29 _tail = 0;
30 }
31 _size++;
32 }
33
34 public T DequeueHead()
35 {
36 T result = _array[_head];
37 _array[_head] = default(T);
38 if (++_head == _array.Length)
39 {
40 _head = 0;
41 }
42 _size--;
43 return result;
44 }
45
46 public T PeekHead()
47 {
48 return _array[_head];
49 }
50
51 public T DequeueTail()
52 {
53 if (--_tail == -1)
54 {
55 _tail = _array.Length - 1;
56 }
57 T result = _array[_tail];
58 _array[_tail] = default(T);
59 _size--;
60 return result;
61 }
62
64 {
65 int pos = _head;
66 int count = _size;
67 while (count-- > 0)
68 {
69 yield return _array[pos];
70 pos = (pos + 1) % _array.Length;
71 }
72 }
73
74 private void Grow()
75 {
76 int num = (int)((long)_array.Length * 2L);
77 if (num < _array.Length + 4)
78 {
79 num = _array.Length + 4;
80 }
81 T[] array = new T[num];
82 if (_head == 0)
83 {
85 }
86 else
87 {
88 Array.Copy(_array, _head, array, 0, _array.Length - _head);
89 Array.Copy(_array, 0, array, _array.Length - _head, _tail);
90 }
91 _array = array;
92 _head = 0;
93 _tail = _size;
94 }
95}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
IEnumerator< T > GetEnumerator()
Definition Deque.cs:63