From 04e367b353002e247a0b4ebcf2aba59ce01fad73 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 22 Mar 2013 16:19:24 -0400 Subject: [PATCH 1/4] trie: rm workaround for issue #3469 --- src/libcore/trie.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index 258de5c81db..167d44b0b7e 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -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; @@ -215,7 +214,7 @@ impl TrieSet { struct TrieNode { count: uint, - children: [Child * 16] // FIXME: #3469: can't use the SIZE constant yet + children: [Child * SIZE] } impl TrieNode { From 55fbb9518b31e47e796110a1d214a2ef5c11fab5 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 22 Mar 2013 16:21:57 -0400 Subject: [PATCH 2/4] trie: inline the other TrieSet wrapper methods --- src/libcore/trie.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index 167d44b0b7e..598bbbf76c6 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -161,12 +161,15 @@ pub struct TrieSet { impl BaseIter 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 { Some(self.len()) } } impl ReverseIter for TrieSet { /// Visit all values in reverse order + #[inline(always)] fn each_reverse(&self, f: &fn(&uint) -> bool) { self.map.each_key_reverse(f) } From 0c8c3b42326c17d59204106d120508969e2b338c Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 22 Mar 2013 17:40:28 -0400 Subject: [PATCH 3/4] trie: make the TrieSet impl public --- src/libcore/trie.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index 598bbbf76c6..653e29b62a5 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -191,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 { @@ -303,7 +303,6 @@ fn insert(count: &mut uint, child: &mut Child, key: uint, value: T, added = insert(&mut x.count, &mut x.children[chunk(key, idx)], key, value, idx + 1); Internal(x) - } Nothing => { *count += 1; From 705c796ffa78417592b9e36d975cd432a4dc8417 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 22 Mar 2013 18:07:09 -0400 Subject: [PATCH 4/4] remove obsolete purity workarounds --- src/libstd/priority_queue.rs | 4 +- src/libstd/treemap.rs | 218 ++++++++++++++++------------------- 2 files changed, 103 insertions(+), 119 deletions(-) diff --git a/src/libstd/priority_queue.rs b/src/libstd/priority_queue.rs index 03d518f1f63..ff00d26882d 100644 --- a/src/libstd/priority_queue.rs +++ b/src/libstd/priority_queue.rs @@ -112,7 +112,7 @@ pub impl PriorityQueue { 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 PriorityQueue { let mut n = q.len() / 2; while n > 0 { n -= 1; - unsafe { q.siftdown(n) }; // purity-checking workaround + q.siftdown(n) } q } diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index e42c6590724..242ffd07881 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -43,11 +43,9 @@ impl Eq for TreeMap { 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(a: &TreeMap, 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 Set for TreeSet { fn is_disjoint(&self, other: &TreeSet) -> 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 Set for TreeSet { fn is_superset(&self, other: &TreeSet) -> 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 Set for TreeSet { 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 Set for TreeSet { 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 Set for TreeSet { 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 Set for TreeSet { 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 } } } }