Skip to content

Commit

Permalink
Enable nullable context in all projects (TheAlgorithms#131)
Browse files Browse the repository at this point in the history
* Enable nullable context in all projects

* Update to .net 3.1

* Remove BitArray comparison behavior

* Add comments to exceptions
  • Loading branch information
siriak authored Jan 18, 2020
1 parent 102fdc1 commit 5db3e7a
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: csharp
solution: C-Sharp.sln
mono: none
dist: xenial
dotnet: 2.2
dotnet: 3.1
script:
- dotnet build
- travis_wait 60 dotnet test --collect:"XPlat Code Coverage"
Expand Down
4 changes: 2 additions & 2 deletions Algorithms.Tests/Algorithms.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>default</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Algorithms/Algorithms.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>default</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
4 changes: 2 additions & 2 deletions Algorithms/DataCompression/HuffmanCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public ListNode(ListNode leftChild, ListNode rightChild)
/// <summary>
/// Gets tODO. TODO.
/// </summary>
public ListNode RightChild { get; }
public ListNode? RightChild { get; }

/// <summary>
/// Gets tODO. TODO.
/// </summary>
public ListNode LeftChild { get; }
public ListNode? LeftChild { get; }
}

private class ListNodeComparer : IComparer<ListNode>
Expand Down
4 changes: 2 additions & 2 deletions Algorithms/DataCompression/ShannonFanoCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public class ListNode
/// <summary>
/// Gets or sets tODO. TODO.
/// </summary>
public ListNode RightChild { get; set; }
public ListNode? RightChild { get; set; }

/// <summary>
/// Gets or sets tODO. TODO.
/// </summary>
public ListNode LeftChild { get; set; }
public ListNode? LeftChild { get; set; }
}
}
}
4 changes: 2 additions & 2 deletions Algorithms/Search/AStar/AStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static void ResetNodes(List<Node> nodes)
public static List<Node> GeneratePath(Node target)
{
var ret = new List<Node>();
var current = target;
while (current != null)
Node? current = target;
while (!(current is null))
{
ret.Add(current);
current = current.Parent;
Expand Down
4 changes: 2 additions & 2 deletions Algorithms/Search/AStar/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public Node(VecN position, bool traversable, double traverseMultiplier)
/// <summary>
/// Gets or sets a list of all connected nodes.
/// </summary>
public Node[] ConnectedNodes { get; set; }
public Node[] ConnectedNodes { get; set; } = new Node[0];

/// <summary>
/// Gets or sets he "previous" node that was processed before this node.
/// </summary>
public Node Parent { get; set; }
public Node? Parent { get; set; }

/// <summary>
/// Gets the positional information of the node.
Expand Down
20 changes: 2 additions & 18 deletions DataStructures.Tests/BitArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public static void TestCompare()
}

[Test]
public static void TestCompareThrowsException()
public static void ArraysOfDifferentLengthsAreNotEqual()
{
// Arrange
var testObj1 = new BitArray("110");
Expand All @@ -376,23 +376,7 @@ public static void TestCompareThrowsException()
// Act

// Assert
_ = Assert.Throws<Exception>(() => Assert.IsTrue(testObj1 == testObj2));
}

[Test]
public static void TestCompareTo()
{
// Arrange
var testObj1 = new BitArray("110");
var testObj2 = new BitArray("110");
var testObj3 = new BitArray("100");

// Act

// Assert
Assert.AreEqual(testObj1.CompareTo(testObj3), 1);
Assert.AreEqual(testObj3.CompareTo(testObj1), -1);
Assert.AreEqual(testObj1.CompareTo(testObj2), 0);
Assert.False(testObj1 == testObj2);
}

#endregion COMPARE TESTS
Expand Down
4 changes: 2 additions & 2 deletions DataStructures.Tests/DataStructures.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>default</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
91 changes: 53 additions & 38 deletions DataStructures/BitArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
// assumes: the input bit-arrays must have same length.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Expand All @@ -125,7 +126,7 @@ namespace DataStructures
/// useful functions/operations to deal with this type of
/// data structure.
/// </summary>
public class BitArray : IComparable, ICloneable, IEnumerator, IEnumerable
public sealed class BitArray : ICloneable, IEnumerator<bool>, IEnumerable<bool>
{
private readonly bool[] field; // the actual bit-field
private int position = -1; // position for enumerator
Expand Down Expand Up @@ -193,9 +194,27 @@ public BitArray(string sequence)
public BitArray(bool[] bits) => field = bits;

/// <summary>
/// Gets the current bit of the array.
/// Gets a value indicating whether the current bit of the array is set.
/// </summary>
public object Current
public bool Current
{
get
{
try
{
return field[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}

/// <summary>
/// Gets a value indicating whether the current bit of the array is set.
/// </summary>
object IEnumerator.Current
{
get
{
Expand Down Expand Up @@ -492,21 +511,29 @@ public bool this[int offset]
/// <returns>Returns True if there inputs are equal; False otherwise.</returns>
public static bool operator ==(BitArray one, BitArray two)
{
var status = true;
if (ReferenceEquals(one, two))
{
return true;
}

if (one?.Length == two?.Length)
if (one is null || two is null)
{
for (var i = 0; i < one?.Length; i++)
{
if (one[i] != two[i])
{
status = false;
}
}
return false;
}
else

if (one.Length != two.Length)
{
throw new Exception("== : inputs haven't same length!");
return false;
}

var status = true;
for (var i = 0; i < one.Length; i++)
{
if (one[i] != two[i])
{
status = false;
break;
}
}

return status;
Expand Down Expand Up @@ -538,36 +565,16 @@ public object Clone()
}

/// <summary>
/// Compares if given bit-array is equal, greater or smaller than current.
/// * CompareTo (interfaces IComparable)
/// assumes: bit-array lentgh must been smaller or equal to 64 bit.
/// Gets a enumerator for this BitArray-Object.
/// </summary>
/// <param name="other">Bit-array object.</param>
/// <returns>0 - if the bit-array a equal; -1 - if this bit-array is smaller; 1 - if this bit-array is greater. .</returns>
public int CompareTo(object other)
{
var status = 0;
var valueThis = ToInt64();
var otherBitArray = (BitArray)other;
var valueOther = otherBitArray.ToInt64();

if (valueThis > valueOther)
{
status = 1;
}
else if (valueOther > valueThis)
{
status = -1;
}

return status;
}
/// <returns>Returns a enumerator for this BitArray-Object.</returns>
public IEnumerator<bool> GetEnumerator() => this;

/// <summary>
/// Gets a enumerator for this BitArray-Object.
/// </summary>
/// <returns>Returns a enumerator for this BitArray-Object.</returns>
public IEnumerator GetEnumerator() => this;
IEnumerator IEnumerable.GetEnumerator() => this;

/// <summary>
/// MoveNext (for interface IEnumerator).
Expand Down Expand Up @@ -880,6 +887,14 @@ public override bool Equals(object other)
/// <returns>hash-code for this BitArray instance.</returns>
public override int GetHashCode() => ToInt32();

/// <summary>
/// Disposes object, nothing to dispose here though.
/// </summary>
public void Dispose()
{
// Done
}

/// <summary>
/// Utility method foir checking a given sequence contains only zeros and ones.
/// This method will used in Constructor (sequence : string) and Compile(sequence : string).
Expand Down
8 changes: 6 additions & 2 deletions DataStructures/DataStructures.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>default</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -23,4 +23,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Utilities\Utilities.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 5db3e7a

Please sign in to comment.