auto merge of #5503 : thestinger/rust/trie, r=pcwalton

This commit is contained in:
bors 2013-03-22 18:00:52 -07:00
commit d60a7259f9
3 changed files with 108 additions and 123 deletions

View File

@ -12,7 +12,6 @@
use prelude::*;
// FIXME: #3469: need to manually update TrieNode when SHIFT changes
// FIXME: #5244: need to manually update the TrieNode constructor
const SHIFT: uint = 4;
const SIZE: uint = 1 << SHIFT;
@ -162,12 +161,15 @@ pub struct TrieSet {
impl BaseIter<uint> for TrieSet {
/// Visit all values in order
#[inline(always)]
fn each(&self, f: &fn(&uint) -> bool) { self.map.each_key(f) }
#[inline(always)]
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
}
impl ReverseIter<uint> for TrieSet {
/// Visit all values in reverse order
#[inline(always)]
fn each_reverse(&self, f: &fn(&uint) -> bool) {
self.map.each_key_reverse(f)
}
@ -189,7 +191,7 @@ impl Mutable for TrieSet {
fn clear(&mut self) { self.map.clear() }
}
impl TrieSet {
pub impl TrieSet {
/// Create an empty TrieSet
#[inline(always)]
fn new() -> TrieSet {
@ -215,7 +217,7 @@ impl TrieSet {
struct TrieNode<T> {
count: uint,
children: [Child<T> * 16] // FIXME: #3469: can't use the SIZE constant yet
children: [Child<T> * SIZE]
}
impl<T> TrieNode<T> {
@ -301,7 +303,6 @@ fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
added = insert(&mut x.count, &mut x.children[chunk(key, idx)], key,
value, idx + 1);
Internal(x)
}
Nothing => {
*count += 1;

View File

@ -112,7 +112,7 @@ pub impl <T:Ord> PriorityQueue<T> {
while end > 1 {
end -= 1;
q.data[end] <-> q.data[0];
unsafe { q.siftdown_range(0, end) } // purity-checking workaround
q.siftdown_range(0, end)
}
q.to_vec()
}
@ -126,7 +126,7 @@ pub impl <T:Ord> PriorityQueue<T> {
let mut n = q.len() / 2;
while n > 0 {
n -= 1;
unsafe { q.siftdown(n) }; // purity-checking workaround
q.siftdown(n)
}
q
}

View File

@ -43,11 +43,9 @@ impl<K: Eq + TotalOrd, V: Eq> Eq for TreeMap<K, V> {
let mut x = self.iter();
let mut y = other.iter();
for self.len().times {
unsafe { // unsafe as a purity workaround
if map_next(&mut x).unwrap() !=
map_next(&mut y).unwrap() {
return false
}
if map_next(&mut x).unwrap() !=
map_next(&mut y).unwrap() {
return false
}
}
true
@ -64,12 +62,10 @@ fn lt<K: Ord + TotalOrd, V>(a: &TreeMap<K, V>,
let (a_len, b_len) = (a.len(), b.len());
for uint::min(a_len, b_len).times {
unsafe { // purity workaround
let (key_a,_) = map_next(&mut x).unwrap();
let (key_b,_) = map_next(&mut y).unwrap();
if *key_a < *key_b { return true; }
if *key_a > *key_b { return false; }
}
let (key_a,_) = map_next(&mut x).unwrap();
let (key_b,_) = map_next(&mut y).unwrap();
if *key_a < *key_b { return true; }
if *key_a > *key_b { return false; }
};
a_len < b_len
@ -311,17 +307,15 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while a.is_some() && b.is_some() {
let a1 = a.unwrap();
let b1 = b.unwrap();
match a1.cmp(b1) {
Less => a = set_next(&mut x),
Greater => b = set_next(&mut y),
Equal => return false
}
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while a.is_some() && b.is_some() {
let a1 = a.unwrap();
let b1 = b.unwrap();
match a1.cmp(b1) {
Less => a = set_next(&mut x),
Greater => b = set_next(&mut y),
Equal => return false
}
}
true
@ -337,25 +331,23 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
fn is_superset(&self, other: &TreeSet<T>) -> bool {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while b.is_some() {
if a.is_none() {
return false
}
let a1 = a.unwrap();
let b1 = b.unwrap();
match a1.cmp(b1) {
Less => (),
Greater => return false,
Equal => b = set_next(&mut y),
}
a = set_next(&mut x);
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while b.is_some() {
if a.is_none() {
return false
}
let a1 = a.unwrap();
let b1 = b.unwrap();
match a1.cmp(b1) {
Less => (),
Greater => return false,
Equal => b = set_next(&mut y),
}
a = set_next(&mut x);
}
true
}
@ -365,29 +357,27 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while a.is_some() {
if b.is_none() {
return do a.while_some() |a1| {
if f(a1) { set_next(&mut x) } else { None }
}
while a.is_some() {
if b.is_none() {
return do a.while_some() |a1| {
if f(a1) { set_next(&mut x) } else { None }
}
}
let a1 = a.unwrap();
let b1 = b.unwrap();
let a1 = a.unwrap();
let b1 = b.unwrap();
let cmp = a1.cmp(b1);
let cmp = a1.cmp(b1);
if cmp == Less {
if !f(a1) { return }
a = set_next(&mut x);
} else {
if cmp == Equal { a = set_next(&mut x) }
b = set_next(&mut y);
}
if cmp == Less {
if !f(a1) { return }
a = set_next(&mut x);
} else {
if cmp == Equal { a = set_next(&mut x) }
b = set_next(&mut y);
}
}
}
@ -398,37 +388,35 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while a.is_some() {
if b.is_none() {
return do a.while_some() |a1| {
if f(a1) { set_next(&mut x) } else { None }
}
while a.is_some() {
if b.is_none() {
return do a.while_some() |a1| {
if f(a1) { set_next(&mut x) } else { None }
}
}
let a1 = a.unwrap();
let b1 = b.unwrap();
let a1 = a.unwrap();
let b1 = b.unwrap();
let cmp = a1.cmp(b1);
let cmp = a1.cmp(b1);
if cmp == Less {
if !f(a1) { return }
a = set_next(&mut x);
if cmp == Less {
if !f(a1) { return }
a = set_next(&mut x);
} else {
if cmp == Greater {
if !f(b1) { return }
} else {
if cmp == Greater {
if !f(b1) { return }
} else {
a = set_next(&mut x);
}
b = set_next(&mut y);
a = set_next(&mut x);
}
b = set_next(&mut y);
}
do b.while_some |b1| {
if f(b1) { set_next(&mut y) } else { None }
}
}
do b.while_some |b1| {
if f(b1) { set_next(&mut y) } else { None }
}
}
@ -437,24 +425,22 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while a.is_some() && b.is_some() {
let a1 = a.unwrap();
let b1 = b.unwrap();
while a.is_some() && b.is_some() {
let a1 = a.unwrap();
let b1 = b.unwrap();
let cmp = a1.cmp(b1);
let cmp = a1.cmp(b1);
if cmp == Less {
a = set_next(&mut x);
} else {
if cmp == Equal {
if !f(a1) { return }
}
b = set_next(&mut y);
if cmp == Less {
a = set_next(&mut x);
} else {
if cmp == Equal {
if !f(a1) { return }
}
b = set_next(&mut y);
}
}
}
@ -464,36 +450,34 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while a.is_some() {
if b.is_none() {
return do a.while_some() |a1| {
if f(a1) { set_next(&mut x) } else { None }
}
while a.is_some() {
if b.is_none() {
return do a.while_some() |a1| {
if f(a1) { set_next(&mut x) } else { None }
}
}
let a1 = a.unwrap();
let b1 = b.unwrap();
let a1 = a.unwrap();
let b1 = b.unwrap();
let cmp = a1.cmp(b1);
let cmp = a1.cmp(b1);
if cmp == Greater {
if !f(b1) { return }
if cmp == Greater {
if !f(b1) { return }
b = set_next(&mut y);
} else {
if !f(a1) { return }
if cmp == Equal {
b = set_next(&mut y);
} else {
if !f(a1) { return }
if cmp == Equal {
b = set_next(&mut y);
}
a = set_next(&mut x);
}
a = set_next(&mut x);
}
do b.while_some |b1| {
if f(b1) { set_next(&mut y) } else { None }
}
}
do b.while_some |b1| {
if f(b1) { set_next(&mut y) } else { None }
}
}
}