-
-
Notifications
You must be signed in to change notification settings - Fork 748
Add shift(): wrapper for front and popFront #4010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| )) | ||
|
|
@@ -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. | ||
|
|
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: please indent the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm after having a short look at the other template constraints in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure where you looked. The only exception is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Andrei seems to prefer the current style.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Guys, I don't really care about this - the main point here is to get
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ParamsandReturnssections needed here. See other functions in this file for examples