Skip to content

Commit

Permalink
Add doc examples for insert_before and shift_insert
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Aug 30, 2024
1 parent 8ca01b0 commit 1d9b5e3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,36 @@ where
///
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
/// perhaps only using the index for new entries with [`VacantEntry::shift_insert`].
///
/// # Examples
///
/// ```
/// use indexmap::IndexMap;
/// let mut map: IndexMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect();
///
/// // The new key '*' goes exactly at the given index.
/// assert_eq!(map.get_index_of(&'*'), None);
/// assert_eq!(map.insert_before(10, '*', ()), (10, None));
/// assert_eq!(map.get_index_of(&'*'), Some(10));
///
/// // Moving the key 'a' up will shift others down, so this moves *before* 10 to index 9.
/// assert_eq!(map.insert_before(10, 'a', ()), (9, Some(())));
/// assert_eq!(map.get_index_of(&'a'), Some(9));
/// assert_eq!(map.get_index_of(&'*'), Some(10));
///
/// // Moving the key 'z' down will shift others up, so this moves to exactly 10.
/// assert_eq!(map.insert_before(10, 'z', ()), (10, Some(())));
/// assert_eq!(map.get_index_of(&'z'), Some(10));
/// assert_eq!(map.get_index_of(&'*'), Some(11));
///
/// // Moving or inserting before the endpoint is also valid.
/// assert_eq!(map.len(), 27);
/// assert_eq!(map.insert_before(map.len(), '*', ()), (26, Some(())));
/// assert_eq!(map.get_index_of(&'*'), Some(26));
/// assert_eq!(map.insert_before(map.len(), '+', ()), (27, None));
/// assert_eq!(map.get_index_of(&'+'), Some(27));
/// assert_eq!(map.len(), 28);
/// ```
pub fn insert_before(&mut self, mut index: usize, key: K, value: V) -> (usize, Option<V>) {
assert!(index <= self.len(), "index out of bounds");
match self.entry(key) {
Expand Down Expand Up @@ -503,6 +533,44 @@ where
///
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
/// perhaps only using the index for new entries with [`VacantEntry::shift_insert`].
///
/// # Examples
///
/// ```
/// use indexmap::IndexMap;
/// let mut map: IndexMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect();
///
/// // The new key '*' goes exactly at the given index.
/// assert_eq!(map.get_index_of(&'*'), None);
/// assert_eq!(map.shift_insert(10, '*', ()), None);
/// assert_eq!(map.get_index_of(&'*'), Some(10));
///
/// // Moving the key 'a' up to 10 will shift others down, including the '*' that was at 10.
/// assert_eq!(map.shift_insert(10, 'a', ()), Some(()));
/// assert_eq!(map.get_index_of(&'a'), Some(10));
/// assert_eq!(map.get_index_of(&'*'), Some(9));
///
/// // Moving the key 'z' down to 9 will shift others up, including the '*' that was at 9.
/// assert_eq!(map.shift_insert(9, 'z', ()), Some(()));
/// assert_eq!(map.get_index_of(&'z'), Some(9));
/// assert_eq!(map.get_index_of(&'*'), Some(10));
///
/// // Existing keys can move to len-1 at most, but new keys can insert at the endpoint.
/// assert_eq!(map.len(), 27);
/// assert_eq!(map.shift_insert(map.len() - 1, '*', ()), Some(()));
/// assert_eq!(map.get_index_of(&'*'), Some(26));
/// assert_eq!(map.shift_insert(map.len(), '+', ()), None);
/// assert_eq!(map.get_index_of(&'+'), Some(27));
/// assert_eq!(map.len(), 28);
/// ```
///
/// ```should_panic
/// use indexmap::IndexMap;
/// let mut map: IndexMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect();
///
/// // This is an invalid index for moving an existing key!
/// map.shift_insert(map.len(), 'a', ());
/// ```
pub fn shift_insert(&mut self, index: usize, key: K, value: V) -> Option<V> {
let len = self.len();
match self.entry(key) {
Expand Down
68 changes: 68 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,36 @@ where
/// Valid indices are `0..=set.len()` (inclusive).
///
/// Computes in **O(n)** time (average).
///
/// # Examples
///
/// ```
/// use indexmap::IndexSet;
/// let mut set: IndexSet<char> = ('a'..='z').collect();
///
/// // The new value '*' goes exactly at the given index.
/// assert_eq!(set.get_index_of(&'*'), None);
/// assert_eq!(set.insert_before(10, '*'), (10, true));
/// assert_eq!(set.get_index_of(&'*'), Some(10));
///
/// // Moving the value 'a' up will shift others down, so this moves *before* 10 to index 9.
/// assert_eq!(set.insert_before(10, 'a'), (9, false));
/// assert_eq!(set.get_index_of(&'a'), Some(9));
/// assert_eq!(set.get_index_of(&'*'), Some(10));
///
/// // Moving the value 'z' down will shift others up, so this moves to exactly 10.
/// assert_eq!(set.insert_before(10, 'z'), (10, false));
/// assert_eq!(set.get_index_of(&'z'), Some(10));
/// assert_eq!(set.get_index_of(&'*'), Some(11));
///
/// // Moving or inserting before the endpoint is also valid.
/// assert_eq!(set.len(), 27);
/// assert_eq!(set.insert_before(set.len(), '*'), (26, false));
/// assert_eq!(set.get_index_of(&'*'), Some(26));
/// assert_eq!(set.insert_before(set.len(), '+'), (27, true));
/// assert_eq!(set.get_index_of(&'+'), Some(27));
/// assert_eq!(set.len(), 28);
/// ```
pub fn insert_before(&mut self, index: usize, value: T) -> (usize, bool) {
let (index, existing) = self.map.insert_before(index, value, ());
(index, existing.is_none())
Expand All @@ -415,6 +445,44 @@ where
/// `0..=set.len()` (inclusive) when inserting a new value.
///
/// Computes in **O(n)** time (average).
///
/// # Examples
///
/// ```
/// use indexmap::IndexSet;
/// let mut set: IndexSet<char> = ('a'..='z').collect();
///
/// // The new value '*' goes exactly at the given index.
/// assert_eq!(set.get_index_of(&'*'), None);
/// assert_eq!(set.shift_insert(10, '*'), true);
/// assert_eq!(set.get_index_of(&'*'), Some(10));
///
/// // Moving the value 'a' up to 10 will shift others down, including the '*' that was at 10.
/// assert_eq!(set.shift_insert(10, 'a'), false);
/// assert_eq!(set.get_index_of(&'a'), Some(10));
/// assert_eq!(set.get_index_of(&'*'), Some(9));
///
/// // Moving the value 'z' down to 9 will shift others up, including the '*' that was at 9.
/// assert_eq!(set.shift_insert(9, 'z'), false);
/// assert_eq!(set.get_index_of(&'z'), Some(9));
/// assert_eq!(set.get_index_of(&'*'), Some(10));
///
/// // Existing values can move to len-1 at most, but new values can insert at the endpoint.
/// assert_eq!(set.len(), 27);
/// assert_eq!(set.shift_insert(set.len() - 1, '*'), false);
/// assert_eq!(set.get_index_of(&'*'), Some(26));
/// assert_eq!(set.shift_insert(set.len(), '+'), true);
/// assert_eq!(set.get_index_of(&'+'), Some(27));
/// assert_eq!(set.len(), 28);
/// ```
///
/// ```should_panic
/// use indexmap::IndexSet;
/// let mut set: IndexSet<char> = ('a'..='z').collect();
///
/// // This is an invalid index for moving an existing value!
/// set.shift_insert(set.len(), 'a');
/// ```
pub fn shift_insert(&mut self, index: usize, value: T) -> bool {
self.map.shift_insert(index, value, ()).is_none()
}
Expand Down

0 comments on commit 1d9b5e3

Please sign in to comment.