-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtil.cs
More file actions
121 lines (105 loc) · 4.46 KB
/
ArrayUtil.cs
File metadata and controls
121 lines (105 loc) · 4.46 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
namespace VT2AssetLib;
public static class ArrayUtil
{
/// <inheritdoc cref="Create{T}(uint)"/>
/// <exception cref="ArgumentOutOfRangeException"/>
public static T[] Create<T>(int length)
{
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length));
return Create<T>((uint)length);
}
/// <summary>
/// Creates and returns an array of the specified length.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="length">The length of the array to create.</param>
/// <returns>
/// A new array with a length equal to <paramref name="length"/>,
/// or <see cref="Array.Empty{T}"/> if <paramref name="length"/> is zero.
/// </returns>
public static T[] Create<T>(uint length)
{
if (length == 0)
return Array.Empty<T>();
return new T[length];
}
/// <inheritdoc cref="CreateAndPopulate{T}(uint)"/>
/// <exception cref="ArgumentOutOfRangeException"/>
public static T[] CreateAndPopulate<T>(int length)
where T : new()
{
return CreateAndPopulate(length, () => new T());
}
/// <summary>
/// Creates and returns an array of the specified length, initializing each element by
/// using the public parameterless constructor of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="length">The length of the array to create.</param>
/// <returns>
/// A new array with each value initialized using the default parameterless constructor of type <typeparamref name="T"/>,
/// or <see cref="Array.Empty{T}"/> if <paramref name="length"/> is zero.
/// </returns>
public static T[] CreateAndPopulate<T>(uint length)
where T : new()
{
return CreateAndPopulate(length, () => new T());
}
/// <inheritdoc cref="CreateAndPopulate{T}(uint, T)"/>
/// <exception cref="ArgumentOutOfRangeException"/>
public static T[] CreateAndPopulate<T>(int length, T value)
{
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length));
return CreateAndPopulate((uint)length, value);
}
/// <summary>
/// Creates and returns an array of the specified length, setting each value in the array
/// to <paramref name="value"/>.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="length">The length of the array to create.</param>
/// <param name="value">The value to initialize each index to.</param>
/// <returns>
/// A new array with each value equal to <paramref name="value"/>,
/// or <see cref="Array.Empty{T}"/> if <paramref name="length"/> is zero.
/// </returns>
public static T[] CreateAndPopulate<T>(uint length, T value)
{
if (length == 0)
return Array.Empty<T>();
T[] result = new T[length];
Array.Fill(result, value);
return result;
}
/// <inheritdoc cref="CreateAndPopulate{T}(uint, Func{T})"/>
/// <exception cref="ArgumentOutOfRangeException"/>
public static T[] CreateAndPopulate<T>(int length, Func<T> valueConstructor)
{
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length));
return CreateAndPopulate((uint)length, valueConstructor);
}
/// <summary>
/// Creates and returns an array of the specified length, intializing every index in the array
/// to the value returned by <paramref name="valueConstructor"/>.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="length">The length of the array to create.</param>
/// <param name="valueConstructor">A method to initialize each index of the array with.</param>
/// <returns>
/// A new array with each value initialized using <paramref name="valueConstructor"/>,
/// or <see cref="Array.Empty{T}"/> if <paramref name="length"/> is zero.
/// </returns>
public static T[] CreateAndPopulate<T>(uint length, Func<T> valueConstructor)
{
ArgumentNullException.ThrowIfNull(valueConstructor);
if (length == 0)
return Array.Empty<T>();
T[] result = new T[length];
for (int i = 0; i < length; i++)
result[i] = valueConstructor();
return result;
}
}