Tanoda
ListAndArrayExtensions.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11
12namespace Leap.Unity {
13
14 public static class ListAndArrayExtensions {
15
16 public static T[] Fill<T>(this T[] array, T value) {
17 for (int i = 0; i < array.Length; i++) {
18 array[i] = value;
19 }
20 return array;
21 }
22
23 public static T[] Fill<T>(this T[] array, Func<T> constructor) {
24 for (int i = 0; i < array.Length; i++) {
25 array[i] = constructor();
26 }
27 return array;
28 }
29
30 public static T[,] Fill<T>(this T[,] array, T value) {
31 for (int i = 0; i < array.GetLength(0); i++) {
32 for (int j = 0; j < array.GetLength(1); j++) {
33 array[i, j] = value;
34 }
35 }
36 return array;
37 }
38
39 public static List<T> Fill<T>(this List<T> list, T value) {
40 for (int i = 0; i < list.Count; i++) {
41 list[i] = value;
42 }
43 return list;
44 }
45
46 public static List<T> Fill<T>(this List<T> list, int count, T value) {
47 list.Clear();
48 for (int i = 0; i < count; i++) {
49 list.Add(value);
50 }
51 return list;
52 }
53
54 public static List<T> FillEach<T>(this List<T> list, Func<T> generator) {
55 for (int i = 0; i < list.Count; i++) {
56 list[i] = generator();
57 }
58 return list;
59 }
60
61 public static List<T> FillEach<T>(this List<T> list, Func<int, T> generator) {
62 for (int i = 0; i < list.Count; i++) {
63 list[i] = generator(i);
64 }
65 return list;
66 }
67
68 public static List<T> FillEach<T>(this List<T> list, int count, Func<T> generator) {
69 list.Clear();
70 for (int i = 0; i < count; i++) {
71 list.Add(generator());
72 }
73 return list;
74 }
75
76 public static List<T> FillEach<T>(this List<T> list, int count, Func<int, T> generator) {
77 list.Clear();
78 for (int i = 0; i < count; i++) {
79 list.Add(generator(i));
80 }
81 return list;
82 }
83
84 public static List<T> Append<T>(this List<T> list, int count, T value) {
85 for (int i = 0; i < count; i++) {
86 list.Add(value);
87 }
88 return list;
89 }
90
91 public static T RemoveLast<T>(this List<T> list) {
92 T last = list[list.Count - 1];
93 list.RemoveAt(list.Count - 1);
94 return last;
95 }
96
101 public static bool RemoveUnordered<T>(this List<T> list, T element) {
102 for (int i = 0; i < list.Count; i++) {
103 if (list[i].Equals(element)) {
104 list[i] = list.RemoveLast();
105 return true;
106 }
107 }
108 return false;
109 }
110
111 public static void RemoveAtUnordered<T>(this List<T> list, int index) {
112 if (list.Count - 1 == index) {
113 list.RemoveLast();
114 } else {
115 list[index] = list.RemoveLast();
116 }
117 }
118
119 public static void InsertUnordered<T>(this List<T> list, int index, T element) {
120 list.Add(list[index]);
121 list[index] = element;
122 }
123
131 public static void RemoveAtMany<T>(this List<T> list, List<int> sortedIndexes) {
132 if (sortedIndexes.Count == 0) return;
133
134 //If just removing one, might as well use built-in function
135 if (sortedIndexes.Count == 1) {
136 list.RemoveAt(sortedIndexes[0]);
137 return;
138 }
139
140 int to = sortedIndexes[0];
141 int from = to;
142 int index = 0;
143
144 while (true) {
145 while (from == sortedIndexes[index]) {
146 from++;
147 index++;
148
149 if (index == sortedIndexes.Count) {
150 //Copy remaining
151 while (from < list.Count) {
152 list[to++] = list[from++];
153 }
154
155 //Remove the last elements on the end
156 list.RemoveRange(list.Count - index, index);
157
158 return;
159 }
160 }
161
162 list[to++] = list[from++];
163 }
164 }
165
173 public static void InsertMany<T>(this List<T> list, List<int> sortedIndexes, List<T> elements) {
174 if (sortedIndexes.Count == 0) return;
175
176 if (sortedIndexes.Count == 1) {
177 list.Insert(sortedIndexes[0], elements[0]);
178 return;
179 }
180
181 int from = list.Count - 1;
182
183 //First expand array to be large enough to hold all the new elements
184 for (int i = 0; i < sortedIndexes.Count; i++) {
185 list.Add(default(T));
186 }
187
188 int to = list.Count - 1;
189
190 int index = sortedIndexes.Count - 1;
191
192 while (true) {
193 while (to == sortedIndexes[index]) {
194 list[to--] = elements[index--];
195
196 if (index == -1) {
197 return;
198 }
199 }
200
201 list[to--] = list[from--];
202 }
203 }
204 }
205}