Skip to content

Commit

Permalink
TO REVERT: Removed breaking binary API changes for Dictionary<TKey, T…
Browse files Browse the repository at this point in the history
…Value>, SortedDictionary<TKey, TValue>, and SortedSet<T>. This commit will need to be reverted in a major version bump to match the BCL APIs.
  • Loading branch information
NightOwl888 committed Oct 20, 2024
1 parent 4461f92 commit 2f87965
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/J2N/Collections/Generic/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ public bool TryAdd([AllowNull] TKey key, [AllowNull] TValue value) =>
/// <para/>
/// Getting the value of this property is an O(1) operation.
/// </remarks>
public KeyCollection Keys => _keys ??= new KeyCollection(this);
public ICollection<TKey> Keys => _keys ??= new KeyCollection(this);

ICollection<TKey> IDictionary<TKey, TValue>.Keys => Keys;

Expand All @@ -854,7 +854,7 @@ public bool TryAdd([AllowNull] TKey key, [AllowNull] TValue value) =>
/// <para/>
/// Getting the value of this property is an O(1) operation.
/// </remarks>
public ValueCollection Values => _values ??= new ValueCollection(this);
public ICollection<TValue> Values => _values ??= new ValueCollection(this);

ICollection<TValue> IDictionary<TKey, TValue>.Values => Values;

Expand Down Expand Up @@ -1404,7 +1404,7 @@ public bool Remove([AllowNull] TKey key, [MaybeNullWhen(false)] out TValue value
/// <para/>
/// This method is an O(1) operation.
/// </remarks>
public Enumerator GetEnumerator() => new Enumerator(this, Enumerator.KeyValuePair);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => new Enumerator(this, Enumerator.KeyValuePair);

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() =>
Count == 0 ? GenericEmptyEnumerator<KeyValuePair<TKey, TValue>>.Instance :
Expand All @@ -1418,9 +1418,9 @@ IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.

bool IDictionary.IsFixedSize => false;

ICollection IDictionary.Keys => Keys;
ICollection IDictionary.Keys => (ICollection)Keys;

ICollection IDictionary.Values => Values;
ICollection IDictionary.Values => (ICollection)Values;

bool IDictionary.IsReadOnly => false;

Expand Down
10 changes: 5 additions & 5 deletions src/J2N/Collections/Generic/SortedDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public IComparer<TKey> Comparer
/// <para/>
/// Getting the value of this property is an O(1) operation.
/// </remarks>
public KeyCollection Keys => _keys ??= new KeyCollection(this);
public ICollection<TKey> Keys => _keys ??= new KeyCollection(this);

ICollection<TKey> IDictionary<TKey, TValue>.Keys => Keys;

Expand All @@ -384,7 +384,7 @@ public IComparer<TKey> Comparer
/// <para/>
/// Getting the value of this property is an O(1) operation.
/// </remarks>
public ValueCollection Values => _values ??= new ValueCollection(this);
public ICollection<TValue> Values => _values ??= new ValueCollection(this);

ICollection<TValue> IDictionary<TKey, TValue>.Values => Values;

Expand Down Expand Up @@ -724,12 +724,12 @@ bool IDictionary.IsReadOnly

ICollection IDictionary.Keys
{
get { return Keys; }
get { return (ICollection)Keys; }
}

ICollection IDictionary.Values
{
get { return Values; }
get { return (ICollection)Values; }
}

object? IDictionary.this[object? key]
Expand Down Expand Up @@ -1038,7 +1038,7 @@ public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDictionaryE

internal Enumerator(SortedDictionary<TKey, TValue> dictionary, int getEnumeratorRetType)
{
_treeEnum = dictionary._set.GetEnumerator();
_treeEnum = dictionary._set.GetEnumeratorInternal();
_getEnumeratorRetType = getEnumeratorRetType;
}

Expand Down
16 changes: 9 additions & 7 deletions src/J2N/Collections/Generic/SortedSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,9 @@ void ICollection.CopyTo(Array array, int index)
/// <para/>
/// This method is an <c>O(log n)</c> operation.
/// </remarks>
public Enumerator GetEnumerator() => new Enumerator(this);
public IEnumerator<T> GetEnumerator() => new Enumerator(this);

internal Enumerator GetEnumeratorInternal() => new Enumerator(this);

IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();

Expand Down Expand Up @@ -1203,8 +1205,8 @@ public void UnionWith(IEnumerable<T> other)
// First do a merge sort to an array.
T[] merged = new T[asSorted.Count + this.Count];
int c = 0;
Enumerator mine = this.GetEnumerator();
Enumerator theirs = asSorted.GetEnumerator();
IEnumerator<T> mine = this.GetEnumerator();
IEnumerator<T> theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext(), theirsEnded = !theirs.MoveNext();
while (!mineEnded && !theirsEnded)
{
Expand Down Expand Up @@ -1359,8 +1361,8 @@ public virtual void IntersectWith(IEnumerable<T> other)
// First do a merge sort to an array.
T[] merged = new T[this.Count];
int c = 0;
Enumerator mine = this.GetEnumerator();
Enumerator theirs = asSorted.GetEnumerator();
IEnumerator<T> mine = this.GetEnumerator();
IEnumerator<T> theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext(), theirsEnded = !theirs.MoveNext();
T? max = Max;

Expand Down Expand Up @@ -1782,8 +1784,8 @@ public bool SetEquals(IEnumerable<T> other)
SortedSet<T>? asSorted = other as SortedSet<T>;
if (asSorted != null && HasEqualComparer(asSorted))
{
Enumerator mine = GetEnumerator();
Enumerator theirs = asSorted.GetEnumerator();
IEnumerator<T> mine = GetEnumerator();
IEnumerator<T> theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext();
bool theirsEnded = !theirs.MoveNext();
while (!mineEnded && !theirsEnded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class Dictionary_Generic_Tests_Keys_AsICollection : ICollection_NonGeneri

protected override ICollection NonGenericICollectionFactory()
{
return new JCG.Dictionary<string, string>().Keys;
return (ICollection)new JCG.Dictionary<string, string>().Keys;
}

protected override ICollection NonGenericICollectionFactory(int count)
Expand All @@ -84,7 +84,7 @@ protected override ICollection NonGenericICollectionFactory(int count)
int seed = 13453;
for (int i = 0; i < count; i++)
list.Add(CreateT(seed++), CreateT(seed++));
return list.Keys;
return (ICollection)list.Keys;
}

private string CreateT(int seed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class Dictionary_Generic_Tests_Values_AsICollection : ICollection_NonGene

protected override ICollection NonGenericICollectionFactory()
{
return new JCG.Dictionary<string, string>().Values;
return (ICollection)new JCG.Dictionary<string, string>().Values;
}

protected override ICollection NonGenericICollectionFactory(int count)
Expand All @@ -88,7 +88,7 @@ protected override ICollection NonGenericICollectionFactory(int count)
int seed = 13453;
for (int i = 0; i < count; i++)
list.Add(CreateT(seed++), CreateT(seed++));
return list.Values;
return (ICollection)list.Values;
}

private string CreateT(int seed)
Expand Down

0 comments on commit 2f87965

Please sign in to comment.