Пример #1
0
        public object dequeue()
        {
            object item = front; //always from front
            front = front.link;

            if (front == null)
                rear = null;

            return item;
        }
        public void AccessFrameHitMiddleTest()
        {
            IQueue<IFrame>[] fifos;
            ConcatenatedLRUQueue<IFrame> target = ConstructNestedConcatQueue(out fifos);

            QueueNode<IFrame> qn = new QueueNode<IFrame>(2, fifos[2].Enqueue(new Frame(999)).ListNode);
            fifos[2].Enqueue(new Frame(22));

            QueueNode<IFrame> actural = target.Access(qn);

            Assert.AreEqual(0u, actural.Index);
            Assert.AreEqual(999u, actural.ListNode.Value.Id);
            Assert.AreEqual(null, actural.ListNode.Previous);
        }
Пример #3
0
        public void enqueue(object item)
        {
            /*var node = new QueueNode {info = item, link = rear};
            rear = node;
            
            if (front == null)
            {
                front = rear;
            }*/

            var node = new QueueNode {info = item, link = null};

            if (rear == null) // always add from rear.
                front = node;
            else
                rear.link = node;
            rear = node; // rear is always the last node added
        }
Пример #4
0
    public T Dequeue()
    {
        T item;

        if (this.Count == 0)
        {
            throw new InvalidOperationException();
        }
        else if (this.Count == 1)
        {
            item      = this.head.Value;
            this.head = this.tail = null;
        }
        else
        {
            item               = this.head.Value;
            this.head          = this.head.NextNode;
            this.head.PrevNode = null;
        }

        this.Count--;
        return(item);
    }
Пример #5
0
		protected override QueueNode<IFrame> OnHit(QueueNode<IFrame> node, AccessType type)
        {
			bool isWrite = (type == AccessType.Write);
            IRRFrame irrf = node.ListNode.Value as IRRFrame;
            if (!irrf.Resident)
            {
                irrf.DataSlotId = pool.AllocSlot();
                if (!isWrite)
                    dev.Read(irrf.Id, pool[irrf.DataSlotId]);
            }

            uint irr = irrQ.AccessIRR(irrf.Id, isWrite);

            if (isWrite)
                irrf.WriteIRR = irr;
            else
                irrf.ReadIRR = irr;

            if (irr == 0)
                irrQ.Enqueue(irrf.Id, isWrite);

            return node;
        }
Пример #6
0
    public T Dequeue()
    {
        if (this.Count == 0)
        {
            throw new InvalidOperationException("Invalid Operation. The Queue is empty!");
        }

        T result = this.tail.Value;

        if (this.Count == 1)
        {
            this.tail = this.head = null;
        }
        else
        {
            this.tail          = this.tail.PrevNode;
            this.tail.NextNode = null;
        }

        this.Count--;

        return(result);
    }
Пример #7
0
        public T Dequeue()
        {
            if (this.Count == 0)
            {
                throw new IndexOutOfRangeException();
            }

            T result = this.tailNode.Value;

            this.tailNode = this.tailNode.PrevNode;
            this.Count--;

            if (!Object.ReferenceEquals(null, this.tailNode))
            {
                this.tailNode.NextNode = null;
            }
            else
            {
                this.headNode = null;
            }

            return(result);
        }
Пример #8
0
        public BTreeNode Dequeue()
        {
            QueueNode delNode = new QueueNode();
            BTreeNode retData = new BTreeNode();

            if (QIsEmpty(this))
            {
                print("Queue Memory Error");
                return(null);
            }

            if (front.GetNext() != null)
            {
                front   = front.GetNext();
                delNode = front;
                retData = delNode.GetData();
                return(retData);
            }
            else
            {
                return(null);
            }
        }
Пример #9
0
        public T Dequeue()
        {
            if (Front == null)
            {
                throw new EmptyQueueException("dequeue");
            }

            else if (Length == 1)
            {
                QueueNode temp = Front;
                Front = null;
                Back  = null;
                Length--;
                return(temp.Value);
            }
            else
            {
                QueueNode temp = Front;
                Front      = Front.Next;
                Front.Prev = null;
                Length--;
                return(temp.Value);
            }
        }
Пример #10
0
    public T Dequeue()
    {
        if (this.Count == 0)
        {
            throw new InvalidOperationException("Queue is empty!");
        }

        QueueNode <T> firstNode = this.head;
        T             element   = firstNode.Value;

        if (this.Count == 1)
        {
            this.head = null;
            this.tail = null;
        }
        else
        {
            this.head          = this.head.NextNode;
            firstNode.NextNode = null;
        }

        this.Count--;
        return(element);
    }
        public void enqueue(object item)
        {
            /*var node = new QueueNode {info = item, link = rear};
             * rear = node;
             *
             * if (front == null)
             * {
             *  front = rear;
             * }*/

            var node = new QueueNode {
                info = item, link = null
            };

            if (rear == null) // always add from rear.
            {
                front = node;
            }
            else
            {
                rear.link = node;
            }
            rear = node; // rear is always the last node added
        }
    /// <summary>
    /// Changes a PathNode's parent and depth, thereby altering it's priority in the queue.
    /// </summary>
    /// <param name="thisId">The id of the node to change</param>
    /// <param name="newParent">The new parent of the node to change</param>
    /// <param name="distParentToThis">The distance from the node to change to its parent</param>
    /// <returns>True if successful, false if a node with that ID does not exist or
    /// if the existing depth is lower than the new depth.</returns>
    public bool ReparentPathNode(int thisId, QueueNode newParent, float distParentToThis)
    {
        if (!_hashSet.Contains(thisId))
        {
            return(false);
        }

        int arrIndex;

        try
        {
            arrIndex = Array.FindIndex(_array, 1, Count, x => x != null && x.ID == thisId);
        }
        catch
        {
            return(false);
        }

        if (_array[arrIndex].Depth < newParent.Depth + distParentToThis)
        {
            return(false);
        }

        _array[arrIndex].Parent = newParent;
        _array[arrIndex].Depth  = newParent.Depth + distParentToThis;
        var parent = ParentIndex(arrIndex);

        while (arrIndex > 1 && _comparer.Compare(_array[arrIndex], _array[parent]) > 0)
        {
            SwapElements(parent, arrIndex);
            arrIndex = parent;
            parent   = ParentIndex(arrIndex);
        }

        return(true);
    }
Пример #13
0
    public T Dequeue()
    {
        if (Count == 0)
        {
            throw new InvalidOperationException();
        }

        var oldTail = _tail;

        _tail = _tail.PrevNode;

        if (Count == 1)
        {
            _head = null;
        }
        else
        {
            _tail.NextNode = null;
        }

        Count--;

        return(oldTail.Value);
    }
        public void Add(T item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            QueueNode <T> node = new QueueNode <T>()
            {
                Data = item
            };

            if (_last != null)
            {
                _last.Next = node;
            }

            _last = node;

            if (_first is null)
            {
                _first = _last;
            }
        }
Пример #15
0
        public void push(T value)
        {
            QueueNode newNode = new QueueNode(value);

            if (Top == null)
            {
                Top = Bottom = newNode;
                count++;
            }
            else if (Top == Bottom)
            {
                Top = newNode;
                Bottom.PreviousNode = Top;
                Top.NextNode        = Bottom;
                count++;
            }
            else
            {
                Top.PreviousNode = newNode;
                newNode.NextNode = Top;
                Top = newNode;
                count++;
            }
        }
Пример #16
0
        /// <summary>
        ///   Returns true if 'higher' has higher priority than 'lower', false otherwise.
        ///   Note that calling HasHigherPriority(node, node) (i.e. both arguments the same node) will return false
        /// </summary>
        bool HasHigherPriority(QueueNode higher, QueueNode lower)
        {
            var cmp = higher.Priority.CompareTo(lower.Priority);

            return(cmp < 0 || cmp == 0 && higher.InsertionIndex < lower.InsertionIndex);
        }
Пример #17
0
 public QueueNode(T value, QueueNode <T> nextNode = null)
 {
     this.Value    = value;
     this.NextNode = nextNode;
 }
Пример #18
0
        /// <summary>
        /// Finds the input that maximizes a function.
        /// </summary>
        /// <param name="bounds">Bounds for the search.</param>
        /// <param name="Evaluate">The function to maximize.</param>
        /// <param name="GetUpperBound">Returns an upper bound to the function in a region.  Need not be tight, but must become tight as the region shrinks.</param>
        /// <param name="xTolerance">Allowable relative error in the solution on any dimension.  Must be greater than zero.</param>
        /// <returns>A Vector close to the global maximum of the function.</returns>
        public static Vector Search(Region bounds, Func <Vector, double> Evaluate, Func <Region, double> GetUpperBound, double xTolerance = 1e-4)
        {
            if (xTolerance <= 0)
            {
                throw new ArgumentOutOfRangeException($"xTolerance <= 0");
            }
            int dim = bounds.Lower.Count;

            if (dim == 0)
            {
                return(Vector.Zero(dim));
            }
            PriorityQueue <QueueNode> queue = new PriorityQueue <QueueNode>();
            double lowerBound      = double.NegativeInfinity;
            Vector argmax          = bounds.GetMidpoint();
            long   upperBoundCount = 0;

            if (Debug)
            {
                Func <Region, double> GetUpperBound1 = GetUpperBound;
                GetUpperBound = region =>
                {
                    Stopwatch watch      = Stopwatch.StartNew();
                    double    upperBound = GetUpperBound1(region);
                    watch.Stop();
                    if (timeAccumulator.Count > 10 && watch.ElapsedMilliseconds > timeAccumulator.Mean + 4 * Math.Sqrt(timeAccumulator.Variance))
                    {
                        Trace.WriteLine($"GetUpperBound took {watch.ElapsedMilliseconds}ms");
                    }
                    timeAccumulator.Add(watch.ElapsedMilliseconds);
                    //if (upperBoundCount % 100 == 0) Trace.WriteLine($"lowerBound = {lowerBound}");
                    return(upperBound);
                };
            }
            Action <Region, int, double> addRegion = delegate(Region region, int splitDim, double upperBound)
            {
                if (upperBound > lowerBound)
                {
                    if (Debug)
                    {
                        Trace.WriteLine($"added region {region} with upperBound = {upperBound}");
                    }
                    QueueNode node = new QueueNode(region, upperBound);
                    queue.Add(node);
                }
                else if (Debug)
                {
                    Trace.WriteLine($"rejected region {region} with upperBound {upperBound} <= lowerBound {lowerBound}");
                }
            };

            upperBoundCount++;
            addRegion(bounds, 0, GetUpperBound(bounds));
            while (queue.Count > 0)
            {
                var node = queue.ExtractMinimum();  // gets the node with highest upper bound
                if (node.UpperBound <= lowerBound)
                {
                    continue;
                }
                Region region = node.Region;
                // compute the lower bound
                Vector    midpoint       = region.GetMidpoint();
                Stopwatch watch          = Stopwatch.StartNew();
                double    nodeLowerBound = Evaluate(midpoint);
                watch.Stop();
                if (Debug)
                {
                    if (timeAccumulator.Count > 10 && watch.ElapsedMilliseconds > timeAccumulator.Mean + 4 * Math.Sqrt(timeAccumulator.Variance))
                    {
                        Trace.WriteLine($"Evaluate took {watch.ElapsedMilliseconds}ms");
                    }
                    timeAccumulator.Add(watch.ElapsedMilliseconds);
                    Trace.WriteLine($"expanding {node} lower bound = {nodeLowerBound}");
                }
                if (nodeLowerBound > node.UpperBound)
                {
                    throw new Exception("nodeLowerBound > node.UpperBound");
                }
                if (nodeLowerBound > lowerBound)
                {
                    argmax     = midpoint;
                    lowerBound = nodeLowerBound;
                }

                Func <int, bool> DimensionCanSplit = i => MMath.AbsDiff(region.Upper[i], region.Lower[i], 1e-10) >= xTolerance;

                int    splitDim;
                bool   lowestChild     = false;
                Region leftRegion      = null;
                double upperBoundLeft  = default(double);
                Region rightRegion     = null;
                double upperBoundRight = default(double);
                if (lowestChild)
                {
                    splitDim = -1;
                    double lowestUpperBound = double.PositiveInfinity;
                    for (int i = 0; i < dim; i++)
                    {
                        if (DimensionCanSplit(i))
                        {
                            double splitValue2 = midpoint[i];
                            Region leftRegion2 = new Region(region);
                            leftRegion2.Upper[i] = splitValue2;
                            upperBoundCount++;
                            double upperBoundLeft2 = GetUpperBound(leftRegion2);
                            Region rightRegion2    = new Region(region);
                            rightRegion2.Lower[i] = splitValue2;
                            upperBoundCount++;
                            double upperBoundRight2 = GetUpperBound(rightRegion2);
                            double lowerUpperBound  = Math.Min(upperBoundLeft2, upperBoundRight2);
                            if (lowerUpperBound < lowestUpperBound)
                            {
                                lowestUpperBound = lowerUpperBound;
                                upperBoundLeft   = upperBoundLeft2;
                                upperBoundRight  = upperBoundRight2;
                                leftRegion       = leftRegion2;
                                rightRegion      = rightRegion2;
                                splitDim         = i;
                            }
                        }
                    }
                    if (splitDim < 0)
                    {
                        break;
                    }
                }
                else
                {
                    // Find a dimension to split on.
                    splitDim = Rand.Int(dim);
                    bool foundSplit = false;
                    for (int i = 0; i < dim; i++)
                    {
                        if (!DimensionCanSplit(splitDim))
                        {
                            splitDim++;
                            if (splitDim == dim)
                            {
                                splitDim = 0;
                            }
                        }
                        else
                        {
                            foundSplit = true;
                            break;
                        }
                    }
                    if (!foundSplit)
                    {
                        break;
                    }
                }

                // split the node
                double splitValue = midpoint[splitDim];
                if (Debug)
                {
                    Trace.WriteLine($"splitting dimension {splitDim}");
                }
                if (region.Upper[splitDim] != splitValue)
                {
                    if (leftRegion == null)
                    {
                        leftRegion = new Region(region);
                        leftRegion.Upper[splitDim] = splitValue;
                        upperBoundCount++;
                        upperBoundLeft = GetUpperBound(leftRegion);
                    }
                    addRegion(leftRegion, splitDim, upperBoundLeft);
                }
                if (region.Lower[splitDim] != splitValue)
                {
                    if (rightRegion == null)
                    {
                        rightRegion = new Region(region);
                        rightRegion.Lower[splitDim] = splitValue;
                        upperBoundCount++;
                        upperBoundRight = GetUpperBound(rightRegion);
                    }
                    addRegion(rightRegion, splitDim, upperBoundRight);
                }
            }
            if (Debug)
            {
                Trace.WriteLine($"BranchAndBound.Search upperBoundCount = {upperBoundCount}");
            }
            return(argmax);
        }
Пример #19
0
 public QueueNode(T value, QueueNode next = null, QueueNode prev = null)
 {
     this.Value = value;
 }
Пример #20
0
 public QueueNode(T element, QueueNode <T> prevNode = null, QueueNode <T> nextNode = null)
 {
     this.Value    = element;
     this.PrevNode = prevNode;
     this.NextNode = nextNode;
 }
Пример #21
0
 /// set node at iTo
 private void set(int iTo, ref QueueNode n)
 {
     n.index           = iTo;
     nodes[iTo]        = n;
     id_to_index[n.id] = iTo;
 }
Пример #22
0
 protected bool Equals(QueueNode other)
 {
     return(IndexInSystemArray == other.IndexInSystemArray);
 }
Пример #23
0
    /// <summary>
    /// Finds the shortest path from every point in the PRM to the goal.
    /// </summary>
    /// <param name="points">A list of points in the PRM</param>
    /// <param name="edges">A matrix of edges in the PRM</param>
    /// <param name="goalID">The index of the goal point in the points array</param>
    /// <param name="ct">A cancellation token to signal the task to abort early</param>
    private void FindPaths(Vector3[] points, float[,] edges, int goalID, CancellationToken ct)
    {
        Stopwatch sw = Stopwatch.StartNew();

        var len = points.Length;

        // Standard Djikstra's algorithm

        var openList = new PriorityQueue(len / 2);

        openList.Add(new QueueNode(points[goalID], 0, null, goalID));

        var closedList = new ClosedList(len);

        while (!openList.IsEmpty())
        {
            // If the background thread is cancelled, abort operation.
            if (ct.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            QueueNode current = openList.Pop();
            closedList.Add(current);

            for (int nextIdx = 0; nextIdx < len; nextIdx++)
            {
                if (nextIdx == current.ID)
                {
                    continue;
                }
                var costCurrentToNext = edges[current.ID, nextIdx];
                if (float.IsNegativeInfinity(costCurrentToNext))
                {
                    continue;
                }
                if (closedList.Contains(nextIdx))
                {
                    continue;
                }

                var totalCostToNext = current.Depth + costCurrentToNext;

                if (openList.Contains(nextIdx))
                {
                    openList.ReparentPathNode(nextIdx, current, costCurrentToNext);
                }
                else
                {
                    openList.Add(new QueueNode(points[nextIdx], totalCostToNext, current, nextIdx));
                }
            }
        }

        sw.Stop();

        Debug.Log("Found paths in " + sw.ElapsedMilliseconds + "ms");

        Results = new PathNode[len];
        foreach (var pnode in closedList.List)
        {
            for (int i = 0; i < len; i++)
            {
                if (pnode.ID != i)
                {
                    continue;
                }

                Vector3 direction = pnode.Parent != null ? pnode.Parent.Position - pnode.Position: Vector3.zero;
                Results[i] = new PathNode(pnode.Position, direction, pnode.Depth);
                break;
            }
        }
    }
Пример #24
0
 public DoubleLinkedQueue()
 {
     this.queueHead = null;
     this.queueTail = null;
     this.Count     = 0;
 }
Пример #25
0
 public void Add(QueueNode node)
 {
     _list.Add(node);
     _hashSet.Add(node.ID);
 }
Пример #26
0
 public LinkedQueue()
 {
     Count = 0;
     _head = _tail = null;
 }
Пример #27
0
            public ServiceNode(Service service)
            {
                Service = service;
                InputQueueNode = new QueueNode(Service.InputQueue);
                ErrorQueueNode = new QueueNode(Service.ErrorQueue);

                Nodes.Add(InputQueueNode);
                Nodes.Add(ErrorQueueNode);

                ImageIndex = SelectedImageIndex = 0;

                UpdateData();
                UpdateNodeText();
            }
Пример #28
0
 public QueueNode(T value, QueueNode <T> nextNode = null, QueueNode <T> prevNote = null)
 {
     this.Value    = value;
     this.NextNode = nextNode;
     this.PrevNode = prevNote;
 }
Пример #29
0
 public QueueNode(A info, QueueNode next)
 {
     Info = info;
     Next = next;
 }
Пример #30
0
 public QueueNode(T value, QueueNode <T> prev = null)
 {
     this.Value    = value;
     this.PrevNode = prev;
 }
Пример #31
0
        /// <summary>
        /// Finds the input that maximizes a function.
        /// </summary>
        /// <param name="bounds">Bounds for the search.</param>
        /// <param name="Evaluate">The function to maximize.</param>
        /// <param name="GetUpperBound">Returns an upper bound to the function in a region.  Need not be tight, but must become tight as the region shrinks.</param>
        /// <param name="xTolerance">Allowable relative error in the solution on any dimension.  Must be greater than zero.</param>
        /// <returns>A Vector close to the global maximum of the function.</returns>
        public static Vector Search(Region bounds, Func <Vector, double> Evaluate, Func <Region, double> GetUpperBound, double xTolerance = 1e-4)
        {
            if (xTolerance <= 0)
            {
                throw new ArgumentOutOfRangeException($"xTolerance <= 0");
            }
            int dim = bounds.Lower.Count;

            if (dim == 0)
            {
                return(Vector.Zero(dim));
            }
            PriorityQueue <QueueNode> queue = new PriorityQueue <QueueNode>();
            double lowerBound              = double.NegativeInfinity;
            Vector argmax                  = bounds.GetMidpoint();
            long   upperBoundCount         = 0;
            Action <Region, int> addRegion = delegate(Region region, int splitDim)
            {
                Stopwatch watch       = Stopwatch.StartNew();
                double    upperBoundF = GetUpperBound(region);
                watch.Stop();
                if (Debug)
                {
                    if (Debug && timeAccumulator.Count > 10 && watch.ElapsedMilliseconds > timeAccumulator.Mean + 4 * Math.Sqrt(timeAccumulator.Variance))
                    {
                        Trace.WriteLine($"GetUpperBound took {watch.ElapsedMilliseconds}ms");
                    }
                    timeAccumulator.Add(watch.ElapsedMilliseconds);
                }
                upperBoundCount++;
                //if (upperBoundCount % 100 == 0) Trace.WriteLine($"lowerBound = {lowerBound}");
                double upperBound = upperBoundF;
                if (upperBound > lowerBound)
                {
                    if (Debug)
                    {
                        Trace.WriteLine($"added region {region} with upperBoundF = {upperBoundF}");
                    }
                    QueueNode node = new QueueNode(region, upperBound, splitDim);
                    queue.Add(node);
                }
                else if (Debug)
                {
                    Trace.WriteLine($"rejected region {region} with upperBound {upperBound} <= lowerBound {lowerBound}");
                }
            };

            addRegion(bounds, 0);
            while (queue.Count > 0)
            {
                var node = queue.ExtractMinimum();  // gets the node with highest upper bound
                if (node.UpperBound <= lowerBound)
                {
                    continue;
                }
                Region region = node.Region;
                // compute the lower bound
                Vector    midpoint       = region.GetMidpoint();
                Stopwatch watch          = Stopwatch.StartNew();
                double    nodeLowerBound = Evaluate(midpoint);
                watch.Stop();
                if (Debug)
                {
                    if (Debug && timeAccumulator.Count > 10 && watch.ElapsedMilliseconds > timeAccumulator.Mean + 4 * Math.Sqrt(timeAccumulator.Variance))
                    {
                        Trace.WriteLine($"Evaluate took {watch.ElapsedMilliseconds}ms");
                    }
                    timeAccumulator.Add(watch.ElapsedMilliseconds);
                }
                if (Debug)
                {
                    Trace.WriteLine($"expanding {node} lower bound = {nodeLowerBound}");
                }
                if (nodeLowerBound > node.UpperBound)
                {
                    throw new Exception("nodeLowerBound > node.UpperBound");
                }
                if (nodeLowerBound > lowerBound)
                {
                    argmax     = midpoint;
                    lowerBound = nodeLowerBound;
                }

                // Find a dimension to split on.
                // As a region gets split, this will cycle through the dimensions.
                int splitDim = (node.SplitDim + 1) % dim;
                // To avoid storing SplitDims, we could make a random choice.
                // However, this takes much longer to converge.
                //int splitDim = Rand.Int(dim);
                bool foundSplit = false;
                for (int i = 0; i < dim; i++)
                {
                    if (MMath.AbsDiff(region.Upper[splitDim], region.Lower[splitDim], 1e-10) < xTolerance)
                    {
                        splitDim++;
                        if (splitDim == dim)
                        {
                            splitDim = 0;
                        }
                    }
                    else
                    {
                        foundSplit = true;
                        break;
                    }
                }
                if (!foundSplit)
                {
                    break;
                }

                // split the node
                double splitValue = midpoint[splitDim];
                if (region.Upper[splitDim] != splitValue)
                {
                    Region leftRegion = new Region(region);
                    leftRegion.Upper[splitDim] = splitValue;
                    addRegion(leftRegion, splitDim);
                }
                if (region.Lower[splitDim] != splitValue)
                {
                    Region rightRegion = new Region(region);
                    rightRegion.Lower[splitDim] = splitValue;
                    addRegion(rightRegion, splitDim);
                }
            }
            //Trace.WriteLine($"BranchAndBound.Search upperBoundCount = {upperBoundCount}");
            return(argmax);
        }
 public QueueNode(T value)
 {
     this.Value    = value;
     this.NextNode = null;
     this.PrevNode = null;
 }
Пример #33
0
 protected abstract QueueNode<IFrame> OnHit(QueueNode<IFrame> node, AccessType type);
 public LinkedQueue()
 {
     this.head  = null;
     this.tail  = null;
     this.Count = 0;
 }
Пример #35
0
 public void QueueNodeConstructor()
 {
     var node1 = new QueueNode <int>();
     var node2 = new QueueNode <string>("aaa");
 }
 //c'tor
 public Manager()
 {
     _documentsManagementSystem = new Dictionary <string, Document>();
     _timeStamps = new QueueNode <TimeData>();
     Timer timerOld = new Timer(DeleteOld, null, 0, 300000);
 }