Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HashRepartitionEnumerator.cs
Go to the documentation of this file.
2
4
5internal sealed class HashRepartitionEnumerator<TInputOutput, THashKey, TIgnoreKey> : QueryOperatorEnumerator<Pair<TInputOutput, THashKey>, int>
6{
7 private sealed class Mutables
8 {
9 internal int _currentBufferIndex;
10
12
13 internal int _currentIndex;
14
15 internal Mutables()
16 {
18 }
19 }
20
21 private readonly int _partitionCount;
22
23 private readonly int _partitionIndex;
24
25 private readonly Func<TInputOutput, THashKey> _keySelector;
26
28
30
32
34
36
38
40 {
42 _partitionCount = partitionCount;
43 _partitionIndex = partitionIndex;
45 _repartitionStream = repartitionStream;
46 _barrier = barrier;
47 _valueExchangeMatrix = valueExchangeMatrix;
49 }
50
51 internal override bool MoveNext(ref Pair<TInputOutput, THashKey> currentElement, ref int currentKey)
52 {
53 if (_partitionCount == 1)
54 {
55 TIgnoreKey currentKey2 = default(TIgnoreKey);
56 TInputOutput currentElement2 = default(TInputOutput);
57 if (_source.MoveNext(ref currentElement2, ref currentKey2))
58 {
59 currentElement = new Pair<TInputOutput, THashKey>(currentElement2, (_keySelector == null) ? default(THashKey) : _keySelector(currentElement2));
60 return true;
61 }
62 return false;
63 }
64 Mutables mutables = _mutables;
65 if (mutables == null)
66 {
67 mutables = (_mutables = new Mutables());
68 }
69 if (mutables._currentBufferIndex == -1)
70 {
72 }
73 while (mutables._currentBufferIndex < _partitionCount)
74 {
75 if (mutables._currentBuffer != null)
76 {
77 if (++mutables._currentIndex < mutables._currentBuffer.Count)
78 {
79 currentElement = mutables._currentBuffer._chunk[mutables._currentIndex];
80 return true;
81 }
82 mutables._currentIndex = -1;
83 mutables._currentBuffer = mutables._currentBuffer.Next;
84 continue;
85 }
87 {
89 mutables._currentBufferIndex = -1;
90 }
91 mutables._currentBufferIndex++;
92 mutables._currentIndex = -1;
94 {
95 mutables._currentBufferIndex++;
96 }
98 {
99 mutables._currentBuffer = _valueExchangeMatrix[mutables._currentBufferIndex][_partitionIndex];
100 }
101 }
102 return false;
103 }
104
106 {
107 Mutables mutables = _mutables;
109 TInputOutput currentElement = default(TInputOutput);
110 TIgnoreKey currentKey = default(TIgnoreKey);
111 int num = 0;
112 while (_source.MoveNext(ref currentElement, ref currentKey))
113 {
114 if ((num++ & 0x3F) == 0)
115 {
117 }
118 THashKey val = default(THashKey);
119 int num2;
120 if (_keySelector != null)
121 {
122 val = _keySelector(currentElement);
124 }
125 else
126 {
127 num2 = _repartitionStream.GetHashCode(currentElement) % _partitionCount;
128 }
130 if (listChunk == null)
131 {
132 listChunk = (array[num2] = new ListChunk<Pair<TInputOutput, THashKey>>(128));
133 }
134 listChunk.Add(new Pair<TInputOutput, THashKey>(currentElement, val));
135 }
136 for (int i = 0; i < _partitionCount; i++)
137 {
139 }
141 mutables._currentBufferIndex = _partitionIndex;
142 mutables._currentBuffer = array[_partitionIndex];
143 mutables._currentIndex = -1;
144 }
145
146 protected override void Dispose(bool disposed)
147 {
148 if (_barrier != null)
149 {
150 if (_mutables == null || _mutables._currentBufferIndex == -1)
151 {
153 _barrier = null;
154 }
156 }
157 }
158}
ListChunk< Pair< TInputOutput, THashKey > > _currentBuffer
readonly Func< TInputOutput, THashKey > _keySelector
readonly HashRepartitionStream< TInputOutput, THashKey, int > _repartitionStream
HashRepartitionEnumerator(QueryOperatorEnumerator< TInputOutput, TIgnoreKey > source, int partitionCount, int partitionIndex, Func< TInputOutput, THashKey > keySelector, HashRepartitionStream< TInputOutput, THashKey, int > repartitionStream, CountdownEvent barrier, ListChunk< Pair< TInputOutput, THashKey > >[][] valueExchangeMatrix, CancellationToken cancellationToken)
readonly QueryOperatorEnumerator< TInputOutput, TIgnoreKey > _source
readonly ListChunk< Pair< TInputOutput, THashKey > >[][] _valueExchangeMatrix
override bool MoveNext(ref Pair< TInputOutput, THashKey > currentElement, ref int currentKey)
void Add(TInputOutput e)
Definition ListChunk.cs:27
bool MoveNext([MaybeNullWhen(false)][AllowNull] ref TElement currentElement, [AllowNull] ref TKey currentKey)