/// <summary> /// 超时检测 /// </summary> internal void Check() { do { startNode.Check(); queueNode nextNode = startNode.CheckNextNode; if (nextNode == null) { DateTime time = startNode.CheckTime; if (time == DateTime.MinValue) { isTask = 0; if (startNode.IsAny && Interlocked.CompareExchange(ref isTask, 1, 0) == 0) { fastCSharp.threading.timerTask.Default.Add(this, thread.callType.TimeoutQueueCheck, date.Now.AddTicks(timeoutTicks)); } } else { fastCSharp.threading.timerTask.Default.Add(this, thread.callType.TimeoutQueueCheck, time.AddTicks(date.SecondTicks)); } return; } queueNode node = startNode; startNode = nextNode; node.NextNode = null; typePool <queueNode> .PushNotNull(node); }while (true); }
private List <Vector2Int> GetPathFrom(List <queueNode> pathList) { if (pathList.Count == 0) { return(null); } List <Vector2Int> lPath = new List <Vector2Int>(); // Lấy đường đi đảo ngược queueNode note = pathList[pathList.Count - 1]; do { lPath.Add(note.pt); note = pathList[note.parent]; } while (note.parent != 0); lPath.Add(note.pt); lPath.Add(pathList[0].pt); // lấy đường đi thuận for (int i = 0; i < lPath.Count / 2; i++) { note.pt = lPath[i]; lPath[i] = lPath[lPath.Count - i - 1]; lPath[lPath.Count - i - 1] = note.pt; } return(lPath); }
/// <summary> /// 添加超时回调信息 /// </summary> /// <param name="checkTimeout">是否超时判断</param> /// <param name="identity">超时判断标识</param> /// <param name="type">超时回调类型</param> internal void Add(object checkTimeout, int identity, callbackType type) { for (queueNode node = endNode; node.Add(checkTimeout, identity, type) == 0; node = endNode) { Monitor.Enter(endNodeLock); if (node == endNode) { Exception exception = null; try { endNode.NextNode = queueNode.Get(timeoutTicks); endNode = endNode.NextNode; } catch (Exception error) { exception = error; } finally { Monitor.Exit(endNodeLock); } if (exception != null) { log.Error.Add(exception, null, false); return; } } else { Monitor.Exit(endNodeLock); } } if (Interlocked.CompareExchange(ref isTask, 1, 0) == 0) { fastCSharp.threading.timerTask.Default.Add(this, thread.callType.TimeoutQueueCheck, date.Now.AddTicks(timeoutTicks)); } }
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "" && (radioButton1.Checked == true || radioButton2.Checked == true || radioButton3.Checked == true)) { if (radioButton1.Checked == true) { queueNode newNode1 = new queueNode(textBox1.Text); queueNode active1 = personQueue.headNode1; while (active1.nextOne != active1) { active1 = active1.nextOne; } newNode1.nextOne = newNode1; active1.nextOne = newNode1; listBoxRefresh(); personQueue.size++; } else if (radioButton2.Checked == true) { queueNode newNode1 = new queueNode(textBox1.Text); queueNode active1 = personQueue.headNode2; while (active1.nextOne != active1) { active1 = active1.nextOne; } active1.nextOne = newNode1; newNode1.nextOne = newNode1; listBoxRefresh(); personQueue.size++; } else if (radioButton3.Checked == true) { queueNode newNode1 = new queueNode(textBox1.Text); queueNode active1 = personQueue.headNode3; while (active1.nextOne != active1) { active1 = active1.nextOne; } active1.nextOne = newNode1; newNode1.nextOne = newNode1; listBoxRefresh(); personQueue.size++; } } else if (textBox1.Text == "" && (radioButton1.Checked == true || radioButton2.Checked == true || radioButton3.Checked == true)) { MessageBox.Show("Lütfen isminizi girdiğinizden emin olun!!!"); } else if (textBox1.Text == "" && (radioButton1.Checked == false || radioButton2.Checked == false || radioButton3.Checked == false)) { MessageBox.Show("Lütfen isminizi girdiğinizden ve işlem önceliğinizi seçtiğinizden emin olun!!!"); } else { MessageBox.Show("Lütfen işlem önceliğinizi seçtiğinizden emin olun!!!"); } }
public queue(string personName) { this.headNode1 = new queueNode(personName); this.headNode1.nextOne = headNode1; this.headNode2 = new queueNode(personName); this.headNode2.nextOne = headNode2; this.headNode3 = new queueNode(personName); this.headNode3.nextOne = headNode3; this.size = 0; }
/// <summary> /// 获取队列节点 /// </summary> /// <param name="timeoutTicks"></param> /// <returns></returns> public static queueNode Get(long timeoutTicks) { queueNode node = typePool <queueNode> .Pop(); if (node == null) { node = new queueNode(timeoutTicks); } else { node.timeoutTicks = timeoutTicks; } return(node); }
static int BFS(int[,] mat, Point src, Point dest) { // check source and destination cell of the matrix have value 1 if (mat[src.x, src.y] < 1 || mat[dest.x, dest.y] < 1) { return(-1); } bool[,] visited = new bool[ROW, COL]; visited[src.x, src.y] = true; Queue <queueNode> q = new Queue <queueNode>(); // Distance of source cell is 0 queueNode s = new queueNode(src, 0); q.Enqueue(s); // Do a BFS starting from source cell while (q.Count != 0) { queueNode curr = q.Peek(); Point pt = curr.pt; // If we have reached the destination cell, we are done if (pt.x == dest.x && pt.y == dest.y) { return(curr.dist); } // Otherwise dequeue the front cell in the queue and enqueue its adjacent cells q.Dequeue(); for (int i = 0; i < 4; i++) { int row = pt.x + rowNum[i]; int col = pt.y + colNum[i]; if (isValid(row, col) && mat[row, col] >= 1 && !visited[row, col]) { visited[row, col] = true; queueNode Adjcell = new queueNode(new Point(row, col), curr.dist + 1); q.Enqueue(Adjcell); } } } return(-1); }
private void button2_Click(object sender, EventArgs e) { queueNode newNode1 = new queueNode(null); newNode1 = personQueue.headNode1; queueNode newNode2 = new queueNode(null); newNode2 = personQueue.headNode2; queueNode newNode3 = new queueNode(null); newNode3 = personQueue.headNode3; textBox2.Text = personQueue.headNode1.nextOne.personName.ToString(); personQueue.headNode1 = personQueue.headNode1.nextOne; listBoxRefresh(); if (newNode1 == personQueue.headNode1) { textBox2.Text = personQueue.headNode2.nextOne.personName.ToString(); personQueue.headNode2 = personQueue.headNode2.nextOne; listBoxRefresh(); } if (newNode2 == personQueue.headNode2 && newNode1 == personQueue.headNode1) { textBox2.Text = personQueue.headNode3.nextOne.personName.ToString(); personQueue.headNode3 = personQueue.headNode3.nextOne; listBoxRefresh(); } if (newNode2 == personQueue.headNode2 && newNode1 == personQueue.headNode1 && newNode3 == personQueue.headNode3) { textBox2.Text = null; MessageBox.Show("Kuyrukta Eleman Yok!!"); listBoxRefresh(); } }
public void listBoxRefresh() { listBox1.Items.Clear(); queueNode active1 = new queueNode(" "); active1 = personQueue.headNode1; while (active1.nextOne != active1) { { listBox1.Items.Add(active1.nextOne.personName); active1 = active1.nextOne; } } queueNode active2 = new queueNode(" "); active2 = personQueue.headNode2; while (active2.nextOne != active2) { { listBox1.Items.Add(active2.nextOne.personName); active2 = active2.nextOne; } } queueNode active3 = new queueNode(" "); active3 = personQueue.headNode3; while (active3.nextOne != active3) { { listBox1.Items.Add(active3.nextOne.personName); active3 = active3.nextOne; } } }
/// <summary> /// 超时队列 /// </summary> /// <param name="seconds">超时秒数</param> private timeoutQueue(int seconds) { startNode = endNode = queueNode.Get(timeoutTicks = date.SecondTicks * seconds); }
// function to find the shortest path between // a given source cell to a destination cell. static int BFS(int[,] mat, Point src, Point dest) { // check source and destination cell // of the matrix have value 1 if (mat[src.x, src.y] != 1 || mat[dest.x, dest.y] != 1) { return(-1); } bool[,] visited = new bool[ROW, COL]; // Mark the source cell as visited visited[src.x, src.y] = true; // Create a queue for BFS Queue <queueNode> q = new Queue <queueNode>(); // Distance of source cell is 0 queueNode s = new queueNode(src, 0); q.Enqueue(s); // Enqueue source cell // Do a BFS starting from source cell while (q.Count != 0) { queueNode curr = q.Peek(); Point pt = curr.pt; // If we have reached the destination cell, // we are done if (pt.x == dest.x && pt.y == dest.y) { return(curr.dist); } // Otherwise dequeue the front cell // in the queue and enqueue // its adjacent cells q.Dequeue(); for (int i = 0; i < 4; i++) { int row = pt.x + rowNum[i]; int col = pt.y + colNum[i]; // if adjacent cell is valid, has path // and not visited yet, enqueue it. if (isValid(row, col) && mat[row, col] == 1 && !visited[row, col]) { // mark cell as visited and enqueue it visited[row, col] = true; queueNode Adjcell = new queueNode(new Point(row, col), curr.dist + 1); q.Enqueue(Adjcell); } } } // Return -1 if destination cannot be reached return(-1); }
private int ShortestPathBinary(int[,] mat, Vector2Int src, bool isGhost, Vector2Int dest) { // check source and destination cell // of the matrix have value 1 if (mat[dest.x, dest.y] == 0) { return(-1); } // These arrays are used to get row and column // numbers of 4 neighbours of a given cell int[] rowNum = { -1, 0, 0, 1 }; int[] colNum = { 0, -1, 1, 0 }; List <queueNode> pathStoredList = new List <queueNode>(); bool[,] visited = new bool[Board.instance.GetRowNumber(), Board.instance.GetColumnNumber()]; for (int i = 0; i < Board.instance.GetRowNumber(); ++i) { for (int j = 0; j < Board.instance.GetColumnNumber(); ++j) { // Mark the source cell as visited visited[src.x, src.y] = true; } } // Create a queue for BFS Queue <queueNode> queue = new Queue <queueNode>(); // Distance of source cell is 0 queueNode s; s.pt = src; s.dist = 0; s.parent = 0; queue.Enqueue(s); // Enqueue source cell int iParent = 0; // Do a BFS starting from source cell while (queue.Count > 0) { queueNode curr = queue.Dequeue(); Vector2Int pt = curr.pt; // If we have reached the destination cell, // we are done if (pt.x == dest.x && pt.y == dest.y) { pathStoredList.Add(curr); m_pathList = GetPathFrom(pathStoredList); return(curr.dist); } iParent = pathStoredList.Count;//luu vi tri myPoint trong list de gan cho con myPoint pathStoredList.Add(curr); // Otherwise dequeue the front cell in the queue // and enqueue its adjacent cells for (int i = 0; i < 4; i++) { int row = pt.x + rowNum[i]; int col = pt.y + colNum[i]; // if adjacent cell is valid, has path and // not visited yet, enqueue it. if (isValid(row, col) && (isGhost || mat[row, col] == 1) && !visited[row, col]) { // mark cell as visited and enqueue it visited[row, col] = true; queueNode Adjcell; Adjcell.pt = new Vector2Int(row, col); Adjcell.dist = curr.dist + 1; Adjcell.parent = iParent; queue.Enqueue(Adjcell); } } } // Return -1 if destination cannot be reached return(-1); }