private void FilterTextChanged()
 {
     if (string.IsNullOrEmpty(FilterText))
     {
         FilteredStops.Clear();
     }
     else
     {
         var query = FilterText.ToLower();
         FilteredStops.ReplaceRange(
             AllStops.Where(s => s.Name.ToLower().Contains(query) || s.Locality.ToLower().Contains(query)).Take(25));
     }
 }
        private void UpdateStops(bool filterOnly = false)
        {
            if (!filterOnly)
            {
                NearbyStops.ReplaceRange(
                    AllStops.OrderBy(s => s.Data.Distance).Take(15)
                    );
                FavoriteStops.ReplaceRange(
                    AllStops.Where(s => s.Data.IsFavorite)
                    );
            }

            FilterTextChanged();
        }
        public override List <Edge> GetShortestRoute(int sourceStopId, int destinationStopId, DateTime when)
        {
            Console.WriteLine("Looking for route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");

            var result           = new List <Edge>();
            var completeNodes    = new List <CompleteNode>();
            var inclompleteNodes = new List <Node>();

            bool traceComplete = false;

            // Kezdetben csak a forráspont van kész, nem vezet hozzá semmi
            completeNodes.Add(new CompleteNode
            {
                stopId        = sourceStopId,
                departureTime = when.TimeOfDay,
                viaNode       = null,
                usedEdges     = new Stack <Edge>(),
                usedRouteIds  = new Stack <int>()
            });

            // Mindenki mást pedig várakozik a kifejtésre
            foreach (var stop in AllStops.Where(s => s.DbId != sourceStopId))
            {
                inclompleteNodes.Add(new Node
                {
                    stopId = stop.DbId
                });
            }

            // Az útvonalkereső hurok
            while ((!traceComplete) && (inclompleteNodes.Count > 0))
            {
                Console.WriteLine("Iteration: " + completeNodes.Count + " cnodes, " + inclompleteNodes.Count + " icnodes");
                CompleteNode nextNode = null;

                var lck     = new Object();
                var garbage = new ConcurrentBag <CompleteNode>();

                #region Következő csomópont keresése
                {
                    completeNodes.AsParallel().ForAll(
                        currentNode =>
                    {
                        var candidate = GetNextCompleteNode(currentNode, inclompleteNodes, when);

                        if (candidate != null)
                        {
                            // van kimenő él

                            // atomi művelet!!
                            lock (lck)
                            {
                                if ((nextNode == null) || (candidate.departureTime < nextNode.departureTime))
                                {
                                    nextNode = candidate;
                                }
                            }
                        }
                        else
                        {
                            garbage.Add(currentNode);
                        }
                    });
                }
                #endregion

                if (nextNode == null)
                {
                    Console.WriteLine("No route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                    break;
                }

                // Takarítás
                garbage.ToList().ForEach(g => completeNodes.Remove(g));
                garbage = new ConcurrentBag <CompleteNode>();

                // Áthelyezés a kész csomópontok közé
                completeNodes.Add(nextNode);
                var removedNode = inclompleteNodes.Single(n => n.stopId == nextNode.stopId);
                inclompleteNodes.Remove(removedNode);

                // Ha megtaláltuk a célt, akkor leállunk
                if (nextNode.stopId == destinationStopId)
                {
                    traceComplete = true;
                    Console.WriteLine("Trace complete between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                    Console.WriteLine("Shortest path Found. # of used routes: " + nextNode.usedRouteIds.Count + " | arrival: " + nextNode.departureTime.ToString());

                    result.AddRange(nextNode.usedEdges.Reverse());
                    result.ToList().ForEach(
                        e =>
                    {
                        Console.WriteLine(e.GetType().Name + " " + e.ToString());
                    });
                }
            }

            return(result);
        }
 private void BusStopFavorited(BusStopFavorited e)
 {
     FavoriteStops.ReplaceRange(
         AllStops.Where(s => s.Data.IsFavorite)
         );
 }
        public override List <Edge> GetShortestRoute(int sourceStopId, int destinationStopId, DateTime when)
        {
            if (!_priorityQueues.ContainsKey(Thread.CurrentThread))
            {
                var storage = new BinaryHeapPriorityQueue();
                _priorityQueues.Add(Thread.CurrentThread, storage);
            }

            var result = new List <Edge>();

            #region Dijkstra
            {
                int iterationCount = 0;

                Console.WriteLine("Looking for route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");

                var  inclompleteNodes = new List <Node>();
                bool traceComplete    = false;

                // Kezdetben csak a forráspont van kész, nem vezet hozzá semmi
                var currentNode = new CompleteNode
                {
                    stopId        = sourceStopId,
                    departureTime = when.TimeOfDay,
                    viaNode       = null,
                    usedEdges     = new Stack <Edge>(),
                    usedRouteIds  = new Stack <int>()
                };

                // Mindenki mást pedig várakozik a kifejtésre
                foreach (var stop in AllStops.Where(s => s.DbId != sourceStopId))
                {
                    inclompleteNodes.Add(new Node
                    {
                        stopId = stop.DbId
                    });
                }

                // Az útvonalkereső hurok
                while ((!traceComplete) && (inclompleteNodes.Count > 0))
                {
                    if ((iterationCount % 25) == 0)
                    {
                        Console.WriteLine("Iteration: " + ((iterationCount / 25) + 1) + " x 25 | with " + inclompleteNodes.Count + " icnodes");
                        iterationCount++;
                    }

                    currentNode = GetNextCompleteNode(currentNode, inclompleteNodes, when);

                    if (currentNode == null)
                    {
                        Console.WriteLine("No route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                        break;
                    }

                    // Áthelyezés a kész csomópontok közé
                    {
                        var removedNode = inclompleteNodes.Single(n => n.stopId == currentNode.stopId);
                        inclompleteNodes.Remove(removedNode);
                    }

                    // Ha megtaláltuk a célt, akkor leállunk
                    if (currentNode.stopId == destinationStopId)
                    {
                        traceComplete = true;
                        Console.WriteLine("Trace complete between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                        Console.WriteLine("Shortest path Found. # of used routes: " + currentNode.usedRouteIds.Count + " | arrival: " + currentNode.departureTime.ToString());

                        result.AddRange(currentNode.usedEdges.Reverse());
                        result.ForEach(
                            e =>
                        {
                            Console.WriteLine(e.GetType().Name + " " + e.ToString());
                        });
                    }
                }
            }
            #endregion

            if (_priorityQueues.ContainsKey(Thread.CurrentThread))
            {
                _priorityQueues.Remove(Thread.CurrentThread);
            }

            return(result);
        }