Callum Watkins CallumWatkins
- London, United Kingdom
- https://www.callumwatkins.com
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); | |
} |
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) |
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; |
View JsonTypeNameSerializationBinder.cs
// ========== | |
// Usage: | |
[JsonTypeName("CustomNameHere")] | |
public class Usage | |
{ | |
public string Example { get; set; } = "Hello World"; | |
} | |
// Serialize: | |
JsonConvert.SerializeObject(new Usage(), | |
new JsonSerializerSettings() |
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. |
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) |
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)); } |
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) | |
{ |
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> |
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; |
NewerOlder