|
| 1 | +//---------------------------------------------------------------- |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +//---------------------------------------------------------------- |
| 4 | + |
| 5 | +namespace System.Activities.DurableInstancing |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Diagnostics.CodeAnalysis; |
| 10 | + using System.Runtime; |
| 11 | + |
| 12 | + sealed class BinaryHeap<TKey, TValue> where TKey : IComparable<TKey> |
| 13 | + { |
| 14 | + const int defaultCapacity = 128; |
| 15 | + readonly KeyValuePair<TKey, TValue> EmptyItem = new KeyValuePair<TKey, TValue>(); |
| 16 | + KeyValuePair<TKey, TValue>[] items; |
| 17 | + int itemCount; |
| 18 | + |
| 19 | + public BinaryHeap() : |
| 20 | + this(defaultCapacity) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + public BinaryHeap(int capacity) |
| 25 | + { |
| 26 | + Fx.Assert(capacity > 0, "Capacity must be a positive value."); |
| 27 | + this.items = new KeyValuePair<TKey, TValue>[capacity]; |
| 28 | + } |
| 29 | + |
| 30 | + public int Count |
| 31 | + { |
| 32 | + get { return this.itemCount; } |
| 33 | + } |
| 34 | + |
| 35 | + public bool IsEmpty |
| 36 | + { |
| 37 | + get { return this.itemCount == 0; } |
| 38 | + } |
| 39 | + |
| 40 | + public void Clear() |
| 41 | + { |
| 42 | + this.itemCount = 0; |
| 43 | + this.items = new KeyValuePair<TKey, TValue>[defaultCapacity]; |
| 44 | + } |
| 45 | + |
| 46 | + public bool Enqueue(TKey key, TValue item) |
| 47 | + { |
| 48 | + if (this.itemCount == this.items.Length) |
| 49 | + { |
| 50 | + ResizeItemStore(this.items.Length * 2); |
| 51 | + } |
| 52 | + |
| 53 | + this.items[this.itemCount++] = new KeyValuePair<TKey, TValue>(key, item); |
| 54 | + int position = this.BubbleUp(this.itemCount - 1); |
| 55 | + |
| 56 | + return (position == 0); |
| 57 | + } |
| 58 | + |
| 59 | + public KeyValuePair<TKey, TValue> Dequeue() |
| 60 | + { |
| 61 | + return Dequeue(true); |
| 62 | + } |
| 63 | + |
| 64 | + KeyValuePair<TKey, TValue> Dequeue(bool shrink) |
| 65 | + { |
| 66 | + Fx.Assert(this.itemCount > 0, "Cannot dequeue empty queue."); |
| 67 | + |
| 68 | + KeyValuePair<TKey, TValue> result = items[0]; |
| 69 | + |
| 70 | + if (this.itemCount == 1) |
| 71 | + { |
| 72 | + this.itemCount = 0; |
| 73 | + this.items[0] = this.EmptyItem; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + --this.itemCount; |
| 78 | + this.items[0] = this.items[itemCount]; |
| 79 | + this.items[itemCount] = this.EmptyItem; |
| 80 | + |
| 81 | + // Keep the structure of the heap valid. |
| 82 | + this.BubbleDown(0); |
| 83 | + } |
| 84 | + |
| 85 | + if (shrink) |
| 86 | + { |
| 87 | + ShrinkStore(); |
| 88 | + } |
| 89 | + |
| 90 | + return result; |
| 91 | + } |
| 92 | + |
| 93 | + public KeyValuePair<TKey, TValue> Peek() |
| 94 | + { |
| 95 | + Fx.Assert(this.itemCount > 0, "Cannot peek at empty queue."); |
| 96 | + return this.items[0]; |
| 97 | + } |
| 98 | + |
| 99 | + [SuppressMessage(FxCop.Category.Design, "CA1006:DoNotNestGenericTypesInMemberSignatures", |
| 100 | + Justification = "This is an internal only API.")] |
| 101 | + [SuppressMessage(FxCop.Category.MSInternal, "CA908:UseApprovedGenericsForPrecompiledAssemblies")] |
| 102 | + public ICollection<KeyValuePair<TKey, TValue>> RemoveAll(Predicate<KeyValuePair<TKey, TValue>> func) |
| 103 | + { |
| 104 | + ICollection<KeyValuePair<TKey, TValue>> result = new List<KeyValuePair<TKey, TValue>>(); |
| 105 | + |
| 106 | + for (int position = 0; position < this.itemCount; position++) |
| 107 | + { |
| 108 | + while (func(this.items[position]) && position < this.itemCount) |
| 109 | + { |
| 110 | + result.Add(this.items[position]); |
| 111 | + |
| 112 | + int lastItem = this.itemCount - 1; |
| 113 | + |
| 114 | + while (func(this.items[lastItem]) && position < lastItem) |
| 115 | + { |
| 116 | + result.Add(this.items[lastItem]); |
| 117 | + this.items[lastItem] = EmptyItem; |
| 118 | + --lastItem; |
| 119 | + } |
| 120 | + |
| 121 | + this.items[position] = this.items[lastItem]; |
| 122 | + this.items[lastItem] = EmptyItem; |
| 123 | + this.itemCount = lastItem; |
| 124 | + |
| 125 | + if (position < lastItem) |
| 126 | + { |
| 127 | + this.BubbleDown(this.BubbleUp(position)); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + this.ShrinkStore(); |
| 133 | + |
| 134 | + return result; |
| 135 | + } |
| 136 | + |
| 137 | + void ShrinkStore() |
| 138 | + { |
| 139 | + // If we are under half capacity and above default capacity size down. |
| 140 | + if (this.items.Length > defaultCapacity && this.itemCount < (this.items.Length >> 1)) |
| 141 | + { |
| 142 | + int newSize = Math.Max( |
| 143 | + defaultCapacity, (((this.itemCount / defaultCapacity) + 1) * defaultCapacity)); |
| 144 | + |
| 145 | + this.ResizeItemStore(newSize); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + [SuppressMessage("Microsoft.MSInternal", "CA908:UseApprovedGenericsForPrecompiledAssemblies")] |
| 150 | + [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", |
| 151 | + Justification = "This is an internal only API.")] |
| 152 | + public ICollection<KeyValuePair<TKey, TValue>> TakeWhile(Predicate<TKey> func) |
| 153 | + { |
| 154 | + ICollection<KeyValuePair<TKey, TValue>> result = new List<KeyValuePair<TKey, TValue>>(); |
| 155 | + |
| 156 | + while (!this.IsEmpty && func(this.Peek().Key)) |
| 157 | + { |
| 158 | + result.Add(this.Dequeue(false)); |
| 159 | + } |
| 160 | + |
| 161 | + ShrinkStore(); |
| 162 | + |
| 163 | + return result; |
| 164 | + } |
| 165 | + |
| 166 | + void ResizeItemStore(int newSize) |
| 167 | + { |
| 168 | + Fx.Assert(itemCount < newSize, "Shrinking now will lose data."); |
| 169 | + Fx.Assert(defaultCapacity <= newSize, "Can not shrink below the default capacity."); |
| 170 | + |
| 171 | + KeyValuePair<TKey, TValue>[] temp = new KeyValuePair<TKey, TValue>[newSize]; |
| 172 | + |
| 173 | + Array.Copy(this.items, 0, temp, 0, this.itemCount); |
| 174 | + |
| 175 | + this.items = temp; |
| 176 | + } |
| 177 | + |
| 178 | + void BubbleDown(int startIndex) |
| 179 | + { |
| 180 | + int currentPosition = startIndex; |
| 181 | + int swapPosition = startIndex; |
| 182 | + |
| 183 | + while (true) |
| 184 | + { |
| 185 | + int leftChildPosition = (currentPosition << 1) + 1; |
| 186 | + int rightChildPosition = leftChildPosition + 1; |
| 187 | + |
| 188 | + if (leftChildPosition < itemCount) |
| 189 | + { |
| 190 | + if (this.items[currentPosition].Key.CompareTo(this.items[leftChildPosition].Key) > 0) |
| 191 | + { |
| 192 | + swapPosition = leftChildPosition; |
| 193 | + } |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + break; |
| 198 | + } |
| 199 | + |
| 200 | + if (rightChildPosition < itemCount) |
| 201 | + { |
| 202 | + if (this.items[swapPosition].Key.CompareTo(this.items[rightChildPosition].Key) > 0) |
| 203 | + { |
| 204 | + swapPosition = rightChildPosition; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if (currentPosition != swapPosition) |
| 209 | + { |
| 210 | + KeyValuePair<TKey, TValue> temp = this.items[currentPosition]; |
| 211 | + this.items[currentPosition] = this.items[swapPosition]; |
| 212 | + this.items[swapPosition] = temp; |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + break; |
| 217 | + } |
| 218 | + |
| 219 | + currentPosition = swapPosition; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + int BubbleUp(int startIndex) |
| 224 | + { |
| 225 | + while (startIndex > 0) |
| 226 | + { |
| 227 | + int parent = (startIndex - 1) >> 1; |
| 228 | + |
| 229 | + if (this.items[parent].Key.CompareTo(this.items[startIndex].Key) > 0) |
| 230 | + { |
| 231 | + KeyValuePair<TKey, TValue> temp = this.items[startIndex]; |
| 232 | + this.items[startIndex] = this.items[parent]; |
| 233 | + this.items[parent] = temp; |
| 234 | + } |
| 235 | + else |
| 236 | + { |
| 237 | + break; |
| 238 | + } |
| 239 | + |
| 240 | + startIndex = parent; |
| 241 | + } |
| 242 | + |
| 243 | + return startIndex; |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments