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};
match ty.sty {
ty::ty_infer(ty::IntVar(vid)) => {
match self.int_unification_table.borrow_mut().get(vid).value {
None => UnconstrainedInt,
_ => Neither,
if self.int_unification_table.borrow_mut().has_value(vid) {
Neither
} else {
UnconstrainedInt
}
},
ty::ty_infer(ty::FloatVar(vid)) => {
match self.float_unification_table.borrow_mut().get(vid).value {
None => UnconstrainedFloat,
_ => Neither,
if self.float_unification_table.borrow_mut().has_value(vid) {
Neither
} else {
UnconstrainedFloat
}
},
_ => Neither,

View file

@ -33,9 +33,9 @@
pub trait UnifyKey : Clone + Debug + PartialEq {
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;
}
@ -123,7 +123,7 @@ pub fn commit(&mut self, snapshot: Snapshot<K>) {
pub fn new_key(&mut self, value: K::Value) -> K {
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: {:?}",
UnifyKey::tag(None::<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
/// prefer to call `probe` below.
pub fn get(&mut self, vid: K) -> Node<K> {
let index = vid.index();
fn get(&mut self, vid: K) -> Node<K> {
let index = vid.index() as usize;
let value = (*self.values.get(index)).clone();
match value {
Redirect(redirect) => {
@ -155,7 +155,8 @@ pub fn get(&mut self, vid: K) -> Node<K> {
}
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,
Root(..) => true,
}
@ -169,7 +170,8 @@ fn set(&mut self, key: K, new_value: VarValue<K>) {
debug!("Updating variable {:?} to {:?}",
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
@ -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
/// your key are non-trivial, you would probably prefer to call
/// `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={:?}))",
node_a.key,
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>> {
let node_a = self.get(a_id);
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 {
type Value = Option<IntVarValue>;
fn index(&self) -> usize { self.index as usize }
fn from_index(i: usize) -> ty::IntVid { ty::IntVid { index: i as u32 } }
fn index(&self) -> u32 { self.index }
fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } }
fn tag(_: Option<ty::IntVid>) -> &'static str { "IntVid" }
}
@ -346,8 +352,8 @@ impl UnifyValue for Option<IntVarValue> { }
impl UnifyKey for ty::FloatVid {
type Value = Option<ast::FloatTy>;
fn index(&self) -> usize { self.index as usize }
fn from_index(i: usize) -> ty::FloatVid { ty::FloatVid { index: i as u32 } }
fn index(&self) -> u32 { self.index }
fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
fn tag(_: Option<ty::FloatVid>) -> &'static str { "FloatVid" }
}