pnevyk / gryf

Graph data structure library aspiring to be convenient, versatile, correct and performant.
MIT License
69 stars 1 forks source link

Subset adapter #67

Closed pnevyk closed 1 year ago

pnevyk commented 1 year ago

A Subset adapter would be used to wrap a graph and overriding the implementations of the traits such that vertices and edges are filtered based on some criterion. Rough sketch of the API:

pub struct Subset<G, S = ()> {
    graph: G,
    state: S,
    filter_vertex: Box<dyn Fn(&G::VertexIndex, &G, &S) -> bool>,
    filter_edge: Box<dyn Fn(&G::EdgeIndex, &G, &S) -> bool>,
}

impl<G> Subset<G> {
    pub fn new(graph: G) -> Self {
        Self::with_state(graph, ())
    }
}

impl<G, S> Subset<G, S> {
    pub fn with_state(graph: G, state: S) -> Self {
        Self {
            graph,
            state,
            filter_vertex: Box::new(|_, _, _| true),
            filter_edge: Box::new(|_, _, _| true),
        }
    }

    pub fn into_inner(self) -> G {
        self.graph
    }

    pub fn filter_vertex<F>(self, predicate: F) -> Self
    where
        F: Fn(&G::VertexIndex, &G, &S) -> bool
    {
        Self {
            filter_vertex: Box::new(predicate),
            ..self
        }
    }

    pub fn filter_edge<F>(self, predicate: F) -> Self
    where
        F: Fn(&G::EdgeIndex, &G, &S) -> bool
    {
        Self {
            filter_edge: Box::new(predicate),
            ..self
        }
    }
}

// impl Vertices, Edges, Neighbors, ...