Skip to content

Commit d60a725

Browse files
committed
auto merge of #5503 : thestinger/rust/trie, r=pcwalton
2 parents 1e41bc7 + 705c796 commit d60a725

File tree

3 files changed

+106
-121
lines changed

3 files changed

+106
-121
lines changed

src/libcore/trie.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
use prelude::*;
1414

15-
// FIXME: #3469: need to manually update TrieNode when SHIFT changes
1615
// FIXME: #5244: need to manually update the TrieNode constructor
1716
const SHIFT: uint = 4;
1817
const SIZE: uint = 1 << SHIFT;
@@ -162,12 +161,15 @@ pub struct TrieSet {
162161

163162
impl BaseIter<uint> for TrieSet {
164163
/// Visit all values in order
164+
#[inline(always)]
165165
fn each(&self, f: &fn(&uint) -> bool) { self.map.each_key(f) }
166+
#[inline(always)]
166167
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
167168
}
168169

169170
impl ReverseIter<uint> for TrieSet {
170171
/// Visit all values in reverse order
172+
#[inline(always)]
171173
fn each_reverse(&self, f: &fn(&uint) -> bool) {
172174
self.map.each_key_reverse(f)
173175
}
@@ -189,7 +191,7 @@ impl Mutable for TrieSet {
189191
fn clear(&mut self) { self.map.clear() }
190192
}
191193

192-
impl TrieSet {
194+
pub impl TrieSet {
193195
/// Create an empty TrieSet
194196
#[inline(always)]
195197
fn new() -> TrieSet {
@@ -215,7 +217,7 @@ impl TrieSet {
215217

216218
struct TrieNode<T> {
217219
count: uint,
218-
children: [Child<T> * 16] // FIXME: #3469: can't use the SIZE constant yet
220+
children: [Child<T> * SIZE]
219221
}
220222

221223
impl<T> TrieNode<T> {
@@ -301,7 +303,6 @@ fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
301303
added = insert(&mut x.count, &mut x.children[chunk(key, idx)], key,
302304
value, idx + 1);
303305
Internal(x)
304-
305306
}
306307
Nothing => {
307308
*count += 1;

src/libstd/priority_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub impl <T:Ord> PriorityQueue<T> {
112112
while end > 1 {
113113
end -= 1;
114114
q.data[end] <-> q.data[0];
115-
unsafe { q.siftdown_range(0, end) } // purity-checking workaround
115+
q.siftdown_range(0, end)
116116
}
117117
q.to_vec()
118118
}
@@ -126,7 +126,7 @@ pub impl <T:Ord> PriorityQueue<T> {
126126
let mut n = q.len() / 2;
127127
while n > 0 {
128128
n -= 1;
129-
unsafe { q.siftdown(n) }; // purity-checking workaround
129+
q.siftdown(n)
130130
}
131131
q
132132
}

src/libstd/treemap.rs

Lines changed: 99 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ impl<K: Eq + TotalOrd, V: Eq> Eq for TreeMap<K, V> {
4343
let mut x = self.iter();
4444
let mut y = other.iter();
4545
for self.len().times {
46-
unsafe { // unsafe as a purity workaround
47-
if map_next(&mut x).unwrap() !=
48-
map_next(&mut y).unwrap() {
49-
return false
50-
}
46+
if map_next(&mut x).unwrap() !=
47+
map_next(&mut y).unwrap() {
48+
return false
5149
}
5250
}
5351
true
@@ -64,12 +62,10 @@ fn lt<K: Ord + TotalOrd, V>(a: &TreeMap<K, V>,
6462

6563
let (a_len, b_len) = (a.len(), b.len());
6664
for uint::min(a_len, b_len).times {
67-
unsafe { // purity workaround
68-
let (key_a,_) = map_next(&mut x).unwrap();
69-
let (key_b,_) = map_next(&mut y).unwrap();
70-
if *key_a < *key_b { return true; }
71-
if *key_a > *key_b { return false; }
72-
}
65+
let (key_a,_) = map_next(&mut x).unwrap();
66+
let (key_b,_) = map_next(&mut y).unwrap();
67+
if *key_a < *key_b { return true; }
68+
if *key_a > *key_b { return false; }
7369
};
7470

7571
a_len < b_len
@@ -311,17 +307,15 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
311307
fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
312308
let mut x = self.iter();
313309
let mut y = other.iter();
314-
unsafe { // purity workaround
315-
let mut a = set_next(&mut x);
316-
let mut b = set_next(&mut y);
317-
while a.is_some() && b.is_some() {
318-
let a1 = a.unwrap();
319-
let b1 = b.unwrap();
320-
match a1.cmp(b1) {
321-
Less => a = set_next(&mut x),
322-
Greater => b = set_next(&mut y),
323-
Equal => return false
324-
}
310+
let mut a = set_next(&mut x);
311+
let mut b = set_next(&mut y);
312+
while a.is_some() && b.is_some() {
313+
let a1 = a.unwrap();
314+
let b1 = b.unwrap();
315+
match a1.cmp(b1) {
316+
Less => a = set_next(&mut x),
317+
Greater => b = set_next(&mut y),
318+
Equal => return false
325319
}
326320
}
327321
true
@@ -337,25 +331,23 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
337331
fn is_superset(&self, other: &TreeSet<T>) -> bool {
338332
let mut x = self.iter();
339333
let mut y = other.iter();
340-
unsafe { // purity workaround
341-
let mut a = set_next(&mut x);
342-
let mut b = set_next(&mut y);
343-
while b.is_some() {
344-
if a.is_none() {
345-
return false
346-
}
347-
348-
let a1 = a.unwrap();
349-
let b1 = b.unwrap();
334+
let mut a = set_next(&mut x);
335+
let mut b = set_next(&mut y);
336+
while b.is_some() {
337+
if a.is_none() {
338+
return false
339+
}
350340

351-
match a1.cmp(b1) {
352-
Less => (),
353-
Greater => return false,
354-
Equal => b = set_next(&mut y),
355-
}
341+
let a1 = a.unwrap();
342+
let b1 = b.unwrap();
356343

357-
a = set_next(&mut x);
344+
match a1.cmp(b1) {
345+
Less => (),
346+
Greater => return false,
347+
Equal => b = set_next(&mut y),
358348
}
349+
350+
a = set_next(&mut x);
359351
}
360352
true
361353
}
@@ -365,29 +357,27 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
365357
let mut x = self.iter();
366358
let mut y = other.iter();
367359

368-
unsafe { // purity workaround
369-
let mut a = set_next(&mut x);
370-
let mut b = set_next(&mut y);
360+
let mut a = set_next(&mut x);
361+
let mut b = set_next(&mut y);
371362

372-
while a.is_some() {
373-
if b.is_none() {
374-
return do a.while_some() |a1| {
375-
if f(a1) { set_next(&mut x) } else { None }
376-
}
363+
while a.is_some() {
364+
if b.is_none() {
365+
return do a.while_some() |a1| {
366+
if f(a1) { set_next(&mut x) } else { None }
377367
}
368+
}
378369

379-
let a1 = a.unwrap();
380-
let b1 = b.unwrap();
370+
let a1 = a.unwrap();
371+
let b1 = b.unwrap();
381372

382-
let cmp = a1.cmp(b1);
373+
let cmp = a1.cmp(b1);
383374

384-
if cmp == Less {
385-
if !f(a1) { return }
386-
a = set_next(&mut x);
387-
} else {
388-
if cmp == Equal { a = set_next(&mut x) }
389-
b = set_next(&mut y);
390-
}
375+
if cmp == Less {
376+
if !f(a1) { return }
377+
a = set_next(&mut x);
378+
} else {
379+
if cmp == Equal { a = set_next(&mut x) }
380+
b = set_next(&mut y);
391381
}
392382
}
393383
}
@@ -398,37 +388,35 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
398388
let mut x = self.iter();
399389
let mut y = other.iter();
400390

401-
unsafe { // purity workaround
402-
let mut a = set_next(&mut x);
403-
let mut b = set_next(&mut y);
391+
let mut a = set_next(&mut x);
392+
let mut b = set_next(&mut y);
404393

405-
while a.is_some() {
406-
if b.is_none() {
407-
return do a.while_some() |a1| {
408-
if f(a1) { set_next(&mut x) } else { None }
409-
}
394+
while a.is_some() {
395+
if b.is_none() {
396+
return do a.while_some() |a1| {
397+
if f(a1) { set_next(&mut x) } else { None }
410398
}
399+
}
411400

412-
let a1 = a.unwrap();
413-
let b1 = b.unwrap();
401+
let a1 = a.unwrap();
402+
let b1 = b.unwrap();
414403

415-
let cmp = a1.cmp(b1);
404+
let cmp = a1.cmp(b1);
416405

417-
if cmp == Less {
418-
if !f(a1) { return }
419-
a = set_next(&mut x);
406+
if cmp == Less {
407+
if !f(a1) { return }
408+
a = set_next(&mut x);
409+
} else {
410+
if cmp == Greater {
411+
if !f(b1) { return }
420412
} else {
421-
if cmp == Greater {
422-
if !f(b1) { return }
423-
} else {
424-
a = set_next(&mut x);
425-
}
426-
b = set_next(&mut y);
413+
a = set_next(&mut x);
427414
}
415+
b = set_next(&mut y);
428416
}
429-
do b.while_some |b1| {
430-
if f(b1) { set_next(&mut y) } else { None }
431-
}
417+
}
418+
do b.while_some |b1| {
419+
if f(b1) { set_next(&mut y) } else { None }
432420
}
433421
}
434422

@@ -437,24 +425,22 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
437425
let mut x = self.iter();
438426
let mut y = other.iter();
439427

440-
unsafe { // purity workaround
441-
let mut a = set_next(&mut x);
442-
let mut b = set_next(&mut y);
428+
let mut a = set_next(&mut x);
429+
let mut b = set_next(&mut y);
443430

444-
while a.is_some() && b.is_some() {
445-
let a1 = a.unwrap();
446-
let b1 = b.unwrap();
431+
while a.is_some() && b.is_some() {
432+
let a1 = a.unwrap();
433+
let b1 = b.unwrap();
447434

448-
let cmp = a1.cmp(b1);
435+
let cmp = a1.cmp(b1);
449436

450-
if cmp == Less {
451-
a = set_next(&mut x);
452-
} else {
453-
if cmp == Equal {
454-
if !f(a1) { return }
455-
}
456-
b = set_next(&mut y);
437+
if cmp == Less {
438+
a = set_next(&mut x);
439+
} else {
440+
if cmp == Equal {
441+
if !f(a1) { return }
457442
}
443+
b = set_next(&mut y);
458444
}
459445
}
460446
}
@@ -464,36 +450,34 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
464450
let mut x = self.iter();
465451
let mut y = other.iter();
466452

467-
unsafe { // purity workaround
468-
let mut a = set_next(&mut x);
469-
let mut b = set_next(&mut y);
453+
let mut a = set_next(&mut x);
454+
let mut b = set_next(&mut y);
470455

471-
while a.is_some() {
472-
if b.is_none() {
473-
return do a.while_some() |a1| {
474-
if f(a1) { set_next(&mut x) } else { None }
475-
}
456+
while a.is_some() {
457+
if b.is_none() {
458+
return do a.while_some() |a1| {
459+
if f(a1) { set_next(&mut x) } else { None }
476460
}
461+
}
477462

478-
let a1 = a.unwrap();
479-
let b1 = b.unwrap();
463+
let a1 = a.unwrap();
464+
let b1 = b.unwrap();
480465

481-
let cmp = a1.cmp(b1);
466+
let cmp = a1.cmp(b1);
482467

483-
if cmp == Greater {
484-
if !f(b1) { return }
468+
if cmp == Greater {
469+
if !f(b1) { return }
470+
b = set_next(&mut y);
471+
} else {
472+
if !f(a1) { return }
473+
if cmp == Equal {
485474
b = set_next(&mut y);
486-
} else {
487-
if !f(a1) { return }
488-
if cmp == Equal {
489-
b = set_next(&mut y);
490-
}
491-
a = set_next(&mut x);
492475
}
476+
a = set_next(&mut x);
493477
}
494-
do b.while_some |b1| {
495-
if f(b1) { set_next(&mut y) } else { None }
496-
}
478+
}
479+
do b.while_some |b1| {
480+
if f(b1) { set_next(&mut y) } else { None }
497481
}
498482
}
499483
}

0 commit comments

Comments
 (0)