| OLD | NEW |
| 1 /* | 1 /* |
| 2 * taken from https://github.com/swenson/sort | 2 * taken from https://github.com/swenson/sort |
| 3 * Kept as is for the moment to be able to apply upstream patches for that | 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 | 4 * code, currently used only to speed up XPath node sorting, see xpath.c |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * All code in this header, unless otherwise specified, is hereby licensed under
the MIT Public License: | 8 * All code in this header, unless otherwise specified, is hereby licensed under
the MIT Public License: |
| 9 | 9 |
| 10 Copyright (c) 2010 Christopher Swenson | 10 Copyright (c) 2010 Christopher Swenson |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 } TEMP_STORAGE_T; | 316 } TEMP_STORAGE_T; |
| 317 | 317 |
| 318 | 318 |
| 319 static void TIM_SORT_RESIZE(TEMP_STORAGE_T *store, const size_t new_size) | 319 static void TIM_SORT_RESIZE(TEMP_STORAGE_T *store, const size_t new_size) |
| 320 { | 320 { |
| 321 if (store->alloc < new_size) | 321 if (store->alloc < new_size) |
| 322 { | 322 { |
| 323 SORT_TYPE *tempstore = (SORT_TYPE *)realloc(store->storage, new_size * sizeo
f(SORT_TYPE)); | 323 SORT_TYPE *tempstore = (SORT_TYPE *)realloc(store->storage, new_size * sizeo
f(SORT_TYPE)); |
| 324 if (tempstore == NULL) | 324 if (tempstore == NULL) |
| 325 { | 325 { |
| 326 fprintf(stderr, "Error allocating temporary storage for tim sort: need %ll
u bytes", (unsigned long long)(sizeof(SORT_TYPE) * new_size)); | 326 fprintf(stderr, "Error allocating temporary storage for tim sort: need %lu
bytes", (unsigned long) (sizeof(SORT_TYPE) * new_size)); |
| 327 exit(1); | 327 exit(1); |
| 328 } | 328 } |
| 329 store->storage = tempstore; | 329 store->storage = tempstore; |
| 330 store->alloc = new_size; | 330 store->alloc = new_size; |
| 331 } | 331 } |
| 332 } | 332 } |
| 333 | 333 |
| 334 static void TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const in
t stack_curr, TEMP_STORAGE_T *store) | 334 static void TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const in
t stack_curr, TEMP_STORAGE_T *store) |
| 335 { | 335 { |
| 336 const int64_t A = stack[stack_curr - 2].length; | 336 const int64_t A = stack[stack_curr - 2].length; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 else | 387 else |
| 388 dst[k] = dst[j--]; | 388 dst[k] = dst[j--]; |
| 389 } | 389 } |
| 390 } | 390 } |
| 391 } | 391 } |
| 392 | 392 |
| 393 static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_cu
rr, TEMP_STORAGE_T *store, const size_t size) | 393 static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_cu
rr, TEMP_STORAGE_T *store, const size_t size) |
| 394 { | 394 { |
| 395 while (1) { | 395 while (1) { |
| 396 int64_t A, B, C, D; | 396 int64_t A, B, C, D; |
| 397 int ABC, BCD, BD, CD; | 397 int ABC, BCD, CD; |
| 398 | 398 |
| 399 /* if the stack only has one thing on it, we are done with the collapse */ | 399 /* if the stack only has one thing on it, we are done with the collapse */ |
| 400 if (stack_curr <= 1) { | 400 if (stack_curr <= 1) { |
| 401 break; | 401 break; |
| 402 } | 402 } |
| 403 | 403 |
| 404 /* if this is the last merge, just do it */ | 404 /* if this is the last merge, just do it */ |
| 405 if ((stack_curr == 2) && (stack[0].length + stack[1].length == size)) { | 405 if ((stack_curr == 2) && (stack[0].length + stack[1].length == size)) { |
| 406 TIM_SORT_MERGE(dst, stack, stack_curr, store); | 406 TIM_SORT_MERGE(dst, stack, stack_curr, store); |
| 407 stack[0].length += stack[1].length; | 407 stack[0].length += stack[1].length; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 424 | 424 |
| 425 if (stack_curr >= 4) { | 425 if (stack_curr >= 4) { |
| 426 A = stack[stack_curr - 4].length; | 426 A = stack[stack_curr - 4].length; |
| 427 ABC = (A <= B + C); | 427 ABC = (A <= B + C); |
| 428 } else { | 428 } else { |
| 429 ABC = 0; | 429 ABC = 0; |
| 430 } | 430 } |
| 431 | 431 |
| 432 BCD = (B <= C + D) || ABC; | 432 BCD = (B <= C + D) || ABC; |
| 433 CD = (C <= D); | 433 CD = (C <= D); |
| 434 BD = (B < D); | |
| 435 | 434 |
| 436 /* Both invariants are good */ | 435 /* Both invariants are good */ |
| 437 if (!BCD && !CD) { | 436 if (!BCD && !CD) { |
| 438 break; | 437 break; |
| 439 } | 438 } |
| 440 | 439 |
| 441 /* left merge */ | 440 /* left merge */ |
| 442 if (BCD && !CD) { | 441 if (BCD && !CD) { |
| 443 TIM_SORT_MERGE(dst, stack, stack_curr - 1, store); | 442 TIM_SORT_MERGE(dst, stack, stack_curr - 1, store); |
| 444 stack[stack_curr - 3].length += stack[stack_curr - 2].length; | 443 stack[stack_curr - 3].length += stack[stack_curr - 2].length; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 #undef BINARY_INSERTION_FIND | 508 #undef BINARY_INSERTION_FIND |
| 510 #undef BINARY_INSERTION_SORT_START | 509 #undef BINARY_INSERTION_SORT_START |
| 511 #undef BINARY_INSERTION_SORT | 510 #undef BINARY_INSERTION_SORT |
| 512 #undef REVERSE_ELEMENTS | 511 #undef REVERSE_ELEMENTS |
| 513 #undef COUNT_RUN | 512 #undef COUNT_RUN |
| 514 #undef TIM_SORT | 513 #undef TIM_SORT |
| 515 #undef TIM_SORT_RESIZE | 514 #undef TIM_SORT_RESIZE |
| 516 #undef TIM_SORT_COLLAPSE | 515 #undef TIM_SORT_COLLAPSE |
| 517 #undef TIM_SORT_RUN_T | 516 #undef TIM_SORT_RUN_T |
| 518 #undef TEMP_STORAGE_T | 517 #undef TEMP_STORAGE_T |
| OLD | NEW |