use oxigraph::model::{Quad, QuadRef};
use slotmap::{SecondaryMap, SlotMap, new_key_type};
new_key_type! {
pub struct QuadKey;
}
#[derive(Clone, Debug, Default)]
pub struct KeyedDataset<T> {
pub quads: SlotMap<QuadKey, Quad>,
pub associated_data: SecondaryMap<QuadKey, T>,
}
impl<T> FromIterator<Quad> for KeyedDataset<T> {
fn from_iter<U: IntoIterator<Item = Quad>>(iter: U) -> Self {
let mut quads = SlotMap::with_key();
for quad in iter {
quads.insert(quad);
}
Self {
quads,
associated_data: SecondaryMap::new(),
}
}
}
impl<'a, T> IntoIterator for &'a KeyedDataset<T> {
type Item = QuadRef<'a>;
type IntoIter = std::iter::Map<slotmap::basic::Values<'a, QuadKey, Quad>, fn(&Quad) -> QuadRef>;
fn into_iter(self) -> Self::IntoIter {
self.quads.values().map(|quad| quad.as_ref())
}
}
impl<T> KeyedDataset<T> {
pub fn extend(&mut self, quads: impl Iterator<Item = Quad>) -> impl Iterator<Item = QuadKey> {
quads.map(|quad| self.quads.insert(quad))
}
pub fn remove(&mut self, key: QuadKey) {
self.quads.remove(key);
self.associated_data.remove(key);
}
pub fn clear(&mut self) {
self.quads.clear();
self.associated_data.clear();
}
pub fn iter_both(&self) -> impl Iterator<Item = (QuadKey, &Quad, &T)> {
self.quads
.iter()
.filter_map(|(key, quad)| self.associated_data.get(key).map(|ad| (key, quad, ad)))
}
}