Skip to content

Commit

Permalink
Merge branch 'morelinq:master' into scanby
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin authored Nov 19, 2022
2 parents 8520a34 + b31c7fd commit 48f3387
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 46 deletions.
8 changes: 8 additions & 0 deletions MoreLinq.Test/FallbackIfEmptyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,13 @@ public void FallbackIfEmptyPreservesFallbackCollectionIfPossible(SourceKind sour
Assert.AreSame(source.FallbackIfEmpty(fallback), fallback);
Assert.AreSame(source.FallbackIfEmpty(fallback.AsEnumerable()), fallback);
}

[Test]
public void FallbackIfEmptyWithEmptyNullableSequence()
{
var source = Enumerable.Empty<int?>().Select(x => x);
var fallback = (int?)null;
source.FallbackIfEmpty(fallback).AssertSequenceEqual(fallback);
}
}
}
2 changes: 2 additions & 0 deletions MoreLinq.Test/PartialSortByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// limitations under the License.
#endregion

#nullable enable

namespace MoreLinq.Test
{
using System;
Expand Down
2 changes: 2 additions & 0 deletions MoreLinq.Test/PartialSortTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// limitations under the License.
#endregion

#nullable enable

namespace MoreLinq.Test
{
using System;
Expand Down
31 changes: 31 additions & 0 deletions MoreLinq/Assume.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2022 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.Runtime.CompilerServices;

static class Assume
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T NotNull<T>(T? obj) where T : class
{
Debug.Assert(obj is not null);
return obj;
}
}
}
31 changes: 31 additions & 0 deletions MoreLinq/Debug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2022 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.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

static class Debug
{
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition,
[CallerArgumentExpression(nameof(condition))] string? message = null) =>
System.Diagnostics.Debug.Assert(condition);
}
}
10 changes: 3 additions & 7 deletions MoreLinq/Experimental/Await.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace MoreLinq.Experimental
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
Expand Down Expand Up @@ -528,18 +527,15 @@ void PostNotice(Notice notice,
catch (OperationCanceledException e) when (e.CancellationToken == consumerCancellationTokenSource.Token)
{
var (error1, error2) = lastCriticalErrors;
Debug.Assert(error1 is not null);
throw new Exception("One or more critical errors have occurred.",
error2 != null ? new AggregateException(error1, error2)
: new AggregateException(error1));
error2 != null ? new AggregateException(Assume.NotNull(error1), error2)
: new AggregateException(Assume.NotNull(error1)));
}

var (kind, result, error) = notice.Current;

if (kind == Notice.Error)
{
error!.Throw();
}
Assume.NotNull(error).Throw();

if (kind == Notice.End)
break;
Expand Down
4 changes: 1 addition & 3 deletions MoreLinq/Experimental/Memoize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ public IEnumerator<T> GetEnumerator()
if (index >= _cache.Count)
{
if (index == _errorIndex)
{
_error!.Throw();
}
Assume.NotNull(_error).Throw();

if (_sourceEnumerator == null)
break;
Expand Down
1 change: 0 additions & 1 deletion MoreLinq/FallbackIfEmpty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace MoreLinq
{
using System;
using System.Collections.Generic;
using System.Diagnostics;

static partial class MoreEnumerable
{
Expand Down
32 changes: 16 additions & 16 deletions MoreLinq/Fold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ static TResult FoldImpl<T, TResult>(IEnumerable<T> source, int count,

return count switch
{
1 => folder1 !(elements[0]),
2 => folder2 !(elements[0], elements[1]),
3 => folder3 !(elements[0], elements[1], elements[2]),
4 => folder4 !(elements[0], elements[1], elements[2], elements[3]),
5 => folder5 !(elements[0], elements[1], elements[2], elements[3], elements[4]),
6 => folder6 !(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5]),
7 => folder7 !(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6]),
8 => folder8 !(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7]),
9 => folder9 !(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8]),
10 => folder10!(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9]),
11 => folder11!(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10]),
12 => folder12!(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11]),
13 => folder13!(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12]),
14 => folder14!(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12], elements[13]),
15 => folder15!(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12], elements[13], elements[14]),
16 => folder16!(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12], elements[13], elements[14], elements[15]),
1 => Assume.NotNull(folder1 )(elements[0]),
2 => Assume.NotNull(folder2 )(elements[0], elements[1]),
3 => Assume.NotNull(folder3 )(elements[0], elements[1], elements[2]),
4 => Assume.NotNull(folder4 )(elements[0], elements[1], elements[2], elements[3]),
5 => Assume.NotNull(folder5 )(elements[0], elements[1], elements[2], elements[3], elements[4]),
6 => Assume.NotNull(folder6 )(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5]),
7 => Assume.NotNull(folder7 )(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6]),
8 => Assume.NotNull(folder8 )(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7]),
9 => Assume.NotNull(folder9 )(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8]),
10 => Assume.NotNull(folder10)(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9]),
11 => Assume.NotNull(folder11)(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10]),
12 => Assume.NotNull(folder12)(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11]),
13 => Assume.NotNull(folder13)(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12]),
14 => Assume.NotNull(folder14)(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12], elements[13]),
15 => Assume.NotNull(folder15)(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12], elements[13], elements[14]),
16 => Assume.NotNull(folder16)(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5], elements[6], elements[7], elements[8], elements[9], elements[10], elements[11], elements[12], elements[13], elements[14], elements[15]),
_ => throw new NotSupportedException()
};
}
Expand Down
29 changes: 15 additions & 14 deletions MoreLinq/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
// SOFTWARE.
#endregion

#if !NET6_0_OR_GREATER
#nullable enable annotations
#pragma warning disable 8602 // Dereference of a possibly null reference.
#pragma warning disable 8603 // Possible null reference return.
#endif

namespace MoreLinq
{
using System;
Expand Down Expand Up @@ -65,7 +59,10 @@ internal static Lookup<TKey, TElement> Create<TSource>(IEnumerable<TSource> sour
var lookup = new Lookup<TKey, TElement>(comparer);

foreach (var item in source)
lookup.GetGrouping(keySelector(item), create: true)!.Add(elementSelector(item));
{
var grouping = Assume.NotNull(lookup.GetGrouping(keySelector(item), create: true));
grouping.Add(elementSelector(item));
}

return lookup;
}
Expand All @@ -78,7 +75,10 @@ internal static Lookup<TKey, TElement> Create(IEnumerable<TElement> source, Func
var lookup = new Lookup<TKey, TElement>(comparer);

foreach (var item in source)
lookup.GetGrouping(keySelector(item), create: true)!.Add(item);
{
var grouping = Assume.NotNull(lookup.GetGrouping(keySelector(item), create: true));
grouping.Add(item);
}

return lookup;
}
Expand All @@ -90,7 +90,10 @@ internal static Lookup<TKey, TElement> CreateForJoin(IEnumerable<TElement> sourc
foreach (var item in source)
{
if (keySelector(item) is { } key)
lookup.GetGrouping(key, create: true)!.Add(item);
{
var grouping = Assume.NotNull(lookup.GetGrouping(key, create: true));
grouping.Add(item);
}
}

return lookup;
Expand Down Expand Up @@ -122,9 +125,7 @@ public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
{
do
{
g = g._next;

Debug.Assert(g is not null);
g = Assume.NotNull(g._next);
yield return g;
}
while (g != _lastGrouping);
Expand Down Expand Up @@ -179,10 +180,10 @@ void Resize()
{
var newSize = checked((_count * 2) + 1);
var newGroupings = new Grouping<TKey, TElement>[newSize];
var g = _lastGrouping!;
var g = Assume.NotNull(_lastGrouping);
do
{
g = g._next!;
g = Assume.NotNull(g._next);
var index = g._hashCode % newSize;
g._hashNext = newGroupings[index];
newGroupings[index] = g;
Expand Down
1 change: 0 additions & 1 deletion MoreLinq/Pad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace MoreLinq
{
using System;
using System.Collections.Generic;
using System.Diagnostics;

static partial class MoreEnumerable
{
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/PartialSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static IEnumerable<TSource> PartialSortByImpl<TSource, TKey>(
{
if (keys != null)
{
var key = keySelector!(item);
var key = Assume.NotNull(keySelector)(item);
if (Insert(keys, key, keyComparer) is {} i)
{
if (top.Count == count)
Expand Down
1 change: 0 additions & 1 deletion MoreLinq/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace MoreLinq
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

static partial class MoreEnumerable
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq/Reactive/Subject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ public void OnNext(T value)
}

public void OnError(Exception error) =>
OnFinality(ref _error, error, (observer, err) => observer.OnError(err!));
OnFinality(ref _error, error, (observer, err) => observer.OnError(err));

public void OnCompleted() =>
OnFinality(ref _completed, true, (observer, _) => observer.OnCompleted());

void OnFinality<TState>(ref TState state, TState value, Action<IObserver<T>, TState> action)
void OnFinality<TState>(ref TState? state, TState value, Action<IObserver<T>, TState> action)
{
if (IsMuted)
return;
Expand Down

0 comments on commit 48f3387

Please sign in to comment.