Skip to content

Commit

Permalink
Merge pull request #382 from epage/asdfuser-main
Browse files Browse the repository at this point in the history
feat(stream): Accumulate into BtreeSet and HashSet
  • Loading branch information
epage authored Dec 5, 2023
2 parents 7014156 + 10b02e0 commit c83cc09
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ use crate::error::ErrMode;

#[cfg(feature = "alloc")]
use crate::lib::std::collections::BTreeMap;
#[cfg(feature = "alloc")]
use crate::lib::std::collections::BTreeSet;
#[cfg(feature = "std")]
use crate::lib::std::collections::HashMap;
#[cfg(feature = "std")]
use crate::lib::std::collections::HashSet;
#[cfg(feature = "alloc")]
use crate::lib::std::string::String;
#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -2435,6 +2439,41 @@ where
}
}

#[cfg(feature = "alloc")]
impl<K> Accumulate<K> for BTreeSet<K>
where
K: crate::lib::std::cmp::Ord,
{
#[inline(always)]
fn initial(_capacity: Option<usize>) -> Self {
BTreeSet::new()
}
#[inline(always)]
fn accumulate(&mut self, key: K) {
self.insert(key);
}
}

#[cfg(feature = "std")]
impl<K, S> Accumulate<K> for HashSet<K, S>
where
K: crate::lib::std::cmp::Eq + crate::lib::std::hash::Hash,
S: BuildHasher + Default,
{
#[inline(always)]
fn initial(capacity: Option<usize>) -> Self {
let h = S::default();
match capacity {
Some(capacity) => HashSet::with_capacity_and_hasher(clamp_capacity::<K>(capacity), h),
None => HashSet::with_hasher(h),
}
}
#[inline(always)]
fn accumulate(&mut self, key: K) {
self.insert(key);
}
}

#[cfg(feature = "alloc")]
#[inline]
pub(crate) fn clamp_capacity<T>(capacity: usize) -> usize {
Expand Down

0 comments on commit c83cc09

Please sign in to comment.