Close issue 212

Introduce Range and constRange aliases for containers.
This commit is contained in:
Eugen Wissner 2017-05-29 11:26:39 +02:00
parent 25791775e6
commit 8ee1d647ce
3 changed files with 59 additions and 35 deletions

View File

@ -174,6 +174,12 @@ struct Range(E)
*/
struct Array(T)
{
/// The range types for $(D_PSYMBOL Array).
alias Range = .Range!T;
/// Ditto.
alias ConstRange = .Range!(const T);
private size_t length_;
private T* data;
private size_t capacity_;
@ -638,7 +644,7 @@ struct Array(T)
*
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
*/
Range!T remove(Range!T r) @trusted
Range remove(Range r) @trusted
in
{
assert(r.container is &this);
@ -648,9 +654,9 @@ struct Array(T)
body
{
auto end = this.data + this.length;
moveAll(Range!T(this, r.end, end), Range!T(this, r.begin, end));
moveAll(.Range!T(this, r.end, end), .Range!T(this, r.begin, end));
length = length - r.length;
return Range!T(this, r.begin, this.data + length);
return .Range!T(this, r.begin, this.data + length);
}
///
@ -788,7 +794,7 @@ struct Array(T)
*
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
*/
size_t insertAfter(R)(Range!T r, R el)
size_t insertAfter(R)(Range r, R el)
if (!isInfinite!R
&& isInputRange!R
&& isImplicitlyConvertible!(ElementType!R, T))
@ -808,7 +814,7 @@ struct Array(T)
}
/// Ditto.
size_t insertAfter(size_t R)(Range!T r, T[R] el)
size_t insertAfter(size_t R)(Range r, T[R] el)
in
{
assert(r.container is &this);
@ -821,7 +827,7 @@ struct Array(T)
}
/// Ditto.
size_t insertAfter(R)(Range!T r, auto ref R el)
size_t insertAfter(R)(Range r, auto ref R el)
if (isImplicitlyConvertible!(R, T))
in
{
@ -848,7 +854,7 @@ struct Array(T)
}
/// Ditto.
size_t insertBefore(R)(Range!T r, R el)
size_t insertBefore(R)(Range r, R el)
if (!isInfinite!R
&& isInputRange!R
&& isImplicitlyConvertible!(ElementType!R, T))
@ -860,11 +866,11 @@ struct Array(T)
}
body
{
return insertAfter(Range!T(this, this.data, r.begin), el);
return insertAfter(.Range!T(this, this.data, r.begin), el);
}
/// Ditto.
size_t insertBefore(size_t R)(Range!T r, T[R] el)
size_t insertBefore(size_t R)(Range r, T[R] el)
in
{
assert(r.container is &this);
@ -877,7 +883,7 @@ struct Array(T)
}
/// Ditto.
size_t insertBefore(R)(Range!T r, auto ref R el)
size_t insertBefore(R)(Range r, auto ref R el)
if (isImplicitlyConvertible!(R, T))
in
{
@ -993,7 +999,7 @@ struct Array(T)
}
/// Ditto.
Range!T opIndexAssign(E : T)(auto ref E value)
Range opIndexAssign(E : T)(auto ref E value)
{
return opSliceAssign(value, 0, length);
}
@ -1017,13 +1023,13 @@ struct Array(T)
*
* Precondition: $(D_INLINECODE length == value.length).
*/
Range!T opIndexAssign(size_t R)(T[R] value)
Range opIndexAssign(size_t R)(T[R] value)
{
return opSliceAssign!R(value, 0, length);
}
/// Ditto.
Range!T opIndexAssign(Range!T value)
Range opIndexAssign(Range value)
{
return opSliceAssign(value, 0, length);
}
@ -1066,13 +1072,13 @@ struct Array(T)
* Returns: Random access range that iterates over elements of the array,
* in forward order.
*/
Range!T opIndex() @trusted
Range opIndex() @trusted
{
return typeof(return)(this, this.data, this.data + length);
}
/// Ditto.
Range!(const T) opIndex() const @trusted
ConstRange opIndex() const @trusted
{
return typeof(return)(this, this.data, this.data + length);
}
@ -1105,13 +1111,13 @@ struct Array(T)
}
/// Ditto.
bool opEquals()(const auto ref typeof(this) that) const @trusted
bool opEquals()(auto ref const typeof(this) that) const @trusted
{
return equal(this.data[0 .. length], that.data[0 .. that.length]);
}
/// Ditto.
bool opEquals(Range!T that)
bool opEquals(Range that)
{
return equal(opIndex(), that);
}
@ -1126,8 +1132,8 @@ struct Array(T)
* Returns: $(D_KEYWORD true) if the array and the range are equal,
* $(D_KEYWORD false) otherwise.
*/
bool opEquals(R)(Range!R that) const
if (is(Unqual!R == T))
bool opEquals(R)(R that) const
if (is(R == Range) || is(R == ConstRange))
{
return equal(opIndex(), that);
}
@ -1216,7 +1222,7 @@ struct Array(T)
*
* Precondition: $(D_INLINECODE i <= j && j <= length).
*/
Range!T opSlice(const size_t i, const size_t j) @trusted
Range opSlice(const size_t i, const size_t j) @trusted
in
{
assert(i <= j);
@ -1228,7 +1234,7 @@ struct Array(T)
}
/// Ditto.
Range!(const T) opSlice(const size_t i, const size_t j) const @trusted
ConstRange opSlice(const size_t i, const size_t j) const @trusted
in
{
assert(i <= j);
@ -1298,7 +1304,7 @@ struct Array(T)
* Precondition: $(D_INLINECODE i <= j && j <= length
* && value.length == j - i)
*/
Range!T opSliceAssign(size_t R)(T[R] value, const size_t i, const size_t j)
Range opSliceAssign(size_t R)(T[R] value, const size_t i, const size_t j)
@trusted
in
{
@ -1312,7 +1318,7 @@ struct Array(T)
}
/// Ditto.
Range!T opSliceAssign(R : T)(auto ref R value, const size_t i, const size_t j)
Range opSliceAssign(R : T)(auto ref R value, const size_t i, const size_t j)
@trusted
in
{
@ -1326,7 +1332,7 @@ struct Array(T)
}
/// Ditto.
Range!T opSliceAssign(Range!T value, const size_t i, const size_t j) @trusted
Range opSliceAssign(Range value, const size_t i, const size_t j) @trusted
in
{
assert(i <= j);

View File

@ -93,6 +93,12 @@ struct SRange(E)
*/
struct SList(T)
{
/// The range types for $(D_PSYMBOL SList).
alias Range = SRange!T;
/// Ditto.
alias ConstRange = SRange!(const T);
private alias Entry = SEntry!T;
// 0th element of the list.
@ -497,7 +503,7 @@ struct SList(T)
}
/// Ditto.
size_t insertBefore(SRange!T r, ref T el) @trusted
size_t insertBefore(Range r, ref T el) @trusted
in
{
assert(checkRangeBelonging(r));
@ -530,7 +536,7 @@ struct SList(T)
*
* Precondition: $(D_PARAM r) is extracted from this list.
*/
size_t insertBefore(size_t R)(SRange!T r, T[R] el)
size_t insertBefore(size_t R)(Range r, T[R] el)
{
return insertFront!(T[])(el[]);
}
@ -685,7 +691,7 @@ struct SList(T)
*
* Precondition: $(D_PARAM r) is extracted from this list.
*/
SRange!T remove(SRange!T r)
Range remove(Range r)
in
{
assert(checkRangeBelonging(r));
@ -716,13 +722,13 @@ struct SList(T)
* Returns: Range that iterates over all elements of the container, in
* forward order.
*/
SRange!T opIndex()
Range opIndex()
{
return typeof(return)(this.head);
}
/// Ditto.
SRange!(const T) opIndex() const
ConstRange opIndex() const
{
return typeof(return)(this.head);
}
@ -966,6 +972,12 @@ struct DRange(E)
*/
struct DList(T)
{
/// The range types for $(D_PSYMBOL DList).
alias Range = DRange!T;
/// Ditto.
alias ConstRange = DRange!(const T);
private alias Entry = DEntry!T;
// 0th and the last elements of the list.
@ -1536,7 +1548,7 @@ struct DList(T)
}
/// Ditto.
size_t insertBefore(DRange!T r, ref T el) @trusted
size_t insertBefore(Range r, ref T el) @trusted
in
{
assert(checkRangeBelonging(r));
@ -1569,7 +1581,7 @@ struct DList(T)
*
* Precondition: $(D_PARAM r) is extracted from this list.
*/
size_t insertBefore(size_t R)(DRange!T r, T[R] el)
size_t insertBefore(size_t R)(Range r, T[R] el)
{
return insertFront!(T[])(el[]);
}
@ -1728,7 +1740,7 @@ struct DList(T)
*
* Precondition: $(D_PARAM r) is extracted from this list.
*/
DRange!T remove(DRange!T r)
Range remove(Range r)
in
{
assert(checkRangeBelonging(r));
@ -1768,13 +1780,13 @@ struct DList(T)
* Returns: Range that iterates over all elements of the container, in
* forward order.
*/
DRange!T opIndex()
Range opIndex()
{
return typeof(return)(this.head, this.tail);
}
/// Ditto.
DRange!(const T) opIndex() const
ConstRange opIndex() const
{
return typeof(return)(this.head, this.tail);
}

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* This module implements a $(D_PSymbol Set) container that stores unique
* This module implements a $(D_PSYMBOL Set) container that stores unique
* values without any particular order.
*
* Copyright: Eugene Wissner 2017.
@ -47,6 +47,12 @@ struct Range(E)
*/
struct Set(T)
{
/// The range types for $(D_PSYMBOL Set).
alias Range = .Range!T;
/// Ditto.
alias ConstRange = .Range!(const T);
invariant
{
assert(this.lengthIndex < primes.length);