/// <summary> /// Perform the necessary actions once the sweep line reach a seed event (meets a seed) /// </summary> /// <param name="seedEvent"></param> private void ProcessSeedEvent(SeedEvent seedEvent) { var node = Events.GetNearest(this, Events.Root, seedEvent, MaximumEdgeLimit); if (node is null || node is BreakpointNode) { return; } if (Math.Abs(node.X - seedEvent.X) < EPSILON_DOUBLE && Math.Abs(node.Y - seedEvent.Y) < EPSILON_DOUBLE) { return; } if (node is Leaf closestLeafNode) { if (closestLeafNode.DisappearEvent != null) { PriorityQueue.Delete(closestLeafNode.DisappearEvent.Handle); closestLeafNode.DisappearEvent = null; } foreach (var circleEvent in Events.AddSeedEvent(closestLeafNode, new TreeItem(seedEvent.Vertex))) { C5.IPriorityQueueHandle <IEvent> h = null; PriorityQueue.Add(ref h, circleEvent); circleEvent.Handle = h; } } }
public static IEnumerable <IPathNode <N> > GetReachableNodes <N>(N start, float maxCost) where N : INode <N> { C5.IDictionary <N, PathNode <N> > nodeDictionary = new C5.HashDictionary <N, PathNode <N> >(); C5.IPriorityQueue <PathNode <N> > openSet = new C5.IntervalHeap <PathNode <N> >(new PathNodeComparer <N>(), C5.MemoryType.Normal); C5.ICollection <N> closedSet = new C5.HashSet <N>(); C5.ArrayList <IPathNode <N> > res = new C5.ArrayList <IPathNode <N> >(C5.MemoryType.Normal); PathNode <N> curNode = new PathNode <N>(start); curNode.g = 0; nodeDictionary.Add(start, curNode); while (true) { res.Add(curNode); foreach (IEdge <N> edge in curNode.node) { N other = edge.GetEnd(); if (!closedSet.Contains(other)) { PathNode <N> otherNode = null; if (!nodeDictionary.Find(ref other, out otherNode)) { otherNode = new PathNode <N>(other); nodeDictionary.Add(other, otherNode); } float newG = edge.GetCost() + curNode.g; if (otherNode.g > newG) { otherNode.g = newG; if (otherNode.queueHandle != null) { openSet.Replace(otherNode.queueHandle, otherNode); } otherNode.prev = curNode; } if (otherNode.queueHandle == null) { C5.IPriorityQueueHandle <PathNode <N> > handle = null; openSet.Add(ref handle, otherNode); otherNode.queueHandle = handle; } } } if (openSet.IsEmpty) { return(res); } closedSet.Add(curNode.node); curNode = openSet.DeleteMin(); if (curNode.g > maxCost) { return(res); } } }
/// <summary> /// Add the seed events for each seed in the diagram (known prior to the diagram generation) /// </summary> private void AddSeedEvents() { for (var i = 0; i < Seeds.Length; i++) { var se = new SeedEvent(Seeds[i]); VoronoiVertices[i] = se.Vertex; C5.IPriorityQueueHandle <IEvent> h = null; PriorityQueue.Add(ref h, se); se.Handle = h; } }
private void ProcessCircleEvent(CircleEvent circleEvent) { var leftHalfEdge = circleEvent.Left.PartialLine; if (leftHalfEdge.Face.Equals(circleEvent.Left.Point)) { leftHalfEdge = leftHalfEdge.Twin; } var centerHalfEdge = circleEvent.Center.PartialLine; if (centerHalfEdge.Face.Equals(circleEvent.Right.Point)) { centerHalfEdge = centerHalfEdge.Twin; } var rc = centerHalfEdge.Twin; rc.CircleEvent = circleEvent; leftHalfEdge.CircleEvent = circleEvent; circleEvent.PartialLine = centerHalfEdge; var prev = circleEvent.CenterLeaf.GetPreviousLeaf(); var next = circleEvent.CenterLeaf.GetNextLeaf(); if (prev?.DisappearEvent != null) { PriorityQueue.Delete(prev.DisappearEvent.Handle); prev.DisappearEvent = null; } if (next?.DisappearEvent != null) { PriorityQueue.Delete(next.DisappearEvent.Handle); next.DisappearEvent = null; } var newCircles = Events.RemoveNode(circleEvent, prev, circleEvent.CenterLeaf, next); if (newCircles == null) { return; } foreach (var ce in newCircles) { C5.IPriorityQueueHandle <IEvent> h = null; PriorityQueue.Add(ref h, ce); ce.Handle = h; } }
/// <summary> /// Adds an item to the collection /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool Add(I key, T value) { if (!_index.ContainsKey(key)) { C5.IPriorityQueueHandle <IndexedItem> handle = null; _priQueue.Add(ref handle, new IndexedItem { Key = key, Value = value }); _index.Add(key, handle); return(true); } return(false); }
internal void AddGroupCrossedAvatarsReadyEvent(Tuple <UUID, UUID> scriptIdUserId) { DeferredEventList eventList; if (_eventsByScript.TryGetValue(scriptIdUserId.Item1, out eventList)) { eventList.GroupCrossedAvatarsReadyList.Add(scriptIdUserId.Item2); } else { eventList = new DeferredEventList(scriptIdUserId.Item1); eventList.GroupCrossedAvatarsReadyList.Add(scriptIdUserId.Item2); _eventsByScript.Add(scriptIdUserId.Item1, eventList); C5.IPriorityQueueHandle <DeferredEventList> handle = null; _sortedEvents.Add(ref handle, eventList); eventList.Handle = handle; } }
internal void AddEnableDisableEvent(UUID itemId, EnableDisableFlag enableDisableFlag) { DeferredEventList eventList; if (_eventsByScript.TryGetValue(itemId, out eventList)) { eventList.EnableDisableList.Add(enableDisableFlag); } else { eventList = new DeferredEventList(itemId); eventList.EnableDisableList.Add(enableDisableFlag); _eventsByScript.Add(itemId, eventList); C5.IPriorityQueueHandle <DeferredEventList> handle = null; _sortedEvents.Add(ref handle, eventList); eventList.Handle = handle; } }
/// <summary> /// Method marks given node as approached by predecessor with given actual distance. Status of the node will be changed to open. It will be also either added to _openQueue or updated in it /// </summary> /// <param name="node"></param> /// <param name="acutalDistance"></param> /// <param name="predecessor"></param> private void OpenNode(TNode node, double acutalDistance, TNode predecessor) { var nodeTag = LoadNodeTag(node); nodeTag.State = NodeTagState.Open; nodeTag.ActualDistance = acutalDistance; nodeTag.Predecessor = predecessor; C5.IPriorityQueueHandle <TNode> handle = nodeTag.Handle; if (handle == null) { _openNodes.Add(ref handle, node); } else { _openNodes.Replace(handle, node); } nodeTag.Handle = handle; }
public void MergeChunks(string outputFilePath, string[] chunkFiles) { Console.WriteLine(nameof(PriorityQueueMergeStrategy)); using (var writer = File.CreateText(outputFilePath)) { List<StreamReader> readers = chunkFiles.Select(s => new StreamReader(s, Encoding.UTF8)).ToList(); var priorityQueue = new C5.IntervalHeap<string>(_comparer); var readerForHandle = new Dictionary<C5.IPriorityQueueHandle<string>, StreamReader>(); // Fill priority queue with initial strings. Each string is associated with reader it came from readers.ForEach(reader => { C5.IPriorityQueueHandle<string> handle = null; priorityQueue.Add(ref handle, reader.ReadLine()); readerForHandle[handle] = reader; }); // Get minimal element from queue and read the next line from respective stream // repeat until all streams are exhausted while (!priorityQueue.IsEmpty) { C5.IPriorityQueueHandle<string> handleMin; string line = priorityQueue.DeleteMin(out handleMin); StreamReader reader = readerForHandle[handleMin]; readerForHandle.Remove(handleMin); writer.WriteLine(line); string nextLine = reader.ReadLine(); if (nextLine != null) { C5.IPriorityQueueHandle<string> handle = null; priorityQueue.Add(ref handle, nextLine); readerForHandle[handle] = reader; } } readers.ForEach(r => r.Dispose()); } chunkFiles.ForEach(f => File.Delete(f)); }
/// <summary> /// Add a new deferred event /// </summary> /// <param name="itemId"></param> /// <param name="eventInfo"></param> public void AddEvent(UUID itemId, VM.PostedEvent eventInfo) { DeferredEventList eventList; if (_eventsByScript.TryGetValue(itemId, out eventList)) { if (eventList.EvtList.Count < MAX_DEFERRED_EVENTS) { eventList.EvtList.Add(eventInfo); } } else { eventList = new DeferredEventList(itemId); eventList.EvtList.Add(eventInfo); _eventsByScript.Add(itemId, eventList); C5.IPriorityQueueHandle <DeferredEventList> handle = null; _sortedEvents.Add(ref handle, eventList); eventList.Handle = handle; } }
public void Dispose () { m_asset = null; PriorityQueueHandle = null; }
static void doPriorityQueueSkeletonCode(Node rootNode, Heuristic selectedHeuristic, IComparer <Node> selectedComparer, Stopwatch timer) { C5.IntervalHeap <Node> nodeQueue = new C5.IntervalHeap <Node>(selectedComparer); nodeQueue.Add(rootNode); //repeated state checking tools HashSet <int> previouslyExpanded = new HashSet <int> { }; Dictionary <int, Node> idsToNode = new Dictionary <int, Node> { }; int nodesPoppedOffQueue = 0; int maxQueueSize = 1; //rootNode is in there while (nodeQueue.Count > 0) { //heap extracts minimum based on selected comparer Node currentNode = nodeQueue.DeleteMin(); nodesPoppedOffQueue += 1; Board currentBoard = currentNode.board; //keeps track of expanded states //we are currently expanding this state. previouslyExpanded.Add(currentBoard.Id); if (currentBoard.isInEndState()) { handleEndState(currentNode, nodesPoppedOffQueue, maxQueueSize); return; } List <Board> children = currentBoard.GenerateNextStates(); foreach (Board child in children) { if (!previouslyExpanded.Contains(child.Id)) { int childPotentialCost = selectedHeuristic.getHeuristicScore(currentNode, child); Node childNode = new Node(child, currentNode, currentNode.cost + child.TileMoved, currentNode.depth + 1, childPotentialCost); C5.IPriorityQueueHandle <Node> h = null; if (idsToNode.ContainsKey(child.Id)) { //already seen this node somewhere. check queue for cheaper, otherwise don't add Node currentNodeInQueue = idsToNode[child.Id]; int currentCostInQueue = currentNodeInQueue.heuristicCost; if (childPotentialCost < currentCostInQueue) { //replace the node with the same configuration with the cheaper one //update the handle in the queue appropriately for future reference h = currentNodeInQueue.handle; nodeQueue.Replace(currentNodeInQueue.handle, childNode); childNode.handle = h; } else { //do not add this child, the one in the queue already is cheaper } } else { //never seen this child before, add it nodeQueue.Add(ref h, childNode); childNode.handle = h; idsToNode.Add(child.Id, childNode); } } } //update maxQueueSize, done adding all potential children if (maxQueueSize < nodeQueue.Count) { maxQueueSize = nodeQueue.Count; } } }
public static IEnumerable <IPathNode <N> > GetShortestPath <N>(N start, N goal, IHeuristic <N> heuristic) where N : INode <N> { C5.IDictionary <N, PathNode <N> > nodeDictionary = new C5.HashDictionary <N, PathNode <N> >(); C5.IPriorityQueue <PathNode <N> > openSet = new C5.IntervalHeap <PathNode <N> >(new PathNodeComparer <N>()); C5.ICollection <N> closedSet = new C5.HashSet <N>(); PathNode <N> curNode = new PathNode <N>(start); curNode.g = 0; nodeDictionary.Add(start, curNode); while (true) { foreach (IEdge <N> edge in curNode.Node) { N other = edge.GetEnd(); if (!closedSet.Contains(other)) { PathNode <N> otherNode = null; if (!nodeDictionary.Find(ref other, out otherNode)) { otherNode = new PathNode <N>(other); nodeDictionary.Add(other, otherNode); } float newG = edge.GetCost() + curNode.g; if (otherNode.g > newG) { otherNode.g = newG; if (otherNode.queueHandle != null) { openSet.Replace(otherNode.queueHandle, otherNode); } otherNode.prev = curNode; } if (otherNode.queueHandle == null) { otherNode.h = heuristic.MinDist(other, goal); C5.IPriorityQueueHandle <PathNode <N> > handle = null; openSet.Add(ref handle, otherNode); otherNode.queueHandle = handle; } } } if (openSet.IsEmpty) { return(null); } closedSet.Add(curNode.Node); curNode = openSet.DeleteMin(); if (curNode.Node.Equals(goal)) { C5.ArrayList <IPathNode <N> > res = new C5.ArrayList <IPathNode <N> >(); do { res.Add(curNode); curNode = curNode.prev; } while (curNode != null); res.Reverse(); return(res); } } }