vrp_scientific/common/
mod.rs

1//! Contains common text reading and writing functionality.
2
3mod text_reader;
4
5pub(crate) use self::text_reader::*;
6use std::sync::Arc;
7
8mod text_writer;
9pub(crate) use self::text_writer::*;
10
11mod initial_reader;
12pub use self::initial_reader::read_init_solution;
13
14mod routing;
15
16pub use self::routing::{CoordIndex, CoordIndexExtraProperty};
17
18use vrp_core::models::Extras;
19use vrp_core::solver::{HeuristicFilterExtraProperty, HeuristicFilterFn};
20
21pub(crate) fn get_extras(coord_index: CoordIndex) -> Extras {
22    let mut extras = Extras::default();
23    let heuristic_filter_fn: HeuristicFilterFn = Arc::new(|name| name != "local_reschedule_departure");
24
25    extras.set_coord_index(Arc::new(coord_index));
26    extras.set_heuristic_filter(Arc::new(heuristic_filter_fn));
27
28    extras
29}
30
31/// A trait to get tuple from collection items.
32/// See `<https://stackoverflow.com/questions/38863781/how-to-create-a-tuple-from-a-vector>`
33pub(crate) trait TryCollect<T> {
34    fn try_collect_tuple(&mut self) -> Option<T>;
35}
36
37/// A macro to get tuple from collection items.
38#[macro_export]
39macro_rules! impl_try_collect_tuple {
40    () => { };
41    ($A:ident $($I:ident)*) => {
42        impl_try_collect_tuple!($($I)*);
43
44        impl<$A: Iterator> TryCollect<($A::Item, $($I::Item),*)> for $A {
45            fn try_collect_tuple(&mut self) -> Option<($A::Item, $($I::Item),*)> {
46                let r = (try_opt!(self.next()),
47                         // hack: we need to use $I in the expansion
48                         $({ let a: $I::Item = try_opt!(self.next()); a}),* );
49                Some(r)
50            }
51        }
52    }
53}
54
55/// A helper macro for getting tuple of collection items.
56#[macro_export]
57macro_rules! try_opt {
58    ($e:expr) => {
59        match $e {
60            Some(e) => e,
61            None => return None,
62        }
63    };
64}
65
66// implement TryCollect<T> where T is a tuple with size 1, 2, .., 10
67impl_try_collect_tuple!(A A A A A A A A A A);