13
13
use prelude:: * ;
14
14
15
15
// FIXME: #3469: need to manually update TrieNode when SHIFT changes
16
+ // FIXME: #5244: need to manually update the TrieNode constructor
16
17
const SHIFT : uint = 4 ;
17
18
const SIZE : uint = 1 << SHIFT ;
18
19
const MASK : uint = SIZE - 1 ;
@@ -56,7 +57,7 @@ impl<T> Container for TrieMap<T> {
56
57
pure fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
57
58
}
58
59
59
- impl < T : Copy > Mutable for TrieMap < T > {
60
+ impl < T > Mutable for TrieMap < T > {
60
61
/// Clear the map, removing all values.
61
62
#[ inline( always) ]
62
63
fn clear ( & mut self ) {
@@ -65,7 +66,7 @@ impl<T: Copy> Mutable for TrieMap<T> {
65
66
}
66
67
}
67
68
68
- impl < T : Copy > Map < uint , T > for TrieMap < T > {
69
+ impl < T > Map < uint , T > for TrieMap < T > {
69
70
/// Return true if the map contains a value for the specified key
70
71
#[ inline( always) ]
71
72
pure fn contains_key ( & self , key : & uint ) -> bool {
@@ -127,7 +128,7 @@ impl<T: Copy> Map<uint, T> for TrieMap<T> {
127
128
}
128
129
}
129
130
130
- impl < T : Copy > TrieMap < T > {
131
+ impl < T > TrieMap < T > {
131
132
#[ inline( always) ]
132
133
static pure fn new( ) -> TrieMap <T > {
133
134
TrieMap { root: TrieNode :: new( ) , length: 0 }
@@ -209,10 +210,15 @@ struct TrieNode<T> {
209
210
children : [ Child < T > * 16 ] // FIXME: #3469: can't use the SIZE constant yet
210
211
}
211
212
212
- impl < T : Copy > TrieNode < T > {
213
+ impl < T > TrieNode < T > {
213
214
#[ inline( always) ]
214
215
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 ] }
216
222
}
217
223
}
218
224
@@ -260,12 +266,16 @@ pure fn chunk(n: uint, idx: uint) -> uint {
260
266
( n >> ( SHIFT * real_idx) ) & MASK
261
267
}
262
268
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 ,
264
270
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 {
266
276
External ( stored_key, stored_value) => {
267
277
if stored_key == key {
268
- false // already in the trie
278
+ External ( stored_key , value )
269
279
} else {
270
280
// conflict - split the node
271
281
let mut new = ~TrieNode :: new ( ) ;
@@ -274,20 +284,24 @@ fn insert<T: Copy>(count: &mut uint, child: &mut Child<T>, key: uint,
274
284
stored_key, stored_value, idx + 1 ) ;
275
285
insert ( & mut new. count , & mut new. children [ chunk ( key, idx) ] , key,
276
286
value, idx + 1 ) ;
277
- * child = Internal ( new ) ;
278
- true
287
+ added = true ;
288
+ Internal ( new )
279
289
}
280
290
}
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
+
284
297
}
285
298
Nothing => {
286
299
* count += 1 ;
287
- * child = External ( key , value ) ;
288
- true
300
+ added = true ;
301
+ External ( key , value )
289
302
}
290
- }
303
+ } ;
304
+ added
291
305
}
292
306
293
307
fn remove < T > ( count : & mut uint , child : & mut Child < T > , key : uint ,
0 commit comments