Skip to content
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

Add ImmutableArrayExtensions class #4

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Ramstack.Structures.Tests/Collections/ArrayViewTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -417,9 +418,15 @@ public void IndexOf()
public void AsView(string? value)
{
var list = value?.ToCharArray();
var immutable = value.AsSpan().ToImmutableArray();

Assert.That(
list.AsView(),
Is.EquivalentTo(value.AsSpan().ToArray()));

Assert.That(
immutable.AsView(),
Is.EquivalentTo(value.AsSpan().ToArray()));
}

[TestCase(null, 0)]
Expand All @@ -432,10 +439,15 @@ public void AsView(string? value)
public void AsView_Index(string? value, int index)
{
var list = value?.ToCharArray();
var immutable = value.AsSpan().ToImmutableArray();

Assert.That(
list.AsView(index),
Is.EquivalentTo(value.AsSpan(index).ToArray()));

Assert.That(
immutable.AsView(index),
Is.EquivalentTo(value.AsSpan(index).ToArray()));
}

[TestCase(null, 0, 0)]
Expand All @@ -449,10 +461,15 @@ public void AsView_Index(string? value, int index)
public void AsView_Range(string? value, int index, int length)
{
var list = value?.ToCharArray();
var immutable = value.AsSpan().ToImmutableArray();

Assert.That(
list.AsView(index, length),
Is.EquivalentTo(value.AsSpan(index, length).ToArray()));

Assert.That(
immutable.AsView(index, length),
Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
}

[TestCase(null, 1)]
Expand All @@ -462,10 +479,15 @@ public void AsView_Range(string? value, int index, int length)
public void AsView_InvalidIndex_ShouldThrow(string? value, int index)
{
var list = value?.ToCharArray();
var immutable = value.AsSpan().ToImmutableArray();

Assert.That(
() => list.AsView(index),
Throws.TypeOf<ArgumentOutOfRangeException>());

Assert.That(
() => immutable.AsView(index),
Throws.TypeOf<ArgumentOutOfRangeException>());
}

[TestCase(null, 1, 0)]
Expand All @@ -479,9 +501,14 @@ public void AsView_InvalidIndex_ShouldThrow(string? value, int index)
public void AsView_InvalidRange_ShouldThrow(string? value, int index, int length)
{
var list = value?.ToCharArray();
var immutable = value.AsSpan().ToImmutableArray();

Assert.That(
() => list.AsView(index, length),
Throws.TypeOf<ArgumentOutOfRangeException>());

Assert.That(
() => immutable.AsView(index, length),
Throws.TypeOf<ArgumentOutOfRangeException>());
}
}
10 changes: 5 additions & 5 deletions Ramstack.Structures/Collections/ArrayViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static ArrayView<T> AsView<T>(this T[]? value) =>
/// Creates a new <see cref="ArrayView{T}"/> over a portion of the target array from a specified position to the end of the array.
/// </summary>
/// <param name="value">The target array.</param>
/// <param name="index">The index at which to begin this slice.</param>
/// <param name="index">The index at which to begin the array view.</param>
/// <returns>
/// An <see cref="ArrayView{T}"/> representing the array.
/// </returns>
Expand All @@ -34,14 +34,14 @@ public static ArrayView<T> AsView<T>(this T[]? value, int index) =>
/// Creates a new <see cref="ArrayView{T}"/> over a portion of the target array from a specified position for a specified number of elements.
/// </summary>
/// <param name="value">The target array.</param>
/// <param name="index">The index at which to begin this slice.</param>
/// <param name="length">The desired length for the slice.</param>
/// <param name="index">The index at which to begin the array view.</param>
/// <param name="count">The number of items in the array view.</param>
/// <returns>
/// An <see cref="ArrayView{T}"/> representing the array.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ArrayView<T> AsView<T>(this T[]? value, int index, int length) =>
new(value ?? [], index, length);
public static ArrayView<T> AsView<T>(this T[]? value, int index, int count) =>
new(value ?? [], index, count);

/// <summary>
/// Finds the index of the first occurrence of a specified value in this instance.
Expand Down
43 changes: 43 additions & 0 deletions Ramstack.Structures/Collections/ImmutableArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Immutable;
using System.Runtime.InteropServices;

namespace Ramstack.Collections;

/// <summary>
/// Provides extension methods for the <see cref="ImmutableArray{T}"/> structure.
/// </summary>
public static class ImmutableArrayExtensions
{
/// <summary>
/// Creates an <see cref="ArrayView{T}"/> over an immutable array.
/// </summary>
/// <param name="value">The immutable array to wrap.</param>
/// <returns>
/// The read-only view of the array.
/// </returns>
public static ArrayView<T> AsView<T>(this ImmutableArray<T> value) =>
ImmutableCollectionsMarshal.AsArray(value).AsView();

/// <summary>
/// Creates an <see cref="ArrayView{T}"/> over an immutable array starting at a specified position to the end of the array.
/// </summary>
/// <param name="value">The immutable array to wrap.</param>
/// <param name="index">The index at which to begin the array view.</param>
/// <returns>
/// The read-only view of the array.
/// </returns>
public static ArrayView<T> AsView<T>(this ImmutableArray<T> value, int index) =>
ImmutableCollectionsMarshal.AsArray(value).AsView(index);

/// <summary>
/// Creates an <see cref="ArrayView{T}"/> over an immutable array starting at a specified position for a specified length.
/// </summary>
/// <param name="value">The immutable array to wrap.</param>
/// <param name="index">The index at which to begin the array view.</param>
/// <param name="count">The number of items in the array view.</param>
/// <returns>
/// The read-only view of the array.
/// </returns>
public static ArrayView<T> AsView<T>(this ImmutableArray<T> value, int index, int count) =>
ImmutableCollectionsMarshal.AsArray(value).AsView(index, count);
}
Loading