11using System ;
2- using System . Collections ;
32using System . Collections . Generic ;
4- using System . Linq ;
53
64namespace PriorityQueues
75{
8- public class MappedBinaryHeapPriorityQueue < T > : IPriorityQueue < T > where T : IPriorityElement
6+ public class MappedBinaryHeapPriorityQueue < T > : BinaryHeapPriorityQueue < T >
97 {
108 #region Private fields
11- private List < T > _heap ;
12-
139 private Dictionary < T , List < int > > _map ;
1410 #endregion
1511
16- #region Public properties
17- /// <inheritdoc/>
18- public int Count => _heap . Count ;
19- #endregion
20-
2112 #region Constructors
22- /// <summary>
23- /// Initializes a new instance of <see cref="MappedBinaryHeapPriorityQueue{T}"/> class that is empty and has the default initial capacity
24- /// </summary>
25- public MappedBinaryHeapPriorityQueue ( )
13+ /// <inheritdoc/>
14+ public MappedBinaryHeapPriorityQueue ( Comparison < T > comparer ) : base ( comparer )
2615 {
27- _heap = new List < T > ( ) ;
28-
2916 _map = new Dictionary < T , List < int > > ( ) ;
3017 }
3118
32- /// <summary>
33- /// Initializes a new instance of <see cref="MappedBinaryHeapPriorityQueue{T}"/> class that is empty and has the specified initial capacity
34- /// </summary>
35- /// <param name="capacity">The number of elements the <see cref="MappedBinaryHeapPriorityQueue{T}"/> can initially store</param>
36- public MappedBinaryHeapPriorityQueue ( int capacity )
19+ /// <inheritdoc/>
20+ public MappedBinaryHeapPriorityQueue ( int capacity , Comparison < T > comparer ) : base ( capacity , comparer )
3721 {
38- _heap = new List < T > ( capacity ) ;
39-
4022 _map = new Dictionary < T , List < int > > ( capacity ) ;
4123 }
4224
43- /// <summary>
44- /// Initializes a new instance of <see cref="MappedBinaryHeapPriorityQueue{T}"/> class that contains elements copied from the specified collection, sorted by their priority value
45- /// </summary>
46- /// <param name="collection">The collection whose elements are copied to the <see cref="MappedBinaryHeapPriorityQueue{T}"/></param>
47- public MappedBinaryHeapPriorityQueue ( IEnumerable < T > collection )
25+ /// <inheritdoc/>
26+ public MappedBinaryHeapPriorityQueue ( IEnumerable < T > collection , Comparison < T > comparer ) : base ( comparer )
4827 {
49- _heap = new List < T > ( collection . Count ( ) ) ;
50-
5128 _map = new Dictionary < T , List < int > > ( ) ;
5229
5330 int i = 0 ;
@@ -68,15 +45,15 @@ public MappedBinaryHeapPriorityQueue(IEnumerable<T> collection)
6845
6946 #region Public methods
7047 /// <inheritdoc/>
71- public void Clear ( )
48+ public override void Clear ( )
7249 {
73- _heap . Clear ( ) ;
50+ base . Clear ( ) ;
7451
7552 _map . Clear ( ) ;
7653 }
7754
7855 /// <inheritdoc/>
79- public bool Contains ( T element )
56+ public override bool Contains ( T element )
8057 {
8158 if ( element == null )
8259 {
@@ -88,40 +65,15 @@ public bool Contains(T element)
8865 }
8966
9067 /// <inheritdoc/>
91- public T Dequeue ( )
92- {
93- return IsEmpty ( ) ? throw new InvalidOperationException ( "Queue is empty" ) : RemoveAt ( 0 ) ;
94- }
95-
96- /// <inheritdoc/>
97- public void Enqueue ( T element )
68+ public override void Enqueue ( T element )
9869 {
99- if ( element == null )
100- {
101- throw new ArgumentNullException ( "element" ) ;
102- }
103-
104- _heap . Add ( element ) ;
70+ base . Enqueue ( element ) ;
10571
10672 MapAdd ( element , _heap . Count - 1 ) ;
107-
108- Swim ( _heap . Count - 1 ) ;
109- }
110-
111- /// <inheritdoc/>
112- public bool IsEmpty ( )
113- {
114- return Count == 0 ;
11573 }
11674
11775 /// <inheritdoc/>
118- public T Peek ( )
119- {
120- return IsEmpty ( ) ? throw new InvalidOperationException ( "Queue is empty" ) : _heap [ 0 ] ;
121- }
122-
123- /// <inheritdoc/>
124- public bool Remove ( T element )
76+ public override bool Remove ( T element )
12577 {
12678 if ( element == null )
12779 {
@@ -138,68 +90,7 @@ public bool Remove(T element)
13890 #endregion
13991
14092 #region Private methods
141- private bool IsHeapInvariantMaintained ( int index )
142- {
143- if ( index >= Count )
144- {
145- return true ;
146- }
147-
148- int leftIndex = 2 * index + 1 ;
149- int rightIndex = 2 * index + 2 ;
150-
151- if ( leftIndex < Count && ! Less ( index , leftIndex ) )
152- {
153- return false ;
154- }
155- if ( rightIndex < Count && ! Less ( index , rightIndex ) )
156- {
157- return false ;
158- }
159-
160- return IsHeapInvariantMaintained ( leftIndex ) && IsHeapInvariantMaintained ( rightIndex ) ;
161- }
162- private bool Less ( int i , int j )
163- {
164- return _heap [ i ] . Priority <= _heap [ j ] . Priority ;
165- }
166-
167- private void Swim ( int index )
168- {
169- int parentIndex = ( index - 1 ) / 2 ;
170-
171- while ( index > 0 && Less ( index , parentIndex ) )
172- {
173- Swap ( parentIndex , index ) ;
174- index = parentIndex ;
175- parentIndex = ( index - 1 ) / 2 ;
176- }
177- }
178-
179- private void Sink ( int index )
180- {
181- while ( true )
182- {
183- int leftChildIndex = 2 * index + 1 ;
184- int rightChildIndex = 2 * index + 2 ;
185- int smallerNodeIndex = leftChildIndex ;
186- if ( rightChildIndex < Count && Less ( rightChildIndex , leftChildIndex ) )
187- {
188- smallerNodeIndex = rightChildIndex ;
189- }
190-
191- // stop if we're outside bounds or we cannot sink anymore
192- if ( leftChildIndex >= Count || Less ( index , smallerNodeIndex ) )
193- {
194- break ;
195- }
196-
197- Swap ( smallerNodeIndex , index ) ;
198- index = smallerNodeIndex ;
199- }
200- }
201-
202- private void Swap ( int i , int j )
93+ protected override void Swap ( int i , int j )
20394 {
20495 T i_elem = _heap [ i ] ;
20596 T j_elem = _heap [ j ] ;
@@ -210,7 +101,7 @@ private void Swap(int i, int j)
210101 MapSwap ( i_elem , j_elem , i , j ) ;
211102 }
212103
213- private T RemoveAt ( int index )
104+ protected override T RemoveAt ( int index )
214105 {
215106 if ( index < 0 || index > Count - 1 )
216107 {
@@ -281,17 +172,5 @@ private void MapSwap(T value1, T value2, int index1, int index2)
281172 _map [ value2 ] . Add ( index1 ) ;
282173 }
283174 #endregion
284-
285- #region IEnumerable interface implementation
286- public IEnumerator < T > GetEnumerator ( )
287- {
288- return ( ( IEnumerable < T > ) _heap ) . GetEnumerator ( ) ;
289- }
290-
291- IEnumerator IEnumerable . GetEnumerator ( )
292- {
293- return ( ( IEnumerable ) _heap ) . GetEnumerator ( ) ;
294- }
295- #endregion
296175 }
297176}
0 commit comments