Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IntervalHeap.cs « heaps « C5.old « Mono.C5 « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ad55893721014bf39f22ef4d338a110d05d126a (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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#if NET_2_0
/*
 Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

using System;
using System.Diagnostics;
using MSG = System.Collections.Generic;

namespace C5
{
	/// <summary>
	/// A priority queue class based on an interval heap data structure.
	/// </summary>
	public class IntervalHeap<T>: CollectionValueBase<T>, IPriorityQueue<T>
	{
		#region Fields
		struct Interval
		{
			internal T first, last;


			public override string ToString() { return String.Format("[{0}; {1}]", first, last); }
		}



		object syncroot = new object();

        int stamp;

        IComparer<T> comparer;

		Interval[] heap;

		int size;
		#endregion

		#region Util
		void heapifyMin(int i)
		{
			int j = i, minpt = j;
			T pv = heap[j].first, min = pv;

			while (true)
			{
				int l = 2 * j + 1, r = l + 1;
				T lv, rv, other;

				if (2 * l < size && comparer.Compare(lv = heap[l].first, min) < 0) { minpt = l; min = lv; }

                if (2 * r < size && comparer.Compare(rv = heap[r].first, min) < 0) { minpt = r; min = rv; }

                if (minpt == j)
					break;

				other = heap[minpt].last;
				heap[j].first = min;
                if (2 * minpt + 1 < size && comparer.Compare(pv, other) > 0)
                { heap[minpt].last = pv; pv = other; }

				min = pv;
				j = minpt;
			}

			if (minpt != i)
				heap[minpt].first = min;
		}


		void heapifyMax(int i)
		{
			int j = i, maxpt = j;
			T pv = heap[j].last, max = pv;

			while (true)
			{
				int l = 2 * j + 1, r = l + 1;
				T lv, rv, other;

                if (2 * l + 1 < size && comparer.Compare(lv = heap[l].last, max) > 0) { maxpt = l; max = lv; }

                if (2 * r + 1 < size && comparer.Compare(rv = heap[r].last, max) > 0) { maxpt = r; max = rv; }

                if (maxpt == j)
					break;

				other = heap[maxpt].first;
				heap[j].last = max;
                if (comparer.Compare(pv, other) < 0)
                {
					heap[maxpt].first = pv;
					pv = other;
				}

				max = pv;
				j = maxpt;
			}

			if (maxpt != i)
				heap[maxpt].last = max;
		}


		void bubbleUpMin(int i)
		{
			if (i > 0)
			{
				T min = heap[i].first, iv = min;
				int p = (i + 1) / 2 - 1;

				while (i > 0)
				{
                    if (comparer.Compare(iv, min = heap[p = (i + 1) / 2 - 1].first) < 0)
                    {
						heap[i].first = min; min = iv;
						i = p;
					}
					else
						break;
				}

				heap[i].first = iv;
			}
		}


		void bubbleUpMax(int i)
		{
			if (i > 0)
			{
				T max = heap[i].last, iv = max;
				int p = (i + 1) / 2 - 1;

				while (i > 0)
				{
                    if (comparer.Compare(iv, max = heap[p = (i + 1) / 2 - 1].last) > 0)
                    {
						heap[i].last = max; max = iv;
						i = p;
					}
					else
						break;
				}

				heap[i].last = iv;
			}
		}

		#endregion

		#region Constructors
		/// <summary>
		/// Create an interval heap with natural item comparer and default initial capacity (16)
		/// </summary>
		public IntervalHeap() : this(16) { }


		/// <summary>
		/// Create an interval heap with external item comparer and default initial capacity (16)
		/// </summary>
		/// <param name="c">The external comparer</param>
		public IntervalHeap(IComparer<T> c) : this(c,16) { }


		/// <summary>
		/// Create an interval heap with natural item comparer and prescribed initial capacity
		/// </summary>
		/// <param name="capacity">The initial capacity</param>
		public IntervalHeap(int capacity)  : this(ComparerBuilder.FromComparable<T>.Examine(),capacity) { }


		/// <summary>
		/// Create an interval heap with external item comparer and prescribed initial capacity
		/// </summary>
		/// <param name="c">The external comparer</param>
		/// <param name="capacity">The initial capacity</param>
		public IntervalHeap(IComparer<T> c, int capacity)
		{
			comparer = c;

			int length = 1;

			while (length < capacity) length <<= 1;

			heap = new Interval[length];
		}
		#endregion

		#region IPriorityQueue<T> Members

		/// <summary>
		/// Find the current least item of this priority queue.
		/// <exception cref="InvalidOperationException"/> if queue is empty
		/// </summary>
		/// <returns>The least item.</returns>
		[Tested]
		public T FindMin()
		{
			if (size == 0)
				throw new InvalidOperationException("Heap is empty");

			return heap[0].first;
		}


		/// <summary>
		/// Remove the least item from this  priority queue.
		/// <exception cref="InvalidOperationException"/> if queue is empty
		/// </summary>
		/// <returns>The removed item.</returns>
		[Tested]
		public T DeleteMin()
		{
            stamp++;
            if (size == 0)
                throw new InvalidOperationException("Heap is empty");

			T retval = heap[0].first;;
			if (size == 1)
			{
				size = 0;
				heap[0].first = default(T);
			}
			else
			{
				int ind = (size - 1) / 2;

				if (size % 2 == 0)
				{
					heap[0].first = heap[ind].last;
					heap[ind].last = default(T);
				}
				else
				{
					heap[0].first = heap[ind].first;
					heap[ind].first = default(T);
				}

				size--;
				heapifyMin(0);
			}

			return retval;
		}


		/// <summary>
		/// Find the current largest item of this priority queue.
		/// <exception cref="InvalidOperationException"/> if queue is empty
		/// </summary>
		/// <returns>The largest item.</returns>
		[Tested]
		public T FindMax()
		{
			if (size == 0)
				throw new InvalidOperationException("Heap is empty");
			else if (size == 1)
				return heap[0].first;
			else
				return heap[0].last;
		}


		/// <summary>
		/// Remove the largest item from this  priority queue.
		/// <exception cref="InvalidOperationException"/> if queue is empty
		/// </summary>
		/// <returns>The removed item.</returns>
		[Tested]
		public T DeleteMax()
		{
            stamp++;
            if (size == 0)
                throw new InvalidOperationException("Heap is empty");

			T retval;

			if (size == 1)
			{
				size = 0;
				retval = heap[0].first;
				heap[0].first = default(T);
				return retval;
			}
			else
			{
				retval = heap[0].last;

				int ind = (size - 1) / 2;

				if (size % 2 == 0)
				{
					heap[0].last = heap[ind].last;
					heap[ind].last = default(T);
				}
				else
				{
					heap[0].last = heap[ind].first;
					heap[ind].first = default(T);
				}

				size--;
				heapifyMax(0);
				return retval;
			}
		}


        /// <summary>
        /// The comparer object supplied at creation time for this collection
        /// </summary>
        /// <value>The comparer</value>
        public IComparer<T> Comparer { get { return comparer; } }

		#endregion

		#region ISink<T> Members

		/// <summary>
		/// 
		/// </summary>
		/// <value>True since this collection has bag semantics</value>
		[Tested]
		public bool AllowsDuplicates { [Tested]get { return true; } }


		/// <summary>
		/// 
		/// </summary>
		/// <value>The distinguished object to use for locking to synchronize multithreaded access</value>
		[Tested]
		public object SyncRoot { [Tested]get { return syncroot; } }


		/// <summary>
		/// 
		/// </summary>
		/// <value>True if this collection is empty.</value>
		[Tested]
		public bool IsEmpty { [Tested]get { return size == 0; } }


		/// <summary>
		/// Add an item to this priority queue.
		/// </summary>
		/// <param name="item">The item to add.</param>
		/// <returns>True</returns>
		[Tested]
		public bool Add(T item)
		{
            stamp++;
            if (size == 0)
			{
				size = 1;
				heap[0].first = item;
				return true;
			}

			if (size == 2 * heap.Length)
			{
				Interval[] newheap = new Interval[2 * heap.Length];

				Array.Copy(heap, newheap, heap.Length);
				heap = newheap;
			}

			if (size % 2 == 0)
			{
				int i = size / 2, p = (i + 1) / 2 - 1;
				T tmp;

				size++;
                if (comparer.Compare(item, tmp = heap[p].last) > 0)
                {
					heap[i].first = tmp;
					heap[p].last = item;
					bubbleUpMax(p);
				}
				else
				{
					heap[i].first = item;
                    if (comparer.Compare(item, heap[p].first) < 0)
                        bubbleUpMin(i);
				}
			}
			else
			{
				int i = size / 2;
				T other = heap[i].first;

				size++;
                if (comparer.Compare(item, other) < 0)
                {
					heap[i].first = item;
					heap[i].last = other;
					bubbleUpMin(i);
				}
				else
				{
					heap[i].last = item;
					bubbleUpMax(i);
				}
			}

			return true;
		}


		/// <summary>
		/// Add the elements from another collection to this collection. 
		/// </summary>
		/// <param name="items">The items to add.</param>
		[Tested]
		public void AddAll(MSG.IEnumerable<T> items)
		{
            //TODO: avoid incrementing stamp repeatedly
			foreach (T item in items)
				Add(item);
		}

        /// <summary>
        /// Add the elements from another collection with a more specialized item type 
        /// to this collection. 
        /// </summary>
        /// <typeparam name="U">The type of items to add</typeparam>
        /// <param name="items">The items to add</param>
        public void AddAll<U>(MSG.IEnumerable<U> items) where U : T
        {
            //TODO: avoid incrementing stamp repeatedly
            foreach (T item in items)
                Add(item);
        }

		#endregion

        #region ICollection<T> members
        /// <summary>
        /// 
        /// </summary>
        /// <value>The size of this collection</value>
        [Tested]
        public override int Count { [Tested]get { return size; } }


        /// <summary>
        /// The value is symbolic indicating the type of asymptotic complexity
        /// in terms of the size of this collection (worst-case or amortized as
        /// relevant).
        /// </summary>
        /// <value>A characterization of the speed of the 
        /// <code>Count</code> property in this collection.</value>
        public override Speed CountSpeed { get { return Speed.Constant; } }

        /// <summary>
        /// Create an enumerator for the collection
        /// <para>Note: the enumerator does *not* enumerate the items in sorted order, 
        /// but in the internal table order.</para>
        /// </summary>
        /// <returns>The enumerator(SIC)</returns>
        [Tested]
        public override MSG.IEnumerator<T> GetEnumerator()
        {
            int mystamp = stamp;
            for (int i = 0; i < size; i++)
            {
                if (mystamp != stamp) throw new InvalidOperationException();
                yield return i % 2 == 0 ? heap[i >> 1].first : heap[i >> 1].last;
            }
            yield break;
        }


        #endregion


		#region Diagnostics
		private bool check(int i, T min, T max)
		{
			bool retval = true;
			Interval interval = heap[i];
			T first = interval.first, last = interval.last;

			if (2 * i + 1 == size)
			{
                if (comparer.Compare(min, first) > 0)
                {
					Console.WriteLine("Cell {0}: parent.first({1}) > first({2})  [size={3}]", i, min, first, size);
					retval = false;
				}

                if (comparer.Compare(first, max) > 0)
                {
					Console.WriteLine("Cell {0}: first({1}) > parent.last({2})  [size={3}]", i, first, max, size);
					retval = false;
				}

				return retval;
			}
			else
			{
                if (comparer.Compare(min, first) > 0)
                {
					Console.WriteLine("Cell {0}: parent.first({1}) > first({2})  [size={3}]", i, min, first, size);
					retval = false;
				}

                if (comparer.Compare(first, last) > 0)
                {
					Console.WriteLine("Cell {0}: first({1}) > last({2})  [size={3}]", i, first, last, size);
					retval = false;
				}

                if (comparer.Compare(last, max) > 0)
                {
					Console.WriteLine("Cell {0}: last({1}) > parent.last({2})  [size={3}]", i, last, max, size);
					retval = false;
				}

				int l = 2 * i + 1, r = l + 1;

				if (2 * l < size)
					retval = retval && check(l, first, last);

				if (2 * r < size)
					retval = retval && check(r, first, last);
			}

			return retval;
		}


		/// <summary>
		/// Check the integrity of the internal data structures of this collection.
		/// Only avaliable in DEBUG builds???
		/// </summary>
		/// <returns>True if check does not fail.</returns>
		[Tested]
		public bool Check()
		{
			if (size == 0)
				return true;

			if (size == 1)
				return (object)(heap[0].first) != null;

			return check(0, heap[0].first, heap[0].last);
		}

		#endregion
	}
}
#endif