Add basic roc_std Dict and Set implementations

This commit is contained in:
Richard Feldman 2022-08-15 10:59:10 -04:00
parent 97fef48864
commit ec0e80f5d2
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
3 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,35 @@
use crate::roc_dict::RocDict;
use core::{
fmt::{self, Debug},
hash::Hash,
};
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RocSet<T>(RocDict<T, ()>);
impl<T> RocSet<T> {
#[allow(unused)]
pub fn with_capacity(capacity: usize) -> Self {
Self(RocDict::with_capacity(capacity))
}
#[allow(unused)]
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.0.iter_keys()
}
}
impl<T: Hash> RocSet<T> {
#[allow(unused)]
pub fn from_iter<I: Iterator<Item = T>>(src: I) -> Self {
Self(RocDict::from_iter(src.map(|elem| (elem, ()))))
}
}
impl<T: Debug> Debug for RocSet<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("RocSet ")?;
f.debug_set().entries(self.iter()).finish()
}
}