public Node head; //头部Node public NodeQueue(NodeQueue next, NodeQueue prev, Node tail, Node head) { this.next = next; this.prev = prev; this.tail = tail; this.head = head; }
// Constructor /// <summary> /// Create a <see cref="SearchGrid"/> provider based on the specified input grid. /// </summary> /// <param name="inputGrid">The 2D input array of <see cref="IPathNode"/></param> public SearchGrid(IPathNode[,] inputGrid) { // Make sur ethe input is acceptable validateInputGrid(inputGrid); // Get sizes width = inputGrid.GetLength(0); height = inputGrid.GetLength(1); // Cache and allocate nodeGrid = new PathNode[width, height]; searchGrid = new PathNode[width, height]; closedMap = new OpenNodeMap <PathNode>(width, height); openMap = new OpenNodeMap <PathNode>(width, height); runtimeMap = new OpenNodeMap <PathNode>(width, height); orderedMap = new NodeQueue <PathNode>(new PathNode(Index.zero, null)); // Create the grid wrapper for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // Create a wrapper path node over the interface nodeGrid[x, y] = new PathNode(new Index(x, y), inputGrid[x, y]); } } }
static void Main(string[] args) { IQueue queue = new NodeQueue(); Console.WriteLine(queue.ToString()); queue.Enqueue(1); Console.WriteLine(queue.ToString()); queue.Enqueue(2); Console.WriteLine(queue.ToString()); queue.Enqueue(3); Console.WriteLine(queue.ToString()); queue.Dequeue(); Console.WriteLine(queue.ToString()); queue.Dequeue(); Console.WriteLine(queue.ToString()); queue.Dequeue(); Console.WriteLine(); Console.WriteLine(queue.ToString()); }
/// <summary> /// Clear /// </summary> public void Clear() { // Clear the queue NodeQueue.Clear(); // Clear nodes foreach (PathNode node in Nodes) { node.Clear(); } }
public void Enumerate() { var test = new NodeQueue<NodeInt>(() => NodeInt.GetValues(5)); test.Insert_001(); int index = 0; int[] indexes = { 0, 0, 1 }; foreach (var item in test.Queue) { Assert.AreEqual<NodeInt>(test.Values[indexes[index++]], item); } }
public void Put(int key, int value) { if (capacity == 0) { return; } Node cn = null; map.TryGetValue(key, out cn); //key已存在的情况下,更新value值,并将Node右移 if (cn != null) { cn.value = value; oneStepUp(cn); return; } //cache已满的情况下,把外层链表尾部的内层链表的尾部Node移除 if (map.Count == capacity) { map.Remove(removeNode(this.tail.tail).key); } //插入新的Node Node n = new Node(key, value); if (this.tail == null) { //tail为null说明此时cache中没有元素,直接把Node封装到NodeQueue里加入 NodeQueue nq = new NodeQueue(null, null, n, n); this.tail = nq; n.nq = nq; } else if (this.tail.tail.frequency == 0) { //外层链表尾部元素的访问次数是0,那么将Node加入到外层链表尾部元素的头部 n.prev = this.tail.head; this.tail.head.next = n; n.nq = this.tail; this.tail.head = n; } else { //外层链表尾部元素的访问次数不是0,那么实例化一个只包含此Node的NodeQueue,加入外层链表尾部 NodeQueue nq = new NodeQueue(this.tail, null, n, n); this.tail.prev = nq; this.tail = nq; n.nq = nq; } //最后把key和Node存入HashMap中 map.Add(key, n); }
private void removeNQ(NodeQueue nq) { if (nq.prev != null) { nq.prev.next = nq.next; } if (nq.next != null) { nq.next.prev = nq.prev; } if (this.tail == nq) { this.tail = nq.next; } }
private void oneStepUp(Node n) { n.frequency++; //访问次数+1 bool singleNodeQ = false; //为true时,代表此NodeQueue中只有一个Node元素 if (n.nq.head == n.nq.tail) { singleNodeQ = true; } if (n.nq.next != null) { if (n.nq.next.tail.frequency == n.frequency) { //右侧NodeQueue的访问次数与Node当前访问次数一样,将此Node置于右侧NodeQueue的头部 removeNode(n); //从当前NodeQueue中删除Node //把Node插入到右侧NodeQueue的头部 n.prev = n.nq.next.head; n.nq.next.head.next = n; n.nq.next.head = n; n.nq = n.nq.next; } else if (n.nq.next.tail.frequency > n.frequency) { //右侧NodeQueue的访问次数大于Node当前访问次数,则需要在两个NodeQueue之间插入一个新的NodeQueue if (!singleNodeQ) { removeNode(n); NodeQueue nnq = new NodeQueue(n.nq.next, n.nq, n, n); n.nq.next.prev = nnq; n.nq.next = nnq; n.nq = nnq; } //如果当前NodeQueue中只有一个Node,那么其实不需要任何额外操作了 } } else { //此NodeQueue的next == null,说明此NodeQueue已经位于外层链表头部了,这时候需要往外侧链表头部插入一个新的NodeQueue if (!singleNodeQ) { removeNode(n); NodeQueue nnq = new NodeQueue(null, n.nq, n, n); n.nq.next = nnq; n.nq = nnq; } //同样地,如果当前NodeQueue中只有一个Node,不需要任何额外操作 } }
public OrNode(SectionSearchQueryPlan[] subqueries) { if (subqueries.Length == 0) { _curDoc = DocIdSetIterator.NO_MORE_DOCS; } else { _pq = new NodeQueue(subqueries.Length); foreach (SectionSearchQueryPlan q in subqueries) { if (q != null) _pq.Add(q); } _curDoc = -1; } }
public void InitMap_Node() { nodes = new NodeQueue(); var nodeList = GameObject.FindGameObjectsWithTag("Node"); foreach (GameObject n in nodeList) { var node = n.GetComponent <Node>(); node.Init(n.transform.position); if (!nodes.Contains(node)) { nodes.Add(node); node.TempleLine.Clear(); node.TempleLineIndex = 0; } } }
public LazyPrimMST(EdgeWeightedGraph g) { _marked = new bool[g.V]; _mst = new NodeQueue <Edge>(); pq = new MinPQ <Edge>(g.E);//MinPQ没有实现自动调整大小,先将就着用吧。 Visit(g, 0); while (!pq.IsEmpty()) { var e = pq.DelMin(); var v = e.Either(); v = !_marked[v] ? v : e.Other(v);//至少会有一条边已在生成树中。 if (!_marked[v]) { _mst.Enqueue(e); Visit(g, v); } } }
public bool next() { if (NodeQueue == null || NodeQueue.Count == 0) { return(false); } Current = NodeQueue.Dequeue(); foreach (Party subPart in Current.getSubParty()) { NodeQueue.Enqueue(subPart); } return(true); //if (Current == null) //{ // return false; //} //if (Current.isLeaf()) //{ // return false; //} //List<Party> subP = Current.getSubParty(); //if (NodeQueue.Count > 0) //{ // Current = NodeQueue.Dequeue(); // foreach (Party subPart in subP) // { // NodeQueue.Enqueue(subPart); // } //} //else //{ // Current = null; //} //return hasNext(); }
public OrNode(SectionSearchQueryPlan[] subqueries) { if (subqueries.Length == 0) { m_curDoc = DocIdSetIterator.NO_MORE_DOCS; } else { m_pq = new NodeQueue(subqueries.Length); foreach (SectionSearchQueryPlan q in subqueries) { if (q != null) { m_pq.Add(q); } } m_curDoc = -1; } }
private void BFS(Graph g, int s) { var queue = new NodeQueue <int>(); _marked[s] = true; queue.Enqueue(s); while (!queue.IsEmpty()) { int v = queue.Dequeue(); foreach (var w in g.Adj(v)) { if (!_marked[w]) { _pathTo[w] = v; _marked[w] = true; queue.Enqueue(w); } } } }
public TreeIterator(Party aRoot) { //Current = aRoot; //if (Current == null) //{ // return; //} //if (!Current.isLeaf()) //{ // List<Party> subP = Current.getSubParty(); // foreach (Party subPart in subP) // { // NodeQueue.Enqueue(subPart); // } //} NodeQueue.Enqueue(aRoot); }
void QueueTest() { var strArr = FileHandler.ReadFileAsStrArr("words3.txt"); strArr.Show(); var queue = new NodeQueue <string>(); foreach (var item in strArr) { queue.Enqueue(item); } foreach (var item in queue) { Console.Write(item + " "); } Console.WriteLine(); while (!queue.IsEmpty()) { Console.Write(queue.Dequeue() + " "); } Console.ReadKey(); }
public void Remove() { var test = new NodeQueue<NodeInt>(() => NodeInt.GetValues(5)); test.Insert_001(); test.Remove_001(); }
public void TestCanCreateEmptyNodeQueue() { NodeQueue testNodeQueue = new NodeQueue(); Assert.Null(testNodeQueue.Front); }
/// <summary> /// /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public List <PathNode> FindPath(Point start, Point end) { Clear(); // Add root node PathNode startnode = GetNode(start); startnode.Parent = null; startnode.IsOpen = false; NodeQueue.Push(startnode); PathNode dest = GetNode(end); int MovementCost = 1; while (NodeQueue.Count > 0) { // No path... if (NodeQueue.Count == 0) { NodeQueue.Clear(); return(null); } // Get first node PathNode node = NodeQueue.Pop(); // if (this node is the goal) if (node == dest) { return(GetPath(node)); } // Move the current node to the closed node.IsOpen = false; // All neighbors Point[] neighbors = new Point[] { new Point(0, -1), // Top new Point(1, 0), // Right new Point(0, 1), // Bottom new Point(-1, 0), // Left new Point(1, -1), // Top right new Point(-1, -1), // Top left new Point(1, 1), // Bottom right new Point(-1, 1), // Bottom left }; // Consider all of its neighbors for (int i = 0; i < neighbors.Length; i++) { // Get next neighbor PathNode neighbor = GetNode(node.Location.X + neighbors[i].X, node.Location.Y + neighbors[i].Y); if (neighbor == null || !neighbor.IsOpen) { continue; } // if (this neighbor is in the closed list and our current g value is lower) if (/*!neighbor.IsOpen &&*/ node.G < neighbor.G) { // update the neighbor with the new, lower, g value neighbor.G = node.G; // change the neighbor's parent to our current node neighbor.Parent = node; neighbor.IsOpen = false; NodeQueue.Push(neighbor); } /* * // else if (this neighbor is in the open list and our current g value is lower) * else if (neighbor.IsOpen && node.G < neighbor.G) * { * // update the neighbor with the new, lower, g value * neighbor.G = node.G; * * // change the neighbor's parent to our current node * neighbor.Parent = node; * NodeQueue.Push(neighbor); * } */ // else this neighbor is not in either the open or closed list else if (neighbor.IsOpen && neighbor.IsWalkable) { // add the neighbor to the open list and set its g value neighbor.Parent = node; neighbor.G = node.G + MovementCost; // Diagonal movement penality if (i > 3) { neighbor.G += 1; } neighbor.H = GetHeuristic(neighbor.Location, end); neighbor.IsOpen = false; NodeQueue.Push(neighbor); } } } return(null); }