Skip to content

Commit 7884c64

Browse files
committed
Implement IEnumerable/IEnumerator in AbstractAppendingInt64Buffer
1 parent cd6dfef commit 7884c64

6 files changed

Lines changed: 83 additions & 44 deletions

File tree

src/Lucene.Net.Tests/Util/Packed/TestPackedInts.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,16 +1195,17 @@ public virtual void TestAppendingLongBuffer()
11951195
Assert.AreEqual(arr[i], buf.Get(i));
11961196
}
11971197

1198-
AbstractAppendingInt64Buffer.Iterator it = buf.GetIterator();
1198+
using var it = buf.GetEnumerator();
11991199
for (int i = 0; i < arr.Length; ++i)
12001200
{
1201+
bool hasNext = it.MoveNext();
12011202
if (Random.NextBoolean())
12021203
{
1203-
Assert.IsTrue(it.HasNext);
1204+
Assert.IsTrue(hasNext);
12041205
}
1205-
Assert.AreEqual(arr[i], it.Next());
1206+
Assert.AreEqual(arr[i], it.Current);
12061207
}
1207-
Assert.IsFalse(it.HasNext);
1208+
Assert.IsFalse(it.MoveNext());
12081209

12091210

12101211
long[] target = new long[arr.Length + 1024]; // check the request for more is OK.

src/Lucene.Net/Index/BinaryDocValuesWriter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private IEnumerable<BytesRef> GetBytesIterator(int maxDocParam)
136136
{
137137
// Use yield return instead of ucsom IEnumerable
138138
var value = new BytesRef();
139-
AppendingDeltaPackedInt64Buffer.Iterator lengthsIterator = lengths.GetIterator();
139+
using var lengthsEnumerator = lengths.GetEnumerator();
140140
int size = (int)lengths.Count;
141141
DataInput bytesIterator = bytes.GetDataInput();
142142
int maxDoc = maxDocParam;
@@ -147,7 +147,8 @@ private IEnumerable<BytesRef> GetBytesIterator(int maxDocParam)
147147
BytesRef v = null;
148148
if (upto < size)
149149
{
150-
int length = (int)lengthsIterator.Next();
150+
lengthsEnumerator.MoveNext();
151+
int length = (int)lengthsEnumerator.Current;
151152
value.Grow(length);
152153
value.Length = length;
153154
try

src/Lucene.Net/Index/NumericDocValuesWriter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public override void Flush(SegmentWriteState state, DocValuesConsumer dvConsumer
102102
private IEnumerable<long?> GetNumericIterator(int maxDoc)
103103
{
104104
// LUCENENET specific: using yield return instead of custom iterator type. Much less code.
105-
AbstractAppendingInt64Buffer.Iterator iter = pending.GetIterator();
105+
using var enumerator = pending.GetEnumerator();
106106
int size = (int)pending.Count;
107107
int upto = 0;
108108

@@ -111,7 +111,8 @@ public override void Flush(SegmentWriteState state, DocValuesConsumer dvConsumer
111111
long? value;
112112
if (upto < size)
113113
{
114-
var v = iter.Next();
114+
enumerator.MoveNext();
115+
var v = enumerator.Current;
115116
if (docsWithField is null || docsWithField.Get(upto))
116117
{
117118
value = v;

src/Lucene.Net/Index/SortedDocValuesWriter.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ public override void Flush(SegmentWriteState state, DocValuesConsumer dvConsumer
123123
ordMap[sortedValues[ord]] = ord;
124124
}
125125

126-
dvConsumer.AddSortedField(fieldInfo, GetBytesRefEnumberable(valueCount, sortedValues),
126+
dvConsumer.AddSortedField(fieldInfo, GetBytesRefEnumerable(valueCount, sortedValues),
127127
// doc -> ord
128-
GetOrdsEnumberable(maxDoc, ordMap));
128+
GetOrdsEnumerable(maxDoc, ordMap));
129129
}
130130

131131
public override void Abort()
132132
{
133133
}
134134

135-
private IEnumerable<BytesRef> GetBytesRefEnumberable(int valueCount, int[] sortedValues)
135+
private IEnumerable<BytesRef> GetBytesRefEnumerable(int valueCount, int[] sortedValues)
136136
{
137137
var scratch = new BytesRef();
138138

@@ -142,14 +142,15 @@ private IEnumerable<BytesRef> GetBytesRefEnumberable(int valueCount, int[] sorte
142142
}
143143
}
144144

145-
private IEnumerable<long?> GetOrdsEnumberable(int maxDoc, int[] ordMap)
145+
private IEnumerable<long?> GetOrdsEnumerable(int maxDoc, int[] ordMap)
146146
{
147-
AppendingDeltaPackedInt64Buffer.Iterator iter = pending.GetIterator();
147+
using var enumerator = pending.GetEnumerator();
148148
if (Debugging.AssertsEnabled) Debugging.Assert(pending.Count == maxDoc);
149149

150150
for (int i = 0; i < maxDoc; ++i)
151151
{
152-
int ord = (int)iter.Next();
152+
enumerator.MoveNext();
153+
int ord = (int)enumerator.Current;
153154
yield return ord == -1 ? ord : ordMap[ord];
154155
}
155156
}

src/Lucene.Net/Index/SortedSetDocValuesWriter.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,23 @@ private IEnumerable<BytesRef> GetValuesEnumerable(int valueCount, int[] sortedVa
202202
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType", Justification = "Matches Lucene")]
203203
private IEnumerable<long?> GetOrdCountEnumerable(int maxDoc)
204204
{
205-
AppendingDeltaPackedInt64Buffer.Iterator iter = pendingCounts.GetIterator();
205+
using var enumerator = pendingCounts.GetEnumerator();
206206

207207
if (Debugging.AssertsEnabled) Debugging.Assert(pendingCounts.Count == maxDoc, "MaxDoc: {0}, pending.Count: {1}", maxDoc, pending.Count);
208208

209209
for (int docUpto = 0; docUpto < maxDoc; ++docUpto)
210210
{
211-
yield return iter.Next();
211+
enumerator.MoveNext();
212+
yield return enumerator.Current;
212213
}
213214
}
214215

215216
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType", Justification = "Matches Lucene")]
216217
private IEnumerable<long?> GetOrdsEnumerable(int[] ordMap, int maxCountPerDoc)
217218
{
218219
int currentUpTo = 0, currentLength = 0;
219-
AppendingPackedInt64Buffer.Iterator iter = pending.GetIterator();
220-
AppendingDeltaPackedInt64Buffer.Iterator counts = pendingCounts.GetIterator();
220+
using var enumerator = pending.GetEnumerator();
221+
using var counts = pendingCounts.GetEnumerator();
221222
int[] cd = new int[maxCountPerDoc]; // LUCENENET specific - renamed from currentDoc to cd to prevent conflict
222223

223224
for (long ordUpto = 0; ordUpto < pending.Count; ++ordUpto)
@@ -226,10 +227,12 @@ private IEnumerable<BytesRef> GetValuesEnumerable(int valueCount, int[] sortedVa
226227
{
227228
// refill next doc, and sort remapped ords within the doc.
228229
currentUpTo = 0;
229-
currentLength = (int)counts.Next();
230+
counts.MoveNext();
231+
currentLength = (int)counts.Current;
230232
for (int j = 0; j < currentLength; j++)
231233
{
232-
cd[j] = ordMap[(int)iter.Next()];
234+
enumerator.MoveNext();
235+
cd[j] = ordMap[(int)enumerator.Current];
233236
}
234237
Array.Sort(cd, 0, currentLength);
235238
}

src/Lucene.Net/Util/Packed/AbstractAppendingLongBuffer.cs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Lucene.Net.Diagnostics;
22
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
35
using System.Runtime.CompilerServices;
46

57
namespace Lucene.Net.Util.Packed
@@ -26,7 +28,8 @@ namespace Lucene.Net.Util.Packed
2628
/// <para/>
2729
/// NOTE: This was AbstractAppendingLongBuffer in Lucene
2830
/// </summary>
29-
public abstract class AbstractAppendingInt64Buffer : Int64Values // LUCENENET NOTE: made public rather than internal because has public subclasses
31+
public abstract class AbstractAppendingInt64Buffer : Int64Values, // LUCENENET NOTE: made public rather than internal because has public subclasses
32+
IEnumerable<long> // LUCENENET specific
3033
{
3134
internal const int MIN_PAGE_SIZE = 64;
3235

@@ -105,7 +108,7 @@ public void Add(long l)
105108
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106109
internal virtual void Grow(int newBlockCount)
107110
{
108-
Array.Resize<PackedInt32s.Reader>(ref values, newBlockCount);
111+
Array.Resize(ref values, newBlockCount);
109112
}
110113

111114
internal abstract void PackPendingValues();
@@ -144,33 +147,27 @@ public int Get(long index, long[] arr, int off, int len)
144147
/// <summary>
145148
/// Return an iterator over the values of this buffer.
146149
/// </summary>
147-
public virtual Iterator GetIterator()
150+
public virtual Enumerator GetEnumerator()
148151
{
149-
return new Iterator(this);
152+
return new Enumerator(this);
150153
}
151154

152-
public sealed class Iterator
155+
IEnumerator<long> IEnumerable<long>.GetEnumerator() => GetEnumerator();
156+
157+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
158+
159+
public sealed class Enumerator : IEnumerator<long>
153160
{
154161
private readonly AbstractAppendingInt64Buffer outerInstance;
155162

156163
internal long[] currentValues;
157164
internal int vOff, pOff;
158165
internal int currentCount; // number of entries of the current page
159166

160-
internal Iterator(AbstractAppendingInt64Buffer outerInstance)
167+
internal Enumerator(AbstractAppendingInt64Buffer outerInstance)
161168
{
162169
this.outerInstance = outerInstance;
163-
vOff = pOff = 0;
164-
if (outerInstance.valuesOff == 0)
165-
{
166-
currentValues = outerInstance.pending;
167-
currentCount = outerInstance.pendingOff;
168-
}
169-
else
170-
{
171-
currentValues = new long[outerInstance.values[0].Count];
172-
FillValues();
173-
}
170+
Reset(); // LUCENENET specific - moved from ctor
174171
}
175172

176173
internal void FillValues()
@@ -191,15 +188,24 @@ internal void FillValues()
191188
}
192189

193190
/// <summary>
194-
/// Whether or not there are remaining values. </summary>
195-
public bool HasNext => pOff < currentCount;
191+
/// Gets the current value.
192+
/// </summary>
193+
public long Current { get; private set; }
194+
195+
object IEnumerator.Current => Current;
196196

197197
/// <summary>
198-
/// Return the next long in the buffer. </summary>
199-
public long Next()
198+
/// Advances the enumerator to the next value.
199+
/// </summary>
200+
public bool MoveNext()
200201
{
201-
if (Debugging.AssertsEnabled) Debugging.Assert(HasNext);
202-
long result = currentValues[pOff++];
202+
if (pOff >= currentCount)
203+
{
204+
return false;
205+
}
206+
207+
Current = currentValues[pOff++];
208+
203209
if (pOff == currentCount)
204210
{
205211
vOff += 1;
@@ -213,7 +219,33 @@ public long Next()
213219
currentCount = 0;
214220
}
215221
}
216-
return result;
222+
223+
return true;
224+
}
225+
226+
/// <summary>
227+
/// Resets the enumerator to its initial position.
228+
/// </summary>
229+
public void Reset()
230+
{
231+
vOff = pOff = 0;
232+
if (outerInstance.valuesOff == 0)
233+
{
234+
currentValues = outerInstance.pending;
235+
currentCount = outerInstance.pendingOff;
236+
}
237+
else
238+
{
239+
currentValues = new long[outerInstance.values[0].Count];
240+
FillValues();
241+
}
242+
}
243+
244+
/// <summary>
245+
/// Dispose of the resources used by the <see cref="Enumerator"/>.
246+
/// </summary>
247+
public void Dispose()
248+
{
217249
}
218250
}
219251

0 commit comments

Comments
 (0)