コード例 #1
0
ファイル: BinaryHeap.cs プロジェクト: ILoveEatRice/Vola
        public void Delete(IHeapNode <T> element)
        {
            var index = _heap.FindLastIndex(e => e.Equals(element));

            if (index == 0 && Size() == 1)
            {
                _heap.RemoveAt(index);
                return;
            }
            if (index == Size() - 1)
            {
                _heap.RemoveAt(index);
                return;
            }
            Swap(index, Size() - 1);
            _heap.RemoveAt(Size() - 1);
            var parent = Parent(index);

            if (index != 0 && Compare(_heap[index], _heap[parent]) < 0)
            {
                UpHeap(index);
            }
            else
            {
                DownHeap(index);
            }
        }
コード例 #2
0
    public static IHeapNode MaxPop(List <IHeapNode> key)
    {
        int       l   = key.Count - 1;
        IHeapNode max = key[0];

        (key[0], key[l]) = (key[l], key[0]);
        key.Remove(max);
        SiftMaxDown(key, 0, l);
        return(max);
    }
コード例 #3
0
    public static IHeapNode MinPop(List <IHeapNode> key)
    {
        int       l   = key.Count - 1;
        IHeapNode min = key[0];

        (key[0], key[l]) = (key[l], key[0]);
        key.Remove(min);
        SiftMinDown(key, 0, l);
        return(min);
    }
コード例 #4
0
ファイル: BinomialHeap.cs プロジェクト: ILoveEatRice/Vola
        public void DecreaseKey(IHeapNode <T> element)
        {
            var node = element as Node <T>;

            if (node == null)
            {
                throw new ArgumentException();
            }
            UpHeap(node, false);
        }
コード例 #5
0
    public static void MaxHeapSort(List <IHeapNode> key)
    {
        int l = key.Count;

        for (int i = l - 1; i > -1; i--)
        {
            IHeapNode save = key[i];
            key[i] = key[0];
            key[0] = save;
            SiftMinDown(key, 0, i);
        }
    }
コード例 #6
0
        private void Analyze(bool full)
        {
            var state  = GetCurrentState();
            var viewer = new ScrollViewer();
            var panel  = new VirtualizingStackPanel();

            viewer.Content = panel;
            UXHeader.Text  = $"{"Address",20} {"Offset",10} {(full ? $"{"Value",10}" : "")}{"Blk",26} {"Blk.Ofs",10} {"Blk.Size",10}";
            IHeapNode blk = null;

            for (int i = 0; i < (int)state.Size; i += (full && blk == null) ? 4 : _ctx.Memory.Reader.PointerSize)
            {
                var ptr = _ctx.Memory.Reader.Read <Ptr>(state.Start + i);
                blk = i % _ctx.Memory.Reader.PointerSize == 0 ? _idx.FindNode(ptr.ValueAddress) : null;
                var line = "";

                if (blk != null || full)
                {
                    line += $"{state.Start + i,26:X16} {i,10:X3}";
                    if (full)
                    {
                        line += $" {_ctx.Memory.Reader.Read<int>(state.Start + i),10:X} ({new string(_ctx.Memory.Reader.Read<byte>(state.Start + i, 4).Select(b => (char)b).Select(c => c >= 0x20 && c <= 0x7E ? c : ' ').ToArray()),4})";
                    }
                }
                if (blk != null)
                {
                    var offset = (int)(ptr.ValueAddress - blk.Data.ValueAddress);
                    line += $" {blk.Address.ToString(),26} {offset,10:X} {blk.Size,10}";
                    if (ptr.ValueAddress >= state.Start && ptr.ValueAddress < state.Start + state.Size)
                    {
                        line += $" this+{ptr.ValueAddress - state.Start:X}";
                    }

                    var link = new Hyperlink(new Run {
                        Text = line
                    });
                    link.Tag    = new Segment(blk.Data.ValueAddress, blk.Size);
                    link.Click += Link_Click;
                    panel.Children.Add(new TextBlock(link)
                    {
                        FontFamily = new FontFamily("Consolas")
                    });
                }
                else if (full)
                {
                    panel.Children.Add(new TextBlock {
                        Text = line, FontFamily = new FontFamily("Consolas")
                    });
                }
            }

            UXView.Content = viewer;
        }
コード例 #7
0
ファイル: BinaryHeap.cs プロジェクト: ILoveEatRice/Vola
        public void DecreaseKey(IHeapNode <T> element)
        {
            var node = element as Node <T>;

            if (node == null)
            {
                throw new ArgumentException();
            }
            var index = _heap.IndexOf(node);

            if (index >= 0)
            {
                UpHeap(index);
            }
        }
コード例 #8
0
        public TValue Remove(IHeapNode <TKey, TValue> node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var casted = node as TNode;

            if (casted == null)
            {
                throw new ArgumentException(FormatTypeMismatchMessage(typeof(TNode), node.GetType()));
            }

            return(this.Remove(casted));
        }
コード例 #9
0
        public void Update(IHeapNode <TKey, TValue> node, TKey key)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var casted = node as TNode;

            if (casted == null)
            {
                throw new ArgumentException(FormatTypeMismatchMessage(typeof(TNode), node.GetType()));
            }

            this.Update(casted, key);
        }
コード例 #10
0
        public void Delete(IHeapNode <T> element)
        {
            var node = element as Node <T>;

            if (node == null)
            {
                throw new ArgumentException();
            }
            var parent = node.Parent;

            if (parent != null)
            {
                Cut(node, parent);
                CascadingCut(parent);
            }
            _minNode = node;
            ExtractMin();
        }
コード例 #11
0
        public void DecreaseKey(IHeapNode <T> element)
        {
            var node = element as Node <T>;

            if (node == null)
            {
                throw new ArgumentException();
            }
            var parent = node.Parent;

            if (parent != null && Compare(node, parent) < 0)
            {
                Cut(node, parent);
                CascadingCut(parent);
            }
            if (Compare(node, _minNode) < 0)
            {
                _minNode = node;
            }
        }
コード例 #12
0
ファイル: BinomialHeap.cs プロジェクト: ILoveEatRice/Vola
        public void Delete(IHeapNode <T> element)
        {
            var node = element as Node <T>;

            if (node == null)
            {
                throw new ArgumentException();
            }
            node = UpHeap(node, true);
            if (_head == node)
            {
                RemoveTreeRoot(node, null);
            }
            else
            {
                var prev = _head;
                while (Compare(prev.Sibling, node) != 0)
                {
                    prev = prev.Sibling;
                }
                RemoveTreeRoot(node, prev);
            }
        }
コード例 #13
0
    public static void SiftMaxDown(List <IHeapNode> heap, int index, int n)
    {
        int indexSon = 2 * index + 1;

        while (indexSon < n)
        {
            if (indexSon + 1 < n && heap[indexSon].Comparable() < heap[indexSon + 1].Comparable())
            {
                indexSon++;
            }

            if (heap[indexSon].Comparable() <= heap[index].Comparable())
            {
                return;
            }

            IHeapNode save = heap[index];
            heap[index]    = heap[indexSon];
            heap[indexSon] = save;
            index          = indexSon;
            indexSon       = indexSon * 2 + 1;
        }
    }
コード例 #14
0
ファイル: Extensions.cs プロジェクト: Torpedoes/Enigma.D3
 public static bool Contains(this IHeapNode node, MemoryAddress address)
 {
     return(node.Data.ValueAddress <= address &&
            node.Data.ValueAddress + node.Size > address);
 }
コード例 #15
0
ファイル: HandleKeyPair.cs プロジェクト: etsangsplk/algo-kit
 public HandleKeyPair(IHeapNode <int, string> node, int key)
 {
     this.Node = node;
     this.Key  = key;
 }
コード例 #16
0
ファイル: BinomialHeap.cs プロジェクト: ILoveEatRice/Vola
 private int Compare(IHeapNode <T> left, IHeapNode <T> right)
 {
     return(_comparer.Compare(left.Value, right.Value));
 }
コード例 #17
0
 public static void MaxPush(List <IHeapNode> key, IHeapNode item)
 {
     key.Add(item);
     SiftMaxUp(key, key.Count - 1);
 }