@@ -393,7 +393,7 @@ where
393
393
///
394
394
/// This is equivalent to finding the position with
395
395
/// [`binary_search_keys`][Self::binary_search_keys], then either updating
396
- /// it or calling [`shift_insert `][Self::shift_insert ] for a new key.
396
+ /// it or calling [`insert_before `][Self::insert_before ] for a new key.
397
397
///
398
398
/// If the sorted key is found in the map, its corresponding value is
399
399
/// updated with `value`, and the older value is returned inside
@@ -415,21 +415,115 @@ where
415
415
self . inner . insert_sorted ( key, value)
416
416
}
417
417
418
- /// Insert a key-value pair in the map at the given index.
418
+ /// Insert a key-value pair in the map before the entry at the given index, or at the end .
419
419
///
420
420
/// If an equivalent key already exists in the map: the key remains and
421
421
/// is moved to the new position in the map, its corresponding value is updated
422
+ /// with `value`, and the older value is returned inside `Some(_)`. The returned index
423
+ /// will either be the given index or one less, depending on how the entry moved.
424
+ /// (See [`shift_insert`](Self::shift_insert) for different behavior here.)
425
+ ///
426
+ /// If no equivalent key existed in the map: the new key-value pair is
427
+ /// inserted exactly at the given index, and `None` is returned.
428
+ ///
429
+ /// ***Panics*** if `index` is out of bounds.
430
+ /// Valid indices are `0..=map.len()` (inclusive).
431
+ ///
432
+ /// Computes in **O(n)** time (average).
433
+ ///
434
+ /// See also [`entry`][Self::entry] if you want to insert *or* modify,
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
+ /// ```
466
+ pub fn insert_before ( & mut self , index : usize , key : K , value : V ) -> ( usize , Option < V > ) {
467
+ self . inner . insert_before ( index, key, value)
468
+ }
469
+
470
+ /// Insert a key-value pair in the map at the given index.
471
+ ///
472
+ /// If an equivalent key already exists in the map: the key remains and
473
+ /// is moved to the given index in the map, its corresponding value is updated
422
474
/// with `value`, and the older value is returned inside `Some(_)`.
475
+ /// Note that existing entries **cannot** be moved to `index == map.len()`!
476
+ /// (See [`insert_before`](Self::insert_before) for different behavior here.)
423
477
///
424
478
/// If no equivalent key existed in the map: the new key-value pair is
425
479
/// inserted at the given index, and `None` is returned.
426
480
///
427
481
/// ***Panics*** if `index` is out of bounds.
482
+ /// Valid indices are `0..map.len()` (exclusive) when moving an existing entry, or
483
+ /// `0..=map.len()` (inclusive) when inserting a new key.
428
484
///
429
485
/// Computes in **O(n)** time (average).
430
486
///
431
487
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
432
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
+ /// ```
433
527
pub fn shift_insert ( & mut self , index : usize , key : K , value : V ) -> Option < V > {
434
528
self . inner . shift_insert ( index, key, value)
435
529
}
@@ -687,6 +781,7 @@ impl<K, V, S> OrderMap<K, V, S> {
687
781
/// This preserves the order of the remaining elements.
688
782
///
689
783
/// Computes in **O(1)** time (average).
784
+ #[ doc( alias = "pop_last" ) ] // like `BTreeMap`
690
785
pub fn pop ( & mut self ) -> Option < ( K , V ) > {
691
786
self . inner . pop ( )
692
787
}
@@ -932,6 +1027,7 @@ impl<K, V, S> OrderMap<K, V, S> {
932
1027
/// Get the first key-value pair
933
1028
///
934
1029
/// Computes in **O(1)** time.
1030
+ #[ doc( alias = "first_key_value" ) ] // like `BTreeMap`
935
1031
pub fn first ( & self ) -> Option < ( & K , & V ) > {
936
1032
self . inner . first ( )
937
1033
}
@@ -943,9 +1039,17 @@ impl<K, V, S> OrderMap<K, V, S> {
943
1039
self . inner . first_mut ( )
944
1040
}
945
1041
1042
+ /// Get the first entry in the map for in-place manipulation.
1043
+ ///
1044
+ /// Computes in **O(1)** time.
1045
+ pub fn first_entry ( & mut self ) -> Option < IndexedEntry < ' _ , K , V > > {
1046
+ self . inner . first_entry ( ) . map ( IndexedEntry :: new)
1047
+ }
1048
+
946
1049
/// Get the last key-value pair
947
1050
///
948
1051
/// Computes in **O(1)** time.
1052
+ #[ doc( alias = "last_key_value" ) ] // like `BTreeMap`
949
1053
pub fn last ( & self ) -> Option < ( & K , & V ) > {
950
1054
self . inner . last ( )
951
1055
}
@@ -957,6 +1061,13 @@ impl<K, V, S> OrderMap<K, V, S> {
957
1061
self . inner . last_mut ( )
958
1062
}
959
1063
1064
+ /// Get the last entry in the map for in-place manipulation.
1065
+ ///
1066
+ /// Computes in **O(1)** time.
1067
+ pub fn last_entry ( & mut self ) -> Option < IndexedEntry < ' _ , K , V > > {
1068
+ self . inner . last_entry ( ) . map ( IndexedEntry :: new)
1069
+ }
1070
+
960
1071
/// Remove the key-value pair by index
961
1072
///
962
1073
/// Valid indices are *0 <= index < self.len()*
0 commit comments