Skip to content

Commit 7c9b808

Browse files
committed
auto merge of #5245 : thestinger/rust/trie, r=graydon
2 parents dec599f + ab5bc5d commit 7c9b808

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/libcore/trie.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use prelude::*;
1414

1515
// FIXME: #3469: need to manually update TrieNode when SHIFT changes
16+
// FIXME: #5244: need to manually update the TrieNode constructor
1617
const SHIFT: uint = 4;
1718
const SIZE: uint = 1 << SHIFT;
1819
const MASK: uint = SIZE - 1;
@@ -56,7 +57,7 @@ impl<T> Container for TrieMap<T> {
5657
pure fn is_empty(&self) -> bool { self.len() == 0 }
5758
}
5859

59-
impl<T: Copy> Mutable for TrieMap<T> {
60+
impl<T> Mutable for TrieMap<T> {
6061
/// Clear the map, removing all values.
6162
#[inline(always)]
6263
fn clear(&mut self) {
@@ -65,7 +66,7 @@ impl<T: Copy> Mutable for TrieMap<T> {
6566
}
6667
}
6768

68-
impl<T: Copy> Map<uint, T> for TrieMap<T> {
69+
impl<T> Map<uint, T> for TrieMap<T> {
6970
/// Return true if the map contains a value for the specified key
7071
#[inline(always)]
7172
pure fn contains_key(&self, key: &uint) -> bool {
@@ -127,7 +128,7 @@ impl<T: Copy> Map<uint, T> for TrieMap<T> {
127128
}
128129
}
129130

130-
impl<T: Copy> TrieMap<T> {
131+
impl<T> TrieMap<T> {
131132
#[inline(always)]
132133
static pure fn new() -> TrieMap<T> {
133134
TrieMap{root: TrieNode::new(), length: 0}
@@ -209,10 +210,15 @@ struct TrieNode<T> {
209210
children: [Child<T> * 16] // FIXME: #3469: can't use the SIZE constant yet
210211
}
211212

212-
impl<T: Copy> TrieNode<T> {
213+
impl<T> TrieNode<T> {
213214
#[inline(always)]
214215
static pure fn new() -> TrieNode<T> {
215-
TrieNode{count: 0, children: [Nothing, ..SIZE]}
216+
// FIXME: #5244: [Nothing, ..SIZE] should be possible without Copy
217+
TrieNode{count: 0,
218+
children: [Nothing, Nothing, Nothing, Nothing,
219+
Nothing, Nothing, Nothing, Nothing,
220+
Nothing, Nothing, Nothing, Nothing,
221+
Nothing, Nothing, Nothing, Nothing]}
216222
}
217223
}
218224

@@ -260,12 +266,16 @@ pure fn chunk(n: uint, idx: uint) -> uint {
260266
(n >> (SHIFT * real_idx)) & MASK
261267
}
262268

263-
fn insert<T: Copy>(count: &mut uint, child: &mut Child<T>, key: uint,
269+
fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint,
264270
value: T, idx: uint) -> bool {
265-
match *child {
271+
let mut tmp = Nothing;
272+
tmp <-> *child;
273+
let mut added = false;
274+
275+
*child = match tmp {
266276
External(stored_key, stored_value) => {
267277
if stored_key == key {
268-
false // already in the trie
278+
External(stored_key, value)
269279
} else {
270280
// conflict - split the node
271281
let mut new = ~TrieNode::new();
@@ -274,20 +284,24 @@ fn insert<T: Copy>(count: &mut uint, child: &mut Child<T>, key: uint,
274284
stored_key, stored_value, idx + 1);
275285
insert(&mut new.count, &mut new.children[chunk(key, idx)], key,
276286
value, idx + 1);
277-
*child = Internal(new);
278-
true
287+
added = true;
288+
Internal(new)
279289
}
280290
}
281-
Internal(ref mut x) => {
282-
insert(&mut x.count, &mut x.children[chunk(key, idx)], key, value,
283-
idx + 1)
291+
Internal(x) => {
292+
let mut x = x;
293+
added = insert(&mut x.count, &mut x.children[chunk(key, idx)], key,
294+
value, idx + 1);
295+
Internal(x)
296+
284297
}
285298
Nothing => {
286299
*count += 1;
287-
*child = External(key, value);
288-
true
300+
added = true;
301+
External(key, value)
289302
}
290-
}
303+
};
304+
added
291305
}
292306

293307
fn remove<T>(count: &mut uint, child: &mut Child<T>, key: uint,

0 commit comments

Comments
 (0)