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