dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

[API Proposal]: System.Collection.BitArray - New constructors to pass a ReadOnlySpan<T> where T is int, bool or byte #80263

Open jimmygilles opened 1 year ago

jimmygilles commented 1 year ago

Background and motivation

Hello,

The System.Collection.BitArray has a constructor that accepts an array of boolean, byte or int. As we now have ReadOnlySpan<byte>, ReadOnlySpan<bool> and ReadOnlySpan<int>, that would be great to add three new constructors to accept these span.

Currently we must do a ".ToArray" on the span.

API Proposal

namespace System.Collections;

public sealed class BitArray : ICollection, ICloneable    // So, the existing one
{
    public BitArray(ReadOnlySpan<byte> bytes);
    public BitArray(ReadOnlySpan<bool> values);
    public BitArray(ReadOnlySpan<int> values);
}

API Usage

ReadOnlySpan<int> myIntSpan = new[] { 1, 2, 3 }
var array1 = new BitArray(myIntSpan);

ReadOnlySpan<bool> myBoolSpan = new[] { true, false, true }
var array2 = new BitArray(myBoolSpan);

ReadOnlySpan<byte> myByteSpan = new[] { 5, 6, 7 }
var array3 = new BitArray(myByteSpan);

Alternative Designs

No response

Risks

No response

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-memory See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation Hello, The System.Collection.BitArray has a constructor that accepts an array of boolean, byte or int. As we now have ReadOnlySpan, ReadOnlySpan and ReadOnlySpan, that would be great to add three new constructors to accept those these span. Currently we must do a ".ToArray" on the span. ### API Proposal ```csharp namespace System.Collections; public sealed class BitArray : ICollection, ICloneable // So, the existing one { public BitArray(ReadOnlySpan bytes); public BitArray(ReadOnlySpan values); public BitArray(ReadOnlySpan values); } ``` ### API Usage ```csharp ReadOnlySpan myIntSpan = new[] { 1, 2, 3 } var array1 = new BitArray(myIntSpan); ReadOnlySpan myBoolSpan = new[] { true, false, true } var array2 = new BitArray(myBoolSpan); ReadOnlySpan myByteSpan = new[] { 5, 6, 7 } var array2 = new BitArray(myByteSpan); ``` ### Alternative Designs _No response_ ### Risks _No response_
Author: jimmygilles
Assignees: -
Labels: `api-suggestion`, `area-System.Memory`
Milestone: -
DrkWzrd commented 1 year ago

array1 should have 96 bits; array2 3 bits and array3 (there's a typo in the example) 24 bits, isn't it?

jimmygilles commented 1 year ago

(Sorry for the typo.) Yes, it should be 96, 3 and 24. In fact it must follow the same behaviour as the existing ones with array (int[], bool[], byte[]).

My request here is just to avoid an unnecessary "ToArray()" on the span.

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation Hello, The System.Collection.BitArray has a constructor that accepts an array of boolean, byte or int. As we now have `ReadOnlySpan`, `ReadOnlySpan` and `ReadOnlySpan`, that would be great to add three new constructors to accept these span. Currently we must do a ".ToArray" on the span. ### API Proposal ```csharp namespace System.Collections; public sealed class BitArray : ICollection, ICloneable // So, the existing one { public BitArray(ReadOnlySpan bytes); public BitArray(ReadOnlySpan values); public BitArray(ReadOnlySpan values); } ``` ### API Usage ```csharp ReadOnlySpan myIntSpan = new[] { 1, 2, 3 } var array1 = new BitArray(myIntSpan); ReadOnlySpan myBoolSpan = new[] { true, false, true } var array2 = new BitArray(myBoolSpan); ReadOnlySpan myByteSpan = new[] { 5, 6, 7 } var array3 = new BitArray(myByteSpan); ``` ### Alternative Designs _No response_ ### Risks _No response_
Author: jimmygilles
Assignees: -
Labels: `api-suggestion`, `area-System.Collections`, `untriaged`
Milestone: -