-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add ReadOnlySet<T> #103306
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
Merged
+290
−48
Merged
Add ReadOnlySet<T> #103306
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/libraries/System.Collections/src/System/Collections/Generic/ReadOnlySet.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace System.Collections.ObjectModel | ||
| { | ||
| /// <summary>Represents a read-only, generic set of values.</summary> | ||
| /// <typeparam name="T">The type of values in the set.</typeparam> | ||
| [DebuggerDisplay("Count = {Count}")] | ||
| public class ReadOnlySet<T> : IReadOnlySet<T>, ISet<T>, ICollection | ||
| { | ||
| /// <summary>The wrapped set.</summary> | ||
| private readonly ISet<T> _set; | ||
|
|
||
| /// <summary>Initializes a new instance of the <see cref="ReadOnlySet{T}"/> class that is a wrapper around the specified set.</summary> | ||
| /// <param name="set">The set to wrap.</param> | ||
| public ReadOnlySet(ISet<T> set) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(set); | ||
| _set = set; | ||
| } | ||
|
|
||
| /// <summary>Gets an empty <see cref="ReadOnlySet{T}"/>.</summary> | ||
| public static ReadOnlySet<T> Empty { get; } = new ReadOnlySet<T>(new HashSet<T>()); | ||
eiriktsarpalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary>Gets the set that is wrapped by this <see cref="ReadOnlySet{T}"/> object.</summary> | ||
| protected ISet<T> Set => _set; | ||
|
|
||
| /// <inheritdoc/> | ||
| public int Count => _set.Count; | ||
|
|
||
| /// <inheritdoc/> | ||
| public IEnumerator<T> GetEnumerator() => | ||
| _set.Count == 0 ? ((IEnumerable<T>)Array.Empty<T>()).GetEnumerator() : | ||
| _set.GetEnumerator(); | ||
|
|
||
| /// <inheritdoc/> | ||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Contains(T item) => _set.Contains(item); | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool IsProperSubsetOf(IEnumerable<T> other) => _set.IsProperSubsetOf(other); | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool IsProperSupersetOf(IEnumerable<T> other) => _set.IsProperSupersetOf(other); | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool IsSubsetOf(IEnumerable<T> other) => _set.IsSubsetOf(other); | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool IsSupersetOf(IEnumerable<T> other) => _set.IsSupersetOf(other); | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Overlaps(IEnumerable<T> other) => _set.Overlaps(other); | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool SetEquals(IEnumerable<T> other) => _set.SetEquals(other); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ICollection<T>.CopyTo(T[] array, int arrayIndex) => _set.CopyTo(array, arrayIndex); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ICollection.CopyTo(Array array, int index) => CollectionHelpers.CopyTo(_set, array, index); | ||
|
|
||
| /// <inheritdoc/> | ||
| bool ICollection<T>.IsReadOnly => true; | ||
|
|
||
| /// <inheritdoc/> | ||
| bool ICollection.IsSynchronized => false; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <inheritdoc/> | ||
| object ICollection.SyncRoot => _set is ICollection c ? c.SyncRoot : this; | ||
|
|
||
| /// <inheritdoc/> | ||
| bool ISet<T>.Add(T item) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ISet<T>.ExceptWith(IEnumerable<T> other) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ISet<T>.IntersectWith(IEnumerable<T> other) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ISet<T>.SymmetricExceptWith(IEnumerable<T> other) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ISet<T>.UnionWith(IEnumerable<T> other) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ICollection<T>.Add(T item) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| void ICollection<T>.Clear() => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| bool ICollection<T>.Remove(T item) => throw new NotSupportedException(); | ||
| } | ||
| } | ||
128 changes: 128 additions & 0 deletions
128
src/libraries/System.Collections/tests/Generic/ReadOnlySet/ReadOnlySetTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| namespace System.Collections.ObjectModel.Tests | ||
| { | ||
| public class ReadOnlySetTests | ||
| { | ||
| [Fact] | ||
| public void Ctor_NullSet_ThrowsArgumentNullException() | ||
| { | ||
| AssertExtensions.Throws<ArgumentNullException>("set", () => new ReadOnlySet<int>(null)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Ctor_SetProperty_Roundtrips() | ||
| { | ||
| var set = new HashSet<int>(); | ||
| Assert.Same(set, new DerivedReadOnlySet<int>(set).Set); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Empty_EmptyAndIdempotent() | ||
| { | ||
| Assert.Same(ReadOnlySet<int>.Empty, ReadOnlySet<int>.Empty); | ||
| Assert.Empty(ReadOnlySet<int>.Empty); | ||
| Assert.Same(ReadOnlySet<int>.Empty.GetEnumerator(), ReadOnlySet<int>.Empty.GetEnumerator()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MembersDelegateToWrappedSet() | ||
| { | ||
| var set = new ReadOnlySet<int>(new HashSet<int>() { 1, 2, 3 }); | ||
|
|
||
| Assert.True(set.Contains(2)); | ||
| Assert.False(set.Contains(4)); | ||
|
|
||
| Assert.Equal(3, set.Count); | ||
|
|
||
| Assert.True(set.IsProperSubsetOf([1, 2, 3, 4])); | ||
| Assert.False(set.IsProperSubsetOf([1, 2, 5])); | ||
|
|
||
| Assert.True(set.IsProperSupersetOf([1, 2])); | ||
| Assert.False(set.IsProperSupersetOf([1, 4])); | ||
|
|
||
| Assert.True(set.IsSubsetOf([1, 2, 3, 4])); | ||
| Assert.False(set.IsSubsetOf([1, 2, 5])); | ||
|
|
||
| Assert.True(set.IsSupersetOf([1, 2])); | ||
| Assert.False(set.IsSupersetOf([1, 4])); | ||
|
|
||
| Assert.True(set.Overlaps([-1, 0, 1])); | ||
| Assert.False(set.Overlaps([-1, 0])); | ||
|
|
||
| Assert.True(set.SetEquals([1, 2, 3])); | ||
| Assert.False(set.SetEquals([1, 2, 4])); | ||
|
|
||
| int[] result = new int[3]; | ||
| ((ICollection<int>)set).CopyTo(result, 0); | ||
| Assert.Equal(result, new int[] { 1, 2, 3 }); | ||
|
|
||
| Array.Clear(result); | ||
| ((ICollection)set).CopyTo(result, 0); | ||
| Assert.Equal(result, new int[] { 1, 2, 3 }); | ||
|
|
||
| Assert.NotNull(set.GetEnumerator()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ChangesToUnderlyingSetReflected() | ||
| { | ||
| var set = new HashSet<int> { 1, 2, 3 }; | ||
| var readOnlySet = new ReadOnlySet<int>(set); | ||
|
|
||
| set.Add(4); | ||
| Assert.Equal(4, readOnlySet.Count); | ||
| Assert.True(readOnlySet.Contains(4)); | ||
|
|
||
| set.Remove(2); | ||
| Assert.Equal(3, readOnlySet.Count); | ||
| Assert.False(readOnlySet.Contains(2)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsReadOnly_True() | ||
| { | ||
| var set = new ReadOnlySet<int>(new HashSet<int> { 1, 2, 3 }); | ||
| Assert.True(((ICollection<int>)set).IsReadOnly); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MutationThrows_CollectionUnmodified() | ||
| { | ||
| var set = new HashSet<int> { 1, 2, 3 }; | ||
| var readOnlySet = new ReadOnlySet<int>(set); | ||
|
|
||
| Assert.Throws<NotSupportedException>(() => ((ICollection<int>)readOnlySet).Add(4)); | ||
| Assert.Throws<NotSupportedException>(() => ((ICollection<int>)readOnlySet).Remove(1)); | ||
| Assert.Throws<NotSupportedException>(() => ((ICollection<int>)readOnlySet).Clear()); | ||
|
|
||
| Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).Add(4)); | ||
| Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).ExceptWith([1, 2, 3])); | ||
| Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).IntersectWith([1, 2, 3])); | ||
| Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).SymmetricExceptWith([1, 2, 3])); | ||
| Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).UnionWith([1, 2, 3])); | ||
|
|
||
| Assert.Equal(3, set.Count); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ICollection_Synchronization() | ||
| { | ||
| var set = new ReadOnlySet<int>(new HashSet<int> { 1, 2, 3 }); | ||
|
|
||
| Assert.False(((ICollection)set).IsSynchronized); | ||
| Assert.Same(set, ((ICollection)set).SyncRoot); | ||
| } | ||
|
|
||
| private class DerivedReadOnlySet<T> : ReadOnlySet<T> | ||
| { | ||
| public DerivedReadOnlySet(HashSet<T> set) : base(set) { } | ||
|
|
||
| public new ISet<T> Set => base.Set; | ||
| } | ||
| } | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.