Пример #1
0
        /// <inheritdoc />
        public bool TryPop(out HeapPair <T, TPriority> value)
        {
            value = default;
            if (IsEmpty)
            {
                return(false);
            }

            // Swap then remove.
            var root = _binaryHeap[0];

            Swap(0, Count - 1);
            _binaryHeap.RemoveAt(Count - 1);

            // Assign item and remove it from mapping.
            value = HeapPair.Create(root.Value, root.Priority);
            _lookup.Remove(root.Value);
            HeapNodePool.Return(root);

            // If there aren't any elements left, no sifting is needed.
            if (1 < Count)
            {
                SiftDown(0);
            }

            return(true);
        }
Пример #2
0
        /// <inheritdoc />
        public bool TryPeek(out HeapPair <T, TPriority> value)
        {
            value = default;
            if (!_binaryHeap.Any())
            {
                return(false);
            }

            var element = _binaryHeap[0];

            value = HeapPair.Create(element.Value, element.Priority);
            return(true);
        }
Пример #3
0
        /// <inheritdoc />
        public bool TryReplace(T newValue, TPriority priority, out HeapPair <T, TPriority> oldValue)
        {
            oldValue = default;
            if (IsEmpty)
            {
                return(false);
            }

            var node = _binaryHeap[0];

            oldValue = HeapPair.Create(node.Value, node.Priority);
            _lookup.Remove(node.Value);
            _lookup.Add(newValue, node);
            node.Value    = newValue;
            node.Priority = priority;
            SiftDown(node.Index);

            return(true);
        }
Пример #4
0
 /// <inheritdoc />
 IEnumerator <HeapPair <T, TPriority> > IEnumerable <HeapPair <T, TPriority> > .GetEnumerator() =>
 _binaryHeap.Select(n => HeapPair.Create(n.Value, n.Priority)).GetEnumerator();