Skip to content

Instantly share code, notes, and snippets.

Callum Watkins CallumWatkins

Block or report user

Report or block CallumWatkins

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@CallumWatkins
CallumWatkins / Quicksort.cs
Created Mar 20, 2019
An in-place quicksort implementation based on the Hoare partition scheme. Designed for Span<T>. License: MIT
View Quicksort.cs
/// <summary>
/// An in-place quicksort implementation based on the Hoare partition scheme.
/// </summary>
/// <typeparam name="T">The type of the elements being sorted.</typeparam>
/// <param name="input">A span of elements to be sorted.</param>
public static void Quicksort<T>(Span<T> input) where T : IComparable<T>
{
Quicksort<T>(input, Comparer<T>.Default);
}
@CallumWatkins
CallumWatkins / SpanBinarySearch.cs
Last active Feb 4, 2019
Two binary search implementations using ReadOnlySpan<T>. One returns the index of the item, the other returns a Boolean. License: MIT
View SpanBinarySearch.cs
/// <summary>
/// Searches an entire one-dimensional sorted span for a value using the specified comparer.
/// </summary>
/// <typeparam name="T">The type of the items being searched.</typeparam>
/// <param name="span">The sorted span to search.</param>
/// <param name="item">The item to search for.</param>
/// <param name="comparer">The comparer implementation to use when comparing elements.</param>
/// <returns>True if the item is in the input; false otherwise.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="comparer"/> is null.</exception>
static bool BinarySearch<T>(ReadOnlySpan<T> span, T item, IComparer<T> comparer)
@CallumWatkins
CallumWatkins / TemperatureConversions.cs
Last active Feb 3, 2019
Functions to convert between the Kelvin, Celsius and Fahrenheit temperature scales. License: MIT
View TemperatureConversions.cs
public static class TemperatureConversions
{
public static double KelvinToCelsius(double kelvin)
{
return kelvin - 273.15;
}
public static double KelvinToFahrenheit(double kelvin)
{
return kelvin * 1.8 - 459.67;
@CallumWatkins
CallumWatkins / JsonTypeNameSerializationBinder.cs
Last active Mar 3, 2018
A Json.NET serialization binder to allow providing a custom type name using an attribute. License: MIT
View JsonTypeNameSerializationBinder.cs
// ==========
// Usage:
[JsonTypeName("CustomNameHere")]
public class Usage
{
public string Example { get; set; } = "Hello World";
}
// Serialize:
JsonConvert.SerializeObject(new Usage(),
new JsonSerializerSettings()
@CallumWatkins
CallumWatkins / RandomlyPick.cs
Last active Sep 13, 2017
Randomly picks a number of generic elements from a set of values and returns an array/IEnumerable. License: MIT
View RandomlyPick.cs
// Two functions below: one returns T[] and one returns an IEnumerable<T> iterator using yield.
// The yield function is best used in situations when you might not use all of the values, or would like to use them one by one.
// The non-yield function is best used when all of the values are required immediately, as this provides the best performance.
// The yield (lazy) function has been designed to perform all input validation immediately on its first call, not when it is iterated.
// This helps detect problems with the input straight away, not later when the values are used.
// Has been tested for uniform distribution and has excellent performance.
// Should NOT be used as a shuffling algorithm by requesting all items to be picked, as this will return the set of values unchanged.
@CallumWatkins
CallumWatkins / MapIntegerRange.cs
Created Jun 26, 2017
Maps an integer value from between one range of integers to another. License: MIT
View MapIntegerRange.cs
/// <summary>
/// Maps an integer value from between one range of integers to another.
/// </summary>
/// <param name="value">The value to map.</param>
/// <param name="existingLowerBound">The lower bound of the starting range, inclusive.</param>
/// <param name="existingUpperBound">The upper bound of the starting range, inclusive.</param>
/// <param name="newLowerBound">The lower bound of the end range, inclusive.</param>
/// <param name="newUpperBound">The upper bound of the end range, inclusive.</param>
/// <param name="roundingMode">The <see cref="System.MidpointRounding"/> mode used to round non-integer results.</param>
static int MapIntegerRange(int value, int existingLowerBound, int existingUpperBound, int newLowerBound, int newUpperBound, MidpointRounding roundingMode = MidpointRounding.ToEven)
@CallumWatkins
CallumWatkins / CheckIfPointsMakeSquare.cs
Last active Feb 18, 2017
Checks whether an array of points forms a square. License: MIT
View CheckIfPointsMakeSquare.cs
using System;
using System.Drawing;
/// <summary>
/// Checks whether an array of <see cref="Point"/> forms a square.
/// </summary>
/// <param name="points">The points to check.</param>
private static bool CheckIfPointsMakeSquare(Point[] points)
{
if (points == null) { throw new ArgumentNullException(nameof(points)); }
@CallumWatkins
CallumWatkins / TestSpeed.cs
Last active Feb 18, 2017
A method for testing the speed of an action. License: MIT
View TestSpeed.cs
/// <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)
{
@CallumWatkins
CallumWatkins / DeepCloneJson.cs
Last active Feb 18, 2017
A generic method for performing a deep clone of an object, using Json.NET as a serialisation method. License: MIT
View DeepCloneJson.cs
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 / GenericSwap.cs
Last active Feb 18, 2017
A generic method for swapping two variables, the type of which implements IEquatable<>, if both variables are not equal. License: MIT
View GenericSwap.cs
/// <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;
You can’t perform that action at this time.