summary refs log tree commit diff
path: root/src/libsyntax/util/interner.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsyntax/util/interner.rs')
-rw-r--r--src/libsyntax/util/interner.rs96
1 files changed, 85 insertions, 11 deletions
diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs
index 51144267519..dffeac6f3f7 100644
--- a/src/libsyntax/util/interner.rs
+++ b/src/libsyntax/util/interner.rs
@@ -14,13 +14,13 @@
 
 use ast::Name;
 
-use std::borrow::BorrowFrom;
+use std::borrow::Borrow;
 use std::cell::RefCell;
 use std::cmp::Ordering;
 use std::collections::HashMap;
+#[cfg(stage0)] use std::collections::hash_map::Hasher;
 use std::fmt;
 use std::hash::Hash;
-use std::collections::hash_map::Hasher;
 use std::ops::Deref;
 use std::rc::Rc;
 
@@ -30,6 +30,7 @@ pub struct Interner<T> {
 }
 
 // when traits can extend traits, we should extend index<Name,T> to get []
+#[cfg(stage0)]
 impl<T: Eq + Hash<Hasher> + Clone + 'static> Interner<T> {
     pub fn new() -> Interner<T> {
         Interner {
@@ -79,7 +80,71 @@ impl<T: Eq + Hash<Hasher> + Clone + 'static> Interner<T> {
     }
 
     pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
-    where Q: BorrowFrom<T> + Eq + Hash<Hasher> {
+    where T: Borrow<Q>, Q: Eq + Hash<Hasher> {
+        let map = self.map.borrow();
+        match (*map).get(val) {
+            Some(v) => Some(*v),
+            None => None,
+        }
+    }
+
+    pub fn clear(&self) {
+        *self.map.borrow_mut() = HashMap::new();
+        *self.vect.borrow_mut() = Vec::new();
+    }
+}
+// when traits can extend traits, we should extend index<Name,T> to get []
+#[cfg(not(stage0))]
+impl<T: Eq + Hash + Clone + 'static> Interner<T> {
+    pub fn new() -> Interner<T> {
+        Interner {
+            map: RefCell::new(HashMap::new()),
+            vect: RefCell::new(Vec::new()),
+        }
+    }
+
+    pub fn prefill(init: &[T]) -> Interner<T> {
+        let rv = Interner::new();
+        for v in init {
+            rv.intern((*v).clone());
+        }
+        rv
+    }
+
+    pub fn intern(&self, val: T) -> Name {
+        let mut map = self.map.borrow_mut();
+        match (*map).get(&val) {
+            Some(&idx) => return idx,
+            None => (),
+        }
+
+        let mut vect = self.vect.borrow_mut();
+        let new_idx = Name((*vect).len() as u32);
+        (*map).insert(val.clone(), new_idx);
+        (*vect).push(val);
+        new_idx
+    }
+
+    pub fn gensym(&self, val: T) -> Name {
+        let mut vect = self.vect.borrow_mut();
+        let new_idx = Name((*vect).len() as u32);
+        // leave out of .map to avoid colliding
+        (*vect).push(val);
+        new_idx
+    }
+
+    pub fn get(&self, idx: Name) -> T {
+        let vect = self.vect.borrow();
+        (*vect)[idx.usize()].clone()
+    }
+
+    pub fn len(&self) -> usize {
+        let vect = self.vect.borrow();
+        (*vect).len()
+    }
+
+    pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
+    where T: Borrow<Q>, Q: Eq + Hash {
         let map = self.map.borrow();
         match (*map).get(val) {
             Some(v) => Some(*v),
@@ -110,34 +175,34 @@ impl Eq for RcStr {}
 
 impl Ord for RcStr {
     fn cmp(&self, other: &RcStr) -> Ordering {
-        self[].cmp(&other[])
+        self[..].cmp(&other[..])
     }
 }
 
 impl fmt::Debug for RcStr {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         use std::fmt::Debug;
-        self[].fmt(f)
+        self[..].fmt(f)
     }
 }
 
 impl fmt::Display for RcStr {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         use std::fmt::Display;
-        self[].fmt(f)
+        self[..].fmt(f)
     }
 }
 
-impl BorrowFrom<RcStr> for str {
-    fn borrow_from(owned: &RcStr) -> &str {
-        &owned.string[]
+impl Borrow<str> for RcStr {
+    fn borrow(&self) -> &str {
+        &self.string[..]
     }
 }
 
 impl Deref for RcStr {
     type Target = str;
 
-    fn deref(&self) -> &str { &self.string[] }
+    fn deref(&self) -> &str { &self.string[..] }
 }
 
 /// A StrInterner differs from Interner<String> in that it accepts
@@ -210,8 +275,17 @@ impl StrInterner {
         self.vect.borrow().len()
     }
 
+    #[cfg(stage0)]
+    pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
+    where RcStr: Borrow<Q>, Q: Eq + Hash<Hasher> {
+        match (*self.map.borrow()).get(val) {
+            Some(v) => Some(*v),
+            None => None,
+        }
+    }
+    #[cfg(not(stage0))]
     pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
-    where Q: BorrowFrom<RcStr> + Eq + Hash<Hasher> {
+    where RcStr: Borrow<Q>, Q: Eq + Hash {
         match (*self.map.borrow()).get(val) {
             Some(v) => Some(*v),
             None => None,