Skip to content

Commit

Permalink
Refresh generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Jan 4, 2025
1 parent 20689be commit da76b5a
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 73 deletions.
143 changes: 70 additions & 73 deletions MoreLinq/BindByIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static partial class MoreEnumerable
/// 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="indicies">The sequence of indicies.</param>
/// <param name="indices">The sequence of indices.</param>
/// <param name="missingSelector">
/// TODO Complete documentation
/// </param>
Expand All @@ -42,9 +42,9 @@ static partial class MoreEnumerable
/// </returns>

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

/// <summary>
/// TODO Complete documentation
Expand All @@ -53,7 +53,7 @@ public static IEnumerable<TResult>
/// 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="indicies">The sequence of indicies.</param>
/// <param name="indices">The sequence of indices.</param>
/// <param name="lookBackSize">Size of look-back buffer.</param>
/// <param name="missingSelector">
/// TODO Complete documentation
Expand All @@ -66,77 +66,79 @@ public static IEnumerable<TResult>
/// </returns>

public static IEnumerable<TResult>
BindByIndex<T, TResult>(this IEnumerable<T> source, IEnumerable<int> indicies,
int lookBackSize,
Func<int, TResult> missingSelector,
Func<T, int, TResult> resultSelector) =>
BindByIndex(source, indicies, (int?) lookBackSize, missingSelector, resultSelector);
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> indicies,
int? lookBackSize,
Func<int, TResult> missingSelector,
Func<T, int, TResult> resultSelector)
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 (indicies == null) throw new ArgumentNullException(nameof(indicies));
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 is int lbs ? lbs > 0 ? new Queue<T>(lbs, lbs) : null
: new Queue<T>());
return _(lookBackSize switch
{
{ } lbs and > 0 => new Queue<T>(lbs, lbs),
0 => null,
_ => new Queue<T>()
});

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

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

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

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

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

if (ri != si)
{
yield return missingSelector(ri);
while (rie.MoveNext())
yield return missingSelector(rie.Current);
}
queue?.Enqueue(item);
si++;
}

if (ri != si)
{
yield return missingSelector(ri);
while (rie.MoveNext())
yield return missingSelector(rie.Current);
}
}
}
Expand All @@ -149,23 +151,13 @@ IEnumerable<TResult> _(Queue<T> queue)
/// directly indexing into the queue to retrieve any one item.
/// </summary>

sealed class Queue<T> : IReadOnlyList<T>
sealed class Queue<T>(int maxCount = 0, int capacity = 0) : IReadOnlyList<T>
{
T[] _items;
int _firstIndex;
readonly int _maxCount;

static readonly T[] ZeroItems = new T[0];
T[] items = capacity > 0 ? new T[capacity] : [];
int firstIndex;
readonly int maxCount = maxCount;

public Queue(int maxCount = 0, int capacity = 0)
{
_items = capacity > 0 ? new T[capacity] : ZeroItems;
_firstIndex = 0;
_maxCount = maxCount;
Count = 0;
}

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

T IReadOnlyList<T>.this[int index] => this[index];
Expand All @@ -175,25 +167,30 @@ public T this[int index]
get
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();
{
#pragma warning disable CA2201 // Do not raise reserved exception types
throw new IndexOutOfRangeException();
#pragma warning restore CA2201
}

return Cell(index);
}
}

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

public void Enqueue(T item)
{
if (_maxCount > 0 && Count == _maxCount)
Dequeue();
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];
_firstIndex = 0;
_items = array;
this.firstIndex = 0;
this.items = array;
}

Cell(Count++) = item;
Expand All @@ -204,7 +201,7 @@ public T Dequeue()
if (Count == 0)
throw new InvalidOperationException();
var result = this[0];
_firstIndex++;
this.firstIndex++;
--Count;
return result;
}
Expand Down
54 changes: 54 additions & 0 deletions MoreLinq/Extensions.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,60 @@ public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSou

}

/// <summary><c>BindByIndex</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
public static partial class BindByIndexExtension
{
/// <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) => MoreEnumerable. BindByIndex(source, indices, 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) => MoreEnumerable. BindByIndex(source, indices, lookBackSize, missingSelector, resultSelector);

}

/// <summary><c>Cartesian</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
Expand Down
5 changes: 5 additions & 0 deletions MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#nullable enable
MoreLinq.Extensions.BindByIndexExtension
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
5 changes: 5 additions & 0 deletions MoreLinq/PublicAPI/net8.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#nullable enable
MoreLinq.Extensions.BindByIndexExtension
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
5 changes: 5 additions & 0 deletions MoreLinq/PublicAPI/net9.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#nullable enable
MoreLinq.Extensions.BindByIndexExtension
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
5 changes: 5 additions & 0 deletions MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#nullable enable
MoreLinq.Extensions.BindByIndexExtension
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
5 changes: 5 additions & 0 deletions MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#nullable enable
MoreLinq.Extensions.BindByIndexExtension
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.Extensions.BindByIndexExtension.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!
static MoreLinq.MoreEnumerable.BindByIndex<T, TResult>(this System.Collections.Generic.IEnumerable<T>! source, System.Collections.Generic.IEnumerable<int>! indices, int lookBackSize, System.Func<int, TResult>! missingSelector, System.Func<T, int, TResult>! resultSelector) -> System.Collections.Generic.IEnumerable<TResult>!

0 comments on commit da76b5a

Please sign in to comment.