Пример #1
0
        public List <IJourney <TNode> > BreadthFirstSearchRoutesWithPortRepetition(TNode start, TNode destination,
                                                                                   int maxNumberOfStops, Func <int, IJourney <TNode>, bool> journeyComparer)
        {
            IJourney <TNode> journey = new Journey <TNode>(_routeRepository).WithPort(start);

            var status = new BreadthFirstSearchRoutesStatus <TNode>
            {
                NodeToProcessQueue = new Queue <KeyValuePair <IJourney <TNode>, IRoute <TNode> > >(),
                ResultJourneys     = new List <IJourney <TNode> >()
            };

            status.NodeToProcessQueue.Enqueue(new KeyValuePair <IJourney <TNode>, IRoute <TNode> >(journey, new Route <TNode>(default(TNode), start, 0)));

            while (status.NodeToProcessQueue.Count != 0)
            {
                var currentNode = status.NodeToProcessQueue.Dequeue();

                //could this scope be an async call !?!?
                if (maxNumberOfStops == 0 && status.NodeToProcessQueue.Count == 0)
                {
                    return(status.ResultJourneys);
                }

                var currentjourney = currentNode.Key;
                if (journeyComparer(maxNumberOfStops, currentjourney))
                {
                    break;
                }

                //this can be an async call.
                status = ProcessAdjacentRoutes(destination, currentNode, status);
            }
            return(status.ResultJourneys);
        }
Пример #2
0
        private BreadthFirstSearchRoutesStatus <TNode> ProcessAdjacentRoutes(TNode destination, KeyValuePair <IJourney <TNode>, IRoute <TNode> > currentNode,
                                                                             BreadthFirstSearchRoutesStatus <TNode> status)
        {
            var adjacentRoutes = GetAdjacentRoutes(currentNode.Value.Destination);

            foreach (var route in adjacentRoutes)
            {
                var nextjourney = GetNextJourney(currentNode, route);
                status = ProcessRoute(destination, route, nextjourney, status);
            }
            return(status);
        }
Пример #3
0
        private async Task <BreadthFirstSearchRoutesStatus <TNode> > ProcessAdjacentRoutesAsync(TNode destination, KeyValuePair <IJourney <TNode>, IRoute <TNode> > currentNode,
                                                                                                BreadthFirstSearchRoutesStatus <TNode> status)
        {
            var adjacentRoutes = GetAdjacentRoutes(currentNode.Value.Destination);

            Parallel.ForEach(adjacentRoutes, route =>
            {
                var nextjourney = GetNextJourney(currentNode, route);
                status          = ProcessRoute(destination, route, nextjourney, status);
            });

            return(status);
        }
Пример #4
0
        private BreadthFirstSearchRoutesStatus <TNode> ProcessRoute(TNode destination, IRoute <TNode> route, IJourney <TNode> nextjourney, BreadthFirstSearchRoutesStatus <TNode> status)
        {
            //for testing purposes.
            //to prove that async is faster only if the proccess is slower.
            //Task.Delay(500);
            if (route.Destination.Equals(destination))
            {
                status.ResultJourneys.Add(nextjourney);
            }
            else
            {
                status.NodeToProcessQueue.Enqueue(new KeyValuePair <IJourney <TNode>, IRoute <TNode> >(nextjourney, route));
            }

            return(status);
        }