-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.cs
45 lines (41 loc) · 1 KB
/
Extension.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace DarkestDungeonRandomizer;
static class Extension
{
public static int? TryParseInt(this string value)
{
if (int.TryParse(value, out int result))
{
return result;
}
return null;
}
public static TValue GetValueOrSetDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue @default)
{
if (dict.ContainsKey(key))
{
return dict[key];
}
else
{
dict[key] = @default;
return @default;
}
}
public static T[] Shuffle<T>(this IEnumerable<T> original, Random random)
{
T[] newData = original.ToArray();
int n = newData.Length;
while (n > 1)
{
n--;
int k = random.Next(n + 1);
T value = newData[k];
newData[k] = newData[n];
newData[n] = value;
}
return newData;
}
}