| 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 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 else if (i >= 0) | 385 else if (i >= 0) |
| 386 dst[k] = storage[i--]; | 386 dst[k] = storage[i--]; |
| 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 { | 396 int64_t A, B, C, D; |
| 397 int64_t A, B, C; | 397 int ABC, BCD, BD, CD; |
| 398 |
| 398 /* 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 */ |
| 399 if (stack_curr <= 1) break; | 400 if (stack_curr <= 1) { |
| 401 break; |
| 402 } |
| 403 |
| 400 /* if this is the last merge, just do it */ | 404 /* if this is the last merge, just do it */ |
| 401 if ((stack_curr == 2) && | 405 if ((stack_curr == 2) && (stack[0].length + stack[1].length == size)) { |
| 402 (stack[0].length + stack[1].length == (int64_t) size)) | |
| 403 { | |
| 404 TIM_SORT_MERGE(dst, stack, stack_curr, store); | 406 TIM_SORT_MERGE(dst, stack, stack_curr, store); |
| 405 stack[0].length += stack[1].length; | 407 stack[0].length += stack[1].length; |
| 406 stack_curr--; | 408 stack_curr--; |
| 407 break; | 409 break; |
| 408 } | 410 } |
| 409 /* check if the invariant is off for a stack of 2 elements */ | 411 /* 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 else if ((stack_curr == 2) && (stack[0].length <= stack[1].length)) { |
| 411 { | |
| 412 TIM_SORT_MERGE(dst, stack, stack_curr, store); | 413 TIM_SORT_MERGE(dst, stack, stack_curr, store); |
| 413 stack[0].length += stack[1].length; | 414 stack[0].length += stack[1].length; |
| 414 stack_curr--; | 415 stack_curr--; |
| 415 break; | 416 break; |
| 417 } else if (stack_curr == 2) { |
| 418 break; |
| 416 } | 419 } |
| 417 else if (stack_curr == 2) | 420 |
| 421 B = stack[stack_curr - 3].length; |
| 422 C = stack[stack_curr - 2].length; |
| 423 D = stack[stack_curr - 1].length; |
| 424 |
| 425 if (stack_curr >= 4) { |
| 426 A = stack[stack_curr - 4].length; |
| 427 ABC = (A <= B + C); |
| 428 } else { |
| 429 ABC = 0; |
| 430 } |
| 431 |
| 432 BCD = (B <= C + D) || ABC; |
| 433 CD = (C <= D); |
| 434 BD = (B < D); |
| 435 |
| 436 /* Both invariants are good */ |
| 437 if (!BCD && !CD) { |
| 418 break; | 438 break; |
| 439 } |
| 419 | 440 |
| 420 A = stack[stack_curr - 3].length; | 441 /* left merge */ |
| 421 B = stack[stack_curr - 2].length; | 442 if (BCD && !CD) { |
| 422 C = stack[stack_curr - 1].length; | 443 TIM_SORT_MERGE(dst, stack, stack_curr - 1, store); |
| 423 | 444 stack[stack_curr - 3].length += stack[stack_curr - 2].length; |
| 424 /* check first invariant */ | 445 stack[stack_curr - 2] = stack[stack_curr - 1]; |
| 425 if (A <= B + C) | 446 stack_curr--; |
| 426 { | 447 } else { |
| 427 if (A < C) | 448 /* right merge */ |
| 428 { | |
| 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]; | |
| 432 stack_curr--; | |
| 433 } | |
| 434 else | |
| 435 { | |
| 436 TIM_SORT_MERGE(dst, stack, stack_curr, store); | |
| 437 stack[stack_curr - 2].length += stack[stack_curr - 1].length; | |
| 438 stack_curr--; | |
| 439 } | |
| 440 } | |
| 441 /* check second invariant */ | |
| 442 else if (B <= C) | |
| 443 { | |
| 444 TIM_SORT_MERGE(dst, stack, stack_curr, store); | 449 TIM_SORT_MERGE(dst, stack, stack_curr, store); |
| 445 stack[stack_curr - 2].length += stack[stack_curr - 1].length; | 450 stack[stack_curr - 2].length += stack[stack_curr - 1].length; |
| 446 stack_curr--; | 451 stack_curr--; |
| 447 } | 452 } |
| 448 else | |
| 449 break; | |
| 450 } | 453 } |
| 454 |
| 451 return stack_curr; | 455 return stack_curr; |
| 452 } | 456 } |
| 453 | 457 |
| 454 void TIM_SORT(SORT_TYPE *dst, const size_t size) | 458 void TIM_SORT(SORT_TYPE *dst, const size_t size) |
| 455 { | 459 { |
| 456 int minrun; | 460 int minrun; |
| 457 TEMP_STORAGE_T _store, *store; | 461 TEMP_STORAGE_T _store, *store; |
| 458 TIM_SORT_RUN_T run_stack[128]; | 462 TIM_SORT_RUN_T run_stack[128]; |
| 459 int stack_curr = 0; | 463 int stack_curr = 0; |
| 460 int64_t len, run; | 464 int64_t len, run; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 #undef BINARY_INSERTION_FIND | 509 #undef BINARY_INSERTION_FIND |
| 506 #undef BINARY_INSERTION_SORT_START | 510 #undef BINARY_INSERTION_SORT_START |
| 507 #undef BINARY_INSERTION_SORT | 511 #undef BINARY_INSERTION_SORT |
| 508 #undef REVERSE_ELEMENTS | 512 #undef REVERSE_ELEMENTS |
| 509 #undef COUNT_RUN | 513 #undef COUNT_RUN |
| 510 #undef TIM_SORT | 514 #undef TIM_SORT |
| 511 #undef TIM_SORT_RESIZE | 515 #undef TIM_SORT_RESIZE |
| 512 #undef TIM_SORT_COLLAPSE | 516 #undef TIM_SORT_COLLAPSE |
| 513 #undef TIM_SORT_RUN_T | 517 #undef TIM_SORT_RUN_T |
| 514 #undef TEMP_STORAGE_T | 518 #undef TEMP_STORAGE_T |
| OLD | NEW |