Revert "[Static if] replace overload constraints with static if (mutation.d)"

This commit is contained in:
Sebastian Wilzbach 2017-05-19 12:03:54 +02:00 committed by GitHub
parent 46a3cda6d4
commit c6f694d633

View file

@ -368,9 +368,7 @@ See_Also:
$(HTTP sgi.com/tech/stl/_copy.html, STL's _copy)
*/
TargetRange copy(SourceRange, TargetRange)(SourceRange source, TargetRange target)
if (isInputRange!SourceRange)
{
static if (areCopyCompatibleArrays!(SourceRange, TargetRange))
if (areCopyCompatibleArrays!(SourceRange, TargetRange))
{
const tlen = target.length;
const slen = source.length;
@ -396,7 +394,12 @@ if (isInputRange!SourceRange)
return target[slen .. $];
}
}
else static if (isOutputRange!(TargetRange, ElementType!SourceRange))
/// ditto
TargetRange copy(SourceRange, TargetRange)(SourceRange source, TargetRange target)
if (!areCopyCompatibleArrays!(SourceRange, TargetRange) &&
isInputRange!SourceRange &&
isOutputRange!(TargetRange, ElementType!SourceRange))
{
// Specialize for 2 random access ranges.
// Typically 2 random access ranges are faster iterated by common
@ -418,13 +421,6 @@ if (isInputRange!SourceRange)
return target;
}
}
else
{
enum msg = "TargetRange is neither copy-compatible with the SourceRange" ~
"nor an OutputRange with the same ElementType.";
static assert(0, msg);
}
}
///
@safe unittest
@ -847,9 +843,7 @@ See_Also:
$(LREF uninitializeFill)
*/
void initializeAll(Range)(Range range)
if (isInputRange!Range)
{
static if (hasLvalueElements!Range && hasAssignableElements!Range)
if (isInputRange!Range && hasLvalueElements!Range && hasAssignableElements!Range)
{
import core.stdc.string : memset, memcpy;
import std.traits : hasElaborateAssign, isDynamicArray;
@ -895,13 +889,14 @@ if (isInputRange!Range)
else
fill(range, T.init);
}
else static if (is(Range == char[]) || is(Range == wchar[]))
/// ditto
void initializeAll(Range)(Range range)
if (is(Range == char[]) || is(Range == wchar[]))
{
alias T = ElementEncodingType!Range;
range[] = T.init;
}
else static assert(0, "Range doesn't have assignable elements.");
}
///
@system unittest
@ -1750,12 +1745,11 @@ Returns:
Range remove
(SwapStrategy s = SwapStrategy.stable, Range, Offset...)
(Range range, Offset offset)
if (isBidirectionalRange!Range
if (s != SwapStrategy.stable
&& isBidirectionalRange!Range
&& hasLvalueElements!Range
&& hasLength!Range
&& Offset.length >= 1)
{
static if (s == SwapStrategy.unstable)
{
Tuple!(size_t, "pos", size_t, "len")[offset.length] blackouts;
foreach (i, v; offset)
@ -1833,7 +1827,15 @@ if (isBidirectionalRange!Range
return range;
}
else static if (s == SwapStrategy.stable)
/// Ditto
Range remove
(SwapStrategy s = SwapStrategy.stable, Range, Offset...)
(Range range, Offset offset)
if (s == SwapStrategy.stable
&& isBidirectionalRange!Range
&& hasLvalueElements!Range
&& Offset.length >= 1)
{
auto result = range;
auto src = range, tgt = range;
@ -1876,8 +1878,6 @@ if (isBidirectionalRange!Range
moveAll(src, tgt);
return result;
}
else static assert(0, "SwapStrategy.semistable is not supported.");
}
///
@safe pure unittest
@ -2132,36 +2132,16 @@ if (isBidirectionalRange!Range
/**
Reverses $(D r) in-place. Performs $(D r.length / 2) evaluations of $(D
swap).
UTF sequences consisting of multiple code units are preserved properly.
Params:
r = a $(REF_ALTTEXT bidirectional range, isBidirectionalRange, std,range,primitives)
with swappable elements, a random access range with a length member or a
narrow string.
with swappable elements or a random access range with a length member
See_Also:
$(HTTP sgi.com/tech/stl/_reverse.html, STL's _reverse), $(REF retro, std,range) for a lazy reversed range view
Bugs:
When passing a sting with unicode modifiers on characters, such as $(D \u0301),
this function will not properly keep the position of the modifier. For example,
reversing $(D ba\u0301d) ("bád") will result in d\u0301ab ("d́ab") instead of
$(D da\u0301b) ("dáb").
*/
void reverse(Range)(Range r)
if (isBidirectionalRange!Range)
{
static if (isRandomAccessRange!Range)
{
//swapAt is in fact the only way to swap non lvalue ranges
immutable last = r.length-1;
immutable steps = r.length/2;
for (size_t i = 0; i < steps; i++)
{
r.swapAt(i, last-i);
}
}
else static if (hasSwappableElements!Range)
if (isBidirectionalRange!Range && !isRandomAccessRange!Range
&& hasSwappableElements!Range)
{
while (!r.empty)
{
@ -2171,29 +2151,6 @@ if (isBidirectionalRange!Range)
r.popBack();
}
}
else static if (isNarrowString!Range && !is(ElementType!Range == const) && !is(ElementType!Range == immutable))
{
import std.string : representation;
import std.utf : stride;
auto repr = representation(r);
for (size_t i = 0; i < r.length; )
{
immutable step = stride(r, i);
if (step > 1)
{
.reverse(repr[i .. i + step]);
i += step;
}
else
{
++i;
}
}
reverse(repr);
}
else static assert(0, "Range is neither RandomAccess, has swappable elements nor a narrow string.");
}
///
@safe unittest
@ -2203,12 +2160,17 @@ if (isBidirectionalRange!Range)
assert(arr == [ 3, 2, 1 ]);
}
///
@safe unittest
///ditto
void reverse(Range)(Range r)
if (isRandomAccessRange!Range && hasLength!Range)
{
char[] arr = "hello\U00010143\u0100\U00010143".dup;
reverse(arr);
assert(arr == "\U00010143\u0100\U00010143olleh");
//swapAt is in fact the only way to swap non lvalue ranges
immutable last = r.length-1;
immutable steps = r.length/2;
for (size_t i = 0; i < steps; i++)
{
r.swapAt(i, last-i);
}
}
@safe unittest
@ -2226,6 +2188,51 @@ if (isBidirectionalRange!Range)
assert(range == [3, 2, 1]);
}
/**
Reverses $(D r) in-place, where $(D r) is a narrow string (having
elements of type $(D char) or $(D wchar)). UTF sequences consisting of
multiple code units are preserved properly.
Params:
s = a narrow string
Bugs:
When passing a sting with unicode modifiers on characters, such as $(D \u0301),
this function will not properly keep the position of the modifier. For example,
reversing $(D ba\u0301d) ("bád") will result in d\u0301ab ("d́ab") instead of
$(D da\u0301b) ("dáb").
*/
void reverse(Char)(Char[] s)
if (isNarrowString!(Char[]) && !is(Char == const) && !is(Char == immutable))
{
import std.string : representation;
import std.utf : stride;
auto r = representation(s);
for (size_t i = 0; i < s.length; )
{
immutable step = stride(s, i);
if (step > 1)
{
.reverse(r[i .. i + step]);
i += step;
}
else
{
++i;
}
}
reverse(r);
}
///
@safe unittest
{
char[] arr = "hello\U00010143\u0100\U00010143".dup;
reverse(arr);
assert(arr == "\U00010143\u0100\U00010143olleh");
}
@safe unittest
{
void test(string a, string b)