Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
OrderedHashRepartitionEnumerator.cs
Go to the documentation of this file.
3
5
6internal sealed class OrderedHashRepartitionEnumerator<TInputOutput, THashKey, TOrderKey> : QueryOperatorEnumerator<Pair<TInputOutput, THashKey>, TOrderKey>
7{
8 private sealed class Mutables
9 {
10 internal int _currentBufferIndex;
11
13
15
16 internal int _currentIndex;
17
18 internal Mutables()
19 {
21 }
22 }
23
24 private readonly int _partitionCount;
25
26 private readonly int _partitionIndex;
27
28 private readonly Func<TInputOutput, THashKey> _keySelector;
29
31
33
35
37
39
41
43
45 {
47 _partitionCount = partitionCount;
48 _partitionIndex = partitionIndex;
50 _repartitionStream = repartitionStream;
51 _barrier = barrier;
52 _valueExchangeMatrix = valueExchangeMatrix;
53 _keyExchangeMatrix = keyExchangeMatrix;
55 }
56
57 internal override bool MoveNext(ref Pair<TInputOutput, THashKey> currentElement, [AllowNull] ref TOrderKey currentKey)
58 {
59 if (_partitionCount == 1)
60 {
61 TInputOutput currentElement2 = default(TInputOutput);
62 if (_source.MoveNext(ref currentElement2, ref currentKey))
63 {
64 currentElement = new Pair<TInputOutput, THashKey>(currentElement2, (_keySelector == null) ? default(THashKey) : _keySelector(currentElement2));
65 return true;
66 }
67 return false;
68 }
69 Mutables mutables = _mutables;
70 if (mutables == null)
71 {
72 mutables = (_mutables = new Mutables());
73 }
74 if (mutables._currentBufferIndex == -1)
75 {
77 }
78 while (mutables._currentBufferIndex < _partitionCount)
79 {
80 if (mutables._currentBuffer != null)
81 {
82 if (++mutables._currentIndex < mutables._currentBuffer.Count)
83 {
84 currentElement = mutables._currentBuffer._chunk[mutables._currentIndex];
85 currentKey = mutables._currentKeyBuffer._chunk[mutables._currentIndex];
86 return true;
87 }
88 mutables._currentIndex = -1;
89 mutables._currentBuffer = mutables._currentBuffer.Next;
90 mutables._currentKeyBuffer = mutables._currentKeyBuffer.Next;
91 continue;
92 }
94 {
96 mutables._currentBufferIndex = -1;
97 }
98 mutables._currentBufferIndex++;
99 mutables._currentIndex = -1;
100 if (mutables._currentBufferIndex == _partitionIndex)
101 {
102 mutables._currentBufferIndex++;
103 }
105 {
106 mutables._currentBuffer = _valueExchangeMatrix[mutables._currentBufferIndex][_partitionIndex];
107 mutables._currentKeyBuffer = _keyExchangeMatrix[mutables._currentBufferIndex][_partitionIndex];
108 }
109 }
110 return false;
111 }
112
114 {
115 Mutables mutables = _mutables;
118 TInputOutput currentElement = default(TInputOutput);
119 TOrderKey currentKey = default(TOrderKey);
120 int num = 0;
121 while (_source.MoveNext(ref currentElement, ref currentKey))
122 {
123 if ((num++ & 0x3F) == 0)
124 {
126 }
127 THashKey val = default(THashKey);
128 int num2;
129 if (_keySelector != null)
130 {
131 val = _keySelector(currentElement);
132 num2 = _repartitionStream.GetHashCode(val) % _partitionCount;
133 }
134 else
135 {
136 num2 = _repartitionStream.GetHashCode(currentElement) % _partitionCount;
137 }
139 ListChunk<TOrderKey> listChunk2 = array2[num2];
140 if (listChunk == null)
141 {
142 listChunk = (array[num2] = new ListChunk<Pair<TInputOutput, THashKey>>(128));
143 listChunk2 = (array2[num2] = new ListChunk<TOrderKey>(128));
144 }
145 listChunk.Add(new Pair<TInputOutput, THashKey>(currentElement, val));
146 listChunk2.Add(currentKey);
147 }
148 for (int i = 0; i < _partitionCount; i++)
149 {
151 _keyExchangeMatrix[_partitionIndex][i] = array2[i];
152 }
154 mutables._currentBufferIndex = _partitionIndex;
155 mutables._currentBuffer = array[_partitionIndex];
156 mutables._currentKeyBuffer = array2[_partitionIndex];
157 mutables._currentIndex = -1;
158 }
159
160 protected override void Dispose(bool disposing)
161 {
162 if (_barrier != null)
163 {
164 if (_mutables == null || _mutables._currentBufferIndex == -1)
165 {
167 _barrier = null;
168 }
170 }
171 }
172}
void Add(TInputOutput e)
Definition ListChunk.cs:27
ListChunk< TInputOutput > Next
Definition ListChunk.cs:16
readonly QueryOperatorEnumerator< TInputOutput, TOrderKey > _source
readonly ListChunk< Pair< TInputOutput, THashKey > >[][] _valueExchangeMatrix
OrderedHashRepartitionEnumerator(QueryOperatorEnumerator< TInputOutput, TOrderKey > source, int partitionCount, int partitionIndex, Func< TInputOutput, THashKey > keySelector, OrderedHashRepartitionStream< TInputOutput, THashKey, TOrderKey > repartitionStream, CountdownEvent barrier, ListChunk< Pair< TInputOutput, THashKey > >[][] valueExchangeMatrix, ListChunk< TOrderKey >[][] keyExchangeMatrix, CancellationToken cancellationToken)
readonly HashRepartitionStream< TInputOutput, THashKey, TOrderKey > _repartitionStream
override bool MoveNext(ref Pair< TInputOutput, THashKey > currentElement, [AllowNull] ref TOrderKey currentKey)
bool MoveNext([MaybeNullWhen(false)][AllowNull] ref TElement currentElement, [AllowNull] ref TKey currentKey)