10using System.Collections.Generic;
14 public static class ListAndArrayExtensions {
16 public static T[] Fill<T>(
this T[] array, T value) {
17 for (
int i = 0; i < array.Length; i++) {
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();
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++) {
39 public static List<T> Fill<T>(
this List<T> list, T value) {
40 for (
int i = 0; i < list.Count; i++) {
46 public static List<T> Fill<T>(
this List<T> list,
int count, T value) {
48 for (
int i = 0; i < count; i++) {
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();
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);
68 public static List<T> FillEach<T>(
this List<T> list,
int count, Func<T> generator) {
70 for (
int i = 0; i < count; i++) {
71 list.Add(generator());
76 public static List<T> FillEach<T>(
this List<T> list,
int count, Func<int, T> generator) {
78 for (
int i = 0; i < count; i++) {
79 list.Add(generator(i));
84 public static List<T> Append<T>(
this List<T> list,
int count, T value) {
85 for (
int i = 0; i < count; i++) {
91 public static T RemoveLast<T>(
this List<T> list) {
92 T last = list[list.Count - 1];
93 list.RemoveAt(list.Count - 1);
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();
111 public static void RemoveAtUnordered<T>(
this List<T> list,
int index) {
112 if (list.Count - 1 == index) {
115 list[index] = list.RemoveLast();
119 public static void InsertUnordered<T>(
this List<T> list,
int index, T element) {
120 list.Add(list[index]);
121 list[index] = element;
131 public static void RemoveAtMany<T>(
this List<T> list, List<int> sortedIndexes) {
132 if (sortedIndexes.Count == 0)
return;
135 if (sortedIndexes.Count == 1) {
136 list.RemoveAt(sortedIndexes[0]);
140 int to = sortedIndexes[0];
145 while (from == sortedIndexes[index]) {
149 if (index == sortedIndexes.Count) {
151 while (from < list.Count) {
152 list[to++] = list[from++];
156 list.RemoveRange(list.Count - index, index);
162 list[to++] = list[from++];
173 public static void InsertMany<T>(
this List<T> list, List<int> sortedIndexes, List<T> elements) {
174 if (sortedIndexes.Count == 0)
return;
176 if (sortedIndexes.Count == 1) {
177 list.Insert(sortedIndexes[0], elements[0]);
181 int from = list.Count - 1;
184 for (
int i = 0; i < sortedIndexes.Count; i++) {
185 list.Add(
default(T));
188 int to = list.Count - 1;
190 int index = sortedIndexes.Count - 1;
193 while (to == sortedIndexes[index]) {
194 list[to--] = elements[index--];
201 list[to--] = list[from--];