Skip to content

Commit 8578612

Browse files
committed
[Array] remove UnmanagedArrayEnumerable and ArrayStructEnumerator is now a struct.
1 parent 36364b1 commit 8578612

File tree

8 files changed

+105
-92
lines changed

8 files changed

+105
-92
lines changed

src/StructLinq.Benchmark/Array.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using BenchmarkDotNet.Attributes;
4+
using StructLinq.Array;
5+
using StructLinq.IEnumerable;
6+
7+
namespace StructLinq.Benchmark
8+
{
9+
[MemoryDiagnoser]
10+
public class Array
11+
{
12+
private readonly ITypedEnumerable<int, ArrayStructEnumerator<int>> safeArray;
13+
private readonly CountAction<int>[] countActions = new CountAction<int>[1];
14+
public int Count = 10000;
15+
private readonly IEnumerable<int> sysEnumerable;
16+
private readonly ITypedEnumerable<int, GenericEnumerator<int>> convertArray;
17+
private readonly int[] array;
18+
public Array()
19+
{
20+
array = Enumerable.Range(0, Count).ToArray();
21+
sysEnumerable = array;
22+
safeArray = array.ToTypedEnumerable();
23+
convertArray = ((IEnumerable<int>) array).ToTypedEnumerable();
24+
}
25+
26+
[Benchmark(Baseline = true)]
27+
public int EnumArray()
28+
{
29+
int count = 0;
30+
foreach (var i in sysEnumerable)
31+
{
32+
count++;
33+
}
34+
return count;
35+
}
36+
37+
[Benchmark]
38+
public int SysArray()
39+
{
40+
int count = 0;
41+
foreach (var i in array)
42+
{
43+
count++;
44+
}
45+
return count;
46+
}
47+
48+
[Benchmark]
49+
public int UnsafeArray()
50+
{
51+
ref CountAction<int> countAction = ref countActions[0];
52+
countAction.Count = 0;
53+
safeArray.ForEach(ref countAction);
54+
return countAction.Count;
55+
}
56+
[Benchmark]
57+
public int ConvertArray()
58+
{
59+
ref CountAction<int> countAction = ref countActions[0];
60+
countAction.Count = 0;
61+
convertArray.ForEach(ref countAction);
62+
return countAction.Count;
63+
}
64+
}
65+
}

src/StructLinq.Benchmark/ForEach.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using BenchmarkDotNet.Attributes;
4-
using StructLinq.ForEach;
54
using StructLinq.IEnumerable;
65
using StructLinq.Range;
76

src/StructLinq.Tests/ArrayTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using Xunit;
34

45
namespace StructLinq.Tests
56
{
6-
public class ArrayTests
7+
public class ArrayTests : AbstractEnumerableTests<int>
78
{
89
[Fact]
9-
public void Test()
10+
public void ShouldSameAsSystem()
1011
{
1112
var sysArray = Enumerable.Range(0, 50).ToArray();
1213
var structArray = Enumerable.Range(0, 50).ToArray().ToTypedEnumerable().ToArray();
1314
Assert.Equal(sysArray, structArray);
1415
}
16+
17+
protected override IEnumerable<int> Build(int size)
18+
{
19+
return Enumerable.Range(-1, size).ToArray().ToTypedEnumerable();
20+
}
1521
}
1622
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
namespace StructLinq.Array
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace StructLinq.Array
25
{
3-
class ArrayEnumerable<T> : AbstractTypedEnumerable<T, ArrayStructEnumerator<T>>
6+
struct ArrayEnumerable<T> : ITypedEnumerable<T, ArrayStructEnumerator<T>>
47
{
58
#region private fields
69
private T[] array;
@@ -9,9 +12,17 @@ public ArrayEnumerable(ref T[] array)
912
{
1013
this.array = array;
1114
}
12-
public override ArrayStructEnumerator<T> GetTypedEnumerator()
15+
public ArrayStructEnumerator<T> GetTypedEnumerator()
1316
{
1417
return new ArrayStructEnumerator<T>(ref array);
1518
}
19+
IEnumerator System.Collections.IEnumerable.GetEnumerator()
20+
{
21+
return GetEnumerator();
22+
}
23+
public IEnumerator<T> GetEnumerator()
24+
{
25+
return GetTypedEnumerator();
26+
}
1627
}
1728
}

src/StructLinq/Array/ArrayStructEnumerable.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ namespace StructLinq
55
{
66
public static partial class StructEnumerable
77
{
8-
public static ITypedEnumerable<T,ArrayStructEnumerator<T>> SafeToTypedEnumerable<T>(this T[] array)
8+
public static ITypedEnumerable<T,ArrayStructEnumerator<T>> ToTypedEnumerable<T>(this T[] array)
99
{
1010
return new ArrayEnumerable<T>(ref array);
1111
}
12-
public static ITypedEnumerable<T, UnmanagedArrayEnumerator<T>> ToTypedEnumerable<T>(this T[] array) where T : unmanaged
13-
{
14-
return new UnmanagedArrayEnumerable<T>(ref array);
15-
}
1612
}
1713
}

src/StructLinq/Array/ArrayStructEnumerator.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,46 @@
44

55
namespace StructLinq.Array
66
{
7-
public class ArrayStructEnumerator<T> : IEnumerator<T>
7+
public struct ArrayStructEnumerator<T> : IEnumerator<T>
88
{
99
#region private fields
1010
private readonly T[] array;
1111
private readonly int endIndex;
12-
private int index = -1;
13-
private T current;
12+
private int index;
1413
#endregion
1514
public ArrayStructEnumerator(ref T[] array)
1615
{
1716
this.array = array;
1817
endIndex = array.Length;
18+
index = 0;
19+
Current = default;
1920
}
2021
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2122
public bool MoveNext()
2223
{
2324
if (index >= endIndex)
2425
return false;
26+
Current = array[index];
2527
++index;
26-
return index < endIndex;
28+
return true;
2729
}
2830
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2931
public void Reset()
3032
{
31-
index = -1;
33+
index = 0;
34+
Current = default;
3235
}
3336
object IEnumerator.Current => Current;
37+
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3439
public void Dispose()
3540
{
3641
}
37-
public T Current => array[index];
42+
public T Current
43+
{
44+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
45+
get;
46+
private set;
47+
}
3848
}
3949
}

src/StructLinq/Array/UnmanagedArrayEnumerable.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/StructLinq/Array/UnmanagedArrayEnumerator.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)