Делал индексную очередь с приоритетами, понадобилось реализовывать интерфейс IEnumerable. Получилось так
1 2 3 4 |
class IndexMaxPQ<Key> : IEnumerable<Key> { } |
Понадобилось реализовать 2 следующих метода
1 2 3 4 5 6 7 8 9 |
public IEnumerator<Key> GetEnumerator() { return new HeapIterator<Key>(comparer, Size(), n, keys); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } |
В контексте логики класса был написан другой класс HeapIterator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
private class HeapIterator<Key> : IEnumerator<Key> { public Key[] Keys { get; set; } // // store items at indices 1 to n, 0 not use.. public int Size { get; set; } public int N { get; set; } public Comparer<Key> Comparer { get; set; } // private int position = -1; private MaxPQ<Key> copy; public HeapIterator() { if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i < N; N++) copy.Insert(Keys[i]); } public HeapIterator(Comparer<Key> Comparer, int Size, int N, Key[] keys) { this.Comparer = Comparer; this.Size = Size; this.N = N; this.Keys = keys; if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i <= N; i++) copy.Insert(this.Keys[i]); } public bool HasNext() { return !copy.IsEmpty(); } public void Remove() { throw new ArgumentException("error"); } public Key Next() { if (!HasNext()) throw new ArgumentException("error"); return copy.DelMax(); } public void Dispose() { //throw new NotImplementedException(); } public bool MoveNext() { position++; //return !copy.IsEmpty(); return position < copy.Size(); } public void Reset() { position = -1; } // object IEnumerator.Current { get { return Current; } } public Key Current { get { try { // return PQ[position]; return Keys[position]; // << Возвращаем ключики... } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } |
Все вместе это выглядит так
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PriorityQueues { class IndexMaxPQ<Key> : IEnumerable<Key> //Maximum-oriented indexed PQ implementation using a binary heap. { private int n; // number of elements on PQ private int[] pq; // binary heap using 1-based indexing private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i private Key[] keys; // keys[i] = priority of i private Comparer<Key> comparer; // Initializes an empty indexed priority queue with indices between 0 and maxN-1 public IndexMaxPQ(int maxN, Comparer<Key> comparer) { if (maxN < 0) throw new ArgumentException(); n = 0; keys = new Key[maxN + 1]; // make this of length maxN?? pq = new int[maxN + 1]; qp = new int[maxN + 1]; // make this of length maxN?? for (int i = 0; i <= maxN; i++) qp[i] = -1; this.comparer = comparer; } // IsEmpty() public bool IsEmpty() { return n == 0; } // Contains public bool Contains(int i) { return qp[i] != -1; } // Size public int Size() { return n; } // public int MaxIndex() { if (n == 0) throw new ArgumentException("Priority queue underflow"); return pq[1]; } public Key MaxKey() { if (n == 0) throw new ArgumentException("Priority queue underflow"); return keys[pq[1]]; } // Removes a maximum key and returns its associated index. public void DelMax(out int index, out Key key) { if (n == 0) throw new ArgumentException("Priority queue underflow"); int min = pq[1]; key = keys[min]; index = min; Exch(1, n--); Sink(1); Debug.Assert(pq[n + 1] == min); qp[min] = -1; // delete keys[min] = default(Key); // to help with garbage collection pq[n + 1] = -1; // not needed //return min; } // Associate key with index i public void Insert(int i, Key key) { if (Contains(i)) throw new ArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = key; Swim(n); } // public Key KeyOf(int i) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); else return keys[i]; } // public void ChangeKey(int i, Key key) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); keys[i] = key; Swim(qp[i]); Sink(qp[i]); } // Increase the key associated with index {@code i} to the specified value. public void IncreaseKey(int i, Key key) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); //if (keys[i].compareTo(key) >= 0) if (comparer.Compare(keys[i], key) < 0) throw new ArgumentException("Calling increaseKey() with given argument would not strictly increase the key"); keys[i] = key; Swim(qp[i]); } // Decrease the key associated with index {@code i} to the specified value. public void DecreaseKey(int i, Key key) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); if (comparer.Compare(keys[i], key) > 0) throw new ArgumentException("Calling decreaseKey() with given argument would not strictly decrease the key"); keys[i] = key; Sink(qp[i]); } // Remove the key on the priority queue associated with index public void Delete(int i) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); int index = qp[i]; Exch(index, n--); Swim(index); Sink(index); keys[i] = default(Key); qp[i] = -1; } /*************************************************************************** * General helper functions. ***************************************************************************/ private bool Less(int i, int j) { // return keys[pq[i]].CompareTo(keys[pq[j]]) < 0; return comparer.Compare(keys[pq[i]], keys[pq[j]]) < 0; } private void Exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } /*************************************************************************** * Heap helper functions. ***************************************************************************/ private void Swim(int k) { while (k > 1 && Less(k / 2, k)) { Exch(k, k / 2); k = k / 2; } } private void Sink(int k) { while (2 * k <= n) { int j = 2 * k; if (j < n && Less(j, j + 1)) j++; if (!Less(k, j)) break; Exch(k, j); k = j; } } /* public int CompareTo(Key other) { throw new NotImplementedException(); } */ public IEnumerator<Key> GetEnumerator() { return new HeapIterator<Key>(comparer, Size(), n, keys); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } private class HeapIterator<Key> : IEnumerator<Key> { public Key[] Keys { get; set; } // // store items at indices 1 to n, 0 not use.. public int Size { get; set; } public int N { get; set; } public Comparer<Key> Comparer { get; set; } // private int position = -1; private MaxPQ<Key> copy; public HeapIterator() { if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i < N; N++) copy.Insert(Keys[i]); } public HeapIterator(Comparer<Key> Comparer, int Size, int N, Key[] keys) { this.Comparer = Comparer; this.Size = Size; this.N = N; this.Keys = keys; if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i <= N; i++) copy.Insert(this.Keys[i]); } public bool HasNext() { return !copy.IsEmpty(); } public void Remove() { throw new ArgumentException("error"); } public Key Next() { if (!HasNext()) throw new ArgumentException("error"); return copy.DelMax(); } public void Dispose() { //throw new NotImplementedException(); } public bool MoveNext() { position++; //return !copy.IsEmpty(); return position < copy.Size(); } public void Reset() { position = -1; } // object IEnumerator.Current { get { return Current; } } public Key Current { get { try { // return PQ[position]; return Keys[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } } } |