fn calc_best_paths(pairs_and_prices: &[(ProviderPair<C>, A)]) -> Result<BTreeMap<Pair<C>, PricePath<C, A>>, CalculatorError> {
        // get unique and indexed currencies and providers
        let currencies = pairs_and_prices.iter().flat_map(|(ProviderPair { pair: Pair{source, target}, .. }, ..)| vec![source, target].into_iter())
            .collect::<BTreeSet<_>>().into_iter().collect::<Vec<_>>();
        let providers = pairs_and_prices.iter().map(|(ProviderPair { provider, .. }, ..)| provider)
            .collect::<BTreeSet<_>>().into_iter().collect::<Vec<_>>();
        let currencies_by_ind = currencies.iter().enumerate().map(|(i, &x)| (x, i)).collect::<BTreeMap<_, _>>();
        let providers_by_ind = providers.iter().enumerate().map(|(i, &x)| (x, i)).collect::<BTreeMap<_, _>>();

        // construct the graph for Floyd-Warshall lib
        let mut graph = Vec::new();
        for &c in currencies.iter() {
            for (pp, cost) in pairs_and_prices {
                if c == &pp.pair.source {
                    graph.push(algo::Edge {
                        pair:        algo::Pair{source: currencies_by_ind[&pp.pair.source], target: currencies_by_ind[&pp.pair.target]},
                        provider:    providers_by_ind[&pp.provider],
                        cost:        TryInto::<u128>::try_into(*cost).map_err(|_| CalculatorError::ConversionError)? as f64 / PRECISION
                    });
                }
            }
        }

        // run Floyd-Warshall for all combinations of currencies in the graph
        let res = algo::longest_paths_mult(&graph)?;
        let res_map = res.into_iter().map(|(algo::Pair{source, target}, algo::Path{total_cost, edges})| {
            let pair = Pair{source: currencies[source].clone(), target: currencies[target].clone()};
            let total_cost_u128 = (total_cost * PRECISION) as u128;
            let path = PricePath{ total_cost: total_cost_u128.try_into().ok().unwrap(), steps: edges.into_iter().map(|algo::Edge{pair: algo::Pair{source, target, ..}, provider, cost}|
                PathStep{pair: Pair{source: currencies[source].clone(), target: currencies[target].clone()}, provider: providers[provider].clone(), cost: ((cost * PRECISION) as u128).try_into().ok().unwrap()}).collect()
            };
            (pair, path)
        }).collect::<BTreeMap<_, _>>();
        Ok(res_map)
	}
/// Wraps Floyd-Warshall's algorithm that uses indexes from/into BestPathCalculator data structures
Wraps Floyd-Warshall's algorithm that uses indexes from/into BestPathCalculator data structures
Wraps Floyd - Warshall ' s algorithm that uses indexes from / into BestPathCalculator data structures
fn calc_best_paths(pairs_and_prices: &[(ProviderPair<C>, A)]) -> Result<BTreeMap<Pair<C>, PricePath<C, A>>, CalculatorError> {
        // get unique and indexed currencies and providers
        let currencies = pairs_and_prices.iter().flat_map(|(ProviderPair { pair: Pair{source, target}, .. }, ..)| vec![source, target].into_iter())
            .collect::<BTreeSet<_>>().into_iter().collect::<Vec<_>>();
        let providers = pairs_and_prices.iter().map(|(ProviderPair { provider, .. }, ..)| provider)
            .collect::<BTreeSet<_>>().into_iter().collect::<Vec<_>>();
        let currencies_by_ind = currencies.iter().enumerate().map(|(i, &x)| (x, i)).collect::<BTreeMap<_, _>>();
        let providers_by_ind = providers.iter().enumerate().map(|(i, &x)| (x, i)).collect::<BTreeMap<_, _>>();
        // construct the graph for Floyd-Warshall lib
        let mut graph = Vec::new();
        for &c in currencies.iter() {
            for (pp, cost) in pairs_and_prices {
                if c == &pp.pair.source {
                    graph.push(algo::Edge {
                        pair:        algo::Pair{source: currencies_by_ind[&pp.pair.source], target: currencies_by_ind[&pp.pair.target]},
                        provider:    providers_by_ind[&pp.provider],
                        cost:        TryInto::<u128>::try_into(*cost).map_err(|_| CalculatorError::ConversionError)? as f64 / PRECISION
                    });
                }
            }
        }
        // run Floyd-Warshall for all combinations of currencies in the graph
        let res = algo::longest_paths_mult(&graph)?;
        let res_map = res.into_iter().map(|(algo::Pair{source, target}, algo::Path{total_cost, edges})| {
            let pair = Pair{source: currencies[source].clone(), target: currencies[target].clone()};
            let total_cost_u128 = (total_cost * PRECISION) as u128;
            let path = PricePath{ total_cost: total_cost_u128.try_into().ok().unwrap(), steps: edges.into_iter().map(|algo::Edge{pair: algo::Pair{source, target, ..}, provider, cost}|
                PathStep{pair: Pair{source: currencies[source].clone(), target: currencies[target].clone()}, provider: providers[provider].clone(), cost: ((cost * PRECISION) as u128).try_into().ok().unwrap()}).collect()
            };
            (pair, path)
        }).collect::<BTreeMap<_, _>>();
        Ok(res_map)
	}
fn calc_best_paths ( pairs_and_prices : & [ ( ProviderPair < C > , A ) ] ) -> Result < BTreeMap < Pair < C > , PricePath < C , A > > , CalculatorError > { // get unique and indexed currencies and providers let currencies = pairs_and_prices . iter ( ) . flat_map ( | ( ProviderPair { pair : Pair { source , target } , .. } , .. ) | vec ! [ source target ] . into_iter ( ) ) . collect :: < BTreeSet < _ > > ( ) . into_iter ( ) . collect :: < Vec < _ > > ( ) ; let providers = pairs_and_prices . iter ( ) . map ( | ( ProviderPair { provider , .. } , .. ) | provider ) . collect :: < BTreeSet < _ > > ( ) . into_iter ( ) . collect :: < Vec < _ > > ( ) ; let currencies_by_ind = currencies . iter ( ) . enumerate ( ) . map ( | ( i , & x ) | ( x , i ) ) . collect :: < BTreeMap < _ , _ > > ( ) ; let providers_by_ind = providers . iter ( ) . enumerate ( ) . map ( | ( i , & x ) | ( x , i ) ) . collect :: < BTreeMap < _ , _ > > ( ) ; // construct the graph for Floyd-Warshall lib let mut graph = Vec :: new ( ) ; for & c in currencies . iter ( ) { for ( pp , cost ) in pairs_and_prices { if c == & pp . pair . source { graph . push ( algo :: Edge { pair : algo :: Pair { source : currencies_by_ind [ & pp . pair . source ] , target : currencies_by_ind [ & pp . pair . target ] } , provider : providers_by_ind [ & pp . provider ] , cost : TryInto :: < u128 > :: try_into ( * cost ) . map_err ( | _ | CalculatorError :: ConversionError ) ? as f64 / PRECISION } ) ; } } } // run Floyd-Warshall for all combinations of currencies in the graph let res = algo :: longest_paths_mult ( & graph ) ? ; let res_map = res . into_iter ( ) . map ( | ( algo :: Pair { source , target } , algo :: Path { total_cost , edges } ) | { let pair = Pair { source : currencies [ source ] . clone ( ) , target : currencies [ target ] . clone ( ) } ; let total_cost_u128 = ( total_cost * PRECISION ) as u128 ; let path = PricePath { total_cost : total_cost_u128 . try_into ( ) . ok ( ) . unwrap ( ) , steps : edges . into_iter ( ) . map ( | algo :: Edge { pair : algo :: Pair { source , target , .. } , provider , cost } | PathStep { pair : Pair { source : currencies [ source ] . clone ( ) , target : currencies [ target ] . clone ( ) } , provider : providers [ provider ] . clone ( ) , cost : ( ( cost * PRECISION ) as u128 ) . try_into ( ) . ok ( ) . unwrap ( ) } ) . collect ( ) } ; ( pair , path ) } ) . collect :: < BTreeMap < _ , _ > > ( ) ; Ok ( res_map ) }
Wraps Floyd-Warshall's algorithm that uses indexes from/into BestPathCalculator data structures
Wraps Floyd - Warshall ' s algorithm that uses indexes from / into BestPathCalculator data structures
pairs_and_prices
&[(ProviderPair<C>, A)]
pairs_and_prices
&[(ProviderPair<C>, A)]