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

Transition to using explicit nullable types (#184) #300

Draft
wants to merge 6 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
4 changes: 2 additions & 2 deletions Bearded.Utilities/Algorithms/CoffmanGraham.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static ImmutableList<ImmutableHashSet<T>> createLayers<T>(

public static ISolver SolverForReducedGraphs(int maxLayerSize) => new ReducedGraphSolver(maxLayerSize);

private struct DecreasingNumberSequence : IComparable<DecreasingNumberSequence>, IComparable
private readonly struct DecreasingNumberSequence : IComparable<DecreasingNumberSequence>, IComparable
{
private readonly ImmutableList<int> numbers;

Expand All @@ -186,7 +186,7 @@ private DecreasingNumberSequence(ImmutableList<int> numbers)
this.numbers = numbers;
}

public int CompareTo(object obj) => CompareTo((DecreasingNumberSequence) obj);
public int CompareTo(object? obj) => CompareTo((DecreasingNumberSequence) (obj ?? throw new ArgumentNullException()));

public int CompareTo(DecreasingNumberSequence other)
{
Expand Down
4 changes: 2 additions & 2 deletions Bearded.Utilities/Collections/DeletableObjectList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class DeletableObjectList<T> : IEnumerable<T>
{
#region Fields and Properties

private readonly List<T> list;
private readonly List<T?> list;

private int enumerators;
private int count;
Expand Down Expand Up @@ -47,7 +47,7 @@ public DeletableObjectList()

public DeletableObjectList(int capacity)
{
list = new List<T>(capacity);
list = new List<T?>(capacity);
MaxEmptyFraction = 0.2f;
}

Expand Down
13 changes: 10 additions & 3 deletions Bearded.Utilities/Collections/DeletableObjectListEnumerator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace Bearded.Utilities.Collections;
Expand All @@ -10,13 +11,19 @@ internal class DeletableObjectListEnumerator<T> : IEnumerator<T>
where T : class, IDeletable
{
private readonly DeletableObjectList<T> deletableObjectList;
private readonly List<T> list;
private readonly List<T?> list;
private int i;
private T? current;

object System.Collections.IEnumerator.Current => Current;
public T Current { get; private set; }

public DeletableObjectListEnumerator(DeletableObjectList<T> deletableObjectList, List<T> list)
public T Current
{
get => current ?? throw new InvalidOperationException();
private set => current = value;
}

public DeletableObjectListEnumerator(DeletableObjectList<T> deletableObjectList, List<T?> list)
{
this.deletableObjectList = deletableObjectList;
this.list = list;
Expand Down
13 changes: 6 additions & 7 deletions Bearded.Utilities/Collections/MutableLinkedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ public sealed class MutableLinkedList<T> : IEnumerable<T>
{
#region Fields and Properties

private readonly LinkedList<MutableLinkedListEnumerator<T>> enumerators =
new LinkedList<MutableLinkedListEnumerator<T>>();
private readonly LinkedList<MutableLinkedListEnumerator<T>> enumerators = new();

public MutableLinkedListNode<T> First { get; private set; }
public MutableLinkedListNode<T>? First { get; private set; }

public MutableLinkedListNode<T> Last { get; private set; }
public MutableLinkedListNode<T>? Last { get; private set; }

public int Count { get; private set; }

Expand Down Expand Up @@ -56,7 +55,7 @@ public void Add(MutableLinkedListNode<T> node)
else
{
node.Prev = Last;
Last.Next = node;
Last!.Next = node; // We know that Last is not null here, as the list is not empty.
}

Last = node;
Expand Down Expand Up @@ -147,7 +146,7 @@ public void InsertBefore(MutableLinkedListNode<T> node, MutableLinkedListNode<T>
throw new ArgumentException("Object must already be in list before inserting.");
if (beforeThis.List != this)
throw new ArgumentException("The object to insert before must be in the same list.");
if (node != Last)
if (node != Last || node.Prev == null)
throw new ArgumentException("Inserted object must be last object in list.");
if (node == beforeThis)
throw new ArgumentException("Cannot insert object before itself.");
Expand All @@ -173,7 +172,7 @@ public IEnumerator<T> GetEnumerator()
{
var e = new MutableLinkedListEnumerator<T>(this);
enumerators.AddFirst(e);
e.SetNode(enumerators.First);
e.SetNode(enumerators.First!); // We know First is not null as we just added e
return e;
}

Expand Down
27 changes: 12 additions & 15 deletions Bearded.Utilities/Collections/MutableLinkedListEnumerator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace Bearded.Utilities.Collections;
Expand All @@ -21,19 +22,15 @@ public MutableLinkedListEnumerator(MutableLinkedList<T> list)
this.list = list;
}

private LinkedListNode<MutableLinkedListEnumerator<T>> node;
private LinkedListNode<MutableLinkedListEnumerator<T>>? node;
public void SetNode(LinkedListNode<MutableLinkedListEnumerator<T>> node)
{
if (this.node == null)
this.node = node;
this.node ??= node;
}

public T Current
{
get { return current.Value; }
}
public T Current => current?.Value ?? throw new InvalidOperationException();

private MutableLinkedListNode<T> current;
private MutableLinkedListNode<T>? current;

public void OnObjectRemove(MutableLinkedListNode<T> obj)
{
Expand All @@ -48,6 +45,8 @@ public void OnObjectRemove(MutableLinkedListNode<T> obj)

public void Dispose()
{
if (node is null) return;

list.ForgetEnumerator(node);
}

Expand All @@ -69,12 +68,14 @@ public bool MoveNext()
currentWasDeleted = false;
return true;
}
current = current.Next;
if (current == null)

if (current?.Next == null)
{
done = true;
return false;
}

current = current.Next;
}
return true;
}
Expand All @@ -87,9 +88,5 @@ public void Reset()
currentWasDeleted = false;
}

object System.Collections.IEnumerator.Current
{
get { return Current; }
}

object System.Collections.IEnumerator.Current => Current;
}
20 changes: 10 additions & 10 deletions Bearded.Utilities/Collections/MutableLinkedListNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using System;

namespace Bearded.Utilities.Collections;

/// <summary>
Expand All @@ -25,23 +27,21 @@ public class MutableLinkedListNode<T>

#region Fields and Properties

private readonly T value;

/// <summary>
/// The value stored in the node.
/// </summary>
public T Value { get { return value; } }
public T? Value { get; }

// Next, Prev and List are internally writeable to
// simplify addition, removal and insertion code.
// Do not mess with them!
internal MutableLinkedListNode<T> Next { get; set; }
internal MutableLinkedListNode<T> Prev { get; set; }
internal MutableLinkedListNode<T>? Next { get; set; }
internal MutableLinkedListNode<T>? Prev { get; set; }

/// <summary>
/// The list the node is part of.
/// </summary>
public MutableLinkedList<T> List { get; internal set; }
public MutableLinkedList<T>? List { get; internal set; }

internal bool ChangingList { get; private set; }

Expand All @@ -51,12 +51,12 @@ public class MutableLinkedListNode<T>

internal MutableLinkedListNode(T value)
{
this.value = value;
Value = value;
}

internal MutableLinkedListNode()
{
value = this as T;
Value = this as T;
}

#endregion
Expand Down Expand Up @@ -89,15 +89,15 @@ public void AddToListBefore(MutableLinkedList<T> list, MutableLinkedListNode<T>
/// <param name="beforeThis">The node to add this before.</param>
public void InsertBefore(MutableLinkedListNode<T> beforeThis)
{
List.InsertBefore(this, beforeThis);
List?.InsertBefore(this, beforeThis);
}

/// <summary>
/// Removes this node from the list it is in.
/// </summary>
public void RemoveFromList()
{
List.Remove(this);
List?.Remove(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion Bearded.Utilities/Collections/PrefixTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private struct Node
{
private readonly Dictionary<char, Node>? values;

public string Key { get; }
public string? Key { get; }

#region creating

Expand Down
4 changes: 3 additions & 1 deletion Bearded.Utilities/Collections/PriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Bearded.Utilities.Collections;
/// </summary>
/// <typeparam name="TPriority"></typeparam>
/// <typeparam name="TValue"></typeparam>
public sealed class PriorityQueue<TPriority, TValue> : StaticPriorityQueue<TPriority, TValue> where TPriority : IComparable<TPriority>
public sealed class PriorityQueue<TPriority, TValue> : StaticPriorityQueue<TPriority, TValue>
where TPriority : IComparable<TPriority>
where TValue : notnull
{
private readonly Dictionary<TValue, int> valueDict = new Dictionary<TValue, int>();

Expand Down
7 changes: 3 additions & 4 deletions Bearded.Utilities/Core/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static Platform detectPlatform()
#endregion

#region User settings directory
private static string userSettingsDirectory;
private static string? userSettingsDirectory;

private static string buildUserSettingsDirectory()
{
Expand All @@ -67,8 +67,7 @@ private static string buildUserSettingsDirectory()
return linuxConfigDir;

var linuxHomeDir = System.Environment.GetEnvironmentVariable("HOME");
// ReSharper disable once AssignNullToNotNullAttribute
return string.IsNullOrEmpty(linuxConfigDir) ? "." : Path.Combine(linuxHomeDir, ".config");
return string.IsNullOrEmpty(linuxConfigDir) ? "." : Path.Combine(linuxHomeDir!, ".config");
default:
throw new InvalidOperationException("Encountered unknown platform.");
}
Expand All @@ -80,7 +79,7 @@ private static string buildUserSettingsDirectory()
/// For OSX: ~/Library/Application Support
/// For Linux: ~/.config
/// </summary>
public static string UserSettingsDirectory => userSettingsDirectory ?? (userSettingsDirectory = buildUserSettingsDirectory());
public static string UserSettingsDirectory => userSettingsDirectory ??= buildUserSettingsDirectory();

/// <summary>
/// Gets the default user setting directory for a given game name.
Expand Down
Loading