-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadOnlyArrayExtensions.cs
More file actions
90 lines (81 loc) · 3.91 KB
/
ReadOnlyArrayExtensions.cs
File metadata and controls
90 lines (81 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
namespace Ramstack.Collections;
/// <summary>
/// Provides extension methods for the <see cref="ReadOnlyArray{T}"/> class.
/// </summary>
public static partial class ReadOnlyArrayExtensions
{
/// <summary>
/// Creates a read-only array from the specified read-only span.
/// </summary>
/// <typeparam name="T">The type of element in the array.</typeparam>
/// <param name="items">The read-only span to create a read-only from.</param>
/// <returns>
/// A read-only array containing the specified items.
/// </returns>
public static ReadOnlyArray<T> ToReadOnlyArray<T>(this Span<T> items) =>
new(items);
/// <summary>
/// Creates a read-only array from the specified read-only span.
/// </summary>
/// <typeparam name="T">The type of element in the array.</typeparam>
/// <param name="items">The read-only span to create a read-only from.</param>
/// <returns>
/// A read-only array containing the specified items.
/// </returns>
public static ReadOnlyArray<T> ToReadOnlyArray<T>(this ReadOnlySpan<T> items) =>
new(items);
/// <summary>
/// Enumerates a sequence exactly once and produces a read-only array of its contents.
/// </summary>
/// <typeparam name="T">The type of element in the sequence.</typeparam>
/// <param name="items">The array to create a read-only from.</param>
/// <returns>
/// A read-only array containing the specified items.
/// </returns>
public static ReadOnlyArray<T> ToReadOnlyArray<T>(this T[] items) =>
ReadOnlyArray.Create(items);
/// <summary>
/// Enumerates a sequence exactly once and produces a read-only array of its contents.
/// </summary>
/// <typeparam name="T">The type of element in the array.</typeparam>
/// <param name="items">The sequence to create a read-only from.</param>
/// <returns>
/// A read-only array containing the specified items.
/// </returns>
[SuppressMessage("ReSharper", "OperatorIsCanBeUsed")]
public static ReadOnlyArray<T> ToReadOnlyArray<T>([InstantHandle] this IEnumerable<T> items)
{
if (items.GetType() == typeof(ReadOnlyArray<T>))
return (ReadOnlyArray<T>)items;
if (items.GetType() == typeof(ImmutableArray<T>))
return (ImmutableArray<T>)items;
if (items.GetType() == typeof(T[]))
return ReadOnlyArray.Create(Unsafe.As<IEnumerable<T>, T[]>(ref items));
return new ReadOnlyArray<T>(items.ToArray());
}
/// <summary>
/// Searches for the specified object and returns the index of its first occurrence.
/// </summary>
/// <typeparam name="T">The type of element in the array.</typeparam>
/// <param name="array">The <see cref="ReadOnlyArray{T}"/> instance.</param>
/// <param name="value">The object to locate in array.</param>
/// <returns>
/// The zero-based index of the first occurrence of <paramref name="value"/>
/// in the <paramref name="array"/>, if found; otherwise, <c>-1</c>.
/// </returns>
public static int IndexOf<T>(this ReadOnlyArray<T> array, T value) =>
Array.IndexOf(array.Inner!, value);
/// <summary>
/// Searches for the specified object and returns the index
/// of the last occurrence within the entire <see cref="ReadOnlyArray{T}"/>.
/// </summary>
/// <typeparam name="T">The type of element in the array.</typeparam>
/// <param name="array">The <see cref="ReadOnlyArray{T}"/> instance.</param>
/// <param name="value">The object to locate in array.</param>
/// <returns>
/// The zero-based index of the last occurrence of <paramref name="value"/>
/// in the <paramref name="array"/>, if found; otherwise, <c>-1</c>.
/// </returns>
public static int LastIndexOf<T>(this ReadOnlyArray<T> array, T value) =>
Array.LastIndexOf(array.Inner!, value);
}