Skip to content

Commit

Permalink
feat: enable nullable annotations (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik authored Mar 22, 2024
1 parent ec25d01 commit 2d7fdd0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
</PropertyGroup>
<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Iesi.Collections.Test/Generic/GenericSetFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Iesi.Collections.Test.Generic;
/// </summary>
public abstract class GenericSetFixture
{
IList<string> _aInitValues;
IList<string> _bInitValues;
protected ISet<string> _set;
IList<string> _aInitValues = default!;
IList<string> _bInitValues = default!;
protected ISet<string> _set = default!;

public static string one = "one";
public static string two = "two";
Expand Down
32 changes: 14 additions & 18 deletions src/Iesi.Collections/Generic/LinkedHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace Iesi.Collections.Generic;
#endif
public class LinkedHashSet<T> : ISet<T>
#if !NET40
, IReadOnlyCollection<T>
, IReadOnlyCollection<T> where T : notnull
#endif
{
readonly Dictionary<T, LinkedHashNode<T>> _elements = new Dictionary<T, LinkedHashNode<T>>();
LinkedHashNode<T> _first, _last;
readonly Dictionary<T, LinkedHashNode<T>> _elements = new();
LinkedHashNode<T>? _first, _last;

/// <summary>
/// Initializes a new instance of the <see cref="LinkedHashSet{T}"/> class.
Expand Down Expand Up @@ -151,8 +151,7 @@ public void CopyTo(T[] array, int arrayIndex)
/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
public bool Remove(T item)
{
LinkedHashNode<T> node;
if (_elements.TryGetValue(item, out node))
if (_elements.TryGetValue(item, out var node))
{
_elements.Remove(item);
Unlink(node);
Expand Down Expand Up @@ -216,8 +215,7 @@ public void SymmetricExceptWith(IEnumerable<T> other)
{
foreach (var item in other)
{
LinkedHashNode<T> node;
if (_elements.TryGetValue(item, out node))
if (_elements.TryGetValue(item, out var node))
{
_elements.Remove(item);
Unlink(node);
Expand Down Expand Up @@ -254,8 +252,7 @@ public bool IsSubsetOf(IEnumerable<T> other)
/// <param name="other">The collection to compare to the current set.</param><exception cref="T:System.ArgumentNullException"><paramref name="other"/> is null.</exception>
public bool IsSupersetOf(IEnumerable<T> other)
{
int numberOfOthersPresent;
var numberOfOthers = CountOthers(other, out numberOfOthersPresent);
var numberOfOthers = CountOthers(other, out var numberOfOthersPresent);

// All others must be present.
return numberOfOthersPresent == numberOfOthers;
Expand All @@ -270,8 +267,7 @@ public bool IsSupersetOf(IEnumerable<T> other)
/// <param name="other">The collection to compare to the current set. </param><exception cref="T:System.ArgumentNullException"><paramref name="other"/> is null.</exception>
public bool IsProperSupersetOf(IEnumerable<T> other)
{
int numberOfOthersPresent;
var numberOfOthers = CountOthers(other, out numberOfOthersPresent);
var numberOfOthers = CountOthers(other, out var numberOfOthersPresent);

// All others must be present, plus we need to have at least one additional item.
return numberOfOthersPresent == numberOfOthers && numberOfOthers < Count;
Expand Down Expand Up @@ -423,8 +419,8 @@ public LinkedHashNode(TElement value)
}

public readonly TElement Value;
public LinkedHashNode<TElement> Next;
public LinkedHashNode<TElement> Previous;
public LinkedHashNode<TElement>? Next;
public LinkedHashNode<TElement>? Previous;
}

/// <summary>
Expand All @@ -435,12 +431,12 @@ public LinkedHashNode(TElement value)
#endif
public struct Enumerator : IEnumerator<T>
{
LinkedHashNode<T> _node;
T _current;
LinkedHashNode<T>? _node;
T? _current;

internal Enumerator(LinkedHashSet<T> set)
{
_current = default(T);
_current = default;
_node = set._first;
}

Expand All @@ -456,10 +452,10 @@ public bool MoveNext()
}

/// <inheritdoc />
public T Current => _current;
public T Current => _current!;

/// <inheritdoc />
object IEnumerator.Current => Current;
object IEnumerator.Current => Current!;

/// <inheritdoc />
void IEnumerator.Reset() => throw new NotSupportedException();
Expand Down

0 comments on commit 2d7fdd0

Please sign in to comment.