Skip to content

Commit

Permalink
Guarded dictionaries return proper Keys and Values (sestoft#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondfisk authored Dec 15, 2019
1 parent 79802ae commit 00b7326
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 132 deletions.
6 changes: 3 additions & 3 deletions C5.Tests/C5.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
68 changes: 68 additions & 0 deletions C5.Tests/Wrappers/GuardedDictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using NUnit.Framework;

namespace C5.Tests.Wrappers
{
[TestFixture]
public class GuardedDictionaryTests
{
[Test]
public void Keys_returns_GuardedCollectionValue()
{
var source = new HashDictionary<int, string>
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedDictionary<int, string>(source);

Assert.IsAssignableFrom<GuardedCollectionValue<int>>(guarded.Keys);
}

[Test]
public void Keys_returns_Keys()
{
var source = new HashDictionary<int, string>
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedDictionary<int, string>(source);

CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, guarded.Keys);
}

[Test]
public void Values_returns_GuardedCollectionValue()
{
var source = new HashDictionary<int, string>
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedDictionary<int, string>(source);

Assert.IsAssignableFrom<GuardedCollectionValue<string>>(guarded.Values);
}

[Test]
public void Values_returns_Values()
{
var source = new HashDictionary<int, string>
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedDictionary<int, string>(source);

CollectionAssert.AreEquivalent(new[] { "one", "two", "three" }, guarded.Values);
}
}
}
76 changes: 76 additions & 0 deletions C5.Tests/Wrappers/GuardedSortedDictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using NUnit.Framework;

namespace C5.Tests.Wrappers
{
[TestFixture]
public class GuardedSortedDictionaryTests
{
[Test]
public void Keys_returns_GuardedSorted()
{
var reverse = ComparerFactory<int>.CreateComparer((a, b) => a > b ? -1 : 1);

var source = new SortedArrayDictionary<int, string>(reverse)
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedSortedDictionary<int, string>(source);

Assert.IsAssignableFrom<GuardedSorted<int>>(guarded.Keys);
}

[Test]
public void Keys_returns_Keys()
{
var reverse = ComparerFactory<int>.CreateComparer((a, b) => a > b ? -1 : 1);

var source = new SortedArrayDictionary<int, string>(reverse)
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedSortedDictionary<int, string>(source);

CollectionAssert.AreEquivalent(new[] { 3, 2, 1 }, guarded.Keys);
}

[Test]
public void Values_returns_GuardedCollectionValue()
{
var reverse = ComparerFactory<int>.CreateComparer((a, b) => a > b ? -1 : 1);

var source = new SortedArrayDictionary<int, string>(reverse)
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedSortedDictionary<int, string>(source);

Assert.IsAssignableFrom<GuardedCollectionValue<string>>(guarded.Values);
}

[Test]
public void Values_returns_Values()
{
var reverse = ComparerFactory<int>.CreateComparer((a, b) => a > b ? -1 : 1);

var source = new SortedArrayDictionary<int, string>(reverse)
{
[1] = "one",
[2] = "two",
[3] = "three"
};

var guarded = new GuardedSortedDictionary<int, string>(source);

CollectionAssert.AreEquivalent(new[] { "one", "two", "three" }, guarded.Values);
}
}
}
52 changes: 21 additions & 31 deletions C5/Interfaces/ISortedDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using SCG = System.Collections.Generic;

namespace C5
{
Expand All @@ -12,44 +12,44 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
///
/// </summary>
/// <value></value>
new ISorted<K>? Keys { get; }
new ISorted<K> Keys { get; }

/// <summary>
/// Find the current least item of this sorted collection.
/// </summary>
/// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
/// <returns>The least item.</returns>
System.Collections.Generic.KeyValuePair<K, V> FindMin();
SCG.KeyValuePair<K, V> FindMin();


/// <summary>
/// Remove the least item from this sorted collection.
/// </summary>
/// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
/// <returns>The removed item.</returns>
System.Collections.Generic.KeyValuePair<K, V> DeleteMin();
SCG.KeyValuePair<K, V> DeleteMin();


/// <summary>
/// Find the current largest item of this sorted collection.
/// </summary>
/// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
/// <returns>The largest item.</returns>
System.Collections.Generic.KeyValuePair<K, V> FindMax();
SCG.KeyValuePair<K, V> FindMax();


/// <summary>
/// Remove the largest item from this sorted collection.
/// </summary>
/// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
/// <returns>The removed item.</returns>
System.Collections.Generic.KeyValuePair<K, V> DeleteMax();
SCG.KeyValuePair<K, V> DeleteMax();

/// <summary>
/// The key comparer used by this dictionary.
/// </summary>
/// <value></value>
System.Collections.Generic.IComparer<K> Comparer { get; }
SCG.IComparer<K> Comparer { get; }

/// <summary>
/// Find the entry in the dictionary whose key is the
Expand All @@ -58,7 +58,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// <param name="key">The key</param>
/// <param name="res">The predecessor, if any</param>
/// <returns>True if key has a predecessor</returns>
bool TryPredecessor(K key, out System.Collections.Generic.KeyValuePair<K, V> res);
bool TryPredecessor(K key, out SCG.KeyValuePair<K, V> res);

/// <summary>
/// Find the entry in the dictionary whose key is the
Expand All @@ -67,7 +67,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// <param name="key">The key</param>
/// <param name="res">The successor, if any</param>
/// <returns>True if the key has a successor</returns>
bool TrySuccessor(K key, out System.Collections.Generic.KeyValuePair<K, V> res);
bool TrySuccessor(K key, out SCG.KeyValuePair<K, V> res);

/// <summary>
/// Find the entry in the dictionary whose key is the
Expand All @@ -76,7 +76,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// <param name="key">The key</param>
/// <param name="res">The predecessor, if any</param>
/// <returns>True if key has a weak predecessor</returns>
bool TryWeakPredecessor(K key, out System.Collections.Generic.KeyValuePair<K, V> res);
bool TryWeakPredecessor(K key, out SCG.KeyValuePair<K, V> res);

/// <summary>
/// Find the entry in the dictionary whose key is the
Expand All @@ -85,42 +85,39 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// <param name="key">The key</param>
/// <param name="res">The weak successor, if any</param>
/// <returns>True if the key has a weak successor</returns>
bool TryWeakSuccessor(K key, out System.Collections.Generic.KeyValuePair<K, V> res);
bool TryWeakSuccessor(K key, out SCG.KeyValuePair<K, V> res);

/// <summary>
/// Find the entry with the largest key less than a given key.
/// </summary>
/// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
/// <param name="key">The key to compare to</param>
/// <returns>The entry</returns>
System.Collections.Generic.KeyValuePair<K, V> Predecessor(K key);

SCG.KeyValuePair<K, V> Predecessor(K key);

/// <summary>
/// Find the entry with the least key greater than a given key.
/// </summary>
/// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
/// <param name="key">The key to compare to</param>
/// <returns>The entry</returns>
System.Collections.Generic.KeyValuePair<K, V> Successor(K key);

SCG.KeyValuePair<K, V> Successor(K key);

/// <summary>
/// Find the entry with the largest key less than or equal to a given key.
/// </summary>
/// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
/// <param name="key">The key to compare to</param>
/// <returns>The entry</returns>
System.Collections.Generic.KeyValuePair<K, V> WeakPredecessor(K key);

SCG.KeyValuePair<K, V> WeakPredecessor(K key);

/// <summary>
/// Find the entry with the least key greater than or equal to a given key.
/// </summary>
/// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
/// <param name="key">The key to compare to</param>
/// <returns>The entry</returns>
System.Collections.Generic.KeyValuePair<K, V> WeakSuccessor(K key);
SCG.KeyValuePair<K, V> WeakSuccessor(K key);

/// <summary>
/// Given a "cut" function from the items of the sorted collection to <code>int</code>
Expand Down Expand Up @@ -159,7 +156,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// on this collection.</param>
/// <returns>True if the cut function is zero somewhere
/// on this collection.</returns>
bool Cut(IComparable<K> cutFunction, out System.Collections.Generic.KeyValuePair<K, V> lowEntry, out bool lowIsValid, out System.Collections.Generic.KeyValuePair<K, V> highEntry, out bool highIsValid);
bool Cut(IComparable<K> cutFunction, out SCG.KeyValuePair<K, V> lowEntry, out bool lowIsValid, out System.Collections.Generic.KeyValuePair<K, V> highEntry, out bool highIsValid);

/// <summary>
/// Query this sorted collection for items greater than or equal to a supplied value.
Expand All @@ -169,8 +166,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// </summary>
/// <param name="bot">The lower bound (inclusive).</param>
/// <returns>The result directed collection.</returns>
IDirectedEnumerable<System.Collections.Generic.KeyValuePair<K, V>> RangeFrom(K bot);

IDirectedEnumerable<SCG.KeyValuePair<K, V>> RangeFrom(K bot);

/// <summary>
/// Query this sorted collection for items between two supplied values.
Expand All @@ -181,8 +177,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// <param name="lowerBound">The lower bound (inclusive).</param>
/// <param name="upperBound">The upper bound (exclusive).</param>
/// <returns>The result directed collection.</returns>
IDirectedEnumerable<System.Collections.Generic.KeyValuePair<K, V>> RangeFromTo(K lowerBound, K upperBound);

IDirectedEnumerable<SCG.KeyValuePair<K, V>> RangeFromTo(K lowerBound, K upperBound);

/// <summary>
/// Query this sorted collection for items less than a supplied value.
Expand All @@ -192,8 +187,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// </summary>
/// <param name="top">The upper bound (exclusive).</param>
/// <returns>The result directed collection.</returns>
IDirectedEnumerable<System.Collections.Generic.KeyValuePair<K, V>> RangeTo(K top);

IDirectedEnumerable<SCG.KeyValuePair<K, V>> RangeTo(K top);

/// <summary>
/// Create a directed collection with the same items as this collection.
Expand All @@ -202,8 +196,7 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
/// </summary>
/// <returns>The result directed collection.</returns>
IDirectedCollectionValue<System.Collections.Generic.KeyValuePair<K, V>> RangeAll();

IDirectedCollectionValue<SCG.KeyValuePair<K, V>> RangeAll();

//TODO: remove now that we assume that we can check the sorting order?
/// <summary>
Expand All @@ -213,24 +206,21 @@ public interface ISortedDictionary<K, V> : IDictionary<K, V>
/// <exception cref="ArgumentException"> if the enumerated items turns out
/// not to be in increasing order.</exception>
/// <param name="items">The collection to add.</param>
void AddSorted(IEnumerable<System.Collections.Generic.KeyValuePair<K, V>> items);

void AddSorted(SCG.IEnumerable<SCG.KeyValuePair<K, V>> items);

/// <summary>
/// Remove all items of this collection above or at a supplied threshold.
/// </summary>
/// <param name="low">The lower threshold (inclusive).</param>
void RemoveRangeFrom(K low);


/// <summary>
/// Remove all items of this collection between two supplied thresholds.
/// </summary>
/// <param name="low">The lower threshold (inclusive).</param>
/// <param name="hi">The upper threshold (exclusive).</param>
void RemoveRangeFromTo(K low, K hi);


/// <summary>
/// Remove all items of this collection below a supplied threshold.
/// </summary>
Expand Down
Loading

0 comments on commit 00b7326

Please sign in to comment.