Skip to content

Instantly share code, notes, and snippets.

@jdh30
Created March 28, 2016 22:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdh30/8e225b7f30715eb588ec to your computer and use it in GitHub Desktop.
Save jdh30/8e225b7f30715eb588ec to your computer and use it in GitHub Desktop.
Rust implementation of a HashSet benchmark from the F# Journal
use std::collections::HashSet;
use std::hash::BuildHasherDefault;
use std::default::Default;
use std::hash::Hasher;
pub struct FnvHasher(u64);
impl Default for FnvHasher {
#[inline]
fn default() -> FnvHasher {
FnvHasher(0xcbf29ce484222325)
}
}
impl Hasher for FnvHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
let FnvHasher(mut hash) = *self;
for byte in bytes.iter() {
hash = hash ^ (*byte as u64);
hash = hash.wrapping_mul(0x100000001b3);
}
*self = FnvHasher(hash);
}
}
type Set = HashSet<(i32, i32), BuildHasherDefault<FnvHasher>>;
fn Empty() -> Set {
let fnv = BuildHasherDefault::<FnvHasher>::default();
HashSet::with_hasher(fnv)
}
fn iterNeighbors<F>(mut f: F, (i, j): (i32, i32)) -> ()
where F: FnMut((i32, i32)) -> ()
{
f((i-1, j));
f((i+1, j));
f((i, j-1));
f((i, j+1));
}
fn nthLoop(n: i32, s1: Set, s2: Set) -> Set {
if n == 0 {
return s1;
} else {
let mut s0 = Empty();
for &p in &s1 {
let add = |p| {
if !(s1.contains(&p) || s2.contains(&p)) {
s0.insert(p);
}
};
iterNeighbors(add, p);
}
drop(s2);
return nthLoop(n-1, s0, s1);
}
}
fn nth(n: i32, p: (i32, i32)) -> Set {
let mut s1 = Empty();
s1.insert(p);
let s2 = Empty();
nthLoop(n, s1, s2)
}
fn main() {
let s = nth(2000, (0, 0));
println!("{}", s.len());
}
@llogiq
Copy link

llogiq commented Apr 4, 2016

Why don't you use let s0 = s1.difference(&s2).collect(); in Lines 55-61?

@masklinn
Copy link

masklinn commented Apr 5, 2016

@llogiq As far as I can tell it doesn't take the elements of S1 which aren't in S2, it takes the neighbors of the elements in S1 which are in neither S1 nor S2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment