Skip to content

Instantly share code, notes, and snippets.

@ppelleti
Created August 12, 2012 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppelleti/3330996 to your computer and use it in GitHub Desktop.
Save ppelleti/3330996 to your computer and use it in GitHub Desktop.
list of hokey hash functions in rustc
/* list of hokey hash functions in rustc as of August 12, 2012
* in response to https://github.com/mozilla/rust/issues/3041 */
// libsyntax/ast_util.rs:
pure fn hash_ty(t: &@ty) -> uint {
let res = (t.span.lo << 16u) + t.span.hi;
return res;
}
pure fn hash_def(d: &ast::def_id) -> uint {
let mut h = 5381u;
h = (h << 5u) + h ^ (d.crate as uint);
h = (h << 5u) + h ^ (d.node as uint);
return h;
}
// rustc/middle/ty.rs:
pure fn hash_cache_entry(k: &val) -> uint {
(k.cnum as uint) + k.pos + k.len
}
pure fn hash_intern_key(k: &intern_key) -> uint {
hash_type_structure(k.struct) +
option::map_default(k.o_def_id, 0u, |d| ast_util::hash_def(&d))
}
pure fn hash_bound_region(br: &bound_region) -> uint {
match *br { // no idea if this is any good
ty::br_self => 0u,
ty::br_anon => 1u,
ty::br_named(str) => str::hash(str),
ty::br_cap_avoid(id, br) => id as uint | hash_bound_region(br)
}
}
pure fn hash_type_structure(st: sty) -> uint {
// lots of hokey hash functions involving addition and << 2u
}
// rustc/middle/borrowck.rs:
pure fn root_map_key_hash(k: &root_map_key) -> uint {
(k.id << 4) as uint | k.derefs
}
// rustc/middle/trans/common.rs:
pure fn hash_mono_id(mi: &mono_id) -> uint {
let mut h = syntax::ast_util::hash_def(&mi.def);
for vec::each(mi.params) |param| {
h = h * match param {
mono_precise(ty, vts) => {
let mut h = ty::type_id(ty);
do option::iter(vts) |vts| {
for vec::each(vts) |vt| {
h += hash_mono_id(&vt);
}
}
h
}
mono_any => 1u,
mono_repr(sz, align) => sz * (align + 2u)
}
}
h
}
// rustc/middle/trans/shape.rs:
pure fn hash_nominal_id(ri: &nominal_id) -> uint {
let mut h = 5381u;
h *= 33u;
h += ri.did.crate as uint;
h *= 33u;
h += ri.did.node as uint;
for vec::each(ri.tps) |t| {
h *= 33u;
h += ty::type_id(t);
}
return h;
}
// rustc/lib/llvm.rs:
pure fn hash(t: &TypeRef) -> uint { *t as uint }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment