EASY: factor implementations of range length into a mixin template

This commit is contained in:
Andrei Alexandrescu 2020-09-19 12:38:07 -04:00 committed by The Dlang Bot
parent 311f00f8ac
commit 71ff9d79c5
3 changed files with 30 additions and 92 deletions

View file

@ -371,10 +371,7 @@ private struct _Cache(R, bool bidir)
return source.empty; return source.empty;
} }
static if (hasLength!R) auto length() @property mixin ImplementLength!source;
{
return source.length;
}
E front() @property E front() @property
{ {
@ -641,15 +638,7 @@ private struct MapResult(alias fun, Range)
} }
} }
static if (hasLength!R) mixin ImplementLength!_input;
{
@property auto length()
{
return _input.length;
}
alias opDollar = length;
}
static if (hasSlicing!R) static if (hasSlicing!R)
{ {
@ -4373,13 +4362,7 @@ if (fun.length >= 1)
} }
} }
static if (hasLength!R) mixin ImplementLength!source;
{
@property size_t length()
{
return source.length;
}
}
} }
return Result(range, args); return Result(range, args);

View file

@ -349,15 +349,7 @@ if (isBidirectionalRange!(Unqual!Range))
} }
} }
static if (hasLength!R) mixin ImplementLength!source;
{
@property auto length()
{
return source.length;
}
alias opDollar = length;
}
} }
return Result!()(r); return Result!()(r);
@ -7075,16 +7067,7 @@ struct FrontTransversal(Ror,
_input[n].front = val; _input[n].front = val;
} }
} }
/// Ditto mixin ImplementLength!_input;
static if (hasLength!RangeOfRanges)
{
@property size_t length()
{
return _input.length;
}
alias opDollar = length;
}
/** /**
Slicing if offered if `RangeOfRanges` supports slicing and all the Slicing if offered if `RangeOfRanges` supports slicing and all the
@ -7414,16 +7397,7 @@ struct Transversal(Ror,
} }
} }
/// Ditto mixin ImplementLength!_input;
static if (hasLength!RangeOfRanges)
{
@property size_t length()
{
return _input.length;
}
alias opDollar = length;
}
/** /**
Slicing if offered if `RangeOfRanges` supports slicing and all the Slicing if offered if `RangeOfRanges` supports slicing and all the
@ -7853,16 +7827,7 @@ if (isRandomAccessRange!Source && isInputRange!Indices &&
} }
} }
static if (hasLength!Indices) mixin ImplementLength!_indices;
{
/// Ditto
@property size_t length()
{
return _indices.length;
}
alias opDollar = length;
}
static if (isRandomAccessRange!Indices) static if (isRandomAccessRange!Indices)
{ {
@ -10304,12 +10269,7 @@ do
static if (hasLength!Range) static if (hasLength!Range)
{ {
size_t length() @property mixin ImplementLength!range;
{
return range.length;
}
alias opDollar = length;
static if (isBidirectionalRange!Range) static if (isBidirectionalRange!Range)
{ {
@ -10852,15 +10812,7 @@ if (isInputRange!Range && !isInstanceOf!(SortedRange, Range))
return result; return result;
} }
/// Ditto mixin ImplementLength!_input;
static if (hasLength!Range)
{
@property size_t length() //const
{
return _input.length;
}
alias opDollar = length;
}
/** /**
Releases the controlled range and returns it. Releases the controlled range and returns it.
@ -11769,15 +11721,11 @@ public:
version (StdDdoc) version (StdDdoc)
{ {
/++ /// Only defined if `hasLength!R` is `true`.
Only defined if `hasLength!R` is `true`. @property size_t length();
+/ /// ditto
@property auto length() {assert(0);} @property size_t length() const;
/// Ditto
/++ Ditto +/
@property auto length() const {assert(0);}
/++ Ditto +/
alias opDollar = length; alias opDollar = length;
} }
else static if (hasLength!R) else static if (hasLength!R)
@ -11786,12 +11734,10 @@ public:
{ {
return (*_range).length; return (*_range).length;
} }
static if (is(typeof((*cast(const R*)_range).length))) @property auto length() const static if (is(typeof((*cast(const R*)_range).length))) @property auto length() const
{ {
return (*_range).length; return (*_range).length;
} }
alias opDollar = length; alias opDollar = length;
} }
@ -12843,13 +12789,7 @@ if (isInputRange!R1 && isOutputRange!(R2, ElementType!R1))
private bool _frontAccessed; private bool _frontAccessed;
} }
static if (hasLength!R1) mixin ImplementLength!_input;
{
@property auto length()
{
return _input.length;
}
}
static if (isInfinite!R1) static if (isInfinite!R1)
{ {

View file

@ -2509,3 +2509,18 @@ if (isAutodecodableString!(T[]))
size_t i = a.length - strideBack(a, a.length); size_t i = a.length - strideBack(a, a.length);
return decode(a, i); return decode(a, i);
} }
/*
Implements `length` for a range by forwarding it to `member`.
*/
package(std) mixin template ImplementLength(alias member)
{
static if (hasLength!(typeof(member)))
{
@property auto length()
{
return member.length;
}
alias opDollar = length;
}
}