private void SwapHeapPositions(int indexX, int indexY) { HeapEntry originalXEntry = _HeapEntries[indexX]; HeapEntry originalYEntry = _HeapEntries[indexY]; if (originalXEntry.IsDeleted) { _HeapEntries[indexY] = originalXEntry; } else { _HeapEntries[indexY] = originalXEntry; InsertIntoHashtable(originalXEntry.Key, indexY); } if (originalYEntry.IsDeleted) { _HeapEntries[indexX] = originalYEntry; } else { _HeapEntries[indexX] = originalYEntry; InsertIntoHashtable(originalYEntry.Key, indexX); } }
private void Swap(int index1, int index2) { HeapEntry heapEntry = m_keys[index1]; m_keys[index1] = m_keys[index2]; m_keys[index2] = heapEntry; }
private void Swap(int index1, int index2) { HeapEntry heapEntry = this.m_keys[index1]; this.m_keys[index1] = this.m_keys[index2]; this.m_keys[index2] = heapEntry; }
public void AddOrReplace(TKey key, TValue value, DateTime due) { if (key == null) { throw new ArgumentNullException("key"); } if (due.Kind != DateTimeKind.Utc) { throw new ArgumentException("DateTime argument not provided in UTC.", "due"); } lock (_InternalLock) // Write operation { int heapIndex = FindHeapIndexInHashtable(key); _Version++; if (heapIndex >= 0) { HeapEntry cancelledAppointment = _HeapEntries[heapIndex]; cancelledAppointment.IsDeleted = true; DeleteFromHashtable(key); _PendingDeleteCount++; } heapIndex = InsertIntoHeap(key, value, due); TrackModification(); } }
private void siftdown(KeyT k, V v, int ndx) { var cnt = pq.Count - 1; var i = ndx; for (var ni = i + i + 1; ni < cnt; ni = ni + ni + 1) { var oi = i; var lk = pq[ni].k; var rk = pq[ni + 1].k; var nk = k; if (k > lk) { i = ni; nk = lk; } if (nk > rk) { ni += 1; i = ni; } if (i != oi) { pq[oi] = pq[i]; } else { break; } } pq[i] = new HeapEntry(k, v); }
/// <summary> /// Remove minimum from heap. /// <returns> Minimum</returns> /// </summary> public HeapEntry Pop() { version++; if (count == 0) { throw new Exception("Heap is empty"); } if (isHeap) { HeapEntry returnValue = elements[0]; HeapEntry value = elements[count - 1]; count--; int current = 0; while (current < count) { int left = (current << 1) + 1; int right = (current << 1) + 2; int swapIndex = current; if (left >= count) { swapIndex = current; } else if (right >= count) { if (comparer.Compare(elements[left].Value, value.Value) < 0) { swapIndex = left; } } else { if (comparer.Compare(elements[left].Value, elements[right].Value) < 0) { if (comparer.Compare(elements[left].Value, value.Value) < 0) { swapIndex = left; } } else if (comparer.Compare(elements[right].Value, value.Value) < 0) { swapIndex = right; } } if (swapIndex == current) { break; } elements[current] = elements[swapIndex]; current = swapIndex; } elements[current] = value; return(returnValue); } else { count--; return(elements[count]); } }
public bool TryRemove(TKey key, out TValue result) { if (key == null) { throw new ArgumentNullException("key"); } lock (_InternalLock) // Write operation { int heapIndex = FindHeapIndexInHashtable(key); if (heapIndex < 0) { result = default(TValue); return(false); } _Version++; HeapEntry cancelledAppointment = _HeapEntries[heapIndex]; cancelledAppointment.IsDeleted = true; DeleteFromHashtable(key); _PendingDeleteCount++; TrackModification(); result = cancelledAppointment.Value; return(true); } }
private HeapEntry PopHeap() { HeapEntry returnValue = elements[0]; int last = head; head = elements[0].Key; keysPosition[head] = -last - 1; elements[0] = elements[count - 1]; HeapEntry value = elements[count - 1]; count--; if (count == 0) { return(returnValue); } int current = 0; while (current < count) { int left = (current << 1) + 1; int right = (current << 1) + 2; int swapIndex = current; if (left > count) { swapIndex = current; } else if (right > count) { if (comparer.Compare(elements[left].Value, value.Value) < 0) { swapIndex = left; } } else { if (comparer.Compare(elements[left].Value, elements[right].Value) < 0) { if (comparer.Compare(elements[left].Value, value.Value) < 0) { swapIndex = left; } } else if (comparer.Compare(elements[right].Value, value.Value) < 0) { swapIndex = right; } } if (swapIndex == current) { break; } elements[current] = elements[swapIndex]; keysPosition[elements[current].Key] = current; current = swapIndex; } elements[current] = value; keysPosition[elements[current].Key] = current; return(returnValue); }
private bool ModifyList(int key, TValue newValue) { if (key >= keysPosition.Length || key < 0 || keysPosition[key] < 0) { return(false); } int index = keysPosition[key]; if (index < 0) { return(false); } for (int i = index; i < count - 1; ++i) { elements[i] = elements[i + 1]; keysPosition[elements[i].Key] = i; } count--; int insertIndex = BinarySearch(newValue); for (int i = count; i >= insertIndex + 1; i--) { elements[i] = elements[i - 1]; keysPosition[elements[i].Key] = i; } elements[insertIndex] = new HeapEntry(key, newValue); keysPosition[key] = insertIndex; count++; return(true); }
private void TrickleDown(int lastHeapIndex) { _IsHeapOrdered = false; int currentHeapIndex = 0; do { int originalHeapIndex = currentHeapIndex; int childHeapIndex1 = (currentHeapIndex << 1) + 1; int childHeapIndex2 = childHeapIndex1 + 1; if (lastHeapIndex > childHeapIndex1 && HeapEntry.Compare(_HeapEntries[currentHeapIndex], _HeapEntries[childHeapIndex1]) > 0) { currentHeapIndex = childHeapIndex1; } if (lastHeapIndex > childHeapIndex2 && HeapEntry.Compare(_HeapEntries[currentHeapIndex], _HeapEntries[childHeapIndex2]) > 0) { currentHeapIndex = childHeapIndex2; } if (currentHeapIndex == originalHeapIndex) { break; } SwapHeapPositions(currentHeapIndex, originalHeapIndex); } while (true); }
public void TestCopyTo() { var pq = new PriorityQueue(); string s; // use a hashtable to check contents of returned array Hashtable ht = new Hashtable(); for (int i = 0; i < 5; i++) { s = "item: " + i.ToString(); ht.Add(s, null); pq.Enqueue(s, i * 2); } HeapEntry[] heArray = new HeapEntry[6]; pq.CopyTo(heArray, 1); foreach (HeapEntry he in heArray) { if (he.Item != null) { Assert.True(ht.Contains(he.Item)); ht.Remove(he.Item); // so we don't see it again } } Assert.Empty(ht); }
public void Insert(K key, V value) { if (m_keys.Length == m_count) { if (m_count < m_maxCapacity || m_maxCapacity == -1) { int num = (int)((double)m_keys.Length * 1.5); if (m_maxCapacity > 0 && num > m_maxCapacity) { num = m_maxCapacity; } Array.Resize(ref m_keys, num); } else { Global.Tracer.Assert(condition: false, "Invalid Operation: Cannot add to heap at maximum capacity"); } } int num2 = m_count; m_count++; m_keys[num2] = new HeapEntry(key, value, m_insertIndex); m_insertIndex++; int num3 = (num2 - 1) / 2; while (num2 > 0 && LessThan(num3, num2)) { Swap(num3, num2); num2 = num3; num3 = (num2 - 1) / 2; } }
private bool ModifyList(TKey userKey, TValue newValue) { int key = -1; if (!keys.TryGetValue(userKey, out key)) { return(false); } int index = keysPosition[key]; if (index < 0) { return(false); } for (int i = index; i < count; ++i) { elements[i] = elements[i + 1]; keysPosition[elements[i].key] = i; } count--; int insertIndex = BinarySearch(newValue); for (int i = count; i >= insertIndex + 1; --i) { elements[i] = elements[i - 1]; keysPosition[elements[i].key] = keysPosition[elements[i - 1].key]; } elements[insertIndex] = new HeapEntry(key, userKey, newValue); keysPosition[key] = insertIndex; count++; return(true); }
/// <summary> /// Copy elements of heap to new array. /// </summary> /// <returns>Array with elements of heap.</returns> public HeapEntry[] ToArray() { HeapEntry[] destinationArray = new HeapEntry[count]; for (int i = 0; i < count; ++i) { destinationArray[i] = elements[i]; } return(destinationArray); }
private int InsertIntoHeap(TKey key, TValue value, DateTime due) { int nextAvailableHeapIndex = HeapCount; EnsureHeapCapacity(); _HeapEntries[nextAvailableHeapIndex] = new HeapEntry(due, _Version, key, value); InsertIntoHashtable(key, nextAvailableHeapIndex); return(TrickleUp(nextAvailableHeapIndex)); }
private void adj(Func <KeyT, V, Tuple <KeyT, V> > f) { var cnt = pq.Count - 1; for (var i = 0; i < cnt; ++i) { var e = pq[i]; var r = f(e.k, e.v); pq[i] = new HeapEntry(r.Item1, r.Item2); } reheap(0); }
private void bubbleUp(int index, HeapEntry he) { int parent = getParent(index); while ((index > 0) && // note: (index > 0) means there is a parent heap[parent].Priority.CompareTo(he.Priority) < 0) { heap[index] = heap[parent]; index = parent; parent = getParent(index); } heap[index] = he; }
public TValue AddOrReplace(TKey key, Func <TValue> addValueFactory, Func <TValue, DateTime, TValue> replaceValueFactory, Func <TValue, DateTime?, DateTime> dueFactory) { if (key == null) { throw new ArgumentNullException("key"); } TValue value; lock (_InternalLock) // Write operation { int heapIndex = FindHeapIndexInHashtable(key); _Version++; if (heapIndex >= 0) { HeapEntry cancelledAppointment = _HeapEntries[heapIndex]; cancelledAppointment.IsDeleted = true; DeleteFromHashtable(key); _PendingDeleteCount++; value = replaceValueFactory(cancelledAppointment.Value, cancelledAppointment.Due); DateTime due = dueFactory(value, cancelledAppointment.Due); if (due.Kind != DateTimeKind.Utc) { throw new ArgumentException("DateTime argument not provided in UTC.", "due"); } heapIndex = InsertIntoHeap(key, value, due); } else { value = addValueFactory(); DateTime due = dueFactory(value, null); if (due.Kind != DateTimeKind.Utc) { throw new ArgumentException("DateTime argument not provided in UTC.", "due"); } heapIndex = InsertIntoHeap(key, value, dueFactory(value, null)); } TrackModification(); } return(value); }
private HeapEntry ModifyTopHeap(TValue newValue) { HeapEntry returnValue = elements[0]; HeapEntry value = new HeapEntry(elements[0].key, elements[0].Key, newValue); int current = 0; while (current < count) { int left = (current << 1) + 1; int right = (current << 1) + 2; int swapIndex = current; if (left >= count) { swapIndex = current; } else if (right >= count) { if (comparer.Compare(elements[left].Value, value.Value) < 0) { swapIndex = left; } } else { if (comparer.Compare(elements[left].Value, elements[right].Value) < 0) { if (comparer.Compare(elements[left].Value, value.Value) < 0) { swapIndex = left; } } else if (comparer.Compare(elements[right].Value, value.Value) < 0) { swapIndex = right; } } if (swapIndex == current) { break; } elements[current] = elements[swapIndex]; keysPosition[elements[current].key] = current; current = swapIndex; } elements[current] = value; keysPosition[elements[current].key] = current; return(returnValue); }
private void AddList(TValue value, TAttachments attachments) { int insertIndex = BinarySearch(value); for (int k = Count; k > insertIndex; k--) { elements[k] = elements[k - 1]; } elements[insertIndex] = new HeapEntry(value, attachments); count++; if (count > ListTreshold) { RebuildToHeap(); } }
private void bubbleUp(long index, HeapEntry he) { long parent = (index - 1) / 2; // note: (index > 0) means there is a parent while ( (index > 0) && ((this.descending && heap[parent].Priority < he.Priority) || (!this.descending && heap[parent].Priority > he.Priority)) ) { heap[index] = heap[parent]; index = parent; parent = (index - 1) / 2; } heap[index] = he; }
void AddHeap(TValue value, TKey key) { int index = count; int next = (index - 1) >> 1; while (index > 0 && comparer.Compare(value, elements[next].Value) < 0) { keysPosition[elements[next].key] = index; elements[index] = elements[next]; index = next; next = (index - 1) >> 1; } elements[index] = new HeapEntry(GetNextFreeKey(), key, value); keysPosition[elements[index].key] = index; keys.Add(key, elements[index].key); count++; }
private int AddHeap(TValue value) { int index = count; int next = (index - 1) >> 1; while (index > 0 && comparer.Compare(value, elements[next].Value) < 0) { keysPosition[elements[next].Key] = index; elements[index] = elements[next]; index = next; next = (index - 1) >> 1; } elements[index] = new HeapEntry(GetNextFreeKey(), value); keysPosition[elements[index].Key] = index; count++; return(elements[index].Key); }
private void trickleDown(int index, HeapEntry he) { int child = getLeftChild(index); while (child < count) { if (((child + 1) < count) && (heap[child].Priority.CompareTo(heap[child + 1].Priority) < 0)) { child++; } heap[index] = heap[child]; index = child; child = getLeftChild(index); } bubbleUp(index, he); }
private void PruneDeletedEntries() { bool deletedEntriesRemoved = false; while (IsDeletedEntryAtHead) { if (!deletedEntriesRemoved) { _Version++; deletedEntriesRemoved = true; } int lastHeapIndex = HeapCount - 1; HeapEntry followingAppointment = _HeapEntries[lastHeapIndex]; if (lastHeapIndex > 0) { if (!followingAppointment.IsDeleted) { DeleteFromHashtable(followingAppointment.Key); } _HeapEntries[lastHeapIndex] = null; _HeapEntries[0] = followingAppointment; _PendingDeleteCount--; if (!followingAppointment.IsDeleted) { InsertIntoHashtable(followingAppointment.Key, 0); } TrickleDown(lastHeapIndex); } else { _HeapEntries[lastHeapIndex] = null; _PendingDeleteCount--; } } if (deletedEntriesRemoved) { LimitHeapSparsity(); } }
private HeapEntry ModifyTopList(TValue newValue) { HeapEntry returnValue = elements[count - 1]; int insertIndex = BinarySearch(newValue); if (insertIndex >= count) { insertIndex = count - 1; } for (int i = count - 1; i > insertIndex; i--) { elements[i] = elements[i - 1]; keysPosition[elements[i].Key] = i; } elements[insertIndex].Key = returnValue.Key; elements[insertIndex].Value = newValue; keysPosition[returnValue.Key] = insertIndex; return(returnValue); }
private void trickleDown(long index, HeapEntry he) { long child = (index * 2) + 1; while (child < count) { if ( ((child + 1) < count) && ((this.descending && heap[child].Priority < heap[child + 1].Priority) || (!this.descending && heap[child].Priority > heap[child + 1].Priority)) ) { child++; } heap[index] = heap[child]; index = child; child = (index * 2) + 1; } bubbleUp(index, he); }
private int TrickleUp(int heapIndex) { while (heapIndex > 0) { int parentHeapIndex = (heapIndex - 1) >> 1; if (HeapEntry.Compare(_HeapEntries[heapIndex], _HeapEntries[parentHeapIndex]) < 0) { _IsHeapOrdered = false; SwapHeapPositions(heapIndex, parentHeapIndex); heapIndex = parentHeapIndex; } else { break; } } return(heapIndex); }
void AddList(TValue value, TKey userKey) { int insertIndex = BinarySearch(value); for (int k = count; k > insertIndex; k--) { elements[k] = elements[k - 1]; keysPosition[elements[k].key] = k; } int key = GetNextFreeKey(); elements[insertIndex] = new HeapEntry(key, userKey, value); keysPosition[key] = insertIndex; count++; keys.Add(userKey, key); if (count >= ListTreshold) { RebuidToHeap(); isHeap = true; } }
private int AddList(TValue value) { int insertIndex = BinarySearch(value); for (int k = Count; k > insertIndex; k--) { elements[k] = elements[k - 1]; keysPosition[elements[k].Key] = k; } int key = GetNextFreeKey(); elements[insertIndex] = new HeapEntry(key, value); keysPosition[key] = insertIndex; count++; if (count == ListTreshold) { RebuidToHeap(); isHeap = true; } return(key); }
private void MergePartialIndexes() { for (int index = 0; index < _indexes.Count; index++) { var partialReaders = _options.GetAllPartialsFor(index) .Select(x => new SourceReader(x, _options.Encoding, new[] { 0, 1 }).ReadFromStream().GetEnumerator()) .ToList(); var heap = new Heap<HeapEntry>(partialReaders.Count, (x, y) => Utils.CompareIndexEntries(x.IndexEntry, y.IndexEntry)); for (int i = 0; i < partialReaders.Count; i++) { var reader = partialReaders[i]; if (reader.MoveNext() == false) // empty reader? continue; var heapEntry = new HeapEntry { Index = index, Reader = reader, IndexEntry = ReadIndexEntry(reader) }; heap.Enqueue(heapEntry); } using (var stream = _options.Create(index)) using (var builder = new IndexBuilder(stream, _options.Encoding)) { while (heap.Count > 0) { var heapEntry = heap.Dequeue(); builder.Add(heapEntry.IndexEntry); if (heapEntry.Reader.MoveNext() == false) continue; heapEntry.IndexEntry = ReadIndexEntry(heapEntry.Reader); heap.Enqueue(heapEntry); } } _options.DeleteAllPartialsFor(index); } }