Skip to content

Instantly share code, notes, and snippets.

View SamChristy's full-sized avatar

Sam Christy SamChristy

View GitHub Profile
@SamChristy
SamChristy / DisjointSet.ts
Last active November 19, 2022 16:58
An efficient Disjoint Set implementation (using compressed trees and dense storage).
/**
* (If a number is used, it must be > -1!)
*/
type DSNode = number | string;
class DisjointSet {
protected readonly parents: Map<DSNode, DSNode | number> = new Map();
protected count = 0;
protected findRoot(node: DSNode): DSNode {
@SamChristy
SamChristy / gist:6652640
Created September 21, 2013 17:49
Notes on timezones from ages ago - posted for the potential benefit of anyone else.
<?php
// ### How to Handle Timezones Correctly! ###
// Time is a presentation issue. Always store date/time as UTC, then convert it
// to the user's timezone post retrieval and prior to output.
//
// Take a calendar application, for example. If Bob wants to play video games at
// 13:00 on 2011/09/01 and Bob lives in London, which would be in BST (UTC+1),
// then this should be saved in UTC (i.e. less an hour). Then, if Jerry lives in
// New York and wants to play the same game with Bob at the same time, the date
// would be retrieved from the database and converted to Jerry's timezone, which
@SamChristy
SamChristy / gist:4734446
Last active December 12, 2015 07:09
Simple function for creating pretty canvas pie charts.
/**
* @author Sam Christy <sam_christy@hotmail.co.uk>
* @licence GNU GPL
*/
var cs1 = {
r : Math.floor(Math.random() * 256),
g : Math.floor(Math.random() * 256),
b : Math.floor(Math.random() * 256)
};
@SamChristy
SamChristy / gist:4734256
Last active December 12, 2015 07:08
JavaScript implementation of Fisher-Yates shuffle (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle).
function shuffle(a){
var j, temp;
for(i = a.length-1; i > 1; i--){
j = Math.floor(Math.random() * (i + 1));
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
@SamChristy
SamChristy / gist:4734247
Last active December 12, 2015 07:08
Removes duplicate primitives from an array.
Array.prototype.unique = function(){
var map = {};
for(var i = 0; i < this.length; i++)
map[this[i]] = this[i];
this.length = 0;
for(var key in map)
this.push(map[key]);
@SamChristy
SamChristy / gist:1649430
Created January 20, 2012 20:38
Super fast, iterative binary search. For those of you who are fed up with indexOf!
/**
* Searches the specified range of a sorted array for a given value; if the
* wanted value is found, its index is returned. -1 is returned if the item
* is not found. If no range is specified, the entire array will be searched.
* Please note: the array that this method is invoked on must be sorted in
* ascending order!
* @param {int|float|string} wanted The value to be searched for.
* @param {int} [first] The start of the search range.
* @param {int} [last] The end of the search range.
* @return {int} The index of the element if found, otherwise -1.
@SamChristy
SamChristy / gist:1395840
Created November 26, 2011 15:17
A simple high-resolution stopwatch class for timing your programs in a Windows environment.
/////////////////////////////////////////////////////////////////////////
/// Title: Stopwatch Class
/// Author: Sam Christy
/// Date: 26/11/2011
/// Licence: GNU GPL
///
/// This class is a simple, high-resolution stopwatch for timing the
/// execution of C++ programs in a Windows eviroment. This class
/// emulates the Stopwatch class of the .NET framework and uses the
/// Windows API to time events. Its precision is much greater than any