forked from izrik/ChamberLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.cs
More file actions
143 lines (130 loc) · 4.38 KB
/
Collection.cs
File metadata and controls
143 lines (130 loc) · 4.38 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// ChamberLib, a cross-platform game engine
// Copyright (C) 2021 izrik and Metaphysics Industries, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
// USA
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChamberLib
{
public static class Collection
{
public static void AddRange<T>(ICollection<T> collection, IEnumerable<T> items)
{
foreach (T item in items)
{
collection.Add(item);
}
}
public static void RemoveRange<T>(ICollection<T> collection, IEnumerable<T> items)
{
foreach (T item in items)
{
collection.Remove(item);
}
}
public static void AddRange<T, U>(this ICollection<T> collection, IEnumerable<U> items)
where U : T
{
foreach (U item in items)
{
collection.Add(item);
}
}
public static void RemoveRange<T, U>(this ICollection<T> collection, IEnumerable<U> items)
where U : T
{
foreach (U item in items)
{
collection.Remove(item);
}
}
public static void RemoveKeys<TKey, TValue, U>(this IDictionary<TKey, TValue> dictionary, IEnumerable<U> keys)
where U : TKey
{
foreach (U key in keys)
{
dictionary.Remove(key);
}
}
public static void EnqueueRange<T>(this Queue<T> queue, IEnumerable<T> items)
{
foreach (T item in items)
{
queue.Enqueue(item);
}
}
// This method is useful if you intend to modify a collection while/after iterating over it.
public static IEnumerable<T> GetEnumeratorOfCopy<T>(this IEnumerable<T> collection)
{
var array = collection.ToArray();
foreach (var item in array)
{
yield return item;
}
}
static Random rand = new Random();
public static void Shuffle<T>(this List<T> list)
{
if (list == null) throw new ArgumentNullException("list");
if (list.Count < 1)
return;
int i;
for (i = 0; i < list.Count; i++)
{
var j = rand.Next(i, list.Count);
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
public static IEnumerable<T> BreadthFirstTraversal<T>(T start, Func<T, IEnumerable<T>> getChildren)
{
var queue = new Queue<T>();
queue.Enqueue(start);
while (queue.Count > 0)
{
var item = queue.Dequeue();
yield return item;
foreach (var child in getChildren(item))
{
queue.Enqueue(child);
}
}
}
public static IEnumerable<T> DepthFirstTraversal<T>(T start, Func<T, IEnumerable<T>> getChildren)
{
yield return start;
foreach (var child in getChildren(start))
{
foreach (var item in DepthFirstTraversal(child, getChildren))
{
yield return item;
}
}
}
public static T[] CloneArray<T>(this T[] array)
{
T[] other = new T[array.Length];
array.CopyTo(other, 0);
return other;
}
}
}