-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayExtension.cs
More file actions
94 lines (84 loc) · 3.08 KB
/
ArrayExtension.cs
File metadata and controls
94 lines (84 loc) · 3.08 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
using System;
using System.Collections.Generic;
using System.Text;
namespace RexStudios.CsharpExtensions
{
public static class ArrayExtension
{
/// <summary>
/// Sets all values.
/// </summary>
/// <typeparam name="T">The type of the elements of the array that will be modified.</typeparam>
/// <param name="array">The one-dimensional, zero-based array</param>
/// <param name="value">The value.</param>
/// <returns>A reference to the changed array.</returns>
public static T[] SetAllValues<T>(this T[] array, T value)
where T : struct
{
for (int i = 0; i < array.Length; i++)
{
array[i] = value;
}
return array;
}
/// <summary>
/// Get the array slice between the two indexes.
/// Inclusive for start index, exclusive for end index.
/// </summary>
/// <typeparam name="T">The type of the elements of the array.</typeparam>
/// <param name="array">The one-dimensional, zero-based array that will be sliced from.</param>
/// <param name="index">The start index.</param>
/// <param name="end">The end index. If end is negative, it is treated like length.</param>
/// <returns>The resulting array.</returns>
public static T[] Slice<T>(this T[] array, int index, int end)
{
// Handles negative ends
if (end < 0)
{
end = index - end - 1;
}
int len = end - index;
// Return new array
T[] res = new T[len];
for (int i = 0; i < len; i++)
{
res[i] = array[i + index];
}
return res;
}
/// <summary>
/// Checks if the Arrays are equal.
/// </summary>
/// <typeparam name="T">Array type.</typeparam>
/// <param name="array1">The <see cref="Array"/> that contains data to compare with.</param>
/// <param name="array2">The <see cref="Array"/> that contains data to compare to.</param>
/// <param name="index">A 32-bit integer that represents the index in the arrays at which comparing begins.</param>
/// <param name="length">A 32-bit integer that represents the number of elements to compare.</param>
/// <returns>
/// Returns <c>true</c> if all element match and <c>false</c> otherwise.
/// </returns>
public static bool ArrayEqual<T>(this T[] array1, T[] array2, int index, int length)
where T : IEquatable<T>
{
if (array1.Length != array2.Length)
{
return false;
}
for (int i = index; i < length; i++)
{
if (!array1[i].Equals(array2[i]))
{
return false;
}
}
return true;
}
/// <summary>
/// Returns empty T array.
/// </summary>
public static T[] Empty<T>()
{
return Array.Empty<T>();
}
}
}