Skip to content

Commit 5f9ee67

Browse files
committed
[Select] fix bug for RefStructEnumerable
1 parent a2941fc commit 5f9ee67

File tree

7 files changed

+105
-24
lines changed

7 files changed

+105
-24
lines changed

src/StructLinq.Tests/RefSelectCollectionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StructLinq.Tests
55
{
66
public class RefSelectCollectionTests : AbstractCollectionTests<double,
77
RefSelectCollection<int, double, ArrayRefEnumerable<int>, ArrayRefStructEnumerator<int>, RefSelectCollectionTests.MultFunction>,
8-
RefSelectEnumerator<int, double, ArrayRefStructEnumerator<int>, RefSelectCollectionTests.MultFunction>>
8+
RefSelectCollectionEnumerator<int, double, ArrayRefStructEnumerator<int>, RefSelectCollectionTests.MultFunction>>
99
{
1010

1111
protected override RefSelectCollection<int, double, ArrayRefEnumerable<int>, ArrayRefStructEnumerator<int>, RefSelectCollectionTests.MultFunction> Build(int size)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using StructLinq.Array;
2+
using StructLinq.Select;
3+
4+
namespace StructLinq.Tests
5+
{
6+
public class RefSelectEnumerableTests : AbstractEnumerableTests<double,
7+
RefSelectEnumerable<int, double, ArrayRefEnumerable<int>, ArrayRefStructEnumerator<int>, RefSelectCollectionTests.MultFunction>,
8+
RefSelectEnumerator<int, double, ArrayRefStructEnumerator<int>, RefSelectCollectionTests.MultFunction>>
9+
{
10+
11+
protected override RefSelectEnumerable<int, double, ArrayRefEnumerable<int>, ArrayRefStructEnumerator<int>, RefSelectCollectionTests.MultFunction> Build(int size)
12+
{
13+
var func = new RefSelectCollectionTests.MultFunction();
14+
var selectEnumerable =
15+
StructEnumerable.Range(-1, size).ToArray().ToRefStructEnumerable().Select(ref func, x=>(IRefStructEnumerable<int, ArrayRefStructEnumerator<int>>)x, x => x);
16+
return selectEnumerable;
17+
}
18+
19+
public struct MultFunction : IInFunction<int, double>
20+
{
21+
public double Eval(in int element)
22+
{
23+
return element * 2.0;
24+
}
25+
}
26+
}
27+
}

src/StructLinq/Select/RefSelectCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace StructLinq.Select
44
{
5-
public struct RefSelectCollection<TIn, TOut, TEnumerable, TEnumerator, TFunction> : IStructCollection<TOut, RefSelectEnumerator<TIn, TOut, TEnumerator, TFunction>>
5+
public struct RefSelectCollection<TIn, TOut, TEnumerable, TEnumerator, TFunction> : IStructCollection<TOut, RefSelectCollectionEnumerator<TIn, TOut, TEnumerator, TFunction>>
66
where TFunction : struct, IInFunction<TIn, TOut>
77
where TEnumerator : struct, IRefCollectionEnumerator<TIn>
88
where TEnumerable : IRefStructCollection<TIn, TEnumerator>
@@ -19,10 +19,10 @@ public RefSelectCollection(ref TFunction function, ref TEnumerable inner)
1919
}
2020

2121
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22-
public RefSelectEnumerator<TIn, TOut, TEnumerator, TFunction> GetEnumerator()
22+
public RefSelectCollectionEnumerator<TIn, TOut, TEnumerator, TFunction> GetEnumerator()
2323
{
2424
var typedEnumerator = inner.GetEnumerator();
25-
return new RefSelectEnumerator<TIn, TOut, TEnumerator, TFunction>(ref function, ref typedEnumerator);
25+
return new RefSelectCollectionEnumerator<TIn, TOut, TEnumerator, TFunction>(ref function, ref typedEnumerator);
2626
}
2727

2828
public int Count
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace StructLinq.Select
4+
{
5+
public struct RefSelectCollectionEnumerator<TIn, TOut, TEnumerator, TFunction> : ICollectionEnumerator<TOut>
6+
where TFunction : struct, IInFunction<TIn, TOut>
7+
where TEnumerator : struct, IRefCollectionEnumerator<TIn>
8+
{
9+
#region private fields
10+
11+
private TFunction function;
12+
private TEnumerator enumerator;
13+
14+
#endregion
15+
16+
public RefSelectCollectionEnumerator(ref TFunction function, ref TEnumerator enumerator)
17+
{
18+
this.function = function;
19+
this.enumerator = enumerator;
20+
}
21+
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
public bool MoveNext()
24+
{
25+
return enumerator.MoveNext();
26+
}
27+
28+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29+
public void Reset()
30+
{
31+
enumerator.Reset();
32+
}
33+
34+
public TOut Current
35+
{
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
get
38+
{
39+
ref var enumeratorCurrent = ref enumerator.Current;
40+
return function.Eval(in enumeratorCurrent);
41+
}
42+
}
43+
44+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
45+
public void Dispose()
46+
{
47+
enumerator.Dispose();
48+
}
49+
50+
public int Count
51+
{
52+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
53+
get => enumerator.Count;
54+
}
55+
56+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
57+
public TOut Get(int i)
58+
{
59+
var element = enumerator.Get(i);
60+
return function.Eval(element);
61+
}
62+
}
63+
64+
}

src/StructLinq/Select/RefSelectEnumerable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace StructLinq.Select
44
{
55
public struct RefSelectEnumerable<TIn, TOut, TEnumerable, TEnumerator, TFunction> : IStructEnumerable<TOut, RefSelectEnumerator<TIn, TOut, TEnumerator, TFunction>>
66
where TFunction : struct, IInFunction<TIn, TOut>
7-
where TEnumerator : struct, IRefCollectionEnumerator<TIn>
7+
where TEnumerator : struct, IRefStructEnumerator<TIn>
88
where TEnumerable : IRefStructEnumerable<TIn, TEnumerator>
99
{
1010
#region private fields

src/StructLinq/Select/RefSelectEnumerator.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace StructLinq.Select
44
{
5-
public struct RefSelectEnumerator<TIn, TOut, TEnumerator, TFunction> : ICollectionEnumerator<TOut>
5+
public struct RefSelectEnumerator<TIn, TOut, TEnumerator, TFunction> : IStructEnumerator<TOut>
66
where TFunction : struct, IInFunction<TIn, TOut>
7-
where TEnumerator : struct, IRefCollectionEnumerator<TIn>
7+
where TEnumerator : struct, IRefStructEnumerator<TIn>
88
{
99
#region private fields
1010
private TFunction function;
@@ -15,16 +15,19 @@ public RefSelectEnumerator(ref TFunction function, ref TEnumerator enumerator)
1515
this.function = function;
1616
this.enumerator = enumerator;
1717
}
18+
1819
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1920
public bool MoveNext()
2021
{
2122
return enumerator.MoveNext();
2223
}
24+
2325
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2426
public void Reset()
2527
{
2628
enumerator.Reset();
2729
}
30+
2831
public TOut Current
2932
{
3033
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -40,18 +43,5 @@ public void Dispose()
4043
{
4144
enumerator.Dispose();
4245
}
43-
44-
public int Count
45-
{
46-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47-
get => enumerator.Count;
48-
}
49-
50-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51-
public TOut Get(int i)
52-
{
53-
var element = enumerator.Get(i);
54-
return function.Eval(element);
55-
}
5646
}
57-
}
47+
}

src/StructLinq/Select/RefStructEnumerable.Select.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static RefSelectEnumerable<TIn, TOut, TEnumerable, TEnumerator, TFunction
1212
this TEnumerable enumerable, ref TFunction function,
1313
Func<TEnumerable, IRefStructEnumerable<TIn, TEnumerator>> _,
1414
Func<TFunction, IInFunction<TIn, TOut>> __)
15-
where TEnumerator : struct, IRefCollectionEnumerator<TIn>
15+
where TEnumerator : struct, IRefStructEnumerator<TIn>
1616
where TFunction : struct, IInFunction<TIn, TOut>
1717
where TEnumerable : struct, IRefStructEnumerable<TIn, TEnumerator>
1818
{
@@ -25,7 +25,7 @@ public static RefSelectEnumerable<TIn, TOut, TEnumerable, TEnumerator, StructInF
2525
this TEnumerable enumerable,
2626
InFunc<TIn, TOut> function,
2727
Func<TEnumerable, IRefStructEnumerable<TIn, TEnumerator>> _)
28-
where TEnumerator : struct, IRefCollectionEnumerator<TIn>
28+
where TEnumerator : struct, IRefStructEnumerator<TIn>
2929
where TEnumerable : struct, IRefStructEnumerable<TIn, TEnumerator>
3030
{
3131
var fct = function.ToStruct();
@@ -36,7 +36,7 @@ public static RefSelectEnumerable<TIn, TOut, TEnumerable, TEnumerator, StructInF
3636
public static RefSelectEnumerable<TIn, TOut, IRefStructEnumerable<TIn, TEnumerator>, TEnumerator, StructInFunction<TIn, TOut>> Select<TIn, TOut, TEnumerator>(
3737
this IRefStructEnumerable<TIn, TEnumerator> enumerable,
3838
InFunc<TIn, TOut> function)
39-
where TEnumerator : struct, IRefCollectionEnumerator<TIn>
39+
where TEnumerator : struct, IRefStructEnumerator<TIn>
4040
{
4141
var fct = function.ToStruct();
4242
return new RefSelectEnumerable<TIn,TOut,IRefStructEnumerable<TIn, TEnumerator>,TEnumerator,StructInFunction<TIn,TOut>>(ref fct, ref enumerable);

0 commit comments

Comments
 (0)