Skip to content

Commit 090b1fc

Browse files
authored
Merge pull request #517 from icarus-consulting/i516-ComparisonForDistinct
add comparison for distinct class
2 parents 6144565 + bb5ee3b commit 090b1fc

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

src/Yaapii.Atoms/Enumerable/Distinct.cs

+22-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23+
using System;
2324
using System.Collections.Generic;
2425

2526
namespace Yaapii.Atoms.Enumerable
@@ -43,13 +44,25 @@ public Distinct(params IEnumerable<T>[] enumerables) : this(
4344
/// The distinct elements of one or multiple Enumerables.
4445
/// </summary>
4546
/// <param name="enumerables">enumerables to get distinct elements from</param>
46-
public Distinct(IEnumerable<IEnumerable<T>> enumerables) : base(() =>
47+
public Distinct(IEnumerable<IEnumerable<T>> enumerables) : this(
48+
enumerables,
49+
(v1, v2) => v1.Equals(v2)
50+
)
51+
{ }
52+
53+
/// <summary>
54+
/// The distinct elements of one or multiple Enumerables.
55+
/// </summary>
56+
/// <param name="enumerables">enumerables to get distinct elements from</param>
57+
/// <param name="comparison">comparison to evaluate distinction</param>
58+
public Distinct(IEnumerable<IEnumerable<T>> enumerables, Func<T, T, bool> comparison) : base(() =>
4759
new LiveMany<T>(() =>
4860
new Enumerator.Distinct<T>(
4961
new Mapped<IEnumerable<T>, IEnumerator<T>>(
5062
(e) => e.GetEnumerator(),
5163
enumerables
52-
)
64+
),
65+
comparison
5366
)
5467
),
5568
false
@@ -73,5 +86,12 @@ public static class Distinct
7386
/// </summary>
7487
/// <param name="enumerables">enumerables to get distinct elements from</param>
7588
public static IEnumerable<T> New<T>(IEnumerable<IEnumerable<T>> enumerables) => new Distinct<T>(enumerables);
89+
90+
/// <summary>
91+
/// The distinct elements of one or multiple Enumerables.
92+
/// </summary>
93+
/// <param name="enumerables">enumerables to get distinct elements from</param>
94+
/// <param name="comparison">comparison to evaluate distinction</param>
95+
public static IEnumerable<T> New<T>(IEnumerable<IEnumerable<T>> enumerables, Func<T, T, bool> comparison) => new Distinct<T>(enumerables, comparison);
7696
}
7797
}

src/Yaapii.Atoms/Enumerator/Distinct.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23+
using System;
2324
using System.Collections;
2425
using System.Collections.Generic;
2526

@@ -34,17 +35,27 @@ namespace Yaapii.Atoms.Enumerator
3435
public sealed class Distinct<T> : IEnumerator<T>
3536
{
3637
private readonly IEnumerable<IEnumerator<T>> originals;
38+
private readonly Func<T, T, bool> comparison;
3739
private readonly Queue<IEnumerator<T>> buffer = new Queue<IEnumerator<T>>();
3840
private readonly List<T> hits = new List<T>();
3941

4042
/// <summary>
4143
/// Enumerator that only gives the distinct elements of multiple enumerators.
4244
/// </summary>
43-
/// <param name="enumerators"></param>
44-
public Distinct(IEnumerable<IEnumerator<T>> enumerators)
45+
public Distinct(IEnumerable<IEnumerator<T>> enumerators) : this(
46+
enumerators,
47+
(v1, v2) => v1.Equals(v2)
48+
)
49+
{ }
50+
51+
/// <summary>
52+
/// Enumerator that only gives the distinct elements of multiple enumerators.
53+
/// </summary>
54+
public Distinct(IEnumerable<IEnumerator<T>> enumerators, Func<T, T, bool> comparison)
4555
{
46-
originals = enumerators;
47-
buffer = new Queue<IEnumerator<T>>(enumerators);
56+
this.originals = enumerators;
57+
this.comparison = comparison;
58+
this.buffer = new Queue<IEnumerator<T>>(enumerators);
4859
}
4960

5061
/// <summary>
@@ -98,7 +109,7 @@ private void SkipKnown()
98109
this.buffer.Dequeue();
99110
}
100111

101-
if (buffer.Count > 0 && !this.hits.Contains(this.buffer.Peek().Current))
112+
if (buffer.Count > 0 && !(new Enumerable.Contains<T>(this.hits, v => comparison(v, this.buffer.Peek().Current)).Value()))
102113
{
103114
this.hits.Add(this.buffer.Peek().Current);
104115
break;

tests/Yaapii.Atoms.Tests/Enumerable/DistinctTest.cs

+26
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
using System.Collections.Generic;
2424
using Xunit;
25+
using Yaapii.Atoms.Number;
2526

2627
namespace Yaapii.Atoms.Enumerable.Tests
2728
{
@@ -39,6 +40,31 @@ public void MergesEntries()
3940
).Value() == 5);
4041
}
4142

43+
[Fact]
44+
public void MergesComparedEntries()
45+
{
46+
Assert.Equal(
47+
5,
48+
new LengthOf(
49+
new Distinct<INumber>(
50+
new ManyOf<IEnumerable<INumber>>(
51+
new ManyOf<INumber>(
52+
new NumberOf(1),
53+
new NumberOf(2),
54+
new NumberOf(3)
55+
),
56+
new ManyOf<INumber>(
57+
new NumberOf(10),
58+
new NumberOf(2),
59+
new NumberOf(30)
60+
)
61+
),
62+
(v1, v2) => v1.AsInt().Equals(v2.AsInt())
63+
)
64+
).Value()
65+
);
66+
}
67+
4268
[Fact]
4369
public void MergesEntriesWithEnumCtor()
4470
{

0 commit comments

Comments
 (0)