Skip to content

Commit

Permalink
Document the first/last methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Dec 15, 2020
1 parent eca535d commit 4819253
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,18 +717,30 @@ impl<K, V, S> IndexMap<K, V, S> {
self.as_entries_mut().get_mut(index).map(Bucket::muts)
}

/// Get the first key-value pair
///
/// Computes in **O(1)** time.
pub fn first(&self) -> Option<(&K, &V)> {
self.as_entries().first().map(Bucket::refs)
}

/// Get the first key-value pair, with mutable access to the value
///
/// Computes in **O(1)** time.
pub fn first_mut(&mut self) -> Option<(&K, &mut V)> {
self.as_entries_mut().first_mut().map(Bucket::ref_mut)
}

/// Get the last key-value pair
///
/// Computes in **O(1)** time.
pub fn last(&self) -> Option<(&K, &V)> {
self.as_entries().last().map(Bucket::refs)
}

/// Get the last key-value pair, with mutable access to the value
///
/// Computes in **O(1)** time.
pub fn last_mut(&mut self) -> Option<(&K, &mut V)> {
self.as_entries_mut().last_mut().map(Bucket::ref_mut)
}
Expand Down
6 changes: 6 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,16 @@ impl<T, S> IndexSet<T, S> {
self.as_entries().get(index).map(Bucket::key_ref)
}

/// Get the first value
///
/// Computes in **O(1)** time.
pub fn first(&self) -> Option<&T> {
self.as_entries().first().map(Bucket::key_ref)
}

/// Get the last value
///
/// Computes in **O(1)** time.
pub fn last(&self) -> Option<&T> {
self.as_entries().last().map(Bucket::key_ref)
}
Expand Down

0 comments on commit 4819253

Please sign in to comment.