Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ where
self.get_index_of(key).is_some()
}

/// Return a reference to the value stored for `key`, if it is present,
/// Return a reference to the stored value for `key`, if it is present,
/// else `None`.
///
/// Computes in **O(1)** time (average).
Expand All @@ -844,7 +844,7 @@ where
}
}

/// Return references to the key-value pair stored for `key`,
/// Return references to the stored key-value pair for the lookup `key`,
/// if it is present, else `None`.
///
/// Computes in **O(1)** time (average).
Expand All @@ -860,7 +860,10 @@ where
}
}

/// Return item index, key and value
/// Return the index with references to the stored key-value pair for the
/// lookup `key`, if it is present, else `None`.
///
/// Computes in **O(1)** time (average).
pub fn get_full<Q>(&self, key: &Q) -> Option<(usize, &K, &V)>
where
Q: ?Sized + Hash + Equivalent<K>,
Expand All @@ -873,7 +876,7 @@ where
}
}

/// Return item index, if it exists in the map
/// Return the item index for `key`, if it is present, else `None`.
///
/// Computes in **O(1)** time (average).
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
Expand All @@ -890,6 +893,10 @@ where
}
}

/// Return a mutable reference to the stored value for `key`,
/// if it is present, else `None`.
///
/// Computes in **O(1)** time (average).
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
Q: ?Sized + Hash + Equivalent<K>,
Expand All @@ -902,6 +909,26 @@ where
}
}

/// Return a reference and mutable references to the stored key-value pair
/// for the lookup `key`, if it is present, else `None`.
///
/// Computes in **O(1)** time (average).
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
if let Some(i) = self.get_index_of(key) {
let entry = &mut self.as_entries_mut()[i];
Some((&entry.key, &mut entry.value))
} else {
None
}
}

/// Return the index with a reference and mutable reference to the stored
/// key-value pair for the lookup `key`, if it is present, else `None`.
///
/// Computes in **O(1)** time (average).
pub fn get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)>
where
Q: ?Sized + Hash + Equivalent<K>,
Expand Down