Make union-find helper fns private, change to u32.

This commit is contained in:
Niko Matsakis 2015-02-16 05:07:22 -05:00
parent 7c62640458
commit c581840dcc
2 changed files with 26 additions and 18 deletions

View file

@ -456,15 +456,17 @@ pub fn type_is_unconstrained_numeric(&'a self, ty: Ty) -> UnconstrainedNumeric {
use middle::ty::UnconstrainedNumeric::{Neither, UnconstrainedInt, UnconstrainedFloat}; use middle::ty::UnconstrainedNumeric::{Neither, UnconstrainedInt, UnconstrainedFloat};
match ty.sty { match ty.sty {
ty::ty_infer(ty::IntVar(vid)) => { ty::ty_infer(ty::IntVar(vid)) => {
match self.int_unification_table.borrow_mut().get(vid).value { if self.int_unification_table.borrow_mut().has_value(vid) {
None => UnconstrainedInt, Neither
_ => Neither, } else {
UnconstrainedInt
} }
}, },
ty::ty_infer(ty::FloatVar(vid)) => { ty::ty_infer(ty::FloatVar(vid)) => {
match self.float_unification_table.borrow_mut().get(vid).value { if self.float_unification_table.borrow_mut().has_value(vid) {
None => UnconstrainedFloat, Neither
_ => Neither, } else {
UnconstrainedFloat
} }
}, },
_ => Neither, _ => Neither,

View file

@ -33,9 +33,9 @@
pub trait UnifyKey : Clone + Debug + PartialEq { pub trait UnifyKey : Clone + Debug + PartialEq {
type Value : UnifyValue; type Value : UnifyValue;
fn index(&self) -> usize; fn index(&self) -> u32;
fn from_index(u: usize) -> Self; fn from_index(u: u32) -> Self;
fn tag(k: Option<Self>) -> &'static str; fn tag(k: Option<Self>) -> &'static str;
} }
@ -123,7 +123,7 @@ pub fn commit(&mut self, snapshot: Snapshot<K>) {
pub fn new_key(&mut self, value: K::Value) -> K { pub fn new_key(&mut self, value: K::Value) -> K {
let index = self.values.push(Root(value, 0)); let index = self.values.push(Root(value, 0));
let k = UnifyKey::from_index(index); let k = UnifyKey::from_index(index as u32);
debug!("{}: created new key: {:?}", debug!("{}: created new key: {:?}",
UnifyKey::tag(None::<K>), UnifyKey::tag(None::<K>),
k); k);
@ -136,8 +136,8 @@ pub fn new_key(&mut self, value: K::Value) -> K {
/// ///
/// NB. This is a building-block operation and you would probably /// NB. This is a building-block operation and you would probably
/// prefer to call `probe` below. /// prefer to call `probe` below.
pub fn get(&mut self, vid: K) -> Node<K> { fn get(&mut self, vid: K) -> Node<K> {
let index = vid.index(); let index = vid.index() as usize;
let value = (*self.values.get(index)).clone(); let value = (*self.values.get(index)).clone();
match value { match value {
Redirect(redirect) => { Redirect(redirect) => {
@ -155,7 +155,8 @@ pub fn get(&mut self, vid: K) -> Node<K> {
} }
fn is_root(&self, key: &K) -> bool { fn is_root(&self, key: &K) -> bool {
match *self.values.get(key.index()) { let index = key.index() as usize;
match *self.values.get(index) {
Redirect(..) => false, Redirect(..) => false,
Root(..) => true, Root(..) => true,
} }
@ -169,7 +170,8 @@ fn set(&mut self, key: K, new_value: VarValue<K>) {
debug!("Updating variable {:?} to {:?}", debug!("Updating variable {:?} to {:?}",
key, new_value); key, new_value);
self.values.set(key.index(), new_value); let index = key.index() as usize;
self.values.set(index, new_value);
} }
/// Either redirects `node_a` to `node_b` or vice versa, depending /// Either redirects `node_a` to `node_b` or vice versa, depending
@ -180,7 +182,7 @@ fn set(&mut self, key: K, new_value: VarValue<K>) {
/// really more of a building block. If the values associated with /// really more of a building block. If the values associated with
/// your key are non-trivial, you would probably prefer to call /// your key are non-trivial, you would probably prefer to call
/// `unify_var_var` below. /// `unify_var_var` below.
pub fn unify(&mut self, node_a: &Node<K>, node_b: &Node<K>, new_value: K::Value) { fn unify(&mut self, node_a: &Node<K>, node_b: &Node<K>, new_value: K::Value) {
debug!("unify(node_a(id={:?}, rank={:?}), node_b(id={:?}, rank={:?}))", debug!("unify(node_a(id={:?}, rank={:?}), node_b(id={:?}, rank={:?}))",
node_a.key, node_a.key,
node_a.rank, node_a.rank,
@ -307,6 +309,10 @@ pub fn unify_var_value(&mut self,
} }
} }
pub fn has_value(&mut self, id: K) -> bool {
self.get(id).value.is_some()
}
pub fn probe(&mut self, tcx: &ty::ctxt<'tcx>, a_id: K) -> Option<Ty<'tcx>> { pub fn probe(&mut self, tcx: &ty::ctxt<'tcx>, a_id: K) -> Option<Ty<'tcx>> {
let node_a = self.get(a_id); let node_a = self.get(a_id);
match node_a.value { match node_a.value {
@ -322,8 +328,8 @@ pub fn probe(&mut self, tcx: &ty::ctxt<'tcx>, a_id: K) -> Option<Ty<'tcx>> {
impl UnifyKey for ty::IntVid { impl UnifyKey for ty::IntVid {
type Value = Option<IntVarValue>; type Value = Option<IntVarValue>;
fn index(&self) -> usize { self.index as usize } fn index(&self) -> u32 { self.index }
fn from_index(i: usize) -> ty::IntVid { ty::IntVid { index: i as u32 } } fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } }
fn tag(_: Option<ty::IntVid>) -> &'static str { "IntVid" } fn tag(_: Option<ty::IntVid>) -> &'static str { "IntVid" }
} }
@ -346,8 +352,8 @@ impl UnifyValue for Option<IntVarValue> { }
impl UnifyKey for ty::FloatVid { impl UnifyKey for ty::FloatVid {
type Value = Option<ast::FloatTy>; type Value = Option<ast::FloatTy>;
fn index(&self) -> usize { self.index as usize } fn index(&self) -> u32 { self.index }
fn from_index(i: usize) -> ty::FloatVid { ty::FloatVid { index: i as u32 } } fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
fn tag(_: Option<ty::FloatVid>) -> &'static str { "FloatVid" } fn tag(_: Option<ty::FloatVid>) -> &'static str { "FloatVid" }
} }