aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/SmartThreadPool/PriorityQueue.cs
blob: 409c879a1f54bad5313200ff092ad55c1b0e3a7f (plain)
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace Amib.Threading.Internal
{
	#region PriorityQueue class

	/// <summary>
	/// PriorityQueue class
	/// This class is not thread safe because we use external lock
	/// </summary>
	public sealed class PriorityQueue : IEnumerable
	{
		#region Private members

		/// <summary>
		/// The number of queues, there is one for each type of priority
		/// </summary>
		private const int _queuesCount = WorkItemPriority.Highest-WorkItemPriority.Lowest+1;

		/// <summary>
		/// Work items queues. There is one for each type of priority
		/// </summary>
        private readonly LinkedList<IHasWorkItemPriority>[] _queues = new LinkedList<IHasWorkItemPriority>[_queuesCount];

		/// <summary>
		/// The total number of work items within the queues 
		/// </summary>
		private int _workItemsCount;

		/// <summary>
		/// Use with IEnumerable interface
		/// </summary>
		private int _version;

		#endregion

		#region Contructor

		public PriorityQueue()
		{
			for(int i = 0; i < _queues.Length; ++i)
			{
                _queues[i] = new LinkedList<IHasWorkItemPriority>();
			}
		}

		#endregion

		#region Methods

		/// <summary>
		/// Enqueue a work item.
		/// </summary>
		/// <param name="workItem">A work item</param>
		public void Enqueue(IHasWorkItemPriority workItem)
		{
			Debug.Assert(null != workItem);

			int queueIndex = _queuesCount-(int)workItem.WorkItemPriority-1;
			Debug.Assert(queueIndex >= 0);
			Debug.Assert(queueIndex < _queuesCount);

			_queues[queueIndex].AddLast(workItem);
			++_workItemsCount;
			++_version;
		}

		/// <summary>
		/// Dequeque a work item.
		/// </summary>
		/// <returns>Returns the next work item</returns>
		public IHasWorkItemPriority Dequeue()
		{
			IHasWorkItemPriority workItem = null;

			if(_workItemsCount > 0)
			{
				int queueIndex = GetNextNonEmptyQueue(-1);
				Debug.Assert(queueIndex >= 0);
                workItem = _queues[queueIndex].First.Value;
				_queues[queueIndex].RemoveFirst();
				Debug.Assert(null != workItem);
				--_workItemsCount;
				++_version;
			}

			return workItem;
		}

		/// <summary>
		/// Find the next non empty queue starting at queue queueIndex+1
		/// </summary>
		/// <param name="queueIndex">The index-1 to start from</param>
		/// <returns>
		/// The index of the next non empty queue or -1 if all the queues are empty
		/// </returns>
		private int GetNextNonEmptyQueue(int queueIndex)
		{
			for(int i = queueIndex+1; i < _queuesCount; ++i)
			{
				if(_queues[i].Count > 0)
				{
					return i;
				}
			}
			return -1;
		}

		/// <summary>
		/// The number of work items 
		/// </summary>
		public int Count
		{
			get
			{
				return _workItemsCount;
			}
		}

		/// <summary>
		/// Clear all the work items 
		/// </summary>
		public void Clear()
		{
			if (_workItemsCount > 0)
			{
				foreach(LinkedList<IHasWorkItemPriority> queue in _queues)
				{
					queue.Clear();
				}
				_workItemsCount = 0;
				++_version;
			}
		}

		#endregion

		#region IEnumerable Members

		/// <summary>
		/// Returns an enumerator to iterate over the work items
		/// </summary>
		/// <returns>Returns an enumerator</returns>
		public IEnumerator GetEnumerator()
		{
			return new PriorityQueueEnumerator(this);
		}

		#endregion

		#region PriorityQueueEnumerator

		/// <summary>
		/// The class the implements the enumerator
		/// </summary>
		private class PriorityQueueEnumerator : IEnumerator
		{
			private readonly PriorityQueue _priorityQueue;
			private int _version;
			private int _queueIndex;
			private IEnumerator _enumerator;

			public PriorityQueueEnumerator(PriorityQueue priorityQueue)
			{
				_priorityQueue = priorityQueue;
				_version = _priorityQueue._version;
				_queueIndex = _priorityQueue.GetNextNonEmptyQueue(-1);
				if (_queueIndex >= 0)
				{
					_enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator();
				}
				else
				{
					_enumerator = null;
				}
			}

			#region IEnumerator Members

			public void Reset()
			{
				_version = _priorityQueue._version;
				_queueIndex = _priorityQueue.GetNextNonEmptyQueue(-1);
				if (_queueIndex >= 0)
				{
					_enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator();
				}
				else
				{
					_enumerator = null;
				}
			}

			public object Current
			{
				get
				{
					Debug.Assert(null != _enumerator);
					return _enumerator.Current;
				}
			}

			public bool MoveNext()
			{
				if (null == _enumerator)
				{
					return false;
				}

				if(_version != _priorityQueue._version)
				{
					throw new InvalidOperationException("The collection has been modified");

				}
				if (!_enumerator.MoveNext())
				{
					_queueIndex = _priorityQueue.GetNextNonEmptyQueue(_queueIndex);
					if(-1 == _queueIndex)
					{
						return false;
					}
					_enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator();
					_enumerator.MoveNext();
					return true;
				}
				return true;
			}

			#endregion
		}

		#endregion
	}

	#endregion
}