-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add specialized FrozenSet implementations for byte and char #110842
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
327ff08
Add specialized FrozenSet implementations for byte and char
MihaZupan 913a204
Remove unnecessary partial from new types
MihaZupan 41e7725
Use field for Items arrays
MihaZupan 94143ae
Merge branch 'main' into frozenset-bytechar
MihaZupan 2479f50
Merge branch 'main' into frozenset-bytechar
MihaZupan 704e7b7
Merge branch 'main' into frozenset-bytechar
MihaZupan 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
67 changes: 67 additions & 0 deletions
67
...ibraries/System.Collections.Immutable/src/System/Collections/Frozen/Byte/ByteFrozenSet.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,67 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace System.Collections.Frozen | ||
| { | ||
| /// <summary>Provides a frozen set to use when the values are <see cref="byte"/>s and the default comparer is used.</summary> | ||
| internal sealed partial class ByteFrozenSet : FrozenSetInternalBase<byte, ByteFrozenSet.GSW> | ||
| { | ||
| private readonly BitVector256 _values; | ||
| private readonly int _count; | ||
| private byte[]? _items; | ||
|
|
||
| internal ByteFrozenSet(ReadOnlySpan<byte> values) : base(EqualityComparer<byte>.Default) | ||
| { | ||
| Debug.Assert(!values.IsEmpty); | ||
|
|
||
| foreach (byte b in values) | ||
| { | ||
| _values.Set(b); | ||
| } | ||
|
|
||
| _count = _values.Count(); | ||
| } | ||
|
|
||
| internal ByteFrozenSet(IEnumerable<byte> values) : base(EqualityComparer<byte>.Default) | ||
| { | ||
| foreach (byte b in values) | ||
| { | ||
| _values.Set(b); | ||
| } | ||
|
|
||
| _count = _values.Count(); | ||
|
|
||
| Debug.Assert(_count > 0); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override byte[] ItemsCore => _items ??= _values.GetByteValues(); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override Enumerator GetEnumeratorCore() => new Enumerator(ItemsCore); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override int CountCore => _count; | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override int FindItemIndex(byte item) => _values.IndexOf(item); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override bool ContainsCore(byte item) => _values.Contains(item); | ||
|
|
||
| internal struct GSW : IGenericSpecializedWrapper | ||
| { | ||
| private ByteFrozenSet _set; | ||
| public void Store(FrozenSet<byte> set) => _set = (ByteFrozenSet)set; | ||
|
|
||
| public int Count => _set.Count; | ||
| public IEqualityComparer<byte> Comparer => _set.Comparer; | ||
| public int FindItemIndex(byte item) => _set.FindItemIndex(item); | ||
| public Enumerator GetEnumerator() => _set.GetEnumerator(); | ||
| } | ||
| } | ||
| } | ||
65 changes: 65 additions & 0 deletions
65
...es/System.Collections.Immutable/src/System/Collections/Frozen/Char/Latin1CharFrozenSet.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,65 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace System.Collections.Frozen | ||
| { | ||
| /// <summary>Provides a frozen set to use when the values are <see cref="char"/>s, the default comparer is used, and all values are <= 255.</summary> | ||
| /// <remarks> | ||
| /// This should practically always be used with ASCII-only values, but since we're using the <see cref="BitVector256"/> helper, | ||
| /// we might as well use it for values between 128-255 too (hence Latin1 instead of Ascii in the name). | ||
| /// </remarks> | ||
| internal sealed partial class Latin1CharFrozenSet : FrozenSetInternalBase<char, Latin1CharFrozenSet.GSW> | ||
| { | ||
| private readonly BitVector256 _values; | ||
| private readonly int _count; | ||
| private char[]? _items; | ||
|
|
||
| internal Latin1CharFrozenSet(ReadOnlySpan<char> values) : base(EqualityComparer<char>.Default) | ||
| { | ||
| Debug.Assert(!values.IsEmpty); | ||
|
|
||
| foreach (char c in values) | ||
| { | ||
| if (c > 255) | ||
| { | ||
| // Source was modified concurrent with the call to FrozenSet.Create. | ||
| ThrowHelper.ThrowInvalidOperationException(); | ||
| } | ||
|
|
||
| _values.Set(c); | ||
| } | ||
|
|
||
| _count = _values.Count(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override char[] ItemsCore => _items ??= _values.GetCharValues(); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override Enumerator GetEnumeratorCore() => new Enumerator(ItemsCore); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override int CountCore => _count; | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override int FindItemIndex(char item) => _values.IndexOf(item); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override bool ContainsCore(char item) => _values.Contains256(item); | ||
|
|
||
| internal struct GSW : IGenericSpecializedWrapper | ||
| { | ||
| private Latin1CharFrozenSet _set; | ||
| public void Store(FrozenSet<char> set) => _set = (Latin1CharFrozenSet)set; | ||
|
|
||
| public int Count => _set.Count; | ||
| public IEqualityComparer<char> Comparer => _set.Comparer; | ||
| public int FindItemIndex(char item) => _set.FindItemIndex(item); | ||
| public Enumerator GetEnumerator() => _set.GetEnumerator(); | ||
| } | ||
| } | ||
| } |
83 changes: 83 additions & 0 deletions
83
...stem.Collections.Immutable/src/System/Collections/Frozen/Char/PerfectHashCharFrozenSet.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,83 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace System.Collections.Frozen | ||
| { | ||
| /// <summary>Provides a frozen set to use when the values are <see cref="char"/>s, the default comparer is used, and some values are >= 255.</summary> | ||
| /// <remarks> | ||
| /// This is logically similar to using a <see cref="FrozenHashTable"/>, but where that is bucketized, | ||
| /// <see cref="PerfectHashLookup"/> is not, so we can avoid some indirection during lookups. | ||
| /// The latter is also specialized for <see cref="char"/> values, with lower memory consumption and a slightly cheaper FastMod. | ||
| /// </remarks> | ||
| internal sealed partial class PerfectHashCharFrozenSet : FrozenSetInternalBase<char, PerfectHashCharFrozenSet.GSW> | ||
| { | ||
| private readonly uint _multiplier; | ||
| private readonly char[] _hashEntries; | ||
| private char[]? _items; | ||
|
|
||
| internal PerfectHashCharFrozenSet(ReadOnlySpan<char> values) : base(EqualityComparer<char>.Default) | ||
| { | ||
| Debug.Assert(!values.IsEmpty); | ||
|
|
||
| int max = 0; | ||
| foreach (char c in values) | ||
| { | ||
| max = Math.Max(max, c); | ||
| } | ||
|
|
||
| PerfectHashLookup.Initialize(values, max, out _multiplier, out _hashEntries); | ||
| } | ||
|
|
||
| private char[] AllocateItemsArray() | ||
| { | ||
| var set = new HashSet<char>(_hashEntries); | ||
| char[] items = new char[set.Count]; | ||
| set.CopyTo(items); | ||
| _items = items; | ||
| return items; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override char[] ItemsCore => _items ?? AllocateItemsArray(); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override Enumerator GetEnumeratorCore() => new Enumerator(ItemsCore); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override int CountCore => ItemsCore.Length; | ||
|
|
||
| /// <inheritdoc /> | ||
| /// <remarks> | ||
| /// This is an internal helper where results are not exposed to the user. | ||
| /// The returned index does not have to correspond to the value in the <see cref="ItemsCore"/> array. | ||
| /// In this case, calculating the real index would be costly, so we return the offset into <see cref="_hashEntries"/> instead. | ||
| /// </remarks> | ||
| private protected override int FindItemIndex(char item) => PerfectHashLookup.IndexOf(_hashEntries, _multiplier, item); | ||
|
|
||
| /// <inheritdoc /> | ||
| private protected override bool ContainsCore(char item) => PerfectHashLookup.Contains(_hashEntries, _multiplier, item); | ||
|
|
||
| /// <inheritdoc /> | ||
| /// <remarks> | ||
| /// We're overriding this method to account for the fact that the indexes returned by <see cref="FindItemIndex(char)"/> | ||
| /// are based on <see cref="_hashEntries"/> instead of <see cref="ItemsCore"/>. | ||
| /// </remarks> | ||
| private protected override KeyValuePair<int, int> CheckUniqueAndUnfoundElements(IEnumerable<char> other, bool returnIfUnfound) => | ||
PranavSenthilnathan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| CheckUniqueAndUnfoundElements(other, returnIfUnfound, _hashEntries.Length); | ||
|
|
||
| internal struct GSW : IGenericSpecializedWrapper | ||
| { | ||
| private PerfectHashCharFrozenSet _set; | ||
| public void Store(FrozenSet<char> set) => _set = (PerfectHashCharFrozenSet)set; | ||
|
|
||
| public int Count => _set.Count; | ||
| public IEqualityComparer<char> Comparer => _set.Comparer; | ||
| public int FindItemIndex(char item) => _set.FindItemIndex(item); | ||
| public Enumerator GetEnumerator() => _set.GetEnumerator(); | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.