mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-05 01:20:20 +00:00
Move stdlib into its own crate
This commit is contained in:
parent
4502d2630f
commit
c7a923d226
11 changed files with 334 additions and 81 deletions
42
stdlib/src/set.rs
Normal file
42
stdlib/src/set.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use im_rc::hashset::HashSet;
|
||||
use std::hash::{Hash};
|
||||
use im_rc::vector::Vector;
|
||||
|
||||
/// A persistent Set which records insertion order and iterates in that order.
|
||||
pub struct Set<K> {
|
||||
store: HashSet<K>,
|
||||
order: Vector<K>
|
||||
}
|
||||
|
||||
impl<K> Set<K>
|
||||
where K : std::hash::Hash
|
||||
{
|
||||
pub fn is_empty(self) -> bool {
|
||||
self.store.is_empty()
|
||||
}
|
||||
|
||||
pub fn insert<B>(self, elem: K) -> Set<B>
|
||||
{
|
||||
let mut new_set: Set<K> = self.clone();
|
||||
|
||||
new_set.store.insert(elem);
|
||||
new_set.order.insert(elem);
|
||||
|
||||
new_set
|
||||
}
|
||||
|
||||
pub fn map<F, B>(self, transform: F) -> Set<B>
|
||||
where F: Fn(K) -> B,
|
||||
B: Hash
|
||||
{
|
||||
let mut new_set: Set<B> = Set::new();
|
||||
|
||||
for elem in self.order.iter() {
|
||||
if self.store.contains(elem) {
|
||||
new_set.insert(transform(elem))
|
||||
}
|
||||
}
|
||||
|
||||
new_set
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue