Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FixedMaxHeap.cs
Go to the documentation of this file.
2
4
5internal sealed class FixedMaxHeap<TElement>
6{
7 private readonly TElement[] _elements;
8
9 private int _count;
10
12
13 internal int Count => _count;
14
15 internal int Size => _elements.Length;
16
17 internal TElement MaxValue
18 {
19 get
20 {
21 if (_count == 0)
22 {
24 }
25 return _elements[0];
26 }
27 }
28
30 {
31 _elements = new TElement[maximumSize];
33 }
34
35 internal void Clear()
36 {
37 _count = 0;
38 }
39
40 internal bool Insert(TElement e)
41 {
42 if (_count < _elements.Length)
43 {
44 _elements[_count] = e;
45 _count++;
47 return true;
48 }
49 if (_comparer.Compare(e, _elements[0]) < 0)
50 {
51 _elements[0] = e;
53 return true;
54 }
55 return false;
56 }
57
58 internal void ReplaceMax(TElement newValue)
59 {
62 }
63
64 internal void RemoveMax()
65 {
66 _count--;
67 if (_count > 0)
68 {
71 }
72 }
73
74 private void Swap(int i, int j)
75 {
76 TElement val = _elements[i];
77 _elements[i] = _elements[j];
78 _elements[j] = val;
79 }
80
81 private void HeapifyRoot()
82 {
83 int num = 0;
84 int count = _count;
85 while (num < count)
86 {
87 int num2 = (num + 1) * 2 - 1;
88 int num3 = num2 + 1;
89 if (num2 < count && _comparer.Compare(_elements[num], _elements[num2]) < 0)
90 {
92 {
93 Swap(num, num3);
94 num = num3;
95 }
96 else
97 {
98 Swap(num, num2);
99 num = num2;
100 }
101 }
102 else
103 {
104 if (num3 >= count || _comparer.Compare(_elements[num], _elements[num3]) >= 0)
105 {
106 break;
107 }
108 Swap(num, num3);
109 num = num3;
110 }
111 }
112 }
113
114 private void HeapifyLastLeaf()
115 {
116 int num = _count - 1;
117 while (num > 0)
118 {
119 int num2 = (num + 1) / 2 - 1;
120 if (_comparer.Compare(_elements[num], _elements[num2]) > 0)
121 {
122 Swap(num, num2);
123 num = num2;
124 continue;
125 }
126 break;
127 }
128 }
129}
FixedMaxHeap(int maximumSize, IComparer< TElement > comparer)
void ReplaceMax(TElement newValue)
readonly TElement[] _elements
readonly IComparer< TElement > _comparer
static string NoElements
Definition SR.cs:16
Definition SR.cs:7