Skip to content

Commit 1009781

Browse files
committed
Add doc examples for insert_before and shift_insert
(cherry picked from commit 1d9b5e3)
1 parent 1112578 commit 1009781

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/map.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,36 @@ where
433433
///
434434
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
435435
/// 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+
/// ```
436466
pub fn insert_before(&mut self, index: usize, key: K, value: V) -> (usize, Option<V>) {
437467
self.inner.insert_before(index, key, value)
438468
}
@@ -456,6 +486,44 @@ where
456486
///
457487
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
458488
/// 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+
/// ```
459527
pub fn shift_insert(&mut self, index: usize, key: K, value: V) -> Option<V> {
460528
self.inner.shift_insert(index, key, value)
461529
}

src/set.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,36 @@ where
369369
/// Valid indices are `0..=set.len()` (inclusive).
370370
///
371371
/// Computes in **O(n)** time (average).
372+
///
373+
/// # Examples
374+
///
375+
/// ```
376+
/// use ordermap::OrderSet;
377+
/// let mut set: OrderSet<char> = ('a'..='z').collect();
378+
///
379+
/// // The new value '*' goes exactly at the given index.
380+
/// assert_eq!(set.get_index_of(&'*'), None);
381+
/// assert_eq!(set.insert_before(10, '*'), (10, true));
382+
/// assert_eq!(set.get_index_of(&'*'), Some(10));
383+
///
384+
/// // Moving the value 'a' up will shift others down, so this moves *before* 10 to index 9.
385+
/// assert_eq!(set.insert_before(10, 'a'), (9, false));
386+
/// assert_eq!(set.get_index_of(&'a'), Some(9));
387+
/// assert_eq!(set.get_index_of(&'*'), Some(10));
388+
///
389+
/// // Moving the value 'z' down will shift others up, so this moves to exactly 10.
390+
/// assert_eq!(set.insert_before(10, 'z'), (10, false));
391+
/// assert_eq!(set.get_index_of(&'z'), Some(10));
392+
/// assert_eq!(set.get_index_of(&'*'), Some(11));
393+
///
394+
/// // Moving or inserting before the endpoint is also valid.
395+
/// assert_eq!(set.len(), 27);
396+
/// assert_eq!(set.insert_before(set.len(), '*'), (26, false));
397+
/// assert_eq!(set.get_index_of(&'*'), Some(26));
398+
/// assert_eq!(set.insert_before(set.len(), '+'), (27, true));
399+
/// assert_eq!(set.get_index_of(&'+'), Some(27));
400+
/// assert_eq!(set.len(), 28);
401+
/// ```
372402
pub fn insert_before(&mut self, index: usize, value: T) -> (usize, bool) {
373403
self.inner.insert_before(index, value)
374404
}
@@ -387,6 +417,44 @@ where
387417
/// `0..=set.len()` (inclusive) when inserting a new value.
388418
///
389419
/// Computes in **O(n)** time (average).
420+
///
421+
/// # Examples
422+
///
423+
/// ```
424+
/// use ordermap::OrderSet;
425+
/// let mut set: OrderSet<char> = ('a'..='z').collect();
426+
///
427+
/// // The new value '*' goes exactly at the given index.
428+
/// assert_eq!(set.get_index_of(&'*'), None);
429+
/// assert_eq!(set.shift_insert(10, '*'), true);
430+
/// assert_eq!(set.get_index_of(&'*'), Some(10));
431+
///
432+
/// // Moving the value 'a' up to 10 will shift others down, including the '*' that was at 10.
433+
/// assert_eq!(set.shift_insert(10, 'a'), false);
434+
/// assert_eq!(set.get_index_of(&'a'), Some(10));
435+
/// assert_eq!(set.get_index_of(&'*'), Some(9));
436+
///
437+
/// // Moving the value 'z' down to 9 will shift others up, including the '*' that was at 9.
438+
/// assert_eq!(set.shift_insert(9, 'z'), false);
439+
/// assert_eq!(set.get_index_of(&'z'), Some(9));
440+
/// assert_eq!(set.get_index_of(&'*'), Some(10));
441+
///
442+
/// // Existing values can move to len-1 at most, but new values can insert at the endpoint.
443+
/// assert_eq!(set.len(), 27);
444+
/// assert_eq!(set.shift_insert(set.len() - 1, '*'), false);
445+
/// assert_eq!(set.get_index_of(&'*'), Some(26));
446+
/// assert_eq!(set.shift_insert(set.len(), '+'), true);
447+
/// assert_eq!(set.get_index_of(&'+'), Some(27));
448+
/// assert_eq!(set.len(), 28);
449+
/// ```
450+
///
451+
/// ```should_panic
452+
/// use ordermap::OrderSet;
453+
/// let mut set: OrderSet<char> = ('a'..='z').collect();
454+
///
455+
/// // This is an invalid index for moving an existing value!
456+
/// set.shift_insert(set.len(), 'a');
457+
/// ```
390458
pub fn shift_insert(&mut self, index: usize, value: T) -> bool {
391459
self.inner.shift_insert(index, value)
392460
}

0 commit comments

Comments
 (0)