Skip to content

Commit 04893b9

Browse files
committed
[Last] improve last for IStructCollection
1 parent cbc0f55 commit 04893b9

File tree

7 files changed

+310
-11
lines changed

7 files changed

+310
-11
lines changed

Documents/BenchmarksResults/LastOnArray.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical
1414

1515

1616
```
17-
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
18-
|-------------------- |------------:|---------:|---------:|------:|--------:|-------:|------:|------:|----------:|
19-
| Linq | 28.54 ns | 0.331 ns | 0.310 ns | 1.00 | 0.00 | - | - | - | - |
20-
| EnumerableLinq | 28.71 ns | 0.352 ns | 0.329 ns | 1.01 | 0.02 | - | - | - | - |
21-
| StructLinq | 1,303.06 ns | 7.074 ns | 5.907 ns | 45.62 | 0.54 | 0.0057 | - | - | 32 B |
22-
| StructLinqZeroAlloc | 1,245.69 ns | 6.048 ns | 5.361 ns | 43.61 | 0.45 | - | - | - | - |
17+
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
18+
|-------------------- |----------:|----------:|----------:|------:|--------:|-------:|------:|------:|----------:|
19+
| Linq | 29.253 ns | 0.3388 ns | 0.3169 ns | 1.00 | 0.00 | - | - | - | - |
20+
| EnumerableLinq | 29.171 ns | 0.5949 ns | 0.6613 ns | 1.00 | 0.03 | - | - | - | - |
21+
| StructLinq | 17.395 ns | 0.3803 ns | 0.4809 ns | 0.60 | 0.02 | 0.0068 | - | - | 32 B |
22+
| StructLinqZeroAlloc | 5.778 ns | 0.1087 ns | 0.1163 ns | 0.20 | 0.00 | - | - | - | - |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using Xunit;
5+
6+
namespace StructLinq.Tests
7+
{
8+
public class CollectionLastTests
9+
{
10+
[Fact]
11+
public void ShouldReturnLastElement()
12+
{
13+
var array = Enumerable.Range(0, 10)
14+
.ToArray()
15+
.ToStructEnumerable()
16+
.Last()
17+
.Should()
18+
.Be(9);
19+
}
20+
21+
[Fact]
22+
public void ShouldReturnLastElementZeroAlloc()
23+
{
24+
var array = Enumerable.Range(0, 10)
25+
.ToArray()
26+
.ToStructEnumerable()
27+
.Last(x => x)
28+
.Should()
29+
.Be(9);
30+
}
31+
32+
[Fact]
33+
public void ShouldThrowException()
34+
{
35+
Assert.Throws<Exception>(() => StructEnumerable.Empty<int>().Last());
36+
}
37+
38+
[Fact]
39+
public void ShouldThrowExceptionZeroAlloc()
40+
{
41+
Assert.Throws<Exception>(() => StructEnumerable.Empty<int>().Last(x => x));
42+
}
43+
44+
45+
[Fact]
46+
public void ShouldReturnLastElementWithFunc()
47+
{
48+
var array = Enumerable.Range(0, 10)
49+
.ToArray()
50+
.ToStructEnumerable()
51+
.Last(x => x > 5)
52+
.Should()
53+
.Be(9);
54+
}
55+
56+
[Fact]
57+
public void ShouldReturnLastElementWithFuncZeroAlloc()
58+
{
59+
var array = Enumerable.Range(0, 10)
60+
.ToArray()
61+
.ToStructEnumerable()
62+
.Last(x => x > 5, x => x)
63+
.Should()
64+
.Be(9);
65+
}
66+
}
67+
}

src/StructLinq.Tests/LastTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public void ShouldReturnLastElement()
1313
var array = Enumerable.Range(0, 10)
1414
.ToArray()
1515
.ToStructEnumerable()
16+
.Where(x=> true)
1617
.Last()
1718
.Should()
1819
.Be(9);
@@ -24,6 +25,7 @@ public void ShouldReturnLastElementZeroAlloc()
2425
var array = Enumerable.Range(0, 10)
2526
.ToArray()
2627
.ToStructEnumerable()
28+
.Where(x=> true)
2729
.Last(x => x)
2830
.Should()
2931
.Be(9);
@@ -48,6 +50,7 @@ public void ShouldReturnLastElementWithFunc()
4850
var array = Enumerable.Range(0, 10)
4951
.ToArray()
5052
.ToStructEnumerable()
53+
.Where(x=> true)
5154
.Last(x => x > 5)
5255
.Should()
5356
.Be(9);
@@ -59,6 +62,7 @@ public void ShouldReturnLastElementWithFuncZeroAlloc()
5962
var array = Enumerable.Range(0, 10)
6063
.ToArray()
6164
.ToStructEnumerable()
65+
.Where(x=> true)
6266
.Last(x => x > 5, x => x)
6367
.Should()
6468
.Be(9);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace StructLinq.Tests
6+
{
7+
public class RefCollectionLastTests
8+
{
9+
[Fact]
10+
public void ShouldReturnLastElement()
11+
{
12+
int last = default;
13+
Enumerable.Range(0, 10)
14+
.ToArray()
15+
.ToRefStructEnumerable()
16+
.TryLast(ref last)
17+
.Should()
18+
.BeTrue();
19+
last.Should().Be(9);
20+
}
21+
22+
[Fact]
23+
public void ShouldReturnLastElementZeroAlloc()
24+
{
25+
int last = default;
26+
var array = Enumerable.Range(0, 10)
27+
.ToArray()
28+
.ToRefStructEnumerable()
29+
.TryLast(ref last, x=>x)
30+
.Should()
31+
.BeTrue();
32+
last.Should().Be(9);
33+
}
34+
35+
[Fact]
36+
public void ShouldThrowException()
37+
{
38+
int last = default;
39+
StructEnumerable.Empty<int>()
40+
.ToArray()
41+
.ToRefStructEnumerable()
42+
.TryLast(ref last)
43+
.Should()
44+
.BeFalse();
45+
}
46+
47+
[Fact]
48+
public void ShouldThrowExceptionZeroAlloc()
49+
{
50+
int last = default;
51+
StructEnumerable.Empty<int>()
52+
.ToArray()
53+
.ToRefStructEnumerable()
54+
.TryLast(ref last, x=>x)
55+
.Should()
56+
.BeFalse();
57+
}
58+
59+
60+
[Fact]
61+
public void ShouldReturnLastElementWithFunc()
62+
{
63+
int last = default;
64+
Enumerable.Range(0, 10)
65+
.ToArray()
66+
.ToRefStructEnumerable()
67+
.TryLast(x=> x > 5, ref last)
68+
.Should()
69+
.BeTrue();
70+
last.Should().Be(9);
71+
}
72+
73+
[Fact]
74+
public void ShouldReturnLastElementWithFuncZeroAlloc()
75+
{
76+
int last = default;
77+
Enumerable.Range(0, 10)
78+
.ToArray()
79+
.ToRefStructEnumerable()
80+
.TryLast(x => x > 5, ref last, x=> x)
81+
.Should()
82+
.BeTrue();
83+
last.Should().Be(9);
84+
}
85+
}
86+
}

src/StructLinq.Tests/RefLastTests.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public void ShouldReturnLastElement()
1313
Enumerable.Range(0, 10)
1414
.ToArray()
1515
.ToRefStructEnumerable()
16+
.Where((in int x)=> true)
1617
.TryLast(ref last)
1718
.Should()
1819
.BeTrue();
@@ -26,7 +27,8 @@ public void ShouldReturnLastElementZeroAlloc()
2627
var array = Enumerable.Range(0, 10)
2728
.ToArray()
2829
.ToRefStructEnumerable()
29-
.TryLast(ref last, x=>x)
30+
.Where((in int x) => true)
31+
.TryLast(ref last, x => x)
3032
.Should()
3133
.BeTrue();
3234
last.Should().Be(9);
@@ -39,6 +41,7 @@ public void ShouldThrowException()
3941
StructEnumerable.Empty<int>()
4042
.ToArray()
4143
.ToRefStructEnumerable()
44+
.Where((in int x) => true)
4245
.TryLast(ref last)
4346
.Should()
4447
.BeFalse();
@@ -51,7 +54,8 @@ public void ShouldThrowExceptionZeroAlloc()
5154
StructEnumerable.Empty<int>()
5255
.ToArray()
5356
.ToRefStructEnumerable()
54-
.TryLast(ref last, x=>x)
57+
.Where((in int x) => true)
58+
.TryLast(ref last, x => x)
5559
.Should()
5660
.BeFalse();
5761
}
@@ -64,7 +68,8 @@ public void ShouldReturnLastElementWithFunc()
6468
Enumerable.Range(0, 10)
6569
.ToArray()
6670
.ToRefStructEnumerable()
67-
.TryLast(x=> x > 5, ref last)
71+
.Where((in int x) => true)
72+
.TryLast(x => x > 5, ref last)
6873
.Should()
6974
.BeTrue();
7075
last.Should().Be(9);
@@ -77,10 +82,12 @@ public void ShouldReturnLastElementWithFuncZeroAlloc()
7782
Enumerable.Range(0, 10)
7883
.ToArray()
7984
.ToRefStructEnumerable()
80-
.TryLast(x => x > 5, ref last, x=> x)
85+
.Where((in int x) => true)
86+
.TryLast(x => x > 5, ref last, x => x)
8187
.Should()
8288
.BeTrue();
8389
last.Should().Be(9);
8490
}
8591
}
92+
8693
}

0 commit comments

Comments
 (0)