| 511 | //------------------------------------------------------------------------ |
| 512 | template<class Cell> |
| 513 | void qsort_cells(Cell** start, unsigned num) |
| 514 | { |
| 515 | Cell** stack[80]; |
| 516 | Cell*** top; |
| 517 | Cell** limit; |
| 518 | Cell** base; |
| 519 | |
| 520 | limit = start + num; |
| 521 | base = start; |
| 522 | top = stack; |
| 523 | |
| 524 | for (;;) |
| 525 | { |
| 526 | int len = int(limit - base); |
| 527 | |
| 528 | Cell** i; |
| 529 | Cell** j; |
| 530 | Cell** pivot; |
| 531 | |
| 532 | if(len > qsort_threshold) |
| 533 | { |
| 534 | // we use base + len/2 as the pivot |
| 535 | pivot = base + len / 2; |
| 536 | swap_cells(base, pivot); |
| 537 | |
| 538 | i = base + 1; |
| 539 | j = limit - 1; |
| 540 | |
| 541 | // now ensure that *i <= *base <= *j |
| 542 | if((*j)->x < (*i)->x) |
| 543 | { |
| 544 | swap_cells(i, j); |
| 545 | } |
| 546 | |
| 547 | if((*base)->x < (*i)->x) |
| 548 | { |
| 549 | swap_cells(base, i); |
| 550 | } |
| 551 | |
| 552 | if((*j)->x < (*base)->x) |
| 553 | { |
| 554 | swap_cells(base, j); |
| 555 | } |
| 556 | |
| 557 | for(;;) |
| 558 | { |
| 559 | int x = (*base)->x; |
| 560 | do i++; while( (*i)->x < x ); |
| 561 | do j--; while( x < (*j)->x ); |
| 562 | |
| 563 | if(i > j) |
| 564 | { |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | swap_cells(i, j); |
| 569 | } |
| 570 |
no test coverage detected