Skip to content
Merged
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
26 changes: 26 additions & 0 deletions std/algorithm/setops.d
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ if (ranges.length >= 2 &&
// For infinite ranges or non-forward ranges, we fall back to the old
// implementation which expands an exponential number of templates.
import std.typecons : tuple;
import std.meta : allSatisfy;
import std.range.primitives : hasLength;

static struct Result
{
Expand Down Expand Up @@ -413,13 +415,37 @@ if (ranges.length >= 2 &&
}
return copy;
}
static if (allSatisfy!(hasLength, RR))
{
@property size_t length()
{
size_t result = 1;
foreach (r; ranges)
{
result *= r.length;
}
return result;
}
}
}
static assert(isForwardRange!Result, Result.stringof ~ " must be a forward"
~ " range");

return Result(ranges);
}
@safe unittest
{
import std.algorithm.setops : cartesianProduct;

// 2 ranges
assert(cartesianProduct([1, 2], [3, 4]).length == 4);

// different sizes
assert(cartesianProduct([1, 2, 3], [4, 5]).length == 6);

// 3 ranges
assert(cartesianProduct([1, 2], [3, 4], [5]).length == 4);
}
// cartesian product of empty ranges should be empty
// https://issues.dlang.org/show_bug.cgi?id=10693
@safe unittest
Expand Down
Loading