Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1443ddda68 | |||
| 1e0990fe6a | |||
| f765becf06 | |||
| 9acc18e1fa | |||
| 7125bbc537 |
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace GameDevLibrary.Extensions
|
namespace GameDevLibrary.Extensions
|
||||||
{
|
{
|
||||||
@@ -117,5 +118,30 @@ namespace GameDevLibrary.Extensions
|
|||||||
{
|
{
|
||||||
return array[currentIndex++ % array.Count];
|
return array[currentIndex++ % array.Count];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shuffle around items in a list.
|
||||||
|
/// Code obtained from https://stackoverflow.com/a/1262619
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="list"></param>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
public static IList<T> Shuffle<T>(this IList<T> list)
|
||||||
|
{
|
||||||
|
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
|
||||||
|
int n = list.Count;
|
||||||
|
while (n > 1)
|
||||||
|
{
|
||||||
|
byte[] box = new byte[1];
|
||||||
|
do provider.GetBytes(box);
|
||||||
|
while (!(box[0] < n * (Byte.MaxValue / n)));
|
||||||
|
int k = (box[0] % n);
|
||||||
|
n--;
|
||||||
|
(list[k], list[n]) = (list[n], list[k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,6 +36,25 @@ namespace GameDevLibrary.Helpers
|
|||||||
return stopwatch.ElapsedMilliseconds;
|
return stopwatch.ElapsedMilliseconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static long RecordAndSaveToResults(string key, Action actionToRecord)
|
||||||
|
{
|
||||||
|
var stopwatch = new Stopwatch();
|
||||||
|
stopwatch.Start();
|
||||||
|
|
||||||
|
actionToRecord.Invoke();
|
||||||
|
|
||||||
|
stopwatch.Stop();
|
||||||
|
|
||||||
|
if (!_benchmarks.ContainsKey(key))
|
||||||
|
{
|
||||||
|
_benchmarks[key] = new List<long>();
|
||||||
|
}
|
||||||
|
|
||||||
|
_benchmarks[key].Add(stopwatch.ElapsedMilliseconds);
|
||||||
|
|
||||||
|
return stopwatch.ElapsedMilliseconds;
|
||||||
|
}
|
||||||
|
|
||||||
public static void Start(string key)
|
public static void Start(string key)
|
||||||
{
|
{
|
||||||
GetStopwatch(key).Restart();
|
GetStopwatch(key).Restart();
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace GameDevLibrary.Models
|
||||||
|
{
|
||||||
|
public abstract class InstancedObject<T> where T : class
|
||||||
|
{
|
||||||
|
public static T Instance { get; private set; }
|
||||||
|
|
||||||
|
protected InstancedObject()
|
||||||
|
{
|
||||||
|
if (Instance != null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"An instance of {typeof(T).Name} already exists.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Instance = this as T;
|
||||||
|
if (Instance == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Failed to cast {GetType().Name} to {typeof(T).Name}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~InstancedObject()
|
||||||
|
{
|
||||||
|
if (Instance == this)
|
||||||
|
{
|
||||||
|
Instance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -84,6 +84,9 @@ namespace GameDevLibrary.Models
|
|||||||
|
|
||||||
public IEnumerable<NeoDictionaryItem<TKey, TValue>> Select(Func<NeoDictionaryItem<TKey, TValue>, NeoDictionaryItem<TKey, TValue>> selector) =>
|
public IEnumerable<NeoDictionaryItem<TKey, TValue>> Select(Func<NeoDictionaryItem<TKey, TValue>, NeoDictionaryItem<TKey, TValue>> selector) =>
|
||||||
Items.Select(selector);
|
Items.Select(selector);
|
||||||
|
|
||||||
|
public int IndexOf(NeoDictionaryItem<TKey, TValue> selector) =>
|
||||||
|
Items.IndexOf(selector);
|
||||||
|
|
||||||
public void Remove(TKey key)
|
public void Remove(TKey key)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user