mirror of
https://github.com/dlang/phobos.git
synced 2025-05-06 19:16:13 +03:00
Add specialized version of optimisticInsertionSort to use swaps, for ranges which don't support assignable elements.
This commit is contained in:
parent
82fef70135
commit
5f3b5b25d3
1 changed files with 19 additions and 0 deletions
|
@ -9223,6 +9223,7 @@ private size_t getPivot(alias less, Range)(Range r)
|
|||
}
|
||||
|
||||
private void optimisticInsertionSort(alias less, Range)(Range r)
|
||||
if(hasAssignableElements!Range)
|
||||
{
|
||||
alias binaryFun!(less) pred;
|
||||
if (r.length < 2) {
|
||||
|
@ -9242,6 +9243,24 @@ private void optimisticInsertionSort(alias less, Range)(Range r)
|
|||
}
|
||||
}
|
||||
|
||||
private void optimisticInsertionSort(alias less, Range)(Range r)
|
||||
if(!hasAssignableElements!Range)
|
||||
{
|
||||
alias binaryFun!(less) pred;
|
||||
if (r.length < 2) {
|
||||
return ;
|
||||
}
|
||||
|
||||
immutable maxJ = r.length - 1;
|
||||
for (size_t i = r.length - 2; i != size_t.max; --i)
|
||||
{
|
||||
for (size_t j = i; j < maxJ && pred(r[j + 1], r[j]); ++j)
|
||||
{
|
||||
swapAt(r, j, j + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
debug(std_algorithm) scope(success)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue