Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions std/range/primitives.d
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ $(BOOKTABLE ,
$(TD Advances a given bidirectional _range from the right by exactly
$(I n) elements.
))
$(TR $(TD $(D $(LREF shift)))
$(TD Pops the first element from $(D r) and returns it.
))
$(TR $(TD $(D $(LREF shiftBack)))
$(TD Pops the last element from $(D r) and returns it.
))
$(TR $(TD $(D $(LREF moveFront)))
$(TD Removes the front element of a _range.
))
Expand Down Expand Up @@ -1988,6 +1994,156 @@ ElementType!R moveAt(R)(R r, size_t i)
}
}

private enum int hasNothrowPopFront(Range) = (functionAttributes!(popFrontN!Range)
& FunctionAttribute.nothrow_);

/**
Pops the first element from a range or array $(D r) and returns it.
It combines $(D front) and $(D popFront) in one method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Params and Returns sections needed here. See other functions in this file for examples

Params:
r = range to consume the first element from

Warning:
If a transient range is used, you should `dup`licate it to avoid it from
being overwritten.
Using shift with `popFront` that might `throw` could lead to a loss of the
`front` element and thus it is prohibited.

Returns:
Next element of the input range
*/
auto shift(Range)(ref Range r)
if (isInputRange!Range && hasNothrowPopFront!Range)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: please indent the if with 4 spaces. Ditto for shiftBack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm after having a short look at the other template constraints in primitives , they also don't seem to use indention

Copy link
Member

@PetarKirov PetarKirov Apr 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where you looked. The only exception is putChar:

$ pcregrep -MHn "\)\nif" range/primitives.d
range/primitives.d:252:correct primitive: $(D r.put(e)) if $(D R) defines $(D put), $(D r.front = e)
if $(D r) is an input range (followed by $(D r.popFront())), or $(D r(e))
range/primitives.d:348:private void putChar(R, E)(ref R r, E e)
if (isSomeChar!E)
$ pcregrep -MHn "\)\n    if" range/primitives.d
range/primitives.d:1596:auto walkLength(Range)(Range range)
    if (isInputRange!Range && !isInfinite!Range)
range/primitives.d:1610:auto walkLength(Range)(Range range, const size_t upTo)
    if (isInputRange!Range)
range/primitives.d:1667:size_t popFrontN(Range)(ref Range r, size_t n)
    if (isInputRange!Range)
range/primitives.d:1703:size_t popBackN(Range)(ref Range r, size_t n)
    if (isBidirectionalRange!Range)
range/primitives.d:1798:void popFrontExactly(Range)(ref Range r, size_t n)
    if (isInputRange!Range)
range/primitives.d:1814:void popBackExactly(Range)(ref Range r, size_t n)
    if (isBidirectionalRange!Range)
$ pcregrep -MHn "\)\n    if" range/package.d | wc -l
86
$ pcregrep -MHn "\)\n    if" algorithm/* | wc -l
170
~/dev/repos/dlang/phobos/std
$ find -type f -name "*.d" -exec pcregrep -MHn "\)\nif" {} \; | wc -l
342

~/dev/repos/dlang/phobos/std
$ find -type f -name "*.d" -exec pcregrep -MHn "\)\n    if" {} \; | wc -l
1278

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Andrei seems to prefer the current style.

#2011 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean Andrei prefers the less common style. OK, can't argue with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only exception is putChar

> grep "^if (" primitives.d
if (r.empty) {}   // can test for empty
if (isSomeChar!E)
if (isInputRange!Range && hasNothrowPopFront!Range)
if (isBidirectionalRange!Range && hasNothrowPopBack!Range)
if (!isNarrowString!(T[]) && !is(T[] == void[]))
if (isNarrowString!(C[]))
if (!isNarrowString!(T[]) && !is(T[] == void[]))
if (isNarrowString!(T[]))
if (!isNarrowString!(T[]) && !is(T[] == void[]))
if (!isNarrowString!(T[]))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean Andrei prefers the less common style. OK, can't argue with that.

Guys, I don't really care about this - the main point here is to get shift into Phobos :)
Btw we should just enable some code style tool like DScanner for Phobos and agree once on one of both ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I agree.

in
{
assert(!r.empty, "empty range");
}
body
{
auto e = r.front;
r.popFront();
return e;
}

///
@safe pure nothrow @nogc unittest
{
int[3] arr = [1, 2, 3];
int[] r = arr;
assert(r.shift == 1);

static immutable result = [2, 3];
assert(r == result);
assert(r.shift == 2);
assert(r.shift == 3);
assert(r.empty);

import std.algorithm.comparison: equal;
import std.range: iota;
// original array stays unmodified
assert(arr[].equal(iota(1, 4)));
}

@safe pure nothrow @nogc unittest
{
import std.range : iota;
auto r = iota(1, 3);
assert(r.shift == 1);
assert(r.shift == 2);
assert(r.empty);
}

unittest
{
struct ThrowingRange
{
bool empty = false;
int[] front = [0];
void popFront(){}
}

ThrowingRange tRange;
assert(tRange.front == [0]);
// if popFront might throw, shift shouldn't be possible
static assert(!__traits(compiles, tRange.shift));
}

private enum int hasNothrowPopBack(Range) = (functionAttributes!(popBackN!Range)
& FunctionAttribute.nothrow_);

/**
Pops the last element from a range or array $(D r) and returns it.
It combines $(D back) and $(D popBack) in one method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments about unittest examples and documentation sections

Params:
r = Bidirectional range or array to consume the last element from

Warning:
If a transient range is used, you should `dup`licate it to avoid it from
being overwritten.
Using shift with `popFront` that might `throw` could lead to a loss of the
`front` element and thus it is prohibited.

Returns:
Last element of the bidirectional range
*/
auto shiftBack(Range)(ref Range r)
if (isBidirectionalRange!Range && hasNothrowPopBack!Range)
in
{
assert(!r.empty, "empty range");
}
body
{
auto e = r.back;
r.popBack();
return e;
}

///
@safe pure nothrow @nogc unittest
{
int[3] arr = [1, 2, 3];
int[] r = arr;
assert(r.shiftBack == 3);

static immutable result = [1, 2];
assert(r == result);
assert(r.shiftBack == 2);
assert(r.shiftBack == 1);
assert(r.empty);

import std.algorithm.comparison: equal;
import std.range: iota;
// original array stays unmodified
assert(arr[].equal(iota(1, 4)));
}

@safe pure nothrow @nogc unittest
{
import std.range : iota;
auto r = iota(1, 3);
assert(r.shiftBack == 2);
assert(r.shiftBack == 1);
assert(r.empty);
}

unittest
{
struct ThrowingRange
{
bool empty = false;
int[] front = [0];
void popBack(){}
}

ThrowingRange tRange;
assert(tRange.front == [0]);
// if popBack might throw, shiftBack shouldn't be possible
static assert(!__traits(compiles, tRange.shiftBack));
}

/**
Implements the range interface primitive $(D empty) for built-in
arrays. Due to the fact that nonmember functions can be called with
Expand Down