File: GSTimSort.m

package info (click to toggle)
gnustep-base 1.28.1%2Breally1.28.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,008 kB
  • sloc: objc: 223,137; ansic: 35,562; sh: 184; makefile: 128; cpp: 122; xml: 32
file content (1174 lines) | stat: -rw-r--r-- 34,521 bytes parent folder | download | duplicates (2)
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
/* Implementation for the timsort sorting algorithm for GNUStep
   Copyright (C) 2012 Free Software Foundation, Inc.

   Written by:  Niels Grewe <niels.grewe@halbordnung.de>
   Date: September 2012

   This file is part of the GNUstep Base Library.

   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 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 USA.
   */

#import "common.h"
#import "Foundation/NSSortDescriptor.h"

#import "Foundation/NSCoder.h"
#import "Foundation/NSException.h"
#import "Foundation/NSKeyValueCoding.h"

#import "GNUstepBase/GSObjCRuntime.h"
#import "GSPrivate.h"
#import "GSSorting.h"

/*
 * About this implementation.
 *
 * Timsort is a stable, adaptive hybrid of merge- and insertion-sort which
 * exploits existing structure in the data to be sorted, so that it takes
 * usually takes less than O(n*log(n)) comparisons for real world data. The
 * algorithm has been developed by Tim Peters and is described in [0].
 *
 * This implementation takes inspiration both from the C implementation in
 * Python [1] and from the Java implementation in OpenJDK [2].
 *
 *  [0] http://svn.python.org/projects/python/trunk/Objects/listsort.txt
 *  [1] http://svn.python.org/projects/python/trunk/Objects/listobject.c
 *  [2] http://cr.openjdk.java.net/~martin/webrevs/openjdk7/timsort/raw_files/new/src/share/classes/java/util/TimSort.java
 */

#define GS_MIN_MERGE 32
#define GS_MIN_GALLOP 7
#define GS_INITIAL_TEMP_STORAGE 256

/*
 * Galloping from left searches for an insertion point for key into the
 * already sorted buffer and returns the point immediately left of the first
 * equal element. We can also use this function, and gallopRight(), its twin, to
 * implement -[NSArray indexOfObject:inSortedRange:options:usingComparator:].
 * Since we want to do that, this implementation is a bit different than the one
 * in Python's listobject.c, since it takes into account the range and does just
 * the buffer's length starting from the base address. The hint argument is used
 * to give galloping a sensible start point for the search (it's usually 0 or
 * (r.length - 1)).
 */
static NSUInteger
gallopLeft(id key, id *buf, NSRange r, NSUInteger hint, id descOrComp,
  GSComparisonType type, void* context)
{
  NSInteger offset =  1;
  NSInteger lastOffset = 0;
  NSInteger k = 0;

  buf += (hint + r.location);
  if (NSOrderedAscending == GSCompareUsingDescriptorOrComparator(*buf,
    key,
    descOrComp,
    type,
    context))
    {
      /* In an ascending order, we gallop to the right until the key
       * is no longer greater than the element from the range
       */
      NSInteger maxOffset = (r.length - hint);
      while (offset < maxOffset)
        {
          if (NSOrderedAscending
            == GSCompareUsingDescriptorOrComparator(buf[offset],
            key,
            descOrComp,
            type,
            context))
            {
              lastOffset = offset;
              // We gallop by 1, 3, 7, 15,...
              offset = (offset << 1) + 1;

              if (offset <= 0)
                {
                  offset = maxOffset;
                }
            }
          else
            {
              break;
            }
        }
      if (offset > maxOffset)
        {
          offset = maxOffset;
        }
      /* we incremented the buf pointer by hint, so we need to
       * account for that in order to get the offset to the base address
       */
      lastOffset += r.location + hint;
      offset += r.location + hint;
    }
  else
    {
      /* In descending (or equal) order, we gallop to the left
       * until the key is no longer less than the element from the range
       */
      NSInteger maxOffset = hint + 1;
      while (offset < maxOffset)
        {
          if (NSOrderedAscending
            == GSCompareUsingDescriptorOrComparator(*(buf - offset),
            key, descOrComp, type, context))
            {
              break;
            }
          lastOffset = offset;
          offset = (offset << 1) + 1;
          if (offset <= 0)
            {
              offset = maxOffset;
            }
        }
      // Translate into positive offsets from array base address again.
      k = lastOffset;
      lastOffset = (r.location + hint) - offset;
      offset = (r.location + hint) - k;
    }
  // Restore base address:
  buf -= (hint + r.location);

  /*
   * We are now sure that we need to insert key somewhere between offset and
   * lastOffset, though the stride might have been to large for the range.
   * Fix the range and do a binary search with a vastly diminished search space.
   */
  offset = MIN(offset, NSMaxRange(r));
  if (lastOffset < (NSInteger)r.location)
    {
      lastOffset = (NSInteger)r.location;
    }
  while (lastOffset < offset)
    {
      NSInteger midPoint = lastOffset + ((offset - lastOffset) >> 1);

      if (NSOrderedAscending
        == GSCompareUsingDescriptorOrComparator(buf[midPoint],
        key, descOrComp, type, context))
        {
          lastOffset = midPoint + 1;
        }
      else
        {
          offset = midPoint;
        }
    }
  return (NSUInteger)offset;
}

/*
 * Equivalent to gallopLeft() except that it searches for an insertion point
 * right of the last equal element.
 */
static NSUInteger
gallopRight(id key, id *buf, NSRange r, NSUInteger hint,
  id descOrComp, GSComparisonType type, void *context)
{
  NSInteger offset = 1;
  NSInteger lastOffset = 0;
  NSInteger k = 0;

  buf += (hint + r.location);
  if (NSOrderedAscending == GSCompareUsingDescriptorOrComparator(key, *buf,
    descOrComp, type, context))
    {
      /* In an ascending order, we gallop to the right until
       * the key is no longer greater than the element from the range
       */
      NSInteger maxOffset = hint + 1;

      while (offset < maxOffset)
        {
          if (NSOrderedAscending == GSCompareUsingDescriptorOrComparator(key,
            *(buf - offset), descOrComp, type, context))
            {
              lastOffset = offset;
              offset = (offset << 1) + 1;
              if (offset <= 0)
                {
                  offset = maxOffset;
                }
            }
          else
            {
              break;
            }
        }
      if (offset > maxOffset)
        {
          offset = maxOffset;
        }
      // Translation to positive offsets against the base address.
      k = lastOffset;
      lastOffset = (r.location + hint) - offset;
      offset = (r.location + hint) - k;
    }
  else
    {
      // In descending (or equal) order, we gallop to the right

      NSInteger maxOffset = (r.length - hint);
      while (offset < maxOffset)
        {
          if (NSOrderedAscending
            == GSCompareUsingDescriptorOrComparator(key, buf[offset],
            descOrComp, type, context))
            {
              break;
            }
          lastOffset = offset;
          offset = (offset << 1) + 1;
          if (offset <= 0)
            {
              offset = maxOffset;
            }
        }
        // Translate into positive offsets from array base address again.
      lastOffset += (hint + r.location);
      offset += (hint + r.location);
    }
  // Restore base address:
  buf -= (hint + r.location);

  /*
   * We are now sure that we need to insert key somewhere between offset and
   * lastOffset, though the stride might have been to large for the range.
   * Fix the range and do a binary search with a vastly diminished search space.
   */
  lastOffset++;
  offset = MIN(offset, NSMaxRange(r));
  if (lastOffset < (NSInteger)r.location)
  {
    lastOffset = (NSInteger)r.location;
  }
  while (lastOffset < offset)
    {
      NSInteger midPoint = lastOffset + ((offset - lastOffset) >> 1);

      if (NSOrderedAscending
        == GSCompareUsingDescriptorOrComparator(key, buf[midPoint],
        descOrComp, type, context))
        {
          offset = midPoint;
        }
      else
        {
          lastOffset = midPoint + 1;
        }
    }
  return (NSUInteger)offset;
}


// Public versions of the galloping functions
NSUInteger
GSLeftInsertionPointForKeyInSortedRange(id key, id *buffer,
  NSRange range, NSComparator cmptr)
{
  return gallopLeft(key, buffer, range, 0, (id)cmptr,
    GSComparisonTypeComparatorBlock, NULL);
}


NSUInteger
GSRightInsertionPointForKeyInSortedRange(id key, id *buffer,
NSRange range, NSComparator cmptr)
{
  return gallopRight(key, buffer, range, 0, (id)cmptr,
    GSComparisonTypeComparatorBlock, NULL);
}

static inline void
reverseRange(id *buffer, NSRange r)
{
  NSUInteger loc = r.location;
  NSUInteger max = (NSMaxRange(r) - 1);

  while (loc < max)
    {
      id temp = buffer[loc];
      buffer[loc++] = buffer[max];
      buffer[max--] = temp;
    }
}

/* In-place binary insertion sorting for small arrays (i.e. those which are
 * smaller than GS_MIN_MERGE. We use this to generate minimal runs for timsort.
 */
static void
internalBinarySort(id *buffer,
  NSRange r,
  NSUInteger start,
  id compOrDesc,
  GSComparisonType type,
  void *context)
{
  NSUInteger min = r.location;
  NSUInteger max = NSMaxRange(r);

  NSCAssert2(NSLocationInRange(start, r),
    @"Start index %lu not in range %@",
    start, NSStringFromRange(r));

  if (min == start)
    {
      start++;
    }
  // We assume that everything before start is sorted.
  for (; start < max; ++start)
    {
      NSUInteger left = min;
      NSUInteger right = start;
      id pivot = buffer[right];
      int i = 0;

      do
        {
          NSUInteger midPoint = (left + ((right - left) >> 1));
          NSComparisonResult res = GSCompareUsingDescriptorOrComparator(pivot,
            buffer[midPoint],
            compOrDesc,
            type,
            context);
          if (NSOrderedAscending == res)
            {
              right = midPoint;
            }
          else
            {
              left = midPoint + 1;
            }
        } while (left < right);
      NSCAssert(left == right, @"Binary sort iteration did not end correctly,");
      // We make room for the pivot and place it at left.
      for (i = start; i > left; --i)
        {
          buffer[i] = buffer[(i - 1)];
        }
      buffer[left] = pivot;
    }
}


/*
 * Count the number of elements in the range that are already ordered.
 * If the order is a descending one, reverse it so that all runs are ordered the
 * same way.
 */
static inline NSUInteger
countAscendizedRun(id *buf, NSRange r, id descOrComp,
  GSComparisonType type, void*context)
{
  NSUInteger min = r.location;
  NSUInteger runMax = min + 1;
  NSUInteger rangeMax = NSMaxRange(r);

  if (runMax == rangeMax)
    {
      return 1;
    }
  if (NSOrderedDescending == GSCompareUsingDescriptorOrComparator(buf[min],
    buf[runMax++], descOrComp, type, context))
    {
      while ((runMax < rangeMax) && NSOrderedDescending
        == GSCompareUsingDescriptorOrComparator(buf[runMax - 1],
        buf[runMax], descOrComp, type, context))
        {
          runMax++;
        }
      reverseRange(buf, NSMakeRange(min, (runMax - min)));
    }
  else // ascending or equal
    {
      while ((runMax < rangeMax) && NSOrderedDescending
        != GSCompareUsingDescriptorOrComparator(buf[runMax - 1],
        buf[runMax], descOrComp, type, context))
        {
          runMax++;
        }
    }
  return (runMax - min);
}

/*
 * Calculate a sensible minimum length for the runs, these need to be powers of
 * two, or less than, but close to, one, but always at least GS_MIN_MERGE. For
 * details on why this is useful, see Python's listsort.txt.
 */
static inline NSUInteger
minimumRunLength(NSUInteger length)
{
  NSUInteger r = 0;

  while (length >= GS_MIN_MERGE)
    {
      r |= length & 1;
      length >>= 1;
    }

  return (length + r);
}

/*
 * For arrays up to GS_MIN_MERGE, we don't do merging. Instead, we identify
 * pre-ordering at the begining of the range and sort the rest using binary
 * sort.
 */
static inline void
miniTimSort(id *buf, NSRange r, id descOrComp, GSComparisonType ty, void *ctx)
{
  NSUInteger firstRunLength = countAscendizedRun(buf, r, descOrComp, ty, ctx);

  if (r.length == firstRunLength)
    {
      // In this case, we have already sorted the array here.
      return;
    }
  internalBinarySort(buf, r, (r.location + firstRunLength),
    descOrComp, ty, ctx);
}

/* These macros make calling the cached IMPs easier,
 * if we choose to do so later.
 */

#define GS_TIMSORT_CACHED_MSG(recv, sel) sel ## Imp(recv,@selector(sel))
#define GS_TIMSORT_CACHED_MSGV(recv, imp, sel, ...) imp(recv, @selector(sel), __VA_ARGS__)

#define GS_TIMSORT_SUGGEST_MERGE(desc) GS_TIMSORT_CACHED_MSG(desc, suggestMerge)
#define GS_TIMSORT_FORCE_MERGE(desc) GS_TIMSORT_CACHED_MSG(desc, forceMerge)
#define GS_TIMSORT_PUSH_RUN(desc, run) \
  GS_TIMSORT_CACHED_MSGV(desc, pushRunImp, pushRun:, run)
#define GS_TIMSORT_MERGE_AT_INDEX(desc, n) \
  GS_TIMSORT_CACHED_MSGV(desc, mergeAtIndexImp, mergeAtIndex:, n)
#define GS_TIMSORT_MERGE_LOW(desc, r1, r2) \
  GS_TIMSORT_CACHED_MSGV(desc, mergeLowImp, mergeLowRun:withRun:, r1, r2)
#define GS_TIMSORT_MERGE_HIGH(desc, r1, r2) \
  GS_TIMSORT_CACHED_MSGV(desc, mergeHighImp, mergeHighRun:withRun:, r1, r2)
#define GS_TIMSORT_ENSURE_TEMP_CAPACITY(desc, length) \
  GS_TIMSORT_CACHED_MSGV(desc, ensureCapImp, ensureTempCapacity:, length)


static IMP pushRunImp;
static IMP suggestMergeImp;
static IMP forceMergeImp;
static IMP mergeAtIndexImp;
static IMP mergeLowImp;
static IMP mergeHighImp;
static IMP ensureCapImp;

@interface GSTimSortPlaceHolder : NSObject
{
  id *objects;
  NSRange sortRange;
  id sortDescriptorOrComparator;
  GSComparisonType comparisonType;
  void *functionContext;
  NSUInteger minGallop;
  NSUInteger tempCapacity;
  id *tempBuffer;
  NSUInteger stackSize;
  NSRange* runStack;
}

- (id)initWithObjects: (id*)theObjects
            sortRange: (NSRange)theSortRange
           descriptor: (NSSortDescriptor*)descriptor;
- (id)initWithObjects: (id*)theObjects
            sortRange: (NSRange)theSortRange
           comparator: (NSComparator)comparator;
- (void)mergeAtIndex: (NSUInteger)index;
- (void)suggestMerge;
- (void)forceMerge;
@end



// Prototype for the actual timsort function.
static void
_GSTimSort(id *objects,
  NSRange sortRange,
  id sortDescriptorOrComparator,
  GSComparisonType comparisonType,
  void *context);

@implementation GSTimSortPlaceHolder
+ (void) load
{
  _GSSortStable = _GSTimSort;
}

+ (void) initialize
{
  if ([GSTimSortPlaceHolder class] == [self class])
    {
      // We need to be fast, so we cache a lot of IMPs
      pushRunImp =
        [self instanceMethodForSelector: @selector(pushRun:)];
      suggestMergeImp =
        [self instanceMethodForSelector: @selector(suggestMerge)];
      forceMergeImp =
        [self instanceMethodForSelector: @selector(forceMerge)];
      mergeAtIndexImp =
        [self instanceMethodForSelector: @selector(mergeAtIndex:)];
      mergeLowImp =
        [self instanceMethodForSelector: @selector(mergeLowRun:withRun:)];
      mergeHighImp =
        [self instanceMethodForSelector: @selector(mergeHighRun:withRun:)];
      ensureCapImp =
        [self instanceMethodForSelector: @selector(ensureTempCapacity:)];
    }
}

+ (void) setUnstable
{
  _GSSortUnstable = _GSTimSort; // Use for unstable even though we are stable
}

- (id) initWithObjects: (id*)theObjects
             sortRange: (NSRange)theSortRange
descriptorOrComparator: (id)descriptorOrComparator
        comparisonType: (GSComparisonType)ty
       functionContext: (void*)ctx
{
  NSUInteger sortLength = theSortRange.length;
  NSUInteger stackSpace = 0;

  if (nil == (self = [super init]))
    {
      return nil;
    }
  /* GSTimSortPlaceHolders are ephemeral objects that just track state, so we
   * don't bother making sure that the objects don't go away.
   */
  objects = theObjects;
  sortRange = theSortRange;
  sortDescriptorOrComparator = descriptorOrComparator;
  comparisonType = ty;
  functionContext = ctx;
  /* minGallop will be adjusted based on heuristics on whether we have a highly
   * structured array (in which case galloping is useful) or a more random one
   * (when it isn't).
   */
  minGallop = GS_MIN_GALLOP;
  stackSize = 0;
  /* timsort needs at most half the array size as temporary storage, so
   * we optimize for arrays that require less storage than we'd usually
   * allocate.
   */
  tempCapacity = ((sortLength < (2 * GS_INITIAL_TEMP_STORAGE))
    ? sortLength >> 1 : GS_INITIAL_TEMP_STORAGE);
  tempBuffer = malloc(sizeof(id) * tempCapacity );

  /* We also allocate the stack in which we track the runs based on the array
   * size. (The values are based of the OpenJDK implementation of timsort)
   */
  stackSpace = (sortLength < 120 ? 5 :
                  sortLength < 1542 ? 10 :
                   sortLength < 119151 ? 19 : 40);
  runStack = malloc(sizeof(NSRange) * stackSpace);
  return self;
}

- (id) initWithObjects: (id*)theObjects
             sortRange: (NSRange)theSortRange
            descriptor: (NSSortDescriptor*)descriptor
{
  return [self initWithObjects: theObjects
                     sortRange: theSortRange
        descriptorOrComparator: descriptor
                comparisonType: GSComparisonTypeSortDescriptor
               functionContext: NULL];
}

- (id) initWithObjects: (id*)theObjects
             sortRange: (NSRange)theSortRange
            comparator: (NSComparator)comparator
{
  return [self initWithObjects: theObjects
                     sortRange: theSortRange
        descriptorOrComparator: (id)comparator
                comparisonType: GSComparisonTypeComparatorBlock
               functionContext: NULL];
}


- (void) pushRun: (NSRange)r
{
  runStack[stackSize] = r;
  stackSize++;
  NSDebugMLLog(@"GSTimSort", @"Pushing run: %@", NSStringFromRange(r));
}

/**
 * Ensure that the invariant enabling the algorithm holds for the stack.
 *
 * see: http://www.envisage-project.eu/proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it/#sec3
 */
- (void) suggestMerge
{
  while (stackSize > 1)
    {
      NSInteger n = stackSize -2;

      if ((n >= 1 && runStack[n-1].length
	  <= (runStack[n].length + runStack[n+1].length))
        || (n >= 2 && runStack[n-2].length
	  <= (runStack[n].length + runStack[n-1].length)))
        {
          if (runStack[n-1].length < runStack[n+1].length)
            {
              n--;
            }
        }
      else if (runStack[n].length > runStack[n+1].length)
        {
          break; //invariant reached
        }
      GS_TIMSORT_MERGE_AT_INDEX(self, n);
    }
}

- (void) ensureTempCapacity: (NSUInteger)elementsRequired
{
  if (elementsRequired <= tempCapacity)
    {
      return;
    }
  /* We don't realloc any memory because we don't care about the contents from
   * previous merge iterations.
   */
  free(tempBuffer);
  tempBuffer = malloc(sizeof(id) * elementsRequired);
  tempCapacity = elementsRequired;
  //TODO: OOM exception
}


/*
 * Main merge algorithm: Does a pairwise merge in the general-case and
 * adaptively switches to galloping mode, which moves around whole chunks of the
 * array. This method is called if r1 is the shorter run (i.e. the one which
 * requires less temporary storage).
 */
- (void) mergeLowRun: (NSRange)r1 withRun: (NSRange)r2
{
  NSUInteger num1 = r1.length;
  NSUInteger num2 = r2.length;
  id *buf1 = objects + r1.location;
  id *buf2 = objects + r2.location;
  id *destination = buf1;
  NSUInteger k = 0;
  // Local variables for performance
  NSUInteger localMinGallop = minGallop;
  id descOrComp = sortDescriptorOrComparator;
  GSComparisonType ty = comparisonType;
  void *ctx = functionContext;

  /* We use the first run as our destination, so we copy out its contents into
   * the temporary storage (which needs to be large enough, though).
   */
  GS_TIMSORT_ENSURE_TEMP_CAPACITY(self, r1.length);
  memcpy(tempBuffer, buf1, (sizeof(id) * r1.length));

  destination = buf1;
  buf1 = tempBuffer;
  *destination++ = *buf2++;
  num2--;

  if (num2 == 0)
    {
      if (0 != num1)
        {
          memcpy(destination, buf1, num1 * sizeof(id));
        }
      return;
    }
  if (num1 == 1)
    {
      memmove(destination, buf2, num2 * sizeof(id));
      destination[num2] = *buf1;
      return;
    }

  NS_DURING
    {
      for (;;)
        {
          // Variables to track whether galloping is useful
          NSUInteger winners1 = 0;
          NSUInteger winners2 = 0;

          do
            {
              if (NSOrderedAscending
                == GSCompareUsingDescriptorOrComparator(*buf2, *buf1,
                descOrComp, ty, ctx))
                {
                  *destination++ = *buf2++;
                  winners2++;
                  winners1 = 0;
                  num2--;
                  if (num2 == 0)
                    {
                      goto Success;
                    }
                }
              else
                {
                  *destination++ = *buf1++;
                  winners1++;
                  winners2 = 0;
                  num1--;
                  if (num1 == 1)
                    {
                      goto CopyB;
                    }
                }
            } while ((winners1 | winners2) < localMinGallop);

          localMinGallop++;
          do
            {
              /* If we fall through here, one of the runs is very structured,
               * so we assume that galloping will also be useful in the future.
               */
              localMinGallop -= localMinGallop > 1;
              minGallop = localMinGallop;
              k = gallopRight(*buf2, buf1,
                NSMakeRange(0,num1), 0, descOrComp, ty, ctx);
              winners1 = k;
              if (0 != k)
                {
                  memcpy(destination, buf1, k * sizeof(id));
                  destination += k;
                  buf1 += k;
                  num1 -= k;
                  if (1 == num1)
                    {
                      goto CopyB;
                    }
                  if (0 == num1)
                    {
                      goto Success;
                    }
                }
              /* Since our galloping run finishes here, the next element
               * comes from r2
               */
              *destination++ = *buf2++;
              num2--;
              if (0 == num2)
                {
                  goto Success;
                }

              // Now we try to gallop into the other direction
              k = gallopLeft(*buf1, buf2, NSMakeRange(0, num2),
                0, descOrComp, ty, ctx);
              winners2 = k;
              if (0 != k)
                {
                  /* buf2 is part of the destination, not the temporary
                   * storage, so we need to memmove instead of memcpy to
                   * account for potential overlap.
                   */
                  memmove(destination, buf2, k * sizeof(id));
                  destination += k;
                  buf2 += k;
                  num2 -= k;
                  if (0 == num2)
                    {
                      goto Success;
                    }
                }
              /* Galloping run for r2 finished, next element comes from r1,
               * and starts the next loop iteration
               */
              *destination++ = *buf1++;
              num1--;
              if (1 == num1)
                {
                  goto CopyB;
                }
            } while (winners1 >= GS_MIN_GALLOP || winners2 >= GS_MIN_GALLOP);
          localMinGallop++;
          minGallop = localMinGallop;
        }
      Success:
        if (0 != num1)
          {
            memcpy(destination, buf1, num1 * sizeof(id));
          }
        NS_VOIDRETURN;
      CopyB:
        memmove(destination, buf2, num2 * sizeof(id));
        destination[num2] = *buf1;
        NS_VOIDRETURN;
    }
  NS_HANDLER
    {
      //In case of an exception, we need to copy back r1 into its original
      //position
      if (0 != num1)
        {
          memcpy(destination, buf1, num1 * sizeof(id));
        }
      [localException raise];
    }
  NS_ENDHANDLER
}

- (void) mergeHighRun: (NSRange)r1 withRun: (NSRange)r2
{
  NSUInteger num1 = r1.length;
  NSUInteger num2 = r2.length;
  id *buf1 = objects + r1.location;
  id *buf2 = objects + r2.location;
  id *base1 = buf1;
  id *base2 = NULL;
  // We are walking backwards, so destination pointer is at the high end
  // initially.
  id *destination = buf2 + num2 - 1;
  NSUInteger k = 0;
  // Local variables for performance
  NSUInteger localMinGallop = minGallop;
  id descOrComp = sortDescriptorOrComparator;
  GSComparisonType ty = comparisonType;
  void *ctx = functionContext;

  // We use the first run as our destination, so we copy out its contents into
  // the temporary storage (which needs to be large enough, though).
  GS_TIMSORT_ENSURE_TEMP_CAPACITY(self, r2.length);
  memcpy(tempBuffer, buf2, (sizeof(id) * r2.length));

  base2 = tempBuffer;
  buf2 = tempBuffer + num2 - 1;
  buf1 += num1 - 1;
  *destination-- = *buf1--;
  num1--;

  if (num1 == 0)
    {
      if (0 != num2)
        {
          memcpy(destination - (num2-1), base2, num2 * sizeof(id));
        }
      return;
    }
  if (num2 == 1)
    {
      destination -= num1;
      buf1 -= num1;
      memmove(destination + 1, buf1 + 1, num1 * sizeof(id));
      *destination = *buf2;
      return;
    }

  NS_DURING
    {
      for (;;)
        {
          // Variables to track whether galloping is useful
          NSUInteger winners1 = 0;
          NSUInteger winners2 = 0;

          do
            {
              if (NSOrderedAscending
                == GSCompareUsingDescriptorOrComparator(*buf2, *buf1,
                descOrComp, ty, ctx))
                {
                  *destination-- = *buf1--;
                  winners1++;
                  winners2 = 0;
                  num1--;
                  if (num1 == 0)
                    {
                      goto Success;
                    }
                }
              else
                {
                  *destination-- = *buf2--;
                  winners2++;
                  winners1 = 0;
                  num2--;
                  if (num2 == 1)
                    {
                      goto CopyA;
                    }
                }
            } while ((winners1 | winners2) < localMinGallop);

          localMinGallop++;
          do
            {
              /* If we fall through here, one of the runs is very structured,
               * so we assume that galloping will also be useful in the future.
               */
              localMinGallop -= localMinGallop > 1;
              minGallop = localMinGallop;
              k = gallopRight(*buf2, base1,
                NSMakeRange(0, num1), num1 - 1, descOrComp, ty, ctx);
              k = num1 - k;
              winners1 = k;
              if (0 != k)
                {
                  destination -= k;
                  buf1 -= k;
                  memmove(destination+1, buf1+1, k * sizeof(id));
                  num1 -= k;
                  if (0 == num1)
                    {
                      goto Success;
                    }
                }
              /* Since our galloping run finishes here,
               * the next element comes from r2
               */
              *destination-- = *buf2--;
              num2--;
              if (1 == num2)
                {
                  goto CopyA;
                }

              // Now we try to gallop into the other direction
              k = gallopLeft(*buf1, base2,
                NSMakeRange(0, num2), num2-1, descOrComp, ty, ctx);
              k = num2 - k;
              winners2 = k;
              if (0 != k)
                {
                  destination -= k;
                  buf2 -= k;
                  memcpy(destination + 1, buf2 + 1, k * sizeof(id));
                  num2 -= k;
                  if (1 == num2)
                    {
                      goto CopyA;
                    }
                  if (0 == num2)
                    {
                      goto Success;
                    }
                }
              /* Galloping run for r2 finished, next element comes from r1,
               * and starts the next loop iteration
               */
              *destination-- = *buf1--;
              num1--;
              if (0 == num1)
                {
                  goto Success;
                }
            } while (winners1 >= GS_MIN_GALLOP || winners2 >= GS_MIN_GALLOP);
          localMinGallop++;
          minGallop = localMinGallop;
        }
      Success:
        if (0 != num2)
          {
            memcpy(destination - (num2-1), base2, num2 * sizeof(id));
          }
        NS_VOIDRETURN;
      CopyA:
        destination -= num1;
        buf1 -= num1;
        memmove(destination + 1, buf1 + 1, num1 * sizeof(id));
        *destination = *buf2;
        NS_VOIDRETURN;
    }
  NS_HANDLER
    {
      //In case of an exception, we need to copy back r1 into its original
      //position
      if (0 != num2)
        {
          memcpy(destination - (num2-1), base2, num2 * sizeof(id));
        }
      [localException raise];
    }
  NS_ENDHANDLER
}

- (void) mergeAtIndex: (NSUInteger)i
{
  NSRange r1;
  NSRange r2;
  NSUInteger insert = 0;
  NSAssert((stackSize >= 2), @"Trying to merge without a plurality of runs.");
  NSAssert(((i == (stackSize - 2)) || (i == (stackSize - 3))),
    @"Trying at an index other than the penultimate or antepenultimate.");

  r1 = runStack[i];
  r2 = runStack[i+1];
  NSDebugMLLog(@"GSTimSort",
    @"Merging stack location %lu (stack size: %lu, run %@ with %@)", i,
    stackSize, NSStringFromRange(r1), NSStringFromRange(r2));

  /* Do some housekeeping on the stack: We combine the two runs
   * being merged and move around the last run on the stack
   * if we are merging on the antepenultimate run.
   * In any case, the run at i+1 is consumed in the merge and
   * the stack shrinks.
   */
  runStack[i] = NSUnionRange(r1, r2);
  if (i == (stackSize - 3))
    {
      runStack[i+1] = runStack[i+2];
    }
  stackSize--;

  // Find an insertion point for the first element in r2 into r1
  insert = gallopRight(objects[r2.location], objects, r1, 0,
    sortDescriptorOrComparator, comparisonType, functionContext);
  r1.length = r1.length - (insert - r1.location);
  r1.location = insert;
  if (r1.length == 0)
    {
      // The entire run r2 lies after r1, just return.
      return;
    }
  NSDebugMLLog(@"GSTimSort",
    @"Insertion point for r2 in r1: %lu, r1 for the merge is now %@.",
    insert, NSStringFromRange(r1));

  // Find an insertion point for the last element of r1 into r2. Subtracting the
  // location from that point gives us the length of the subrange we need to
  // merge.
  r2.length = (gallopLeft(objects[NSMaxRange(r1) - 1], objects, r2,
    (r2.length - 1),
    sortDescriptorOrComparator, comparisonType, functionContext)
     - r2.location);
  if (r2.length == 0)
    {
      return;
    }

  (r1.length <= r2.length)
    ? GS_TIMSORT_MERGE_LOW(self, r1, r2)
    : GS_TIMSORT_MERGE_HIGH(self, r1, r2);
}

/**
 * Force a final merge of the runs on the stack, so that only one run, covering
 * the whole array, remains.
 */
- (void) forceMerge
{
  while (stackSize > 1)
    {
      NSInteger n = stackSize - 2;
      if ((n > 0) && (runStack[n-1].length < runStack[n+1].length))
        {
          n--;
        }
      GS_TIMSORT_MERGE_AT_INDEX(self, n);
    }
}

- (void) dealloc
{
  free(runStack);
  free(tempBuffer);
  [super dealloc];
}

@end

static void
_GSTimSort(id *objects,
  NSRange sortRange,
  id sortDescriptorOrComparator,
  GSComparisonType comparisonType,
  void *context)
{
  NSUInteger sortStart = sortRange.location;
  NSUInteger sortEnd = NSMaxRange(sortRange);
  NSUInteger sortLen = sortRange.length;
  NSUInteger minimalRunLen = 0;
  GSTimSortPlaceHolder *desc = nil;
  if (sortLen < 2)
    {
      // Don't sort anything that doesn't contain at least two elements.
      return;
    }

  if (sortLen < GS_MIN_MERGE)
    {
      miniTimSort(objects, sortRange,
        sortDescriptorOrComparator, comparisonType, context);
      return;
    }

  // Now we need a timsort descriptor for state-tracking.
  desc = [[GSTimSortPlaceHolder alloc] initWithObjects: objects
    sortRange: sortRange
    descriptorOrComparator: sortDescriptorOrComparator
    comparisonType: comparisonType
    functionContext: context];

  NS_DURING
    {
      minimalRunLen = minimumRunLength(sortLen);
      do
        {
          NSUInteger runLen = countAscendizedRun(objects,
            NSMakeRange(sortStart, sortLen),
            sortDescriptorOrComparator,
            comparisonType, context);

          /* If the run is too short, coerce it up to minimalRunLen
           * or the end of the sortRange.
           */
          if (runLen < MIN(sortLen, minimalRunLen))
            {
              NSUInteger coercionLen;

              coercionLen = sortLen <= minimalRunLen ? sortLen : minimalRunLen;
              internalBinarySort(objects,
                NSMakeRange(sortStart, coercionLen),
                sortStart + runLen,
                sortDescriptorOrComparator,
                comparisonType,
                context);
              runLen = coercionLen;
            }

          GS_TIMSORT_PUSH_RUN(desc, NSMakeRange(sortStart, runLen));
          GS_TIMSORT_SUGGEST_MERGE(desc);
          sortStart += runLen;
          sortLen -= runLen;
        } while (sortLen != 0);

      NSCAssert(sortStart == sortEnd, @"Sorting did not complete");
      GS_TIMSORT_FORCE_MERGE(desc);
    }
  NS_HANDLER
    {
      [desc release];
      [localException raise];
    }
  NS_ENDHANDLER
  [desc release];
}