SparseGraphBuilder

Struct SparseGraphBuilder 

Source
pub struct SparseGraphBuilder<T, D> {
    vsize: usize,
    edges: Vec<(usize, usize)>,
    rest: Vec<T>,
    _marker: PhantomData<fn() -> D>,
}

Fields§

§vsize: usize§edges: Vec<(usize, usize)>§rest: Vec<T>§_marker: PhantomData<fn() -> D>

Implementations§

Source§

impl<T, D> SparseGraphBuilder<T, D>

Source

pub fn new(vsize: usize) -> Self

Examples found in repository?
crates/competitive/src/graph/sparse_graph.rs (line 55)
54    pub fn builder<T>(vsize: usize) -> SparseGraphBuilder<T, D> {
55        SparseGraphBuilder::new(vsize)
56    }
Source

pub fn new_with_esize(vsize: usize, esize: usize) -> Self

Examples found in repository?
crates/competitive/src/graph/sparse_graph.rs (line 58)
57    pub fn builder_with_esize<T>(vsize: usize, esize: usize) -> SparseGraphBuilder<T, D> {
58        SparseGraphBuilder::new_with_esize(vsize, esize)
59    }
60}
61
62pub trait SparseGraphConstruction: Sized {
63    fn construct_graph(vsize: usize, edges: Vec<(usize, usize)>) -> SparseGraph<Self>;
64}
65
66impl<D> SparseGraph<D>
67where
68    D: SparseGraphConstruction,
69{
70    /// Construct graph from edges.
71    pub fn from_edges(vsize: usize, edges: Vec<(usize, usize)>) -> Self {
72        D::construct_graph(vsize, edges)
73    }
74    pub fn reverse_graph(&self) -> SparseGraph<D> {
75        let edges = self.edges.iter().map(|&(from, to)| (to, from)).collect();
76        D::construct_graph(self.vsize, edges)
77    }
78}
79
80impl SparseGraphConstruction for DirectedEdge {
81    fn construct_graph(vsize: usize, edges: Vec<(usize, usize)>) -> SparseGraph<Self> {
82        let mut start: Vec<_> = vec![0usize; vsize + 1];
83        for (from, _) in edges.iter().cloned() {
84            start[from] += 1;
85        }
86        for i in 1..=vsize {
87            start[i] += start[i - 1];
88        }
89        let mut elist = Vec::<Adjacency>::with_capacity(edges.len());
90        let ptr = elist.as_mut_ptr();
91        for (id, (from, to)) in edges.iter().cloned().enumerate() {
92            start[from] -= 1;
93            unsafe { ptr.add(start[from]).write(Adjacency::new(id, to)) };
94        }
95        unsafe { elist.set_len(edges.len()) };
96        SparseGraph {
97            vsize,
98            start,
99            elist,
100            edges,
101            _marker: PhantomData,
102        }
103    }
104}
105
106impl SparseGraphConstruction for UndirectedEdge {
107    fn construct_graph(vsize: usize, edges: Vec<(usize, usize)>) -> SparseGraph<Self> {
108        let mut start: Vec<_> = vec![0usize; vsize + 1];
109        for (from, to) in edges.iter().cloned() {
110            start[to] += 1;
111            start[from] += 1;
112        }
113        for i in 1..=vsize {
114            start[i] += start[i - 1];
115        }
116        let mut elist = Vec::<Adjacency>::with_capacity(edges.len() * 2);
117        let ptr = elist.as_mut_ptr();
118        for (id, (from, to)) in edges.iter().cloned().enumerate() {
119            start[from] -= 1;
120            unsafe { ptr.add(start[from]).write(Adjacency::new(id, to)) };
121            start[to] -= 1;
122            unsafe { ptr.add(start[to]).write(Adjacency::new(id, from)) };
123        }
124        unsafe { elist.set_len(edges.len() * 2) };
125        SparseGraph {
126            vsize,
127            start,
128            elist,
129            edges,
130            _marker: PhantomData,
131        }
132    }
133}
134
135impl SparseGraphConstruction for BidirectionalEdge {
136    fn construct_graph(vsize: usize, edges: Vec<(usize, usize)>) -> SparseGraph<Self> {
137        let mut start: Vec<_> = vec![0usize; vsize + 1];
138        for (from, to) in edges.iter().cloned() {
139            start[to] += 1;
140            start[from] += 1;
141        }
142        for i in 1..=vsize {
143            start[i] += start[i - 1];
144        }
145        let mut elist = Vec::<Adjacency>::with_capacity(edges.len() * 2);
146        let ptr = elist.as_mut_ptr();
147        for (id, (from, to)) in edges.iter().cloned().enumerate() {
148            start[from] -= 1;
149            unsafe { ptr.add(start[from]).write(Adjacency::new(id * 2, to)) };
150            start[to] -= 1;
151            unsafe { ptr.add(start[to]).write(Adjacency::new(id * 2 + 1, from)) };
152        }
153        unsafe { elist.set_len(edges.len() * 2) };
154        SparseGraph {
155            vsize,
156            start,
157            elist,
158            edges,
159            _marker: PhantomData,
160        }
161    }
162}
163
164pub type DirectedSparseGraph = SparseGraph<DirectedEdge>;
165pub type UndirectedSparseGraph = SparseGraph<UndirectedEdge>;
166pub type BidirectionalSparseGraph = SparseGraph<BidirectionalEdge>;
167
168pub struct SparseGraphBuilder<T, D> {
169    vsize: usize,
170    edges: Vec<(usize, usize)>,
171    rest: Vec<T>,
172    _marker: Marker<D>,
173}
174impl<T, D> SparseGraphBuilder<T, D> {
175    pub fn new(vsize: usize) -> Self {
176        Self {
177            vsize,
178            edges: Default::default(),
179            rest: Default::default(),
180            _marker: PhantomData,
181        }
182    }
183    pub fn new_with_esize(vsize: usize, esize: usize) -> Self {
184        Self {
185            vsize,
186            edges: Vec::with_capacity(esize),
187            rest: Vec::with_capacity(esize),
188            _marker: PhantomData,
189        }
190    }
191    pub fn add_edge(&mut self, u: usize, v: usize, w: T) {
192        self.edges.push((u, v));
193        self.rest.push(w);
194    }
195}
196impl<T, D> SparseGraphBuilder<T, D>
197where
198    D: SparseGraphConstruction,
199{
200    pub fn build(self) -> (SparseGraph<D>, Vec<T>) {
201        let graph = SparseGraph::from_edges(self.vsize, self.edges);
202        (graph, self.rest)
203    }
204}
205
206pub struct SparseGraphScanner<U, T, D>
207where
208    U: IterScan<Output = usize>,
209    T: IterScan,
210{
211    vsize: usize,
212    esize: usize,
213    _marker: Marker<(U, T, D)>,
214}
215
216impl<U, T, D> SparseGraphScanner<U, T, D>
217where
218    U: IterScan<Output = usize>,
219    T: IterScan,
220{
221    pub fn new(vsize: usize, esize: usize) -> Self {
222        Self {
223            vsize,
224            esize,
225            _marker: PhantomData,
226        }
227    }
228}
229
230impl<U, T, D> MarkedIterScan for SparseGraphScanner<U, T, D>
231where
232    U: IterScan<Output = usize>,
233    T: IterScan,
234    D: SparseGraphConstruction,
235{
236    type Output = (SparseGraph<D>, Vec<<T as IterScan>::Output>);
237    fn mscan<'a, I: Iterator<Item = &'a str>>(self, iter: &mut I) -> Option<Self::Output> {
238        let mut builder = SparseGraphBuilder::new_with_esize(self.vsize, self.esize);
239        for _ in 0..self.esize {
240            builder.add_edge(U::scan(iter)?, U::scan(iter)?, T::scan(iter)?);
241        }
242        Some(builder.build())
243    }
Source

pub fn add_edge(&mut self, u: usize, v: usize, w: T)

Examples found in repository?
crates/competitive/src/graph/sparse_graph.rs (line 240)
237    fn mscan<'a, I: Iterator<Item = &'a str>>(self, iter: &mut I) -> Option<Self::Output> {
238        let mut builder = SparseGraphBuilder::new_with_esize(self.vsize, self.esize);
239        for _ in 0..self.esize {
240            builder.add_edge(U::scan(iter)?, U::scan(iter)?, T::scan(iter)?);
241        }
242        Some(builder.build())
243    }
Source§

impl<T, D> SparseGraphBuilder<T, D>

Source

pub fn build(self) -> (SparseGraph<D>, Vec<T>)

Examples found in repository?
crates/competitive/src/graph/sparse_graph.rs (line 242)
237    fn mscan<'a, I: Iterator<Item = &'a str>>(self, iter: &mut I) -> Option<Self::Output> {
238        let mut builder = SparseGraphBuilder::new_with_esize(self.vsize, self.esize);
239        for _ in 0..self.esize {
240            builder.add_edge(U::scan(iter)?, U::scan(iter)?, T::scan(iter)?);
241        }
242        Some(builder.build())
243    }

Auto Trait Implementations§

§

impl<T, D> Freeze for SparseGraphBuilder<T, D>

§

impl<T, D> RefUnwindSafe for SparseGraphBuilder<T, D>
where T: RefUnwindSafe,

§

impl<T, D> Send for SparseGraphBuilder<T, D>
where T: Send,

§

impl<T, D> Sync for SparseGraphBuilder<T, D>
where T: Sync,

§

impl<T, D> Unpin for SparseGraphBuilder<T, D>
where T: Unpin,

§

impl<T, D> UnwindSafe for SparseGraphBuilder<T, D>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.