pub struct Workspace<'g> { /* private fields */ }Expand description
Workspace holds the reusable per-solve buffers for one Graph. Create one
per thread (it borrows the graph immutably); it is not shareable while in
use because its methods take &mut self.
Implementations§
Source§impl Workspace<'_>
impl Workspace<'_>
Sourcepub fn assign(&mut self, dest_id: usize, demand: &[f64]) -> DestResult<'_>
pub fn assign(&mut self, dest_id: usize, demand: &[f64]) -> DestResult<'_>
Runs the full assignment (optimal strategy + demand loading) for one
destination index. demand is a per-node slice of trips heading to
dest_id (demand[dest_id] is ignored). The result borrows the
workspace and is valid until the next assign on it.
Sourcepub fn solve_each<F: FnMut(&DestResult<'_>)>(
&mut self,
od: &HashMap<String, HashMap<String, f64>>,
callback: F,
)
pub fn solve_each<F: FnMut(&DestResult<'_>)>( &mut self, od: &HashMap<String, HashMap<String, f64>>, callback: F, )
Assigns every destination present in od (an
origin -> destination -> demand matrix) and calls callback with the
arena-indexed result for each. It transposes od into per-destination
columns once (reusing the workspace buffers), so there are no
per-destination allocations after warm-up. The result is reused between
calls, so copy out anything that must outlive the callback.
§Example
Build the graph once, reuse the workspace, and assign a full OD (here one trip A -> B on the paper network):
use std::collections::{HashMap, HashSet};
use hyperpaths_rs::{DestResult, Graph, Link};
let nodes: HashSet<String> = ["A", "X", "X2", "Y", "Y3", "B"]
.iter()
.map(|s| s.to_string())
.collect();
let links = vec![
Link::new("A", "B", "Line 1", 25.0, 6.0),
Link::new("A", "X2", "Line 2", 7.0, 6.0),
Link::new("X2", "X", "Line 2", 0.0, 0.0),
Link::new("X", "X2", "Line 2", 0.0, 6.0),
Link::new("X2", "Y", "Line 2", 6.0, 0.0),
Link::new("Y3", "Y", "Line 3", 0.0, 15.0),
Link::new("Y", "B", "Line 4", 10.0, 3.0),
Link::new("X", "Y3", "Line 3", 4.0, 15.0),
Link::new("Y", "Y3", "Line 3", 0.0, 15.0),
Link::new("Y3", "B", "Line 3", 4.0, 0.0),
];
let graph = Graph::new(&links, &nodes);
let mut w = graph.new_workspace();
let a = graph.node_index("A").unwrap();
let od = HashMap::from([("A".to_string(), HashMap::from([("B".to_string(), 1.0)]))]);
let mut a_to_b = 0.0;
w.solve_each(&od, |res: &DestResult| {
a_to_b = res.labels[a];
});
assert!((a_to_b - 27.75).abs() < 1e-9);