Skip to content

Commit

Permalink
Set::capacity, adjusted inlines
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Nov 29, 2023
1 parent b7d57af commit 9857495
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Set` now implements `Serialize` and `Deserialize` when the `serde` feature is
enabled.
- `Set` and `Map` now implement `Hash`.
- `Set::capacity` returns the currently allocated capacity.

## v0.2.1

Expand Down
26 changes: 23 additions & 3 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ where
///
/// This is similar to using [`Map::entry`], except this function does not
/// require that `Key` implement [`ToOwned`].
#[inline]
pub fn insert_with(&mut self, key: Key, value: impl FnOnce() -> Value) -> Option<Key> {
match self.find_key_index(&key) {
Err(insert_at) => {
Expand Down Expand Up @@ -258,7 +259,6 @@ where
}
}

#[inline]
fn find_key<SearchFor>(&self, search_for: &SearchFor) -> Result<&Field<Key, Value>, usize>
where
Key: Sort<SearchFor>,
Expand All @@ -268,7 +268,6 @@ where
.map(|index| &self.fields[index])
}

#[inline]
fn find_key_mut<SearchFor>(
&mut self,
search_for: &SearchFor,
Expand All @@ -281,7 +280,6 @@ where
.map(|index| &mut self.fields[index])
}

#[inline]
fn find_key_index<SearchFor>(&self, search_for: &SearchFor) -> Result<usize, usize>
where
Key: Sort<SearchFor>,
Expand Down Expand Up @@ -481,6 +479,7 @@ where

/// Returns an iterator that returns all of the elements in this collection.
/// After the iterator is dropped, this object will be empty.
#[inline]
pub fn drain(&mut self) -> Drain<'_, Key, Value> {
Drain(self.fields.drain(..))
}
Expand All @@ -494,6 +493,7 @@ where
/// This iterator is guaranteed to return results in the sort order of the
/// `Key` type.
#[must_use]
#[inline]
pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, Key, Value> {
Union {
left: self.iter().peekable(),
Expand All @@ -511,6 +511,7 @@ where
/// This iterator is guaranteed to return results in the sort order of the
/// `Key` type.
#[must_use]
#[inline]
pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, Key, Value> {
Intersection {
left: self.iter().peekable(),
Expand All @@ -528,6 +529,7 @@ where
/// This iterator is guaranteed to return results in the sort order of the
/// `Key` type.
#[must_use]
#[inline]
pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, Key, Value> {
Difference {
left: self.iter().peekable(),
Expand All @@ -553,6 +555,7 @@ where
}

impl<'key, K> From<K> for SearchKey<'key, K, K> {
#[inline]
fn from(value: K) -> Self {
SearchKey::Owned(value)
}
Expand All @@ -562,6 +565,7 @@ impl<'key, Key, Borrowed> From<&'key Borrowed> for SearchKey<'key, Key, Borrowed
where
Borrowed: ?Sized,
{
#[inline]
fn from(value: &'key Borrowed) -> Self {
SearchKey::Borrowed(value)
}
Expand All @@ -572,13 +576,15 @@ where
Key: Borrow<Borrowed>,
Borrowed: ToOwned<Owned = Key> + ?Sized,
{
#[inline]
fn as_ref(&self) -> &Borrowed {
match self {
SearchKey::Borrowed(key) => key,
SearchKey::Owned(owned) => owned.borrow(),
}
}

#[inline]
fn into_owned(self) -> Key {
match self {
SearchKey::Borrowed(key) => key.to_owned(),
Expand All @@ -592,6 +598,7 @@ where
Key: Debug + Sort<Key>,
Value: Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = f.debug_map();
for Field { key, value } in self {
Expand Down Expand Up @@ -621,6 +628,7 @@ where
type IntoIter = IntoIter<Key, Value>;
type Item = Field<Key, Value>;

#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.fields.into_iter())
}
Expand Down Expand Up @@ -739,6 +747,7 @@ where
/// mutable reference to the entry's value is returned.
///
/// This function does not change the entry if it is present.
#[inline]
pub fn or_default(self) -> &'a mut Value
where
Key: Borrow<BorrowedKey>,
Expand Down Expand Up @@ -1455,6 +1464,7 @@ where
{
type Item = Unioned<'a, K, V>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some(left) = self.left.peek() {
if let Some(right) = self.right.peek() {
Expand All @@ -1476,6 +1486,7 @@ where
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.left.len(), Some(self.left.len() + self.right.len()))
}
Expand Down Expand Up @@ -1539,6 +1550,7 @@ impl<'a, K, V> Unioned<'a, K, V> {
/// assert_eq!(merged.get("a"), Some(&1));
/// assert_eq!(merged.get("b"), Some(&2));
/// ```
#[inline]
pub fn map_both<R>(self, merge: impl FnOnce(&'a K, &'a V, &'a V) -> R) -> EntryRef<'a, K, V>
where
R: Into<OwnedOrRef<'a, V>>,
Expand Down Expand Up @@ -1591,6 +1603,7 @@ pub struct EntryRef<'a, K, V> {
impl<'a, K, V> EntryRef<'a, K, V> {
/// Returns the owned versions of the contained key and value, cloning as
/// needed.
#[inline]
pub fn into_owned(self) -> (K, V)
where
K: Clone,
Expand All @@ -1614,6 +1627,7 @@ pub enum OwnedOrRef<'a, K> {
impl<'a, K> OwnedOrRef<'a, K> {
/// Converts the contained value into an owned representation, cloning only
/// if needed.
#[inline]
pub fn into_owned(self) -> K
where
K: Clone,
Expand All @@ -1626,12 +1640,14 @@ impl<'a, K> OwnedOrRef<'a, K> {
}

impl<K> From<K> for OwnedOrRef<'_, K> {
#[inline]
fn from(value: K) -> Self {
Self::Owned(value)
}
}

impl<'a, K> From<&'a K> for OwnedOrRef<'a, K> {
#[inline]
fn from(value: &'a K) -> Self {
Self::Ref(value)
}
Expand All @@ -1658,6 +1674,7 @@ where
{
type Item = (&'a K, &'a V, &'a V);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
let left = self.left.peek()?;
Expand All @@ -1678,6 +1695,7 @@ where
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.left.len().min(self.right.len())))
}
Expand Down Expand Up @@ -1705,6 +1723,7 @@ where
{
type Item = (&'a K, &'a V);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
let left = self.left.peek()?;
Expand All @@ -1729,6 +1748,7 @@ where
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.left.len()))
}
Expand Down
Loading

0 comments on commit 9857495

Please sign in to comment.