Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FixedMaxHeap.cs
Go to the documentation of this file.
1
using
System.Collections.Generic
;
2
3
namespace
System.Linq.Parallel
;
4
5
internal
sealed
class
FixedMaxHeap
<TElement>
6
{
7
private
readonly TElement[]
_elements
;
8
9
private
int
_count
;
10
11
private
readonly
IComparer<TElement>
_comparer
;
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
{
23
throw
new
InvalidOperationException
(
System
.
SR
.
NoElements
);
24
}
25
return
_elements
[0];
26
}
27
}
28
29
internal
FixedMaxHeap
(
int
maximumSize
,
IComparer<TElement>
comparer
)
30
{
31
_elements
=
new
TElement[
maximumSize
];
32
_comparer
=
comparer
;
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
++;
46
HeapifyLastLeaf
();
47
return
true
;
48
}
49
if
(
_comparer
.
Compare
(e,
_elements
[0]) < 0)
50
{
51
_elements
[0] = e;
52
HeapifyRoot
();
53
return
true
;
54
}
55
return
false
;
56
}
57
58
internal
void
ReplaceMax
(TElement
newValue
)
59
{
60
_elements
[0] =
newValue
;
61
HeapifyRoot
();
62
}
63
64
internal
void
RemoveMax
()
65
{
66
_count
--;
67
if
(
_count
> 0)
68
{
69
_elements
[0] =
_elements
[
_count
];
70
HeapifyRoot
();
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
{
91
if
(
num3
<
count
&&
_comparer
.
Compare
(
_elements
[
num2
],
_elements
[
num3
]) < 0)
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
}
System.Collections.Generic.Dictionary
Definition
Dictionary.cs:14
System.InvalidOperationException
Definition
InvalidOperationException.cs:9
System.Linq.Parallel.FixedMaxHeap.Clear
void Clear()
Definition
FixedMaxHeap.cs:35
System.Linq.Parallel.FixedMaxHeap.HeapifyLastLeaf
void HeapifyLastLeaf()
Definition
FixedMaxHeap.cs:114
System.Linq.Parallel.FixedMaxHeap.FixedMaxHeap
FixedMaxHeap(int maximumSize, IComparer< TElement > comparer)
Definition
FixedMaxHeap.cs:29
System.Linq.Parallel.FixedMaxHeap.HeapifyRoot
void HeapifyRoot()
Definition
FixedMaxHeap.cs:81
System.Linq.Parallel.FixedMaxHeap.Insert
bool Insert(TElement e)
Definition
FixedMaxHeap.cs:40
System.Linq.Parallel.FixedMaxHeap.Count
int Count
Definition
FixedMaxHeap.cs:13
System.Linq.Parallel.FixedMaxHeap.ReplaceMax
void ReplaceMax(TElement newValue)
Definition
FixedMaxHeap.cs:58
System.Linq.Parallel.FixedMaxHeap._elements
readonly TElement[] _elements
Definition
FixedMaxHeap.cs:7
System.Linq.Parallel.FixedMaxHeap._comparer
readonly IComparer< TElement > _comparer
Definition
FixedMaxHeap.cs:11
System.Linq.Parallel.FixedMaxHeap.MaxValue
TElement MaxValue
Definition
FixedMaxHeap.cs:18
System.Linq.Parallel.FixedMaxHeap._count
int _count
Definition
FixedMaxHeap.cs:9
System.Linq.Parallel.FixedMaxHeap.Size
int Size
Definition
FixedMaxHeap.cs:15
System.Linq.Parallel.FixedMaxHeap.RemoveMax
void RemoveMax()
Definition
FixedMaxHeap.cs:64
System.Linq.Parallel.FixedMaxHeap.Swap
void Swap(int i, int j)
Definition
FixedMaxHeap.cs:74
System.Linq.Parallel.FixedMaxHeap
Definition
FixedMaxHeap.cs:6
System.SR.NoElements
static string NoElements
Definition
SR.cs:16
System.SR
Definition
SR.cs:7
System.Collections.Generic.IComparer.Compare
int Compare(T? x, T? y)
System.Collections.Generic
Definition
IHashKeyCollection.cs:1
System.Linq.Parallel
Definition
AnyAllSearchOperator.cs:5
System.Linq.ExceptionArgument.count
@ count
System.ExceptionArgument.comparer
@ comparer
System
Definition
BlockingCollection.cs:8
source
System.Linq.Parallel
System.Linq.Parallel
FixedMaxHeap.cs
Generated by
1.10.0