当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.Contains方法代码示例

本文整理汇总了C#中System.Collections.Generic.PriorityQueue.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.Contains方法的具体用法?C# PriorityQueue.Contains怎么用?C# PriorityQueue.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Collections.Generic.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.Contains方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestBasicFunctionality

        public void TestBasicFunctionality()
        {
            var q = new PriorityQueue<int>();
            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(0, q.Capacity);
            Assert.Throws<InvalidOperationException>(() => q.Peek());
            Assert.Throws<InvalidOperationException>(() => q.Dequeue());

            q.Enqueue(5);
            q.Enqueue(2);
            q.Enqueue(4);

            Assert.AreEqual(3, q.Count);
            Assert.IsTrue(q.Capacity >= 3);

            Assert.IsTrue(q.Contains(2));
            Assert.IsFalse(q.Contains(3));

            Assert.AreEqual(3, q.ToArray().Length);
            CollectionAssert.AreEqual(q.ToArray(), q);

            Assert.AreEqual(2, q.Peek());

            Assert.AreEqual(2, q.Dequeue());
            Assert.AreEqual(4, q.Dequeue());
            Assert.AreEqual(5, q.Dequeue());
        }
开发者ID:GHScan,项目名称:DailyProjects,代码行数:27,代码来源:PriorityQueueTest.cs

示例2: Main

        public static void Main()
        {
            Node[] nodes = new Node[7];
            nodes[1] = new Node(1);
            nodes[2] = new Node(2);
            nodes[3] = new Node(3);
            nodes[4] = new Node(4);
            nodes[5] = new Node(5);
            nodes[6] = new Node(6);

            InitializeEdges(nodes);
            PriorityQueue<Edge> edges = new PriorityQueue<Edge>();
            bool[] usedNodes = new bool[7];
            Node startNode = nodes[1];
            usedNodes[startNode.Number] = true;
            foreach (var edge in startNode.Edges)
            {
                if (!edges.Contains(edge) && !usedNodes[edge.EndNode.Number])
                {
                    edges.Enqueue(edge);
                }
            }

            List<Edge> traversedEdges = new List<Edge>();

            while (edges.Count != 0)
            {
                Edge currentEdge = edges.Dequeue();
                if (!usedNodes[currentEdge.EndNode.Number])
                {
                    usedNodes[currentEdge.EndNode.Number] = true;
                    traversedEdges.Add(currentEdge);
                    foreach (var edge in currentEdge.EndNode.Edges)
                    {
                        if (!edges.Contains(edge) && !usedNodes[edge.EndNode.Number])
                        {
                            edges.Enqueue(edge);
                        }
                    }
                }
            }

            foreach (var edge in traversedEdges)
            {
                Console.WriteLine(edge);
            }
        }
开发者ID:PetarPenev,项目名称:Telerik,代码行数:47,代码来源:Program.cs

示例3: findPath

        public static Path findPath(Node startNode, Node destination)
        {
            List<Node> closedList = new List<Node>();
            PriorityQueue<Node> openList = new PriorityQueue<Node>();

            Dictionary<Node, int> gScore = new Dictionary<Node,int>();
            gScore[startNode] = 0;

            Dictionary<Node, int> fScore = new Dictionary<Node,int>();
            fScore[startNode] = startNode.HeuristicDistance(destination);

            Dictionary<Node, Node> prevNode = new Dictionary<Node, Node>();

            openList.Add(fScore[startNode], startNode);

            while (openList.Count > 0)
            {
                Node current = openList.RemoveMin();
                if (current.Equals(destination))
                {
                    return getPath(prevNode, destination);
                }
                else
                {
                    closedList.Add(current);
                    Node[] neighbours = current.GetNeighbours();
                    foreach (Node next in neighbours)
                    {
                        if (closedList.Contains(next))
                            continue;

                        int newGScore = gScore[current] + current.distanceBetween(next);

                        if (!openList.Contains(next) || newGScore < gScore[next])
                        {
                            prevNode[next] = current;
                            gScore[next] = newGScore;
                            fScore[next] = gScore[next] + next.HeuristicDistance(destination);
                            if (!openList.Contains(next))
                                openList.Add(fScore[next], next);
                        }
                    }
                }
            }

            return null;
        }
开发者ID:cedwards145,项目名称:ld33,代码行数:47,代码来源:AStarPathfinder.cs

示例4: Default_PriorityQueue_will_contain_all_added_items

        public Property Default_PriorityQueue_will_contain_all_added_items(long[] items)
        {
            var priorityQueue = new PriorityQueue<long>();
            foreach (var item in items)
                priorityQueue.Enqueue(item);

            return items.All(x => priorityQueue.Contains(x)).Label($"Expected priority queue to contain [{string.Join(";", items)}] " +
                                                                   $"but instead was [{string.Join(";", priorityQueue)}] (unsorted)");

        }
开发者ID:helios-io,项目名称:helios,代码行数:10,代码来源:PriorityQueueSpecs.cs

示例5: Search

        public static SearchResult Search(AbstractNode initialNode, IKnowledgeBase kb)
        {
            var frontier = new PriorityQueue<AbstractNode>();
            var explored = new List<AbstractState>();
            var statesSearched = 0; //Bliver kun brugt af os af ren interesse
            var end = initialNode.Target;
            frontier.Add(initialNode);
            explored.Add(initialNode.State);

            while (frontier.Count > 0)
            {
                // Chooses the lowest-cost node in the frontier
                var currentNode = frontier.Pop();
                statesSearched++;
                if (currentNode.State.Equals(end))
                    return new SearchResult(currentNode, statesSearched, true);

                var actions = kb.ActionsForNode(currentNode);
            //Explore /expand the current node
                foreach (var action in actions)
                {
                    var child = kb.Resolve(currentNode, action, end);
                    //System.Console.WriteLine("Frontier.Count: " + frontier.Count);

                    if (!explored.Contains(child.State) && !frontier.Contains(child))
                    {
                        explored.Add(child.State);
                        frontier.Add(child);

                    }
                    else if(true)
                    {
                        for (int i = 0; i < frontier.Count; i++)
                        {
                            var frontierNode = frontier[i];
                            if (frontierNode.State.Equals(child.State) && frontierNode.PathCost > child.PathCost)
                            {
                                frontier[i] = child;
                                break;
                            }
                        }
                    }
                }
            }

            return new SearchResult(null, statesSearched, false);
        }
开发者ID:altschuler,项目名称:a-star,代码行数:47,代码来源:AStar.cs

示例6: getChildren

        static List<Node> getChildren(Node current, List<Node> closedList, PriorityQueue<Node> openList)
        {
            string gameString = current.GameString;
            List<Node> children = new List<Node>();

            for (int i = 0; i < gameString.Length - 1; i++)
            {
                char[] charString = gameString.ToCharArray();

                char temp = charString[i];
                charString[i] = charString[i+1];
                charString[i+1] = temp;

                string s = new string(charString);
                Node childNode = new Node(s);
                childNode.G = current.G + 1;
                childNode.Parent = current;

                //if openlist and closed list does not contain new node, add to children
                if (!openList.Contains(childNode) && !closedList.Contains(childNode) && !childNode.Equals(current) )
                    children.Add(childNode);
            }
            return children;
        }
开发者ID:sseng,项目名称:ai,代码行数:24,代码来源:Program.cs

示例7: ContainsTest

        public void ContainsTest()
        {
            PriorityQueue<int> actual = new PriorityQueue<int> { 45, 12, 1, 0 };

            Assert.IsTrue(actual.Contains(12));
        }
开发者ID:robertrancz,项目名称:dsa,代码行数:6,代码来源:PriorityQueueTest.cs

示例8: ContainsTest

 public void ContainsTest(int[] elemList)
 {
     PriorityQueue<int> actual = new PriorityQueue<int>(elemList);
     for(int i = 0; i < elemList.Length; i++)
         PexAssert.IsTrue(actual.Contains(elemList[i]));
 }
开发者ID:taoxiease,项目名称:asegrp,代码行数:6,代码来源:PriorityQueueTest.cs

示例9: GetPath

        public List<MapTile> GetPath(Point start, Point goal) {
            var startTile = GetTile(start);
            var goalTile = GetTile(goal);

            // check that the start and goal positions are valid, and are not the same
            if (!Within(start) || !Within(goal) || start == goal || startTile == null || goalTile == null) {
                return new List<MapTile>();
            }
            // Check that start and goal are walkable and that a path can exist between them
            if (startTile.Set != goalTile.Set) {
                return new List<MapTile>();
            }


            // reset costs
            foreach (var t in _tiles) {
                t.F = t.G = float.MaxValue;
            }
            var open = new PriorityQueue<MapTile>(_tiles.Length);
            var closed = new HashSet<MapTile>();

            startTile.G = 0;
            startTile.F = h(start, goal);

            open.Enqueue(startTile, startTile.F);

            MapTile current = null;
            while (open.Any() && current != goalTile) {
                current = open.Dequeue();
                closed.Add(current);
                for (var i = 0; i < 8; i++) {
                    var edge = current.Edges[i];

                    if (edge == null) {
                        continue;
                    }
                    var neighbor = edge.Node2;
                    var cost = current.G + edge.Cost;



                    if (open.Contains(neighbor) && cost < neighbor.G) {
                        open.Remove(neighbor);
                    }
                    if (closed.Contains(neighbor) && cost < neighbor.G) {
                        closed.Remove(neighbor);
                    }
                    if (!open.Contains(neighbor) && !closed.Contains(neighbor)) {
                        neighbor.G = cost;
                        var f = cost + h(neighbor.MapPosition, goal);
                        open.Enqueue(neighbor, f);
                        neighbor.Parent = current;

                    }
                }
            }
            System.Diagnostics.Debug.Assert(current == goalTile);
            var path = new List<MapTile>();


            while (current != startTile) {
                path.Add(current);
                current = current.Parent;
            }
            path.Reverse();
            return path;
        }
开发者ID:amitprakash07,项目名称:dx11,代码行数:67,代码来源:Terrain.cs

示例10: Step

        private SearchState Step(PriorityQueue<Node> open)
        {
            Node n, child;
            n = open.Dequeue();
            n.visited = true;
            if(n.Equals(goal)){
                thePath = MakePath(n);
                bestCost = n.cost;
                return SearchState.REACHED_THE_GOAL;
            }

            while(n.HasMoreChildren){
                child = n.GetNextChildren();
                if(child.visited) continue;
                int newCost = n.cost + Cost(n, child);
                if(open.Contains(child) && child.cost <= newCost) continue;
                child.parent = n;
                child.cost = newCost;
                if(!open.Contains(child)) open.Enqueue(child);
                else open.FullRefresh();
            }
            return SearchState.STILL_SEARCH;
        }
开发者ID:AreYouReal,项目名称:DijkstraWalker,代码行数:23,代码来源:DijkstraWalker.cs

示例11: UpdateGraphWithDistancesFromNode

        void UpdateGraphWithDistancesFromNode(Node source)
        {
            PriorityQueue<Node> unVisitedNodes = new PriorityQueue<Node>(Nodes); // Time to create a min heap - O(n)

            // Does this update the value of 'source' in Nodes ?
            source.Distance = 0;

            while (!unVisitedNodes.Empty()) // O(n)
            {
                Node current = unVisitedNodes.Peek();

                if (current.Distance == Constants.INFINITY)
                {
                    break;
                }

                foreach (Node neighbor in current.Neighbors) // O(nm)
                {
                    if (unVisitedNodes.Contains(neighbor))
                    {
                        int tentative = 0;
                        Edge edge = FindEdge(current, neighbor);  // O(nml)
                        tentative = current.Distance + edge.Cost;
                        if (tentative < neighbor.Distance)
                        {
                            neighbor.Distance = tentative;
                            neighbor.Previous = current;
                        }
                    }
                }

                unVisitedNodes.Dequeue();
            }
        }
开发者ID:VegaFromLyra,项目名称:Djikstra,代码行数:34,代码来源:Graph.cs

示例12: WorkPriorityQueue

        public void WorkPriorityQueue()
        {
            PriorityQueue<string> q1 = new PriorityQueue<string>();
            PriorityQueue<int> q2 = new PriorityQueue<int>();
            PriorityQueue<Group> q3 = new PriorityQueue<Group>();

            q1.Enqueue(new List<string> { "A", "B", "C" }, 5);
            q1.Enqueue(new List<string> { "F", "G", "H" }, 1);
            q1.Enqueue(new List<string> { "X", "V", "B" }, 0);
            q1.Enqueue(new List<string> { "Q", "W", "E" }, 1);
            q1.Enqueue("Z", 1);

            q2.Enqueue(new List<int> { 4, 5, 6 }, 5);
            q2.Enqueue(new List<int> { 1, 2, 3 }, 1);
            q2.Enqueue(new List<int> { 7, 8, 9 }, 0);
            q2.Enqueue(new List<int> { 0, 3, 5 }, 1);
            q2.Enqueue(9, 2);

            q3.Enqueue(new Group("CS1", 7), 1);
            q3.Enqueue(new Group("CS2", 14), 3);
            q3.Enqueue(new Group("CS3", 24), 5);
            q3.Enqueue(new Group("CS4", 31), 1);

            Console.WriteLine("Dequeue in Q1: " + q1.Dequeue());
            Console.WriteLine("First in Q1: " + q1.First());
            Console.WriteLine("First with priority = 1 in Q1: " + q1.First(1));
            Console.WriteLine("Last in Q1: " + q1.Last());
            Console.WriteLine("Last with priority = 1 in Q1: " + q1.Last(1));
            Console.WriteLine("Count in Q1: " + q1.Count);
            Console.WriteLine("Count with priority = 1 in Q1: " + q1.GetCount(1));

            Console.WriteLine("__________________________________________________");

            Console.WriteLine("Dequeue in Q2: " + q2.Dequeue());
            Console.WriteLine("First in Q2: " + q2.First());
            Console.WriteLine("First with priority = 1 in Q2: " + q2.First(1));
            Console.WriteLine("Last in Q2: " + q2.Last());
            Console.WriteLine("Last with priority = 1 in Q2: " + q2.Last(1));
            Console.WriteLine("Count in Q2: " + q2.Count);
            Console.WriteLine("Count with priority = 1 in Q2: " + q2.GetCount(1));

            Console.WriteLine("__________________________________________________");

            Console.WriteLine("Dequeue in Q3: " + q3.Dequeue().Title);
            Console.WriteLine("First in Q3: " + q3.First().Title);
            Console.WriteLine("First with priority = 1 in Q3: " + q3.First(1).Title);
            Console.WriteLine("Last in Q3: " + q3.Last().Title);
            Console.WriteLine("Last with priority = 1 in Q3: " + q3.Last(1).Title);
            Console.WriteLine("Count in Q3: " + q3.Count);
            Console.WriteLine("Count with priority = 1 in Q3: " + q3.GetCount(1));

            Console.WriteLine("__________________________________________________");
            bool q = q1.Contains(new Tuple<string, int>("7", 1));
            Console.WriteLine("Contains('7', 1) in Q1: " + q);
            q = q1.Contains(new Tuple<string, int>("Z", 1));
            Console.WriteLine("Contains('Z', 1) in Q1: " + q);

            q2.Clear();
            Console.WriteLine("Clear in Q2: " + q2.Count);

            Tuple<string, int>[] array = new Tuple<string, int>[15];

            q1.CopyTo(array, 0);

            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("Array[{0}] = ( {1}  {2})", i, array[i].Item2, array[i].Item1);
            }

            Console.ReadLine();
        }
开发者ID:SalamanderSunburn,项目名称:tasks_cs1,代码行数:71,代码来源:CollectionsExercises.cs

示例13: TestRemove1

        public void TestRemove1()
        {
            var queue = new PriorityQueue<string> { "string", "anotherString" };

            var result = queue.Remove("string");

            Assert.IsTrue(result);
            Assert.AreEqual(1, queue.Count);
            Assert.IsFalse(queue.Contains("string"));
        }
开发者ID:pkubryk,项目名称:SummerBatch,代码行数:10,代码来源:PriorityQueueTest.cs

示例14: TestPeek1

        public void TestPeek1()
        {
            var queue = new PriorityQueue<string> { "string", "anotherString" };

            var poll = queue.Peek();

            Assert.AreEqual(2, queue.Count);
            Assert.AreEqual("anotherString", poll);
            Assert.IsTrue(queue.Contains("anotherString"));
        }
开发者ID:pkubryk,项目名称:SummerBatch,代码行数:10,代码来源:PriorityQueueTest.cs

示例15: TestPoll1

        public void TestPoll1()
        {
            var queue = new PriorityQueue<string> { "string", "anotherString" };

            var poll = queue.Poll();

            Assert.AreEqual(1, queue.Count);
            Assert.AreEqual("anotherString", poll);
            Assert.IsFalse(queue.Contains("anotherString"));
        }
开发者ID:pkubryk,项目名称:SummerBatch,代码行数:10,代码来源:PriorityQueueTest.cs


注:本文中的System.Collections.Generic.PriorityQueue.Contains方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。