@@ -433,6 +433,36 @@ where
433
433
///
434
434
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
435
435
/// perhaps only using the index for new entries with [`VacantEntry::shift_insert`].
436
+ ///
437
+ /// # Examples
438
+ ///
439
+ /// ```
440
+ /// use ordermap::OrderMap;
441
+ /// let mut map: OrderMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect();
442
+ ///
443
+ /// // The new key '*' goes exactly at the given index.
444
+ /// assert_eq!(map.get_index_of(&'*'), None);
445
+ /// assert_eq!(map.insert_before(10, '*', ()), (10, None));
446
+ /// assert_eq!(map.get_index_of(&'*'), Some(10));
447
+ ///
448
+ /// // Moving the key 'a' up will shift others down, so this moves *before* 10 to index 9.
449
+ /// assert_eq!(map.insert_before(10, 'a', ()), (9, Some(())));
450
+ /// assert_eq!(map.get_index_of(&'a'), Some(9));
451
+ /// assert_eq!(map.get_index_of(&'*'), Some(10));
452
+ ///
453
+ /// // Moving the key 'z' down will shift others up, so this moves to exactly 10.
454
+ /// assert_eq!(map.insert_before(10, 'z', ()), (10, Some(())));
455
+ /// assert_eq!(map.get_index_of(&'z'), Some(10));
456
+ /// assert_eq!(map.get_index_of(&'*'), Some(11));
457
+ ///
458
+ /// // Moving or inserting before the endpoint is also valid.
459
+ /// assert_eq!(map.len(), 27);
460
+ /// assert_eq!(map.insert_before(map.len(), '*', ()), (26, Some(())));
461
+ /// assert_eq!(map.get_index_of(&'*'), Some(26));
462
+ /// assert_eq!(map.insert_before(map.len(), '+', ()), (27, None));
463
+ /// assert_eq!(map.get_index_of(&'+'), Some(27));
464
+ /// assert_eq!(map.len(), 28);
465
+ /// ```
436
466
pub fn insert_before ( & mut self , index : usize , key : K , value : V ) -> ( usize , Option < V > ) {
437
467
self . inner . insert_before ( index, key, value)
438
468
}
@@ -456,6 +486,44 @@ where
456
486
///
457
487
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
458
488
/// perhaps only using the index for new entries with [`VacantEntry::shift_insert`].
489
+ ///
490
+ /// # Examples
491
+ ///
492
+ /// ```
493
+ /// use ordermap::OrderMap;
494
+ /// let mut map: OrderMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect();
495
+ ///
496
+ /// // The new key '*' goes exactly at the given index.
497
+ /// assert_eq!(map.get_index_of(&'*'), None);
498
+ /// assert_eq!(map.shift_insert(10, '*', ()), None);
499
+ /// assert_eq!(map.get_index_of(&'*'), Some(10));
500
+ ///
501
+ /// // Moving the key 'a' up to 10 will shift others down, including the '*' that was at 10.
502
+ /// assert_eq!(map.shift_insert(10, 'a', ()), Some(()));
503
+ /// assert_eq!(map.get_index_of(&'a'), Some(10));
504
+ /// assert_eq!(map.get_index_of(&'*'), Some(9));
505
+ ///
506
+ /// // Moving the key 'z' down to 9 will shift others up, including the '*' that was at 9.
507
+ /// assert_eq!(map.shift_insert(9, 'z', ()), Some(()));
508
+ /// assert_eq!(map.get_index_of(&'z'), Some(9));
509
+ /// assert_eq!(map.get_index_of(&'*'), Some(10));
510
+ ///
511
+ /// // Existing keys can move to len-1 at most, but new keys can insert at the endpoint.
512
+ /// assert_eq!(map.len(), 27);
513
+ /// assert_eq!(map.shift_insert(map.len() - 1, '*', ()), Some(()));
514
+ /// assert_eq!(map.get_index_of(&'*'), Some(26));
515
+ /// assert_eq!(map.shift_insert(map.len(), '+', ()), None);
516
+ /// assert_eq!(map.get_index_of(&'+'), Some(27));
517
+ /// assert_eq!(map.len(), 28);
518
+ /// ```
519
+ ///
520
+ /// ```should_panic
521
+ /// use ordermap::OrderMap;
522
+ /// let mut map: OrderMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect();
523
+ ///
524
+ /// // This is an invalid index for moving an existing key!
525
+ /// map.shift_insert(map.len(), 'a', ());
526
+ /// ```
459
527
pub fn shift_insert ( & mut self , index : usize , key : K , value : V ) -> Option < V > {
460
528
self . inner . shift_insert ( index, key, value)
461
529
}
0 commit comments