summaryrefslogtreecommitdiff
path: root/gee/timsort.vala
blob: ad1e52e8691413d634c43fb8a71747f5d50e5fe8 (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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/* timsort.vala
 *
 * Copyright (C) 2009  Didier Villevalois
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Didier 'Ptitjes Villevalois <ptitjes@free.fr>
 */

/**
 * A stable, adaptive, iterative mergesort that requires far fewer than n*lg(n)
 * comparisons when running on partially sorted arrays, while offering
 * performance comparable to a traditional mergesort when run on random arrays.
 * Like all proper mergesorts, this sort is stable and runs O(n*log(n)) time
 * (worst case). In the worst case, this sort requires temporary storage space
 * for n/2 object references; in the best case, it requires only a small
 * constant amount of space.
 *
 * This implementation was adapted from Tim Peters's list sort for Python,
 * which is described in detail here:
 *   [[http://svn.python.org/projects/python/trunk/Objects/listsort.txt]]
 *
 * Tim's C code may be found here:
 *   [[http://svn.python.org/projects/python/trunk/Objects/listobject.c]]
 *
 * The underlying techniques are described in this paper (and may have even
 * earlier origins):
 *
 *   "Optimistic Sorting and Information Theoretic Complexity"
 *   Peter McIlroy
 *   SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms), pp
 *   467-474, Austin, Texas, 25-27 January 1993.
 */
internal class Gee.TimSort<G> : Object {

	public static void sort<G> (List<G> list, CompareDataFunc<G> compare) {
		if (list is ArrayList) {
			TimSort.sort_arraylist<G> ((ArrayList<G>) list, compare);
		} else {
			TimSort.sort_list<G> (list, compare);
		}
	}

	private static void sort_list<G> (List<G> list, CompareDataFunc<G> compare) {
		TimSort<G> helper = new TimSort<G> ();

		helper.list_collection = list;
		helper.array = list.to_array ();
		helper.list = helper.array;
		helper.index = 0;
		helper.size = list.size;
		helper.compare = compare;

		helper.do_sort ();

		// TODO Use a list iterator and use iter.set (item)
		list.clear ();
		foreach (G item in helper.array) {
			list.add (item);
		}
	}

	private static void sort_arraylist<G> (ArrayList<G> list, CompareDataFunc<G> compare) {
		TimSort<G> helper = new TimSort<G> ();

		helper.list_collection = list;
		helper.list = list._items;
		helper.index = 0;
		helper.size = list._size;
		helper.compare = compare;

		helper.do_sort ();
	}

	private static const int MINIMUM_GALLOP = 7;

	private List<G> list_collection;
	private G[] array;
	private void** list;
	private int index;
	private int size;
	private Slice<G>[] pending;
	private int minimum_gallop;
	private unowned CompareDataFunc<G> compare;

	private void do_sort () {
		if (size < 2) {
			return;
		}

		pending = new Slice<G>[0];
		minimum_gallop = MINIMUM_GALLOP;

		Slice<G> remaining = new Slice<G> (list, index, size);
		int minimum_length = compute_minimum_run_length (remaining.length);

		while (remaining.length > 0) {
			// Get the next run
			bool descending;
			Slice<G> run = compute_longest_run (remaining, out descending);
			#if DEBUG
				message ("New run (%d, %d) %s", run.index, run.length,
				         descending ? "descending" : "ascending");
			#endif
			if (descending) {
				run.reverse ();
			}

			// Extend it to minimum_length, if needed
			if (run.length < minimum_length) {
				int sorted_count = run.length;
				run.length = int.min (minimum_length, remaining.length);
				insertion_sort (run, sorted_count);
				#if DEBUG
					message ("Extended to (%d, %d) and sorted from index %d",
					         run.index, run.length, sorted_count);
				#endif
			}

			// Move remaining after run
			remaining.shorten_start (run.length);

			// Add run to pending runs and try to merge
			pending += (owned) run;
			merge_collapse ();
		}

		assert (remaining.index == size);

		merge_force_collapse ();

		assert (pending.length == 1);
		assert (pending[0].index == 0);
		assert (pending[0].length == size);
	}

	private delegate bool LowerFunc (G left, G right);

	private inline bool lower_than (G left, G right) {
            return compare (left, right) < 0;
	}

	private inline bool lower_than_or_equal_to (G left, G right) {
            return compare (left, right) <= 0;
	}

	private int compute_minimum_run_length (int length) {
		int run_length = 0;
		while (length >= 64) {
			run_length |= length & 1;
			length >>= 1;
		}
		return length + run_length;
	}

	private Slice<G> compute_longest_run (Slice<G> a, out bool descending) {
		int run_length;
		if (a.length <= 1) {
			run_length = a.length;
			descending = false;
		} else {
			run_length = 2;
			if (lower_than (a.list[a.index + 1], a.list[a.index])) {
				descending = true;
				for (int i = a.index + 2; i < a.index + a.length; i++) {
					if (lower_than (a.list[i], a.list[i-1])) {
						run_length++;
					} else {
						break;
					}
				}
			} else {
				descending = false;
				for (int i = a.index + 2; i < a.index + a.length; i++) {
					if (lower_than (a.list[i], a.list[i-1])) {
						break;
					} else {
						run_length++;
					}
				}
			}
		}
		return new Slice<G> (a.list, a.index, run_length);
	}

	private void insertion_sort (Slice<G> a, int offset) {
		#if DEBUG
			message ("Sorting (%d, %d) at %d", a.index, a.length, offset);
		#endif
		for (int start = a.index + offset; start < a.index + a.length; start++) {
			int left = a.index;
			int right = start;
			void* pivot = a.list[right];

			while (left < right) {
				int p = left + ((right - left) >> 1);
				if (lower_than (pivot, a.list[p])) {
					right = p;
				} else {
					left = p + 1;
				}
			}
			assert (left == right);

			Memory.move (&a.list[left + 1], &a.list[left], sizeof (G) * (start - left));
			a.list[left] = pivot;
		}
	}

	private void merge_collapse () {
		#if DEBUG
			message ("Merge Collapse");
		#endif
		int count = pending.length;
		while (count > 1) {
			#if DEBUG
				message ("Pending count: %d", count);
				if (count >= 3) {
					message ("pending[count-3]=%p; pending[count-2]=%p; pending[count-1]=%p",
					         pending[count-3], pending[count-2], pending[count-1]);
				}
			#endif
			if (count >= 3 && pending[count-3].length <= pending[count-2].length + pending[count-1].length) {
				if (pending[count-3].length < pending[count-1].length) {
					merge_at (count-3);
				} else {
					merge_at (count-2);
				}
			} else if (pending[count-2].length <= pending[count-1].length) {
				merge_at (count-2);
			} else {
				break;
			}
			count = pending.length;
			#if DEBUG
				message ("New pending count: %d", count);
			#endif
		}
	}

	private void merge_force_collapse () {
		#if DEBUG
			message ("Merge Force Collapse");
		#endif
		int count = pending.length;
		#if DEBUG
			message ("Pending count: %d", count);
		#endif
		while (count > 1) {
			if (count >= 3 && pending[count-3].length < pending[count-1].length) {
				merge_at (count-3);
			} else {
				merge_at (count-2);
			}
			count = pending.length;
			#if DEBUG
				message ("New pending count: %d", count);
			#endif
		}
	}

	private void merge_at (int index) {
		#if DEBUG
			message ("Merge at %d", index);
		#endif
		Slice<G> a = (owned) pending[index];
		Slice<G> b = (owned) pending[index + 1];
		
		assert (a.length > 0);
		assert (b.length > 0);
		assert (a.index + a.length == b.index);

		pending[index] = new Slice<G> (list, a.index, a.length + b.length);
		pending.move (index + 2, index + 1, pending.length - index - 2);
		pending.length -= 1;

		int sorted_count = gallop_rightmost (b.peek_first (), a, 0);
		a.shorten_start (sorted_count);
		if (a.length == 0) {
			return;
		}

		b.length = gallop_leftmost (a.peek_last (), b, b.length - 1);
		if (b.length == 0) {
			return;
		}

		if (a.length <= b.length) {
			merge_low ((owned) a, (owned) b);
		} else {
			merge_high ((owned) a, (owned) b);
		}
	}

	private int gallop_leftmost (G key, Slice<G> a, int hint) {
		#if DEBUG
			message ("Galop leftmost in (%d, %d), hint=%d", a.index, a.length, hint);
		#endif
		assert (0 <= hint);
		assert (hint < a.length);

		int p = a.index + hint;
		int last_offset = 0;
		int offset = 1;
		if (lower_than (a.list[p], key)) {
			int max_offset = a.length - hint;
			while (offset < max_offset) {
				if (lower_than (a.list[p + offset], key)) {
					last_offset = offset;
					offset <<= 1;
					offset++;
				} else {
					break;
				}
			}

			if (offset > max_offset) {
				offset = max_offset;
			}

			last_offset = hint + last_offset;
			offset = hint + offset;
		} else {
			int max_offset = hint + 1;
			while (offset < max_offset) {
				if (lower_than (a.list[p - offset], key)) {
					break;
				} else {
					last_offset = offset;
					offset <<= 1;
					offset++;
				}
			}

			if (offset > max_offset) {
				offset = max_offset;
			}

			int temp_last_offset = last_offset;
			int temp_offset = offset;
			last_offset = hint - temp_offset;
			offset = hint - temp_last_offset;
		}

		assert (-1 <= last_offset);
		assert (last_offset < offset);
		assert (offset <= a.length);

		last_offset += 1;
		while (last_offset < offset) {
			int m = last_offset + ((offset - last_offset) >> 1);
			if (lower_than (a.list[a.index + m], key)) {
				last_offset = m + 1;
			} else {
				offset = m;
			}
		}

		assert (last_offset == offset);
		return offset;
	}

	private int gallop_rightmost (G key, Slice<G> a, int hint) {
		#if DEBUG
			message ("Galop rightmost in (%d, %d), hint=%d", a.index, a.length, hint);
		#endif
		assert (0 <= hint);
		assert (hint < a.length);

		int p = a.index + hint;
		int last_offset = 0;
		int offset = 1;
		if (lower_than_or_equal_to (a.list[p], key)) {
			int max_offset = a.length - hint;
			while (offset < max_offset) {
				if (lower_than_or_equal_to (a.list[p + offset], key)) {
					last_offset = offset;
					offset <<= 1;
					offset++;
				} else {
					break;
				}
			}

			if (offset > max_offset) {
				offset = max_offset;
			}

			last_offset = hint + last_offset;
			offset = hint + offset;
		} else {
			int max_offset = hint + 1;
			while (offset < max_offset) {
				if (lower_than_or_equal_to (a.list[p - offset], key)) {
					break;
				} else {
					last_offset = offset;
					offset <<= 1;
					offset++;
				}
			}

			if (offset > max_offset) {
				offset = max_offset;
			}

			int temp_last_offset = last_offset;
			int temp_offset = offset;
			last_offset = hint - temp_offset;
			offset = hint - temp_last_offset;
		}

		assert (-1 <= last_offset);
		assert (last_offset < offset);
		assert (offset <= a.length);

		last_offset += 1;
		while (last_offset < offset) {
			int m = last_offset + ((offset - last_offset) >> 1);
			if (lower_than_or_equal_to (a.list[a.index + m], key)) {
				last_offset = m + 1;
			} else {
				offset = m;
			}
		}

		assert (last_offset == offset);
		return offset;
	}

	private void merge_low (owned Slice<G> a, owned Slice<G> b) {
		#if DEBUG
			message ("Merge low (%d, %d) (%d, %d)", a.index, a.length, b.index, b.length);
		#endif
		assert (a.length > 0);
		assert (b.length > 0);
		assert (a.index + a.length == b.index);

		int minimum_gallop = this.minimum_gallop;
		int dest = a.index;
		a.copy ();

		try {
			list[dest++] = b.pop_first ();
			if (a.length == 1 || b.length == 0) {
				return;
			}

			while (true) {
				int a_count = 0;
				int b_count = 0;

				while (true) {
					if (lower_than (b.peek_first (), a.peek_first ())) {
						list[dest++] = b.pop_first ();
						if (b.length == 0) {
							return;
						}

						b_count++;
						a_count = 0;
						if (b_count >= minimum_gallop) {
							break;
						}
					} else {
						list[dest++] = a.pop_first ();
						if (a.length == 1) {
							return;
						}

						a_count++;
						b_count = 0;
						if (a_count >= minimum_gallop) {
							break;
						}
					}
				}

				minimum_gallop++;

				while (true) {
					minimum_gallop -= (minimum_gallop > 1 ? 1 : 0);
					this.minimum_gallop = minimum_gallop;

					a_count = gallop_rightmost (b.peek_first (), a, 0);
					a.merge_in (list, a.index, dest, a_count);
					dest += a_count;
					a.shorten_start (a_count);
					if (a.length <= 1) {
						return;
					}

					list[dest++] = b.pop_first ();
					if (b.length == 0) {
						return;
					}

					b_count = gallop_leftmost (a.peek_first (), b, 0);
					b.merge_in (list, b.index, dest, b_count);
					dest += b_count;
					b.shorten_start (b_count);
					if (b.length == 0) {
						return;
					}

					list[dest++] = a.pop_first ();
					if (a.length == 1) {
						return;
					}

					if (a_count < MINIMUM_GALLOP && b_count < MINIMUM_GALLOP) {
						break;
					}
				}

				minimum_gallop++;
				this.minimum_gallop = minimum_gallop;
			}
		} finally {
			assert (a.length >= 0);
			assert (b.length >= 0);
			b.merge_in (list, b.index, dest, b.length);
			a.merge_in (list, a.index, dest + b.length, a.length);
		}
	}

	private void merge_high (owned Slice<G> a, owned Slice<G> b) {
		#if DEBUG
			message ("Merge high (%d, %d) (%d, %d)", a.index, a.length, b.index, b.length);
		#endif
		assert (a.length > 0);
		assert (b.length > 0);
		assert (a.index + a.length == b.index);

		int minimum_gallop = this.minimum_gallop;
		int dest = b.index + b.length;
		b.copy ();

		try {
			list[--dest] = a.pop_last ();
			if (a.length == 0 || b.length == 1) {
				return;
			}

			while (true) {
				int a_count = 0;
				int b_count = 0;

				while (true) {
					if (lower_than (b.peek_last (), a.peek_last ())) {
						list[--dest] = a.pop_last ();
						if (a.length == 0) {
							return;
						}

						a_count++;
						b_count = 0;
						if (a_count >= minimum_gallop) {
							break;
						}
					} else {
						list[--dest] = b.pop_last ();
						if (b.length == 1) {
							return;
						}

						b_count++;
						a_count = 0;
						if (b_count >= minimum_gallop) {
							break;
						}
					}
				}

				minimum_gallop++;

				while (true) {
					minimum_gallop -= (minimum_gallop > 1 ? 1 : 0);
					this.minimum_gallop = minimum_gallop;

					int k = gallop_rightmost (b.peek_last (), a, a.length - 1);
					a_count = a.length - k;
					a.merge_in_reversed (list, a.index + k, dest - a_count, a_count);
					dest -= a_count;
					a.shorten_end (a_count);
					if (a.length == 0) {
						return;
					}

					list[--dest] = b.pop_last ();
					if (b.length == 1) {
						return;
					}

					k = gallop_leftmost (a.peek_last (), b, b.length - 1);
					b_count = b.length - k;
					b.merge_in_reversed (list, b.index + k, dest - b_count, b_count);
					dest -= b_count;
					b.shorten_end (b_count);
					if (b.length <= 1) {
						return;
					}

					list[--dest] = a.pop_last ();
					if (a.length == 0) {
						return;
					}

					if (a_count < MINIMUM_GALLOP && b_count < MINIMUM_GALLOP) {
						break;
					}
				}

				minimum_gallop++;
				this.minimum_gallop = minimum_gallop;
			}
		} finally {
			assert (a.length >= 0);
			assert (b.length >= 0);
			a.merge_in_reversed (list, a.index, dest - a.length, a.length);
			b.merge_in_reversed (list, b.index, dest - a.length - b.length, b.length);
		}
	}

	[Compact]
	private class Slice<G> {

		public void** list;
		public void** new_list;
		public int index;
		public int length;

		public Slice (void** list, int index, int length) {
			this.list = list;
			this.index = index;
			this.length = length;
		}
		
		~Slice () {
			if (new_list != null)
				free (new_list);
		}

		public void copy () {
			new_list = Memory.dup (&list[index], (uint) sizeof (G) * length);
			list = new_list;
			index = 0;
		}

		public inline void merge_in (void** dest_array, int index, int dest_index, int count) {
			Memory.move (&dest_array[dest_index], &list[index], sizeof (G) * count);
		}

		public inline void merge_in_reversed (void** dest_array, int index, int dest_index, int count) {
			Memory.move (&dest_array[dest_index], &list[index], sizeof (G) * count);
		}

		public inline void shorten_start (int n) {
			index += n;
			length -= n;
		}

		public inline void shorten_end (int n) {
			length -= n;
		}

		public inline void* pop_first () {
			length--;
			return list[index++];
		}

		public inline void* pop_last () {
			length--;
			return list[index + length];
		}

		public inline unowned void* peek_first () {
			return list[index];
		}

		public inline unowned void* peek_last () {
			return list[index + length - 1];
		}

		public void reverse () {
			int low = index;
			int high = index + length - 1;
			while (low < high) {
				swap (low++, high--);
			}
		}

		private inline void swap (int i, int j) {
			void* temp = list[i];
			list[i] = list[j];
			list[j] = temp;
		}
	}
}