mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 22:50:38 +03:00
Added strictlyOrdered
This commit is contained in:
parent
5ca24b8392
commit
d54047a6a0
1 changed files with 31 additions and 3 deletions
|
@ -198,15 +198,21 @@ Like $(D isSorted), returns $(D true) if the given $(D values) are ordered
|
||||||
according to the comparison operation $(D less). Unlike $(D isSorted), takes values
|
according to the comparison operation $(D less). Unlike $(D isSorted), takes values
|
||||||
directly instead of structured in a range.
|
directly instead of structured in a range.
|
||||||
|
|
||||||
The predicate must be a strict ordering just like with $(D isSorted). For
|
$(D ordered) allows repeated values, e.g. $(D ordered(1, 1, 2)) is $(D true). To verify
|
||||||
example, $(D "a <= b") is incorrect and will cause failed assertions.
|
that the values are ordered strictly monotonically, use $(D strictlyOrdered);
|
||||||
|
$(D strictlyOrdered(1, 1, 2)) is $(D false).
|
||||||
|
|
||||||
|
With either function, the predicate must be a strict ordering just like with $(D isSorted). For
|
||||||
|
example, using $(D "a <= b") instead of $(D "a < b") is incorrect and will cause failed
|
||||||
|
assertions.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
values = The tested value
|
values = The tested value
|
||||||
less = The comparison predicate
|
less = The comparison predicate
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
$(D true) if the values are ordered.
|
$(D true) if the values are ordered; $(D ordered) allows for duplicates,
|
||||||
|
$(D strictlyOrdered) does not.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool ordered(alias less = "a < b", T...)(T values)
|
bool ordered(alias less = "a < b", T...)(T values)
|
||||||
|
@ -228,15 +234,37 @@ if ((T.length == 2 && is(typeof(binaryFun!less(values[1], values[0])) : bool))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ditto
|
||||||
|
bool strictlyOrdered(alias less = "a < b", T...)(T values)
|
||||||
|
if (is(typeof(ordered!less(values))))
|
||||||
|
{
|
||||||
|
foreach (i, _; T[0 .. $ - 1])
|
||||||
|
{
|
||||||
|
if (!binaryFun!less(values[i], values[i + 1]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
assert(!binaryFun!less(values[i + 1], values[i]),
|
||||||
|
__FUNCTION__ ~ ": incorrect non-strict predicate.");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
assert(ordered(42, 42, 43));
|
assert(ordered(42, 42, 43));
|
||||||
|
assert(!strictlyOrdered(43, 42, 45));
|
||||||
|
assert(ordered(42, 42, 43));
|
||||||
|
assert(!strictlyOrdered(42, 42, 43));
|
||||||
assert(!ordered(43, 42, 45));
|
assert(!ordered(43, 42, 45));
|
||||||
// Ordered lexicographically
|
// Ordered lexicographically
|
||||||
assert(ordered("Jane", "Jim", "Joe"));
|
assert(ordered("Jane", "Jim", "Joe"));
|
||||||
|
assert(strictlyOrdered("Jane", "Jim", "Joe"));
|
||||||
// Incidentally also ordered by length decreasing
|
// Incidentally also ordered by length decreasing
|
||||||
assert(ordered!((a, b) => a.length > b.length)("Jane", "Jim", "Joe"));
|
assert(ordered!((a, b) => a.length > b.length)("Jane", "Jim", "Joe"));
|
||||||
|
// ... but not strictly so: "Jim" and "Joe" have the same length
|
||||||
|
assert(!strictlyOrdered!((a, b) => a.length > b.length)("Jane", "Jim", "Joe"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// partition
|
// partition
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue