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