public void Add(T value, IHandle ihandle) { if (this.size == this.items.Length) { int capacity = (int)((this.items.Length * 200L) / 100L); if (capacity < (this.items.Length + DEFAULT_CAPACITY)) { capacity = this.items.Length + DEFAULT_CAPACITY; } Array.Resize(ref this.items, capacity); } Handle handle = null; if (ihandle != null) { handle = ValidateHandle(ihandle); handle.heap = this; } HeapItem item = new HeapItem(value, handle); Set(item, this.size); BubbleUp(this.size++); }
public HeapItem[] TestItems() { var a = new HeapItem("item1", 1); var b = new HeapItem("item2", 2); return(new HeapItem[] { a, b }); }
private void Up(int index) { if (index == 0) { return; } int parentIndex = (index - 1) / 2; HeapItem swap = _store[index]; if (_comparer.Compare(_store[parentIndex].Key, swap.Key) <= 0) { return; } while (true) { SwapIndices(parentIndex, index); index = parentIndex; if (index == 0) { break; } parentIndex = (index - 1) / 2; if (_comparer.Compare(_store[parentIndex].Key, swap.Key) <= 0) { break; } } InsertItem(ref swap, index); }
private void itemSwap(int index0, int index1) { HeapItem item = _array[index0]; _array[index0] = _array[index1]; _array[index1] = item; }
//시각화를 위한 부분 : RETURN pC.IsEmpty public bool next(ref Bitmap visual, ref Graphics g, ref int[,] Vs, int width, int height) { if (pC.IsEmpty) { return(pC.IsEmpty); } HeapItem top_item = pC.DeleteItem(); int v = (int)top_item.Value; //시각화 visualize(ref visual, ref g, ref Vs, width, height, v, -1, "힙(min-heap)에서 팝(POP)한 정점 " + v + "을 선택"); int cnt = W[v].Count; for (int i = 0; i < cnt; i++) { int u = W[v][i].v; visualize(ref visual, ref g, ref Vs, width, height, v, i, "선택한 정점 " + v + "와 연결된 정점 " + u + "탐색"); if (C[u] == -1 || C[u] > C[v] + W[v][i].w) { C[u] = C[v] + W[v][i].w; pC.InsertItem(new HeapItem(C[u], u)); preV[u] = v; //시각화 visualize(ref visual, ref g, ref Vs, width, height, v, i, "정점 " + u + "로 가는 최소 경로 갱신 및 힙(min-heap)에 삽입(PUSH)"); } } return(false); }
private void BubbleDown(int index) { HeapItem item = this.items[index]; int current, child; for (current = index, child = (2 * current) + 1; current < this.size / 2; current = child, child = (2 * current) + 1) { if ((child < this.size - 1) && this.comparison(this.items[child].value, this.items[child + 1].value) > 0) { ++child; } if (this.comparison(this.items[child].value, item.value) >= 0) { break; } Set(this.items[child], current); } if (current != index) { Set(item, current); ++this.version; } }
private void GrowHeap() { Capacity = (Capacity * 2) + 1; HeapItem[] newHeap = new HeapItem[Capacity]; System.Array.Copy(heap, 0, newHeap, 0, Count); heap = newHeap; }
public T Remove(IHandle ihandle) { Handle handle = ValidateThisHandle(ihandle); HeapItem item = this.items[handle.index]; RemoveAt(handle.index); return(item.value); }
private void Set(HeapItem item, int index) { this.items[index] = item; if (item.handle != null) { item.handle.index = index; } }
public void Push(HeapItem v) { if (Count >= heap.Length - 1) { throw new InvalidOperationException(); } // Usually this would be the place so increase size of heap if needed, but it's pre-allocated. heap[Count] = v; SiftUp(Count++); }
private static TestCaseData GenerateHeapTestCase(params int[] values) { var testCaseData = new HeapItem[values.Length]; for (var i = 0; i < values.Length; i++) { testCaseData[i] = new HeapItem(values[i]); } return(new TestCaseData(new[] { testCaseData }).SetName($"Values: {string.Join(", ", values)}")); }
public void Add(T item, int priority) { var node = new HeapItem <T>(item, priority); _items[_currentIndex] = node; _indexes[_items[_currentIndex].Item] = _currentIndex; PercolateUp(_currentIndex); _currentIndex++; }
public void Insert(TValue value, TKey key) { EnsureCapacity(Capacity + 1); var item = new HeapItem(key, value); _store[Count] = item; Up(Count); Count++; }
int CompareCost(HeapItem a, HeapItem b) { if (a.Cost < b.Cost) { return(1); } if (a.Cost > b.Cost) { return(-1); } return(0); }
public static HeapItem <int>[] ToHeap(this int[] array) { HeapItem <int>[] heap = new HeapItem <int> [array.Length]; for (int i = 0; i < array.Length; i++) { heap[i] = new HeapItem <int>(); heap[i].Prioridade = array[i]; heap[i].Value = array[i]; } return(heap); }
private void BubbleUp(int idx, HeapItem item) { int iParent = (idx - 1) / 2; while (idx > 0 && heap[iParent].Priority < item.Priority) { heap[idx] = heap[iParent]; idx = iParent; iParent = (idx - 1) / 2; } heap[idx] = item; }
public T RemoveMin() { if (this.size == 0) { throw new InvalidOperationException("Heap is empty"); } HeapItem item = this.items[0]; RemoveAt(0); return(item.value); }
private void BubbleUp(int index, HeapItem he) { int parent = Parent(index); while (index > 0 && heap[parent].Priority < he.Priority) { heap[index] = heap[parent]; index = parent; parent = Parent(index); } heap[index] = he; }
/// <summary> /// Creates an empty collection. /// </summary> /// <param name="maxAddress">maximum address allowed</param> public AddressPriorityQueue(int maxAddress) { if (maxAddress < 0 || maxAddress > 0x7FFFFFFE) { throw new Exception("AddressPriorityQueue: Maximum capacity is 1<<31 addresses!"); } this.maxAddress = maxAddress; allocatedSize = 4 * (int)Math.Sqrt(maxAddress + 1); // good for addresses in a 2D-space binaryHeap = new HeapItem[allocatedSize]; binaryHeapSize = 0; states = new uint[maxAddress + 1]; lastTakenDistance = 0; taken = 0; }
public void Push(int item) { if (Size == capacity) { HeapItem[] extendedArray = new HeapItem[capacity * 2]; for (int i = 0; i < capacity; i++) extendedArray[i] = items[i]; items = extendedArray; capacity *= 2; } Size++; items[Size - 1] = new HeapItem() { Value = item, OperationNumber = Operations}; SiftUp(Size - 1); Operations++; }
public void InserenaHeap(int valor, int prioridade) { HeapItem <int>[] novaHeap = new HeapItem <int> [H.Length + 1]; for (int i = 0; i < H.Length; i++) { novaHeap[i] = H[i]; } novaHeap[H.Length] = new HeapItem <int>() { Value = valor, Prioridade = prioridade }; H = novaHeap; CorrigeHeapSubindo(novaHeap.Length - 1); }
private void Down(int index) { if (Count == index + 1) { return; } int left = index * 2 + 1; int right = left + 1; HeapItem swap = _store[index]; while (right <= Count) // While the current node has children { var nLeft = _store[left]; var nRight = _store[right]; if (right == Count || _comparer.Compare(nLeft.Key, nRight.Key) < 0) // Only the left child exists or the left child is smaller { if (_comparer.Compare(swap.Key, nLeft.Key) <= 0) { break; } SwapIndices(left, index); index = left; left = index * 2 + 1; right = left + 1; } else // Right child exists and is smaller { if (_comparer.Compare(swap.Key, nRight.Key) <= 0) { break; } SwapIndices(right, index); index = right; left = index * 2 + 1; right = left + 1; } } InsertItem(ref swap, index); }
private void TrickleDown(int idx, HeapItem item) { int iChild = idx * 2 + 1; while (iChild < count) { if (iChild + 1 < count && heap[iChild].Priority < heap[iChild + 1].Priority) { ++iChild; } heap[idx] = heap[iChild]; idx = iChild; iChild = idx * 2 + 1; } BubbleUp(idx, item); }
public void RemoveDaHeap() { if (H.Length > 0) { H[0] = H[H.Length - 1]; CorrigeHeapDescendo(0); } HeapItem <int>[] novaHeap = new HeapItem <int> [H.Length - 1]; for (int i = 0; i < novaHeap.Length; i++) { novaHeap[i] = H[i]; } H = novaHeap; }
public void MaxHeap_Contains_False(HeapItem[] items) { //Create the heap and add the items. var heap = new MaxHeap <HeapItem>(items.Length); for (var i = 0; i < items.Length; i++) { heap.Add(items[i]); } //Create new items and check if contains returns false for (var i = 0; i < items.Length; i++) { var item = new HeapItem(items[i].Value); Assert.IsFalse(heap.Contains(item), $"Contains returned true for item {item}"); } }
/// <summary> /// Get and remove address with shortest offered distance. /// </summary> /// <param name="address">the address</param> /// <param name="distance">the distance</param> /// <returns>true if operation was successful, false for empty collection</returns> public bool TakeClosest(out int address, out int distance) { if (binaryHeapSize == 0) { address = 0; distance = 0; return(false); } address = binaryHeap[0].address; distance = binaryHeap[0].distance; states[address] = (uint)distance | wasTakenFlag; lastTakenDistance = distance; taken++; #region binary heap shrink algo binaryHeapSize--; if (binaryHeapSize > 0) { HeapItem last = binaryHeap[binaryHeapSize]; int index = 0; int child = 1; while (child < binaryHeapSize) { if (child < binaryHeapSize - 1 && binaryHeap[child].distance > binaryHeap[child + 1].distance) { child++; // right child instead of left } if (last.distance <= binaryHeap[child].distance) { break; } binaryHeap[index] = binaryHeap[child]; states[binaryHeap[index].address] = (uint)index + 1; index = child; child = 2 * child + 1; } binaryHeap[index] = last; states[last.address] = (uint)index + 1; } #endregion return(true); }
private void TrickleDown(int index, HeapItem he) { int child = LeftChild(index); while (child < Count) { if (child + 1 < Count && heap[child].Priority < heap[child + 1].Priority) { ++child; } heap[index] = heap[child]; index = child; child = LeftChild(index); } BubbleUp(index, he); }
private bool BubbleUp(int index) { HeapItem item = this.items[index]; int current, parent; for (current = index, parent = (current - 1) / 2; (current > 0) && (this.comparison(this.items[parent].value, item.value)) > 0; current = parent, parent = (current - 1) / 2) { Set(this.items[parent], current); } if (current != index) { Set(item, current); ++this.version; return(true); } return(false); }
public void InsertItem(HeapItem item) { if (_lastIndex + 1 == _capacity) { _capacity += 2; Array.Resize(ref _array, _capacity); } _array[++_lastIndex] = item; int currentPosition = _lastIndex; int parentPosition = getParentIndex(currentPosition); while (currentPosition > 0 && (_array[currentPosition].Ranking < _array[parentPosition].Ranking)) { itemSwap(currentPosition, parentPosition); currentPosition = parentPosition; parentPosition = getParentIndex(currentPosition); } }
//다익스트라 알고리즘 작동 public void run() { while (!pC.IsEmpty) { HeapItem top_item = pC.DeleteItem(); int v = (int)top_item.Value; int cnt = W[v].Count; for (int i = 0; i < cnt; i++) { int u = W[v][i].v; if (C[u] == -1 || C[u] > C[v] + W[v][i].w) { C[u] = C[v] + W[v][i].w; pC.InsertItem(new HeapItem(C[u], u)); preV[u] = v; //pC.PrintHeap(); } } } }
public void EnsureCapacity(int capacity) { if (_store.Length >= capacity) { return; } //double capacity until we reach the minimum requested capacity (or greater) int newcap = Capacity * 2; while (newcap < capacity) { newcap *= 2; } var newArray = new HeapItem[newcap]; Array.Copy(_store, newArray, Capacity); _store = newArray; Capacity = newcap; }