diff options
| author | Nathan Sashihara <21227491+n8sh@users.noreply.github.com> | 2018-11-05 17:06:09 -0500 |
|---|---|---|
| committer | Nathan Sashihara <21227491+n8sh@users.noreply.github.com> | 2018-11-05 18:43:58 -0500 |
| commit | c290c850884ef27d978e82f4ee1864e1428d2644 (patch) | |
| tree | c7c0df6dc87a21776c35f1bb523f4ebde1fe2770 /source | |
| parent | 184d307e40832c1c69c14729a3dc7d559614abc1 (diff) | |
| download | tanya-c290c850884ef27d978e82f4ee1864e1428d2644.tar.gz | |
retro(retro(range)) is range
Diffstat (limited to 'source')
| -rw-r--r-- | source/tanya/algorithm/iteration.d | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/source/tanya/algorithm/iteration.d b/source/tanya/algorithm/iteration.d index 27fce71..95f077f 100644 --- a/source/tanya/algorithm/iteration.d +++ b/source/tanya/algorithm/iteration.d @@ -408,22 +408,10 @@ if (isInputRange!R) } } -/** - * Iterates a bidirectional range backwards. - * - * If $(D_PARAM Range) is a random-access range as well, the resulting range - * is a random-access range too. - * - * Params: - * Range = Bidirectional range type. - * range = Bidirectional range. - * - * Returns: Bidirectional range with the elements order reversed. - */ -auto retro(Range)(Range range) -if (isBidirectionalRange!Range) +private { - static struct Retro + // Reverse-access-order range returned by `retro`. + struct Retro(Range) { Range source; @@ -526,9 +514,31 @@ if (isBidirectionalRange!Range) } } } + + version (unittest) static assert(isBidirectionalRange!Retro); } +} - return Retro(range); +/** + * Iterates a bidirectional range backwards. + * + * If $(D_PARAM Range) is a random-access range as well, the resulting range + * is a random-access range too. + * + * Params: + * Range = Bidirectional range type. + * range = Bidirectional range. + * + * Returns: Bidirectional range with the elements order reversed. + */ +auto retro(Range)(return Range range) +if (isBidirectionalRange!Range) +{ + // Special case: retro(retro(range)) is range + static if (is(Range == Retro!RRange, RRange)) + return range.source; + else + return Retro!Range(range); } /// |
