Skip to content

Instantly share code, notes, and snippets.

View CallumWatkins's full-sized avatar

Callum Watkins CallumWatkins

View GitHub Profile
@CallumWatkins
CallumWatkins / CheckIfPointsMakeSquare.cs
Last active February 18, 2017 16:34
Checks whether an array of points forms a square. License: MIT
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 / MapIntegerRange.cs
Created June 26, 2017 12:05
Maps an integer value from between one range of integers to another. License: MIT
/// <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 / SplitInParts.cs
Last active June 27, 2017 19:25
Splits a string into blocks of a specified length, delimited by a specified string. License: MIT
/// <summary>
/// Splits a string into blocks of a specified length, delimited by a specified string.
/// </summary>
/// <param name="value">The string to split up.</param>
/// <param name="partLength">The number of characters between each delimiter.</param>
/// <param name="delimiter">The delimiter to be inserted.</param>
public static string SplitInParts(string value, int partLength, string delimiter)
{
if (value == null) { throw new ArgumentNullException(nameof(value)); }
if (partLength < 1) { throw new ArgumentException("Part length must be positive and greater than zero.", nameof(partLength)); }
@CallumWatkins
CallumWatkins / RandomlyPick.cs
Last active September 13, 2017 22:43
Randomly picks a number of generic elements from a set of values and returns an array/IEnumerable. License: MIT
// 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 / JsonTypeNameSerializationBinder.cs
Last active March 3, 2018 17:06
A Json.NET serialization binder to allow providing a custom type name using an attribute. License: MIT
// ==========
// Usage:
[JsonTypeName("CustomNameHere")]
public class Usage
{
public string Example { get; set; } = "Hello World";
}
// Serialize:
JsonConvert.SerializeObject(new Usage(),
new JsonSerializerSettings()
@CallumWatkins
CallumWatkins / TemperatureConversions.cs
Last active February 3, 2019 03:20
Functions to convert between the Kelvin, Celsius and Fahrenheit temperature scales. License: MIT
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 / SpanBinarySearch.cs
Last active February 4, 2019 16:25
Two binary search implementations using ReadOnlySpan<T>. One returns the index of the item, the other returns a Boolean. License: MIT
/// <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 / Quicksort.cs
Created March 20, 2019 04:38
An in-place quicksort implementation based on the Hoare partition scheme. Designed for Span<T>. License: MIT
/// <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 / NetworkChecker.bat
Last active March 20, 2024 08:58
A batch file to periodically check your internet connection, restart on failed connection and write to log. License: MIT
@echo off
mode con: cols=55 lines=12
title Network Checker
REM Server to be pinged
SET server=google.co.uk
REM Size of packet to be send to server (bytes)
SET packetSize=1
REM Network info
SET netSSID=[SSID]