mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 07:00:37 +03:00
Review
This commit is contained in:
parent
dcd00a7609
commit
15ee49f84f
1 changed files with 32 additions and 22 deletions
|
@ -2603,7 +2603,6 @@ schwartzSort(alias transform, alias less = "a < b",
|
||||||
foreach (ref p; probs)
|
foreach (ref p; probs)
|
||||||
{
|
{
|
||||||
if (!p) continue;
|
if (!p) continue;
|
||||||
//enforce(p > 0 && p <= 1, "Wrong probability passed to entropy");
|
|
||||||
result -= p * log2(p);
|
result -= p * log2(p);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -2637,7 +2636,6 @@ schwartzSort(alias transform, alias less = "a < b",
|
||||||
foreach (ref p; probs)
|
foreach (ref p; probs)
|
||||||
{
|
{
|
||||||
if (!p) continue;
|
if (!p) continue;
|
||||||
//enforce(p > 0 && p <= 1, "Wrong probability passed to entropy");
|
|
||||||
result -= p * log2(p);
|
result -= p * log2(p);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -3146,15 +3144,16 @@ void medianOf(
|
||||||
alias less = "a < b",
|
alias less = "a < b",
|
||||||
Flag!"leanRight" flag = No.leanRight,
|
Flag!"leanRight" flag = No.leanRight,
|
||||||
Range,
|
Range,
|
||||||
Index...)
|
Indexes...)
|
||||||
(Range r, Index i)
|
(Range r, Indexes i)
|
||||||
if (isRandomAccessRange!Range && Index.length >= 2 && Index.length <= 5 &&
|
if (isRandomAccessRange!Range && hasLength!Range &&
|
||||||
allSatisfy!(isIntegral, Index))
|
Indexes.length >= 2 && Indexes.length <= 5 &&
|
||||||
|
allSatisfy!(isIntegral, Indexes))
|
||||||
{
|
{
|
||||||
assert(r.length >= Index.length);
|
assert(r.length >= Indexes.length);
|
||||||
import std.functional : binaryFun;
|
import std.functional : binaryFun;
|
||||||
alias lt = binaryFun!less;
|
alias lt = binaryFun!less;
|
||||||
enum k = Index.length;
|
enum k = Indexes.length;
|
||||||
import std.algorithm : swapAt;
|
import std.algorithm : swapAt;
|
||||||
|
|
||||||
alias a = i[0];
|
alias a = i[0];
|
||||||
|
@ -3263,20 +3262,31 @@ if (isRandomAccessRange!Range && Index.length >= 2 && Index.length <= 5 &&
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import std.random : uniform;
|
// Verify medianOf for all permutations of [1, 2, 2, 3, 4].
|
||||||
import std.algorithm.iteration : map;
|
int[5] data = [1, 2, 2, 3, 4];
|
||||||
auto a = iota(0, 18).map!(_ => uniform(-10, 10)).array;
|
do
|
||||||
medianOf(a, 0, 1);
|
{
|
||||||
assert(a[0] <= a[1]);
|
int[5] a = data;
|
||||||
medianOf(a, 2, 3, 4);
|
medianOf(a[], 0, 1);
|
||||||
assert(a[2] <= a[3] && a[3] <= a[4]);
|
assert(a[0] <= a[1]);
|
||||||
medianOf(a, 5, 6, 7, 8);
|
|
||||||
assert(a[5] <= a[6] && a[6] <= a[7] && a[6] <= a[8]);
|
a[] = data[];
|
||||||
medianOf!("a < b", Yes.leanRight)(a, 9, 10, 11, 12);
|
medianOf(a[], 0, 1, 2);
|
||||||
assert(a[9] <= a[11] && a[10] <= a[11] && a[11] <= a[12]);
|
assert(ordered(a[0], a[1], a[2]));
|
||||||
medianOf(a, 13, 14, 15, 16, 17);
|
|
||||||
assert(a[13] <= a[15] && a[14] <= a[15] && a[15] <= a[16] &&
|
a[] = data[];
|
||||||
a[15] <= a[17]);
|
medianOf(a[], 0, 1, 2, 3);
|
||||||
|
assert(a[0] <= a[1] && a[1] <= a[2] && a[1] <= a[3]);
|
||||||
|
|
||||||
|
a[] = data[];
|
||||||
|
medianOf!("a < b", Yes.leanRight)(a[], 0, 1, 2, 3);
|
||||||
|
assert(a[0] <= a[2] && a[1] <= a[2] && a[2] <= a[3]);
|
||||||
|
|
||||||
|
a[] = data[];
|
||||||
|
medianOf(a[], 0, 1, 2, 3, 4);
|
||||||
|
assert(a[0] <= a[2] && a[1] <= a[2] && a[2] <= a[3] && a[2] <= a[4]);
|
||||||
|
}
|
||||||
|
while (nextPermutation(data[]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// nextPermutation
|
// nextPermutation
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue