Skip to content

Trie fixes + rm purity workarounds #5503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/libcore/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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> {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down
214 changes: 99 additions & 115 deletions src/libstd/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
let mut a = set_next(&mut x);
let mut b = set_next(&mut y);
while b.is_some() {
if a.is_none() {
return false
}

match a1.cmp(b1) {
Less => (),
Greater => return false,
Equal => b = set_next(&mut y),
}
let a1 = a.unwrap();
let b1 = b.unwrap();

a = set_next(&mut x);
match a1.cmp(b1) {
Less => (),
Greater => return false,
Equal => b = set_next(&mut y),
}

a = set_next(&mut x);
}
true
}
Expand All @@ -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);
}
}
}
Expand All @@ -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 }
}
}

Expand All @@ -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);
}
}
}
Expand All @@ -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 }
}
}
}
Expand Down