-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListExtens.cs
128 lines (118 loc) · 4.61 KB
/
ListExtens.cs
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
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Godot.CollectionsExtens.List;
public static partial class ListExt
{
/// <summary>
/// Assigns elements of another array into the array.
/// Resizes the array to match array.
/// Performs type conversions if the array is typed.
/// </summary>
/// <param name="p_item">Element.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Assign<T>(this List<T> p_arr, List<T> p_item)
{
p_arr.Clear();
p_arr.AddRange(p_item);
}
/// <summary>
/// Returns the last element of the array.
/// Prints an error and returns null if the array is empty.
/// Note: Calling this function is not the same as writing array[-1].
/// If the array is empty, accessing by index will pause project execution when running from the editor.
/// </summary>
/// <returns>The last element of the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Back<T>(this List<T> p_arr) => p_arr[^1];
/// <summary>
/// Returns the first element of the array.
/// Prints an error and returns null if the array is empty.
/// Note: Calling this function is not the same as writing array[0].
/// If the array is empty, accessing by index will pause project execution when running from the editor.
/// </summary>
/// <returns>The first element of the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Front<T>(this List<T> p_arr)
{
return p_arr[0];
}
/// <summary>
/// Removes and returns the element of the array at index position.
/// If negative, position is considered relative to the end of the array.
/// Leaves the array untouched and returns null if the array is empty or if it's accessed out of bounds.
/// An error message is printed when the array is accessed out of bounds, but not when the array is empty.
/// Note: On large arrays, this method can be slower than pop_back as it will reindex the array's elements
/// that are located after the removed element.
/// The larger the array and the lower the index of the removed element, the slower pop_at will be.
/// </summary>
/// <param name="position">Index position.</param>
/// <returns>The element of the array at index position.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T PopAt<T>(this List<T> p_arr, int position)
{
T element = p_arr[position];
p_arr.RemoveAt(position);
return element;
}
/// <summary>
/// Adds an element at the beginning of the array. See also push_back.
/// Note: On large arrays, this method is much slower than push_back as it will reindex all the array's elements every time it's called.
/// The larger the array, the slower push_front will be.
/// </summary>
/// <param name="item">Element.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PushFront<T>(this List<T> p_arr, T item)
{
p_arr.Insert(0, item);
}
/// <summary>
/// Appends an element at the end of the array. See also push_front.
/// </summary>
/// <param name="item">Element.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PushBack<T>(this List<T> p_arr, T item)
{
p_arr.Insert(p_arr.Count, item);
}
/// <summary>
/// Removes and returns the first element of the array.
/// Returns null if the array is empty, without printing an error message. See also pop_back.
/// Note: On large arrays,
/// this method is much slower than pop_back as it will reindex all the array's elements every time
/// it's called. The larger the array, the slower pop_front will be.
/// </summary>
/// <returns>The first element of the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T PopFront<T>(this List<T> p_arr)
{
T firstElement = p_arr[0];
p_arr.RemoveAt(0);
return firstElement;
}
/// <summary>
/// Removes and returns the last element of the array.
/// Returns null if the array is empty, without printing an error message.
/// See also pop_front.
/// </summary>
/// <returns>The last element of the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T PopBack<T>(this List<T> p_arr)
{
T element = p_arr[^1];
p_arr.RemoveAt(p_arr.Count - 1);
return element;
}
/// <summary>
/// Executes a provided function once for each array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="action"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (T item in items)
action(item);
}
}