2 * taken from https://github.com/swenson/sort
3 * Kept as is for the moment to be able to apply upstream patches for that
4 * code, currently used only to speed up XPath node sorting, see xpath.c
8 * All code in this header, unless otherwise specified, is hereby licensed under the MIT Public License:
10 Copyright (c) 2010 Christopher Swenson
12 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:
14 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
16 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.
25 #ifdef HAVE_INTTYPES_H
28 typedef __int64
int64_t;
29 typedef unsigned __int64
uint64_t;
34 #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300
35 #define MK_UINT64(x) ((uint64_t)(x))
37 #define MK_UINT64(x) x##ULL
42 #define MAX(x,y) (((x) > (y) ? (x) : (y)))
45 #define MIN(x,y) (((x) < (y) ? (x) : (y)))
48 int compute_minrun(uint64_t);
51 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ > 3))
52 #define CLZ __builtin_clzll
57 /* adapted from Hacker's Delight */
58 int clzll(uint64_t x
) /* {{{ */
62 if (x
== 0) return(64);
64 if (x
<= MK_UINT64(0x00000000FFFFFFFF)) {n
= n
+ 32; x
= x
<< 32;}
65 if (x
<= MK_UINT64(0x0000FFFFFFFFFFFF)) {n
= n
+ 16; x
= x
<< 16;}
66 if (x
<= MK_UINT64(0x00FFFFFFFFFFFFFF)) {n
= n
+ 8; x
= x
<< 8;}
67 if (x
<= MK_UINT64(0x0FFFFFFFFFFFFFFF)) {n
= n
+ 4; x
= x
<< 4;}
68 if (x
<= MK_UINT64(0x3FFFFFFFFFFFFFFF)) {n
= n
+ 2; x
= x
<< 2;}
69 if (x
<= MK_UINT64(0x7FFFFFFFFFFFFFFF)) {n
= n
+ 1;}
78 int compute_minrun(uint64_t size
) /* {{{ */
80 const int top_bit
= 64 - CLZ(size
);
81 const int shift
= MAX(top_bit
, 6) - 6;
82 const int minrun
= size
>> shift
;
83 const uint64_t mask
= (MK_UINT64(1) << shift
) - 1;
84 if (mask
& size
) return minrun
+ 1;
90 #error "Must declare SORT_NAME"
94 #error "Must declare SORT_TYPE"
98 #define SORT_CMP(x, y) ((x) < (y) ? -1 : ((x) == (y) ? 0 : 1))
102 #define SORT_SWAP(x,y) {SORT_TYPE __SORT_SWAP_t = (x); (x) = (y); (y) = __SORT_SWAP_t;}
104 #define SORT_CONCAT(x, y) x ## _ ## y
105 #define SORT_MAKE_STR1(x, y) SORT_CONCAT(x,y)
106 #define SORT_MAKE_STR(x) SORT_MAKE_STR1(SORT_NAME,x)
108 #define BINARY_INSERTION_FIND SORT_MAKE_STR(binary_insertion_find)
109 #define BINARY_INSERTION_SORT_START SORT_MAKE_STR(binary_insertion_sort_start)
110 #define BINARY_INSERTION_SORT SORT_MAKE_STR(binary_insertion_sort)
111 #define REVERSE_ELEMENTS SORT_MAKE_STR(reverse_elements)
112 #define COUNT_RUN SORT_MAKE_STR(count_run)
113 #define CHECK_INVARIANT SORT_MAKE_STR(check_invariant)
114 #define TIM_SORT SORT_MAKE_STR(tim_sort)
115 #define TIM_SORT_RESIZE SORT_MAKE_STR(tim_sort_resize)
116 #define TIM_SORT_MERGE SORT_MAKE_STR(tim_sort_merge)
117 #define TIM_SORT_COLLAPSE SORT_MAKE_STR(tim_sort_collapse)
119 #define TIM_SORT_RUN_T SORT_MAKE_STR(tim_sort_run_t)
120 #define TEMP_STORAGE_T SORT_MAKE_STR(temp_storage_t)
127 void BINARY_INSERTION_SORT(SORT_TYPE
*dst
, const size_t size
);
128 void TIM_SORT(SORT_TYPE
*dst
, const size_t size
);
130 /* Function used to do a binary search for binary insertion sort */
131 static int64_t BINARY_INSERTION_FIND(SORT_TYPE
*dst
, const SORT_TYPE x
, const size_t size
)
141 /* check for beginning conditions */
142 if (SORT_CMP(x
, lx
) < 0)
144 else if (SORT_CMP(x
, lx
) == 0)
147 while (SORT_CMP(x
, dst
[i
]) == 0) i
++;
154 const int val
= SORT_CMP(x
, cx
);
157 if (c
- l
<= 1) return c
;
162 if (r
- c
<= 1) return c
+ 1;
171 } while (SORT_CMP(x
, cx
) == 0);
174 c
= l
+ ((r
- l
) >> 1);
179 /* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */
180 static void BINARY_INSERTION_SORT_START(SORT_TYPE
*dst
, const size_t start
, const size_t size
)
183 for (i
= start
; i
< (int64_t) size
; i
++)
188 /* If this entry is already correct, just move along */
189 if (SORT_CMP(dst
[i
- 1], dst
[i
]) <= 0) continue;
191 /* Else we need to find the right place, shift everything over, and squeeze in */
193 location
= BINARY_INSERTION_FIND(dst
, x
, i
);
194 for (j
= i
- 1; j
>= location
; j
--)
202 /* Binary insertion sort */
203 void BINARY_INSERTION_SORT(SORT_TYPE
*dst
, const size_t size
)
205 BINARY_INSERTION_SORT_START(dst
, 1, size
);
208 /* timsort implementation, based on timsort.txt */
210 static void REVERSE_ELEMENTS(SORT_TYPE
*dst
, int64_t start
, int64_t end
)
214 if (start
>= end
) return;
215 SORT_SWAP(dst
[start
], dst
[end
]);
221 static int64_t COUNT_RUN(SORT_TYPE
*dst
, const int64_t start
, const size_t size
)
224 if (size
- start
== 1) return 1;
225 if (start
>= (int64_t) size
- 2)
227 if (SORT_CMP(dst
[size
- 2], dst
[size
- 1]) > 0)
228 SORT_SWAP(dst
[size
- 2], dst
[size
- 1]);
234 if (SORT_CMP(dst
[start
], dst
[start
+ 1]) <= 0)
239 if (curr
== (int64_t) size
- 1) break;
240 if (SORT_CMP(dst
[curr
- 1], dst
[curr
]) > 0) break;
250 if (curr
== (int64_t) size
- 1) break;
251 if (SORT_CMP(dst
[curr
- 1], dst
[curr
]) <= 0) break;
254 /* reverse in-place */
255 REVERSE_ELEMENTS(dst
, start
, curr
- 1);
260 #define PUSH_NEXT() do {\
261 len = COUNT_RUN(dst, curr, size);\
263 if (run < minrun) run = minrun;\
264 if (run > (int64_t) size - curr) run = size - curr;\
267 BINARY_INSERTION_SORT_START(&dst[curr], len, run);\
271 run_stack[stack_curr].start = curr;\
272 run_stack[stack_curr].length = len;\
276 if (curr == (int64_t) size)\
279 while (stack_curr > 1) \
281 TIM_SORT_MERGE(dst, run_stack, stack_curr, store); \
282 run_stack[stack_curr - 2].length += run_stack[stack_curr - 1].length; \
285 if (store->storage != NULL)\
287 free(store->storage);\
288 store->storage = NULL;\
295 static int CHECK_INVARIANT(TIM_SORT_RUN_T
*stack
, const int stack_curr
)
298 if (stack_curr
< 2) return 1;
301 const int64_t A1
= stack
[stack_curr
- 2].length
;
302 const int64_t B1
= stack
[stack_curr
- 1].length
;
303 if (A1
<= B1
) return 0;
306 A
= stack
[stack_curr
- 3].length
;
307 B
= stack
[stack_curr
- 2].length
;
308 C
= stack
[stack_curr
- 1].length
;
309 if ((A
<= B
+ C
) || (B
<= C
)) return 0;
319 static void TIM_SORT_RESIZE(TEMP_STORAGE_T
*store
, const size_t new_size
)
321 if (store
->alloc
< new_size
)
323 SORT_TYPE
*tempstore
= (SORT_TYPE
*)realloc(store
->storage
, new_size
* sizeof(SORT_TYPE
));
324 if (tempstore
== NULL
)
326 fprintf(stderr
, "Error allocating temporary storage for tim sort: need %llu bytes", (unsigned long long)(sizeof(SORT_TYPE
) * new_size
));
329 store
->storage
= tempstore
;
330 store
->alloc
= new_size
;
334 static void TIM_SORT_MERGE(SORT_TYPE
*dst
, const TIM_SORT_RUN_T
*stack
, const int stack_curr
, TEMP_STORAGE_T
*store
)
336 const int64_t A
= stack
[stack_curr
- 2].length
;
337 const int64_t B
= stack
[stack_curr
- 1].length
;
338 const int64_t curr
= stack
[stack_curr
- 2].start
;
342 TIM_SORT_RESIZE(store
, MIN(A
, B
));
343 storage
= store
->storage
;
348 memcpy(storage
, &dst
[curr
], A
* sizeof(SORT_TYPE
));
352 for (k
= curr
; k
< curr
+ A
+ B
; k
++)
354 if ((i
< A
) && (j
< curr
+ A
+ B
))
356 if (SORT_CMP(storage
[i
], dst
[j
]) <= 0)
357 dst
[k
] = storage
[i
++];
363 dst
[k
] = storage
[i
++];
372 memcpy(storage
, &dst
[curr
+ A
], B
* sizeof(SORT_TYPE
));
376 for (k
= curr
+ A
+ B
- 1; k
>= curr
; k
--)
378 if ((i
>= 0) && (j
>= curr
))
380 if (SORT_CMP(dst
[j
], storage
[i
]) > 0)
383 dst
[k
] = storage
[i
--];
386 dst
[k
] = storage
[i
--];
393 static int TIM_SORT_COLLAPSE(SORT_TYPE
*dst
, TIM_SORT_RUN_T
*stack
, int stack_curr
, TEMP_STORAGE_T
*store
, const size_t size
)
398 /* if the stack only has one thing on it, we are done with the collapse */
399 if (stack_curr
<= 1) break;
400 /* if this is the last merge, just do it */
401 if ((stack_curr
== 2) &&
402 (stack
[0].length
+ stack
[1].length
== (int64_t) size
))
404 TIM_SORT_MERGE(dst
, stack
, stack_curr
, store
);
405 stack
[0].length
+= stack
[1].length
;
409 /* check if the invariant is off for a stack of 2 elements */
410 else if ((stack_curr
== 2) && (stack
[0].length
<= stack
[1].length
))
412 TIM_SORT_MERGE(dst
, stack
, stack_curr
, store
);
413 stack
[0].length
+= stack
[1].length
;
417 else if (stack_curr
== 2)
420 A
= stack
[stack_curr
- 3].length
;
421 B
= stack
[stack_curr
- 2].length
;
422 C
= stack
[stack_curr
- 1].length
;
424 /* check first invariant */
429 TIM_SORT_MERGE(dst
, stack
, stack_curr
- 1, store
);
430 stack
[stack_curr
- 3].length
+= stack
[stack_curr
- 2].length
;
431 stack
[stack_curr
- 2] = stack
[stack_curr
- 1];
436 TIM_SORT_MERGE(dst
, stack
, stack_curr
, store
);
437 stack
[stack_curr
- 2].length
+= stack
[stack_curr
- 1].length
;
441 /* check second invariant */
444 TIM_SORT_MERGE(dst
, stack
, stack_curr
, store
);
445 stack
[stack_curr
- 2].length
+= stack
[stack_curr
- 1].length
;
454 void TIM_SORT(SORT_TYPE
*dst
, const size_t size
)
457 TEMP_STORAGE_T _store
, *store
;
458 TIM_SORT_RUN_T run_stack
[128];
465 BINARY_INSERTION_SORT(dst
, size
);
469 /* compute the minimum run length */
470 minrun
= compute_minrun(size
);
472 /* temporary storage for merges */
475 store
->storage
= NULL
;
483 if (!CHECK_INVARIANT(run_stack
, stack_curr
))
485 stack_curr
= TIM_SORT_COLLAPSE(dst
, run_stack
, stack_curr
, store
, size
);
493 #undef SORT_MAKE_STR1
498 #undef TEMP_STORAGE_T
499 #undef TIM_SORT_RUN_T
503 #undef SORT_MAKE_STR1
505 #undef BINARY_INSERTION_FIND
506 #undef BINARY_INSERTION_SORT_START
507 #undef BINARY_INSERTION_SORT
508 #undef REVERSE_ELEMENTS
511 #undef TIM_SORT_RESIZE
512 #undef TIM_SORT_COLLAPSE
513 #undef TIM_SORT_RUN_T
514 #undef TEMP_STORAGE_T