Created
February 28, 2022 19:47
-
-
Save xacrimon/fe1e8367fb61ffac8a06afbf20590555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use hashbrown::{hash_map, HashMap}; | |
use super::TaggedHandle; | |
pub struct ObjectSet { | |
set: HashMap<TaggedHandle, (), ()>, | |
} | |
impl ObjectSet { | |
pub fn new() -> Self { | |
Self { | |
set: HashMap::with_hasher(()), | |
} | |
} | |
fn entry_mut( | |
&mut self, | |
handle: TaggedHandle, | |
) -> hash_map::RawEntryMut<'_, TaggedHandle, (), ()> { | |
let hash = handle.hash(); | |
self.set | |
.raw_entry_mut() | |
.from_hash(hash, |other| handle.value() == other.value()) | |
} | |
pub fn insert(&mut self, handle: TaggedHandle) { | |
if let hash_map::RawEntryMut::Vacant(entry) = self.entry_mut(handle) { | |
let hash = handle.hash(); | |
entry.insert_with_hasher(hash, handle, (), |handle| handle.value() as u64); | |
} else { | |
unreachable!() | |
} | |
} | |
pub fn remove(&mut self, handle: TaggedHandle) { | |
if let hash_map::RawEntryMut::Occupied(entry) = self.entry_mut(handle) { | |
entry.remove(); | |
} | |
unreachable!() | |
} | |
fn contains(&self, handle: TaggedHandle) -> bool { | |
let hash = handle.hash(); | |
self.set | |
.raw_entry() | |
.from_hash(hash, |other| handle.value() == other.value()) | |
.is_some() | |
} | |
pub fn iter(&self) -> impl Iterator<Item = TaggedHandle> + '_ { | |
self.set.keys().copied() | |
} | |
pub fn difference<'a>(&'a self, other: &'a Self) -> impl Iterator<Item = TaggedHandle> + 'a { | |
self.set | |
.keys() | |
.filter(|handle| !other.contains(**handle)) | |
.copied() | |
} | |
pub fn clear(&mut self) { | |
self.set.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment