Skip to content

Instantly share code, notes, and snippets.

View CallumWatkins's full-sized avatar

Callum Watkins CallumWatkins

View GitHub Profile
Verifying that +callumwatkins is my blockchain ID. https://onename.com/callumwatkins

Keybase proof

I hereby claim:

  • I am CallumWatkins on github.
  • I am callumwatkins (https://keybase.io/callumwatkins) on keybase.
  • I have a public key whose fingerprint is 7A2C 9986 AF66 4A9E 2A8D 869F C71C 342D A44D AB84

To claim this, I am signing this object:

@CallumWatkins
CallumWatkins / IsInIntegerRange.cs
Last active February 18, 2017 16:29
Checks whether any given integer is located in the range between two other integers. License: MIT
/// <summary>
/// Checks whether any given integer is located in the range between two other integers.
/// </summary>
/// <param name="i">The integer to check.</param>
/// <param name="min">The minimum value of the range.</param>
/// <param name="max">The maximum value of the range.</param>
/// <param name="inclusive">Whether or not the minumum and maximum values are included in the range.</param>
/// <returns>Boolean</returns>
private static bool IsInIntegerRange(this int i, int min, int max, bool inclusive = true)
{
@CallumWatkins
CallumWatkins / OpenWindowsExplorerSearch.cs
Last active February 18, 2017 16:30
Opens a Windows Explorer file/folder search for the specified query, with the option of a location. License: MIT
public void SearchWindowsExplorer(string query, string location = null)
{
if (location != null && !Directory.Exists(location))
{
throw new ArgumentException("Directory does not exist.", "location");
}
string uri = "search:query=" + Uri.EscapeDataString(query) +
(location == null ? string.Empty : "&crumb=location:" + Uri.EscapeDataString(location));
Process.Start(uri);
}
@CallumWatkins
CallumWatkins / CreateFileAndDirectory.cs
Last active February 18, 2017 16:31
Creates all directories and subdirectories unless they already exist and creates or overwrites a file in the specified path. License: MIT
using System;
using System.IO;
/// <summary>
/// Creates all directories and subdirectories unless they already exist and creates or overwrites a file in the specified path.
/// </summary>
/// <param name="path">The full path of the file.</param>
public static FileStream CreateFileAndDirectory(string path)
{
if (path == null) { throw new ArgumentNullException(nameof(path)); }
@CallumWatkins
CallumWatkins / Compass.cs
Last active February 18, 2017 16:31
A class to convert between bearings and compass directions. License: MIT
public static class Compass
{
public enum CompassDirection { N, NbE, NNE, NEbN, NE, NEbE, ENE, EbN, E, EbS, ESE, SEbE, SE, SEbS, SSE, SbE, S, SbW, SSW, SWbS, SW, SWbW, WSW, WbS, W, WbN, WNW, NWbW, NW, NWbN, NNW, NbW }
public enum CompassPoints { Four = 4, Eight = 8, Sixteen = 16, ThirtyTwo = 32 }
public static Dictionary<CompassDirection, string> CompassDirections
{
get
{
return new Dictionary<CompassDirection, string>()
@CallumWatkins
CallumWatkins / SumBetween.cs
Last active February 18, 2017 16:31
Calculates the sum of all the integers in the range between two integers, with the option to exclude them. License: MIT
/// <summary>
/// Calculates the sum of all the integers in the range between two integers.
/// </summary>
/// <param name="start">The starting integer.</param>
/// <param name="end">The ending integer.</param>
/// <param name="exclusive">Whether or not the <paramref name="start"/> and <paramref name="end"/> integers are excluded from the sum.</param>
/// <returns>Returns the sum of the integers in the range.</returns>
private static long SumBetween(int start, int end, bool exclusive = false)
{
if (start > end) { throw new ArgumentException("End value must be more than or equal to start value."); }
@CallumWatkins
CallumWatkins / GenericSwap.cs
Last active February 18, 2017 16:32
A generic method for swapping two variables, the type of which implements IEquatable<>, if both variables are not equal. License: MIT
/// <summary>
/// Swaps two variables of type T with one another.
/// </summary>
/// <typeparam name="T">The type of both variables. Must implement IEquatable.</typeparam>
/// <param name="a">The first variable.</param>
/// <param name="b">The second variable.</param>
static void Swap<T>(ref T a, ref T b) where T : IEquatable<T>
{
if (a.Equals(b)) return;
T tempA = a;
@CallumWatkins
CallumWatkins / DeepCloneJson.cs
Last active February 18, 2017 16:32
A generic method for performing a deep clone of an object, using Json.NET as a serialisation method. License: MIT
using Newtonsoft.Json;
// Requires Newtonsoft's Json.NET
// http://www.newtonsoft.com/json
/// <summary>
/// Perform a deep clone of the object, using Json as a serialisation method.
/// </summary>
/// <typeparam name="T">The type of object being cloned.</typeparam>
/// <param name="source">The object instance to clone.</param>
/// <returns>The cloned object.</returns>
@CallumWatkins
CallumWatkins / TestSpeed.cs
Last active February 18, 2017 16:33
A method for testing the speed of an action. License: MIT
/// <summary>
/// Tests the speed of an action.
/// </summary>
/// <param name="action">The method to be tested.</param>
/// <param name="iterations">The number of times to perform the action in each test.</param>
/// <param name="repetitions">The number of times to repeat the test.</param>
/// <param name="name">The name of the test.</param>
/// <param name="warmupMilliseconds">The number of milliseconds to warm up for.</param>
static void TestSpeed(Action action, long iterations, int repetitions, string name, int warmupMilliseconds = 5000)
{