@@ -17,13 +17,13 @@ use self::Entry::*;
17
17
18
18
use core:: prelude:: * ;
19
19
20
- use core:: cmp:: Ordering ;
20
+ use core:: cmp:: { max , Ordering } ;
21
21
use core:: default:: Default ;
22
22
use core:: fmt;
23
23
use core:: hash:: { Hash , Hasher } ;
24
24
use core:: iter:: { Enumerate , FilterMap , Map , FromIterator , IntoIterator } ;
25
25
use core:: iter;
26
- use core:: mem:: replace;
26
+ use core:: mem:: { replace, swap } ;
27
27
use core:: ops:: { Index , IndexMut } ;
28
28
29
29
use { vec, slice} ;
@@ -320,6 +320,95 @@ impl<V> VecMap<V> {
320
320
IntoIter { iter : self . v . into_iter ( ) . enumerate ( ) . filter_map ( filter) }
321
321
}
322
322
323
+ /// Moves all elements from `other` into the map while overwriting existing keys.
324
+ ///
325
+ /// # Examples
326
+ ///
327
+ /// ```
328
+ /// use std::collections::VecMap;
329
+ ///
330
+ /// let mut a = VecMap::new();
331
+ /// a.insert(1, "a");
332
+ /// a.insert(2, "b");
333
+ ///
334
+ /// let mut b = VecMap::new();
335
+ /// b.insert(3, "c");
336
+ /// b.insert(4, "d");
337
+ ///
338
+ /// a.append(&mut b);
339
+ ///
340
+ /// assert_eq!(a.len(), 4);
341
+ /// assert_eq!(b.len(), 0);
342
+ /// assert_eq!(a[1], "a");
343
+ /// assert_eq!(a[2], "b");
344
+ /// assert_eq!(a[3], "c");
345
+ /// assert_eq!(a[4], "d");
346
+ /// ```
347
+ #[ unstable( feature = "collections" ,
348
+ reason = "recently added as part of collections reform 2" ) ]
349
+ pub fn append ( & mut self , other : & mut Self ) {
350
+ self . extend ( other. drain ( ) ) ;
351
+ }
352
+
353
+ /// Splits the collection into two at the given key.
354
+ ///
355
+ /// Returns a newly allocated `Self`. `self` contains elements `[0, at)`,
356
+ /// and the returned `Self` contains elements `[at, max_key)`.
357
+ ///
358
+ /// Note that the capacity of `self` does not change.
359
+ ///
360
+ /// # Examples
361
+ ///
362
+ /// ```
363
+ /// use std::collections::VecMap;
364
+ ///
365
+ /// let mut a = VecMap::new();
366
+ /// a.insert(1, "a");
367
+ /// a.insert(2, "b");
368
+ /// a.insert(3, "c");
369
+ /// a.insert(4, "d");
370
+ ///
371
+ /// let b = a.split_off(3);
372
+ ///
373
+ /// assert_eq!(a[1], "a");
374
+ /// assert_eq!(a[2], "b");
375
+ ///
376
+ /// assert_eq!(b[3], "c");
377
+ /// assert_eq!(b[4], "d");
378
+ /// ```
379
+ #[ unstable( feature = "collections" ,
380
+ reason = "recently added as part of collections reform 2" ) ]
381
+ pub fn split_off ( & mut self , at : usize ) -> Self {
382
+ let mut other = VecMap :: new ( ) ;
383
+
384
+ if at == 0 {
385
+ // Move all elements to other
386
+ swap ( self , & mut other) ;
387
+ return other
388
+ } else if at > self . v . len ( ) {
389
+ // No elements to copy
390
+ return other;
391
+ }
392
+
393
+ // Look up the index of the first non-None item
394
+ let first_index = self . v . iter ( ) . position ( |el| el. is_some ( ) ) ;
395
+ let start_index = match first_index {
396
+ Some ( index) => max ( at, index) ,
397
+ None => {
398
+ // self has no elements
399
+ return other;
400
+ }
401
+ } ;
402
+
403
+ // Fill the new VecMap with `None`s until `start_index`
404
+ other. v . extend ( ( 0 ..start_index) . map ( |_| None ) ) ;
405
+
406
+ // Move elements beginning with `start_index` from `self` into `other`
407
+ other. v . extend ( self . v [ start_index..] . iter_mut ( ) . map ( |el| el. take ( ) ) ) ;
408
+
409
+ other
410
+ }
411
+
323
412
/// Returns an iterator visiting all key-value pairs in ascending order of
324
413
/// the keys, emptying (but not consuming) the original `VecMap`.
325
414
/// The iterator's element type is `(usize, &'r V)`. Keeps the allocated memory for reuse.
@@ -1141,6 +1230,85 @@ mod test_map {
1141
1230
assert_eq ! ( map. len( ) , 0 ) ;
1142
1231
}
1143
1232
1233
+ #[ test]
1234
+ fn test_append ( ) {
1235
+ let mut a = VecMap :: new ( ) ;
1236
+ a. insert ( 1 , "a" ) ;
1237
+ a. insert ( 2 , "b" ) ;
1238
+ a. insert ( 3 , "c" ) ;
1239
+
1240
+ let mut b = VecMap :: new ( ) ;
1241
+ b. insert ( 3 , "d" ) ; // Overwrite element from a
1242
+ b. insert ( 4 , "e" ) ;
1243
+ b. insert ( 5 , "f" ) ;
1244
+
1245
+ a. append ( & mut b) ;
1246
+
1247
+ assert_eq ! ( a. len( ) , 5 ) ;
1248
+ assert_eq ! ( b. len( ) , 0 ) ;
1249
+ // Capacity shouldn't change for possible reuse
1250
+ assert ! ( b. capacity( ) >= 4 ) ;
1251
+
1252
+ assert_eq ! ( a[ 1 ] , "a" ) ;
1253
+ assert_eq ! ( a[ 2 ] , "b" ) ;
1254
+ assert_eq ! ( a[ 3 ] , "d" ) ;
1255
+ assert_eq ! ( a[ 4 ] , "e" ) ;
1256
+ assert_eq ! ( a[ 5 ] , "f" ) ;
1257
+ }
1258
+
1259
+ #[ test]
1260
+ fn test_split_off ( ) {
1261
+ // Split within the key range
1262
+ let mut a = VecMap :: new ( ) ;
1263
+ a. insert ( 1 , "a" ) ;
1264
+ a. insert ( 2 , "b" ) ;
1265
+ a. insert ( 3 , "c" ) ;
1266
+ a. insert ( 4 , "d" ) ;
1267
+
1268
+ let b = a. split_off ( 3 ) ;
1269
+
1270
+ assert_eq ! ( a. len( ) , 2 ) ;
1271
+ assert_eq ! ( b. len( ) , 2 ) ;
1272
+
1273
+ assert_eq ! ( a[ 1 ] , "a" ) ;
1274
+ assert_eq ! ( a[ 2 ] , "b" ) ;
1275
+
1276
+ assert_eq ! ( b[ 3 ] , "c" ) ;
1277
+ assert_eq ! ( b[ 4 ] , "d" ) ;
1278
+
1279
+ // Split at 0
1280
+ a. clear ( ) ;
1281
+ a. insert ( 1 , "a" ) ;
1282
+ a. insert ( 2 , "b" ) ;
1283
+ a. insert ( 3 , "c" ) ;
1284
+ a. insert ( 4 , "d" ) ;
1285
+
1286
+ let b = a. split_off ( 0 ) ;
1287
+
1288
+ assert_eq ! ( a. len( ) , 0 ) ;
1289
+ assert_eq ! ( b. len( ) , 4 ) ;
1290
+ assert_eq ! ( b[ 1 ] , "a" ) ;
1291
+ assert_eq ! ( b[ 2 ] , "b" ) ;
1292
+ assert_eq ! ( b[ 3 ] , "c" ) ;
1293
+ assert_eq ! ( b[ 4 ] , "d" ) ;
1294
+
1295
+ // Split behind max_key
1296
+ a. clear ( ) ;
1297
+ a. insert ( 1 , "a" ) ;
1298
+ a. insert ( 2 , "b" ) ;
1299
+ a. insert ( 3 , "c" ) ;
1300
+ a. insert ( 4 , "d" ) ;
1301
+
1302
+ let b = a. split_off ( 5 ) ;
1303
+
1304
+ assert_eq ! ( a. len( ) , 4 ) ;
1305
+ assert_eq ! ( b. len( ) , 0 ) ;
1306
+ assert_eq ! ( a[ 1 ] , "a" ) ;
1307
+ assert_eq ! ( a[ 2 ] , "b" ) ;
1308
+ assert_eq ! ( a[ 3 ] , "c" ) ;
1309
+ assert_eq ! ( a[ 4 ] , "d" ) ;
1310
+ }
1311
+
1144
1312
#[ test]
1145
1313
fn test_show ( ) {
1146
1314
let mut map = VecMap :: new ( ) ;
0 commit comments