Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindByIndex implementation #552

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions MoreLinq.Test/BindByIndexTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2018 Atif Aziz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq.Test
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using MoreLinq.Extensions;
using NUnit.Framework;

public class BindByIndexTest
{
[TestCase(new[] { 2, 3 }, ExpectedResult = new[] { "baz", "qux" })]
[TestCase(new[] { 1, 3 }, ExpectedResult = new[] { "bar", "qux" })]
[TestCase(new[] { 0, 2, 3 }, ExpectedResult = new[] { "foo", "baz", "qux" })]
[TestCase(new[] { 0, 1, 1 }, ExpectedResult = new[] { "foo", "bar", "bar" })]
[TestCase(new[] { 3, 1, 2 }, ExpectedResult = new[] { "qux", "bar", "baz" })]
[TestCase(new[] { -1, 1, 2, 10 }, ExpectedResult = new[] { "?-1", "bar", "baz", "?10" })]
public string[] WithoutSpecificLookBackSize(IEnumerable<int> indices)
{
using var source = TestingSequence.Of("foo", "bar", "baz", "qux");
return source.BindByIndex(indices, i => $"?{i.ToInvariantString()}", (s, _) => s)
.ToArray();
}

[TestCase(0, new[] { 2, 3 }, ExpectedResult = new[] { "baz", "qux" })]
[TestCase(0, new[] { 1, 3 }, ExpectedResult = new[] { "bar", "qux" })]
[TestCase(0, new[] { 0, 2, 3 }, ExpectedResult = new[] { "foo", "baz", "qux" })]
[TestCase(0, new[] { 0, 1, 1 }, ExpectedResult = new[] { "foo", "bar", "bar" })]
[TestCase(0, new[] { 3, 1, 2 }, ExpectedResult = new[] { "qux", "?1", "?2" })]
[TestCase(4, new[] { 3, 1, 2 }, ExpectedResult = new[] { "qux", "bar", "baz" })]
[TestCase(1, new[] { 3, 1, 2 }, ExpectedResult = new[] { "qux", "?1", "baz" })]
[TestCase(0, new[] { -1, 1, 2, 10 }, ExpectedResult = new[] { "?-1", "bar", "baz", "?10" })]
public string[] WithSpecificLookBackSize(int lookBackSize, IEnumerable<int> indices)
{
using var source = TestingSequence.Of("foo", "bar", "baz", "qux");
return source.BindByIndex(indices, lookBackSize, i => $"?{i.ToInvariantString()}", (s, _) => s)
.ToArray();
}

[Test]
public void ParsingExample()
{
const string csv = """
# Generated using https://mockaroo.com/
id,first_name,last_name,email,gender,ip_address
1,Maggee,Hould,[email protected],Female,158.221.234.250
2,Judas,Vedekhov,[email protected],Male,26.25.8.252
3,Sharity,Desquesnes,[email protected],Female,27.224.140.230
4,Della,Conant,[email protected],Female,229.74.161.94
5,Sansone,Hardson,[email protected],Male,51.154.224.38
6,Lloyd,Cromley,[email protected],Male,168.145.20.63
7,Ty,Bamsey,[email protected],Male,129.204.46.174
8,Hurlee,Dumphy,[email protected],Male,95.17.55.115
9,Andy,Vickarman,[email protected],Male,10.159.118.60
10,Jerad,Kerley,[email protected],Male,3.19.136.57
""";

// Parse CSV into rows of fields with commented lines, those starting with pound or hash
// (#), removed.

var rows =
from row in Regex.Split(csv.Trim(), "\r?\n")
select row.Trim() into row
where row.Length > 0 && row[0] != '#'
select row.Trim().Split(',');

// Split header and data rows:

var (header, data) =
rows.Index()
.Partition(e => e.Key == 0,
(hr, dr) => (hr.Single().Value, from e in dr select e.Value));

// Locate indices of headers:

int[] bindings = [..from h in new[] { "id", "email", "last_name", "first_name", "foo" }
select Array.FindIndex(header, sh => sh == h)];

// Bind to data using indices:

string? missing = null;

var result =
from row in data
select row.BindByIndex(bindings, bindings.Length, _ => missing, (f, _) => f)
.Fold((id, email, ln, fn, foo) =>
id is null || email is null || ln is null || fn is null
? null
: new
{
Id = int.Parse(id, NumberStyles.None, CultureInfo.InvariantCulture),
FirstName = fn,
LastName = ln,
Email = email,
Foo = foo,
});

result.AssertSequenceEqual(
new { Id = 1 , FirstName = "Maggee" , LastName = "Hould" , Email = "[email protected]" , Foo = missing },
new { Id = 2 , FirstName = "Judas" , LastName = "Vedekhov" , Email = "[email protected]" , Foo = missing },
new { Id = 3 , FirstName = "Sharity", LastName = "Desquesnes", Email = "[email protected]", Foo = missing },
new { Id = 4 , FirstName = "Della" , LastName = "Conant" , Email = "[email protected]" , Foo = missing },
new { Id = 5 , FirstName = "Sansone", LastName = "Hardson" , Email = "[email protected]" , Foo = missing },
new { Id = 6 , FirstName = "Lloyd" , LastName = "Cromley" , Email = "[email protected]" , Foo = missing },
new { Id = 7 , FirstName = "Ty" , LastName = "Bamsey" , Email = "[email protected]" , Foo = missing },
new { Id = 8 , FirstName = "Hurlee" , LastName = "Dumphy" , Email = "[email protected]" , Foo = missing },
new { Id = 9 , FirstName = "Andy" , LastName = "Vickarman" , Email = "[email protected]" , Foo = missing },
new { Id = 10, FirstName = "Jerad" , LastName = "Kerley" , Email = "[email protected]" , Foo = missing });
}
}
}
217 changes: 217 additions & 0 deletions MoreLinq/BindByIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2018 Atif Aziz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq
{
using System;
using System.Collections;
using System.Collections.Generic;

static partial class MoreEnumerable
{
/// <summary>
/// TODO Complete documentation
/// </summary>
/// <typeparam name="T">
/// Type of elements in <paramref name="source"/> sequence.</typeparam>
/// <typeparam name="TResult">Type of result elements returned.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="indices">The sequence of indices.</param>
/// <param name="missingSelector">
/// TODO Complete documentation
/// </param>
/// <param name="resultSelector">
/// TODO Complete documentation
/// </param>
/// <returns>
/// TODO Complete documentation
/// </returns>

public static IEnumerable<TResult>
BindByIndex<T, TResult>(this IEnumerable<T> source, IEnumerable<int> indices,
Func<int, TResult> missingSelector, Func<T, int, TResult> resultSelector) =>
BindByIndex(source, indices, null, missingSelector, resultSelector);

/// <summary>
/// TODO Complete documentation
/// </summary>
/// <typeparam name="T">
/// Type of elements in <paramref name="source"/> sequence.</typeparam>
/// <typeparam name="TResult">Type of result elements returned.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="indices">The sequence of indices.</param>
/// <param name="lookBackSize">Size of look-back buffer.</param>
/// <param name="missingSelector">
/// TODO Complete documentation
/// </param>
/// <param name="resultSelector">
/// TODO Complete documentation
/// </param>
/// <returns>
/// TODO Complete documentation
/// </returns>

public static IEnumerable<TResult>
BindByIndex<T, TResult>(this IEnumerable<T> source, IEnumerable<int> indices,
int lookBackSize,
Func<int, TResult> missingSelector,
Func<T, int, TResult> resultSelector) =>
BindByIndex(source, indices, (int?)lookBackSize, missingSelector, resultSelector);

static IEnumerable<TResult>
BindByIndex<T, TResult>(IEnumerable<T> source, IEnumerable<int> indices,
int? lookBackSize,
Func<int, TResult> missingSelector,
Func<T, int, TResult> resultSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (indices == null) throw new ArgumentNullException(nameof(indices));
if (lookBackSize < 0) throw new ArgumentOutOfRangeException(nameof(lookBackSize));
if (missingSelector == null) throw new ArgumentNullException(nameof(missingSelector));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

// TODO A version optimized for lists

return _(lookBackSize switch
{
{ } lbs and > 0 => new Queue<T>(lbs, lbs),
0 => null,
_ => new Queue<T>()
});

IEnumerable<TResult> _(Queue<T>? queue)
{
using var rie = indices.GetEnumerator();
if (!rie.MoveNext())
yield break;

Check warning on line 100 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L100

Added line #L100 was not covered by tests

while (rie.Current < 0)
{
yield return missingSelector(rie.Current);
if (!rie.MoveNext())
yield break;

Check warning on line 106 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L106

Added line #L106 was not covered by tests
}

var ri = rie.Current;
var si = 0;

foreach (var item in source)
{
while (si == ri)
{
yield return resultSelector(item, si);
do
{
if (!rie.MoveNext())
yield break;
ri = rie.Current;
if (ri < si)
{
if (queue is { } q && si - q.Count is var qi && ri >= qi)
yield return resultSelector(q[ri - qi], ri);
else
yield return missingSelector(ri);
}
}
while (ri < si);
}

queue?.Enqueue(item);
si++;
}

if (ri != si)
{
yield return missingSelector(ri);
while (rie.MoveNext())
yield return missingSelector(rie.Current);

Check warning on line 141 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L141

Added line #L141 was not covered by tests
}
}
}
}

/// <summary>
/// A queue implementation similar to <see cref="System.Collections.Generic.Queue{T}"/> but
/// which supports a maximum count (exceeding which will cause an item to be dequeued to make
/// space for a new one being queued) as well as directly indexing into the queue to retrieve
/// any one item.
/// </summary>

file sealed class Queue<T>(int maxCount = 0, int capacity = 0) : IReadOnlyList<T>
{
T[] items = capacity > 0 ? new T[capacity] : [];
int firstIndex;
readonly int maxCount = maxCount;

int Capacity => this.items.Length;
public int Count { get; private set; }

T IReadOnlyList<T>.this[int index] => this[index];

Check warning on line 163 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L163

Added line #L163 was not covered by tests

public T this[int index]
{
get
{
if (index < 0 || index >= Count)
{

Check warning on line 170 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L170

Added line #L170 was not covered by tests
#pragma warning disable CA2201 // Do not raise reserved exception types
throw new IndexOutOfRangeException();

Check warning on line 172 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L172

Added line #L172 was not covered by tests
#pragma warning restore CA2201
}

return Cell(index);
}
}

ref T Cell(int index) => ref this.items[(this.firstIndex + index) % Capacity];

public void Enqueue(T item)
{
if (this.maxCount > 0 && Count == this.maxCount)
_ = Dequeue();

if (Count == Capacity)
{
var array = new T[Math.Max(4, Capacity * 2)];
for (var i = 0; i < Count; i++)
array[i] = this[i];

Check warning on line 191 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L191

Added line #L191 was not covered by tests
this.firstIndex = 0;
this.items = array;
}

Cell(Count++) = item;
}

public T Dequeue()
{
if (Count == 0)
throw new InvalidOperationException();

Check warning on line 202 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L202

Added line #L202 was not covered by tests
var result = this[0];
this.firstIndex++;
--Count;
return result;
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

Check warning on line 209 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L209

Added line #L209 was not covered by tests

public IEnumerator<T> GetEnumerator()
{

Check warning on line 212 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L212

Added line #L212 was not covered by tests
for (var i = 0; i < Count; i++)
yield return this[i];
}

Check warning on line 215 in MoreLinq/BindByIndex.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/BindByIndex.cs#L214-L215

Added lines #L214 - L215 were not covered by tests
}
}
Loading
Loading