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

Annotate nullability of reference types #582

Merged
merged 59 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
13994f4
Use C# 8
atifaziz Apr 29, 2019
9fbb80d
Annotate nullability of reference types
atifaziz May 2, 2019
52406ab
Use VS 2019 Preview image for AppVeyor
atifaziz May 3, 2019
30e06c4
Install .NET Core SDK on AppVeyor
atifaziz May 3, 2019
6b4468e
Don't send telemetry data
atifaziz May 3, 2019
b37e941
Prevent dotnet from pre-populating packages cache
atifaziz May 3, 2019
8328713
Use dotnet (instead of MSBuild) for main build
atifaziz May 3, 2019
cd955b2
Revert to VS 2017 image on AppVeyor
atifaziz May 3, 2019
0f3405e
Use dotnet CLI for packing
atifaziz May 3, 2019
3bb9293
Merge branch 'master' into nullability
atifaziz May 3, 2019
e15c498
Use VS 2019 image for AppVeyor
atifaziz May 22, 2019
c24d362
Merge branch 'master' into nullability
atifaziz May 22, 2019
a337fd6
Review ScanBy for nullable reference types
atifaziz May 22, 2019
320bb9a
Simplify Count/ScanBy patterns
atifaziz May 22, 2019
11f704c
Merge remote-tracking branch 'MoreLinqRepo/master' into nullability
Orace Nov 14, 2019
9723435
Use <Nullable> instead of <NullableContextOptions>
Orace Nov 14, 2019
311b5e3
Fix build
Orace Nov 14, 2019
22f358e
Fix c#8 nullable-default
moh-hassan Nov 16, 2019
03e0d35
Merge branch 'master' of https://github.com/moh-hassan/MoreLINQ into …
moh-hassan Nov 17, 2019
24375db
Add Nullable attributes from Nullable package.
moh-hassan Nov 17, 2019
6c08614
Resolve appveyor CI of "unexpected trailing whitespace"
moh-hassan Nov 17, 2019
68d152b
modify Extensions.g.cs and set nullability
moh-hassan Nov 17, 2019
467426f
Merge branch 'master' into nullability
atifaziz Aug 12, 2020
97d424d
Fix build errors from previous merge
atifaziz Aug 12, 2020
e8669c2
Revert change in Subject visibility
atifaziz Aug 12, 2020
fe52462
Revert changes to Await, Flatten & Memoize
atifaziz Aug 12, 2020
406a256
Revert some uses of dammit (!)
atifaziz Aug 12, 2020
347b752
Add to-do review note for TrySingle
atifaziz Aug 12, 2020
22ca363
Fix enumerators type in Transpose
atifaziz Aug 12, 2020
f003d51
Revert change to condition in PartialSort
atifaziz Aug 12, 2020
aea5493
Remove unused namespace post removal of assertions
atifaziz Aug 12, 2020
cca9227
Require .NET Core SDK 3.1.300
atifaziz Aug 13, 2020
3d602b9
Simplify variable declaration/init
atifaziz Aug 13, 2020
939686f
Remove need for dammit (!) in FillForward
atifaziz Aug 13, 2020
b8303de
Remove need for dammit (!) in GroupAdjacent
atifaziz Aug 13, 2020
9725078
Revert assert removal
atifaziz Aug 13, 2020
ea9f318
Remove need for dammit (!) in ScanBy
atifaziz Aug 13, 2020
948c83c
Add minimal null-aware dictionary wrapper
atifaziz Aug 13, 2020
88be3ac
Extract & reuse dict wrapper in CountBy
atifaziz Aug 13, 2020
38437b3
Add missing Collections file
atifaziz Aug 13, 2020
e4a4922
Fix type of DelegatingDisposable field
atifaziz Aug 13, 2020
3c6282e
Revert most EndsWith edits
atifaziz Aug 13, 2020
8439ced
Revert unrelated edit in Await
atifaziz Aug 13, 2020
0c3e2e8
Note why "#nullable enable" is needed for generated code
atifaziz Aug 13, 2020
d596449
Remove extra blank line
atifaziz Aug 13, 2020
b87b381
Remove need for dammit (!) in FallbackIfEmpty
atifaziz Aug 13, 2020
5fbe0f0
Revert unneeded change in FillBackward
atifaziz Aug 13, 2020
b2486f2
More honest "Extrema<,>" in light of nullability
atifaziz Aug 13, 2020
cda81f0
Revert unrelated style changes in OrderBy
atifaziz Aug 13, 2020
c6d59ff
Reduce one dammit (!) via better PartialSort/By
atifaziz Aug 13, 2020
4d27c75
Move empty array class to own file
atifaziz Aug 13, 2020
a534a9b
Revert changes to generator project
atifaziz Aug 13, 2020
2e5edfc
Remove need for dammit (!) in ZipImpl
atifaziz Aug 13, 2020
1ae5275
Revert "Remove need for dammit (!) in ZipImpl"
atifaziz Aug 13, 2020
7644df2
Revert some accidental changes in 3d602b9
atifaziz Aug 13, 2020
baade11
Note about disallowing ToDataTable expressions to be null
atifaziz Aug 13, 2020
b0ea8ee
First/Single/LastOrDefault may return nullable
atifaziz Aug 14, 2020
903a07f
Merge branch 'master' into nullability
atifaziz Aug 14, 2020
861585c
Make Dictionary more honest (convert to class)
atifaziz Aug 20, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ solution: MoreLinq.sln
mono: 5.0.1
dist: xenial
fsateler marked this conversation as resolved.
Show resolved Hide resolved
sudo: required
dotnet: 3.1.102
dotnet: 3.1.300
env:
- CONFIGURATION=Debug
- CONFIGURATION=Release
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static IEnumerable<TSource> Assert<TSource>(this IEnumerable<TSource> sou
/// </remarks>

public static IEnumerable<TSource> Assert<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate, Func<TSource, Exception> errorSelector)
Func<TSource, bool> predicate, Func<TSource, Exception>? errorSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSou

IEnumerable<TResult> Batch(int size)
{
TSource[] bucket = null;
TSource[]? bucket = null;
var count = 0;

foreach (var item in source)
Expand Down
82 changes: 82 additions & 0 deletions MoreLinq/Collections/Dictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2020 Atif Aziz, Leandro F. Vieira (leandromoh). 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.Collections
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// A minimal <see cref="System.Collections.Generic.Dictionary{TKey,TValue}"/> wrapper that
/// allows null keys when <typeparamref name="TKey"/> is a
/// reference type.
/// </summary>

// Add members if and when needed to keep coverage.

struct Dictionary<TKey, TValue>
atifaziz marked this conversation as resolved.
Show resolved Hide resolved
{
readonly System.Collections.Generic.Dictionary<TKey, TValue> _dict;
(bool, TValue) _null;

public Dictionary(IEqualityComparer<TKey> comparer) : this()
{
_dict = new System.Collections.Generic.Dictionary<TKey, TValue>(comparer);
_null = default;
}

public TValue this[TKey key]
{
set
{
DefaultGuard();

if (key is null)
_null = (true, value);
else
_dict[key] = value;
}
}

public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{
DefaultGuard();

if (key is null)
{
switch (_null)
fsateler marked this conversation as resolved.
Show resolved Hide resolved
{
case (true, {} v):
value = v;
return true;
case (false, _):
value = default!;
return false;
}
}

return _dict.TryGetValue(key, out value);
}

void DefaultGuard()
{
if (_dict is null)
throw new InvalidOperationException();
}
}
}
34 changes: 9 additions & 25 deletions MoreLinq/CountBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I
/// If null, the default equality comparer for <typeparamref name="TSource"/> is used.</param>
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>

public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
Expand Down Expand Up @@ -78,51 +78,35 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I

void Loop(IEqualityComparer<TKey> cmp)
{
var dic = new Dictionary<TKey, int>(cmp);
var nullIndex = (int?) null;

bool TryGetIndex(TKey key, out int i)
{
if (key == null)
{
i = nullIndex.GetValueOrDefault();
return nullIndex.HasValue;
}

return dic.TryGetValue(key, out i);
}
var dic = new Collections.Dictionary<TKey, int>(cmp);
fsateler marked this conversation as resolved.
Show resolved Hide resolved

keys = new List<TKey>();
counts = new List<int>();
var havePrevKey = false;
var prevKey = default(TKey);
(bool, TKey) prevKey = default;
var index = 0;

foreach (var item in source)
{
var key = keySelector(item);

if (// key same as the previous? then re-use the index
havePrevKey && cmp.GetHashCode(prevKey) == cmp.GetHashCode(key)
&& cmp.Equals(prevKey, key)
prevKey is (true, {} pk)
fsateler marked this conversation as resolved.
Show resolved Hide resolved
&& cmp.GetHashCode(pk) == cmp.GetHashCode(key)
&& cmp.Equals(pk, key)
// otherwise try & find index of the key
|| TryGetIndex(key, out index))
|| dic.TryGetValue(key, out index))
{
counts[index]++;
}
else
{
index = keys.Count;
if (key != null)
dic[key] = index;
else
nullIndex = index;
dic[key] = index;
keys.Add(key);
counts.Add(1);
}

prevKey = key;
havePrevKey = true;
prevKey = (true, key);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions MoreLinq/Delegating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public static IDisposable Disposable(Action delegatee) =>
new DelegatingDisposable(delegatee);

public static IObserver<T> Observer<T>(Action<T> onNext,
Action<Exception> onError = null,
Action onCompleted = null) =>
Action<Exception>? onError = null,
Action? onCompleted = null) =>
new DelegatingObserver<T>(onNext, onError, onCompleted);
}

sealed class DelegatingDisposable : IDisposable
{
Action _delegatee;
Action? _delegatee;

public DelegatingDisposable(Action delegatee) =>
_delegatee = delegatee ?? throw new ArgumentNullException(nameof(delegatee));
Expand All @@ -57,12 +57,12 @@ public void Dispose()
sealed class DelegatingObserver<T> : IObserver<T>
{
readonly Action<T> _onNext;
readonly Action<Exception> _onError;
readonly Action _onCompleted;
readonly Action<Exception>? _onError;
readonly Action? _onCompleted;

public DelegatingObserver(Action<T> onNext,
Action<Exception> onError = null,
Action onCompleted = null)
Action<Exception>? onError = null,
Action? onCompleted = null)
{
_onNext = onNext ?? throw new ArgumentNullException(nameof(onNext));
_onError = onError;
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/DistinctBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
/// comparing them by the specified key projection.</returns>

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
Expand Down
7 changes: 7 additions & 0 deletions MoreLinq/EmptyArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MoreLinq
{
static class EmptyArray<T>
{
public static readonly T[] Value = new T[0];
}
}
2 changes: 1 addition & 1 deletion MoreLinq/EndsWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second)
/// elements at the same index.
/// </remarks>

public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T> comparer)
public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T>? comparer)
{
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
Expand Down
12 changes: 4 additions & 8 deletions MoreLinq/EquiZip.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 Expand Up @@ -169,15 +168,12 @@ public static IEnumerable<TResult> EquiZip<T1, T2, T3, T4, TResult>(
}

static IEnumerable<TResult> EquiZipImpl<T1, T2, T3, T4, TResult>(
IEnumerable<T1> s1,
IEnumerable<T2> s2,
IEnumerable<T3> s3,
IEnumerable<T4> s4,
IEnumerable<T1> s1,
IEnumerable<T2> s2,
IEnumerable<T3>? s3,
IEnumerable<T4>? s4,
Func<T1, T2, T3, T4, TResult> resultSelector)
{
Debug.Assert(s1 != null);
Debug.Assert(s2 != null);

const int zero = 0, one = 1;

var limit = 1 + (s3 != null ? one : zero)
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/ExceptBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSou
public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> first,
IEnumerable<TSource> second,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> keyComparer)
IEqualityComparer<TKey>? keyComparer)
{
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
Expand Down
Loading