Skip to content

Commit 3cad21e

Browse files
authored
Merge pull request #4 from rameel/immutable-array-extensions
Add ImmutableArrayExtensions class
2 parents 28dbfe4 + 0aec325 commit 3cad21e

3 files changed

Lines changed: 75 additions & 5 deletions

File tree

Ramstack.Structures.Tests/Collections/ArrayViewTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Immutable;
12
using System.Diagnostics.CodeAnalysis;
23
using System.Runtime.CompilerServices;
34

@@ -417,9 +418,15 @@ public void IndexOf()
417418
public void AsView(string? value)
418419
{
419420
var list = value?.ToCharArray();
421+
var immutable = value.AsSpan().ToImmutableArray();
422+
420423
Assert.That(
421424
list.AsView(),
422425
Is.EquivalentTo(value.AsSpan().ToArray()));
426+
427+
Assert.That(
428+
immutable.AsView(),
429+
Is.EquivalentTo(value.AsSpan().ToArray()));
423430
}
424431

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

436444
Assert.That(
437445
list.AsView(index),
438446
Is.EquivalentTo(value.AsSpan(index).ToArray()));
447+
448+
Assert.That(
449+
immutable.AsView(index),
450+
Is.EquivalentTo(value.AsSpan(index).ToArray()));
439451
}
440452

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

453466
Assert.That(
454467
list.AsView(index, length),
455468
Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
469+
470+
Assert.That(
471+
immutable.AsView(index, length),
472+
Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
456473
}
457474

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

466484
Assert.That(
467485
() => list.AsView(index),
468486
Throws.TypeOf<ArgumentOutOfRangeException>());
487+
488+
Assert.That(
489+
() => immutable.AsView(index),
490+
Throws.TypeOf<ArgumentOutOfRangeException>());
469491
}
470492

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

483506
Assert.That(
484507
() => list.AsView(index, length),
485508
Throws.TypeOf<ArgumentOutOfRangeException>());
509+
510+
Assert.That(
511+
() => immutable.AsView(index, length),
512+
Throws.TypeOf<ArgumentOutOfRangeException>());
486513
}
487514
}

Ramstack.Structures/Collections/ArrayViewExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static ArrayView<T> AsView<T>(this T[]? value) =>
2222
/// Creates a new <see cref="ArrayView{T}"/> over a portion of the target array from a specified position to the end of the array.
2323
/// </summary>
2424
/// <param name="value">The target array.</param>
25-
/// <param name="index">The index at which to begin this slice.</param>
25+
/// <param name="index">The index at which to begin the array view.</param>
2626
/// <returns>
2727
/// An <see cref="ArrayView{T}"/> representing the array.
2828
/// </returns>
@@ -34,14 +34,14 @@ public static ArrayView<T> AsView<T>(this T[]? value, int index) =>
3434
/// Creates a new <see cref="ArrayView{T}"/> over a portion of the target array from a specified position for a specified number of elements.
3535
/// </summary>
3636
/// <param name="value">The target array.</param>
37-
/// <param name="index">The index at which to begin this slice.</param>
38-
/// <param name="length">The desired length for the slice.</param>
37+
/// <param name="index">The index at which to begin the array view.</param>
38+
/// <param name="count">The number of items in the array view.</param>
3939
/// <returns>
4040
/// An <see cref="ArrayView{T}"/> representing the array.
4141
/// </returns>
4242
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43-
public static ArrayView<T> AsView<T>(this T[]? value, int index, int length) =>
44-
new(value ?? [], index, length);
43+
public static ArrayView<T> AsView<T>(this T[]? value, int index, int count) =>
44+
new(value ?? [], index, count);
4545

4646
/// <summary>
4747
/// Finds the index of the first occurrence of a specified value in this instance.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Immutable;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Ramstack.Collections;
5+
6+
/// <summary>
7+
/// Provides extension methods for the <see cref="ImmutableArray{T}"/> structure.
8+
/// </summary>
9+
public static class ImmutableArrayExtensions
10+
{
11+
/// <summary>
12+
/// Creates an <see cref="ArrayView{T}"/> over an immutable array.
13+
/// </summary>
14+
/// <param name="value">The immutable array to wrap.</param>
15+
/// <returns>
16+
/// The read-only view of the array.
17+
/// </returns>
18+
public static ArrayView<T> AsView<T>(this ImmutableArray<T> value) =>
19+
ImmutableCollectionsMarshal.AsArray(value).AsView();
20+
21+
/// <summary>
22+
/// Creates an <see cref="ArrayView{T}"/> over an immutable array starting at a specified position to the end of the array.
23+
/// </summary>
24+
/// <param name="value">The immutable array to wrap.</param>
25+
/// <param name="index">The index at which to begin the array view.</param>
26+
/// <returns>
27+
/// The read-only view of the array.
28+
/// </returns>
29+
public static ArrayView<T> AsView<T>(this ImmutableArray<T> value, int index) =>
30+
ImmutableCollectionsMarshal.AsArray(value).AsView(index);
31+
32+
/// <summary>
33+
/// Creates an <see cref="ArrayView{T}"/> over an immutable array starting at a specified position for a specified length.
34+
/// </summary>
35+
/// <param name="value">The immutable array to wrap.</param>
36+
/// <param name="index">The index at which to begin the array view.</param>
37+
/// <param name="count">The number of items in the array view.</param>
38+
/// <returns>
39+
/// The read-only view of the array.
40+
/// </returns>
41+
public static ArrayView<T> AsView<T>(this ImmutableArray<T> value, int index, int count) =>
42+
ImmutableCollectionsMarshal.AsArray(value).AsView(index, count);
43+
}

0 commit comments

Comments
 (0)