-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cs
More file actions
116 lines (102 loc) · 3.65 KB
/
PriorityQueue.cs
File metadata and controls
116 lines (102 loc) · 3.65 KB
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
// Stephen Toub
// stoub@microsoft.com
//
// PriorityQueue.cs
// A C# implementation of a max priority queue.
//
// HISTORY:
// v1.0.0 - Original version
//
// October 4th, 2002
// v1.0.0
#region Namespaces
using System;
using System.Collections;
#endregion
namespace Toub.Collections
{
/// <summary>A priority queue.</summary>
public class PriorityQueue : ICollection
{
#region Member Variables
/// <summary>The binary heap on which the priority queue is based.</summary>
private BinaryHeap _heap;
#endregion
#region Construction
/// <summary>Initialize the queue.</summary>
public PriorityQueue() { _heap = new BinaryHeap(); }
/// <summary>Initialize the queue.</summary>
/// <param name="queue">The queue is intialized with a shalled-copy of this queue.</param>
public PriorityQueue(PriorityQueue queue)
{
_heap = queue._heap.Clone();
}
#endregion
#region Methods
/// <summary>Enqueues an item to the priority queue.</summary>
/// <param name="priority">The priority of the object to be enqueued.</param>
/// <param name="value">The object to be enqueued.</param>
public virtual void Enqueue(int priority, object value)
{
_heap.Insert(priority, value);
}
/// <summary>Dequeues an object from the priority queue.</summary>
/// <returns>The top item (max priority) from the queue.</returns>
public virtual object Dequeue()
{
return _heap.Remove();
}
/// <summary>Empties the queue.</summary>
public virtual void Clear()
{
_heap.Clear();
}
#endregion
#region Implementation of ICollection
/// <summary>Copies the priority queue to an array.</summary>
/// <param name="array">The array to which the queue should be copied.</param>
/// <param name="index">The starting index.</param>
public virtual void CopyTo(System.Array array, int index) { _heap.CopyTo(array, index); }
/// <summary>Determines whether the priority queue is synchronized.</summary>
public virtual bool IsSynchronized { get { return _heap.IsSynchronized; } }
/// <summary>Gets the number of items in the queue.</summary>
public virtual int Count { get { return _heap.Count; } }
/// <summary>Gets the synchronization root object for the queue.</summary>
public object SyncRoot { get { return _heap.SyncRoot; } }
#endregion
#region Implementation of IEnumerable
/// <summary>Gets the enumerator for the queue.</summary>
/// <returns>An enumerator for the queue.</returns>
public IEnumerator GetEnumerator() { return _heap.GetEnumerator(); }
#endregion
#region Synchronization
/// <summary>Returns a synchronized wrapper around the queue.</summary>
/// <param name="queue">The queue to be synchronized.</param>
/// <returns>A synchronized priority queue.</returns>
public static PriorityQueue Synchronize(PriorityQueue queue)
{
// Return the queue if it is already synchronized. Otherwise, wrap it
// with a synchronized wrapper.
if (queue is SyncPriorityQueue) return queue;
return new SyncPriorityQueue(queue);
}
#endregion
/// <summary>A synchronized PriorityQueue.</summary>
public class SyncPriorityQueue : PriorityQueue
{
#region Construction
/// <summary>Initialize the priority queue.</summary>
/// <param name="queue">The queue to be synchronized.</param>
internal SyncPriorityQueue(PriorityQueue queue)
{
// NOTE: We're synchronizing just be using a synchronized heap!
// This implementation will need to change if we get more state.
if (!(_heap is BinaryHeap.SyncBinaryHeap))
{
_heap = BinaryHeap.Synchronize(_heap);
}
}
#endregion
}
}
}