Skip to content

Instantly share code, notes, and snippets.

@jdh30
Last active April 2, 2016 23:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdh30/1e76f4235fedff437701 to your computer and use it in GitHub Desktop.
Save jdh30/1e76f4235fedff437701 to your computer and use it in GitHub Desktop.
F# implementation of a HashSet benchmark from the F# Journal with unboxed tuples
type [<Struct>] P =
val i : int
val j : int
new(i, j) = {i=i; j=j}
let cmp =
{ new System.Collections.Generic.IEqualityComparer<P> with
member __.Equals(this, that) =
this.i = that.i && this.j = that.j
member __.GetHashCode this = this.i + 4000 * this.j }
let inline iterNeighbors f (p: P) =
let i, j = p.i, p.j
f(P(i-1, j))
f(P(i+1, j))
f(P(i, j-1))
f(P(i, j+1))
let rec nthLoop n (s1: HashSet<_>) (s2: HashSet<_>) =
match n with
| 0 -> s1
| n ->
let s0 = HashSet(cmp)
let add p =
if not(s1.Contains p || s2.Contains p) then
ignore(s0.Add p)
Seq.iter (fun p -> iterNeighbors add p) s1
nthLoop (n-1) s0 s1
let nth n p =
nthLoop n (HashSet([p], cmp)) (HashSet(cmp))
(nth 2000 (P(0, 0))).Count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment