| Thread overview | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 10, 2014 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #19 from bearophile_hugs@eml.cc --- This pull has fixed the most glaring problems of cartesianProduct: https://github.com/D-Programming-Language/phobos/pull/2276 I leave this ER open for the "repeat" argument request (compile time argument or run-time one), and for the desire of benchmarking. -- | ||||
January 08, 2015 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #20 from bearophile_hugs@eml.cc --- Thinking some more about this. Currently the "repeat" argument needs to be a template argument: cartesianProduct!2([0, 1]) ==> tuple(0,0), tuple(0,1), tuple(1,0), tuple(1,1) But it's probably better to change everything, so now I think it's better to have a differently named function with a different API. Something like: cartesianPower!true([0, 1], 2) => [0,0], [0,1], [1,0], [1,1] cartesianPower!false([0, 1], 2) => [0,0], [0,1], [1,0], [1,1] This means it yields arrays instead of tuples, and the power argument is a run-time argument. The boolean template argument is true by default, and it's named doCopy, if it's true every output array is a new array, otherwise if doCopy is false, it's always the same array with different contents. cartesianPower works efficiently, it's not recursive. If you like this idea, I can close down this issue, and open a new enhancement request for cartesianPower. -- | ||||
January 08, 2015 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #21 from bearophile_hugs@eml.cc --- A third optional argument can be a "buffer" array, if it's large enough (as much as the "power" argument or larger), it gets used to make cartesianPower a @nogc function: cartesianPower!false([0, 1], 2, buffer) -- | ||||
January 08, 2015 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #22 from bearophile_hugs@eml.cc --- The 3-arguments cartesianPower!false overload can't be annotated with @nogc because the given buffer array can be shorter than the "power" argument. Unless you generate a run-time error in this case. -- | ||||
January 10, 2015 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #23 from bearophile_hugs@eml.cc --- This doesn't use an efficient algorithm, but it shows the kind of API I was thinking of (it can be a random access range): struct CartesianPower(bool doCopy=true, T) { T[] items; uint repeat; T[] row; uint i, maxN; this(T[] items_, in uint repeat_, T[] buffer) pure nothrow @safe @nogc { this.items = items_; this.repeat = repeat_; row = buffer[0 .. repeat]; row[] = items[0]; maxN = items.length ^^ repeat; } static if (doCopy) { @property T[] front() pure nothrow @safe @nogc { return row.dup; } } else { @property T[] front() pure nothrow @safe @nogc { return row; } } @property bool empty() pure nothrow @safe @nogc { return i >= maxN; } void popFront() pure nothrow @safe @nogc { i++; if (empty) return; uint n = i; size_t count = repeat - 1; while (n) { row[count] = items[n % items.length]; count--; n /= items.length; } } } auto cartesianPower(bool doCopy=true, T)(T[] items, in uint repeat) pure nothrow @safe { return CartesianPower!(doCopy, T)(items, repeat, new T[repeat]); } auto cartesianPower(bool doCopy=true, T)(T[] items, in uint repeat, T[] buffer) pure nothrow @safe @nogc { if (buffer.length >= repeat) { return CartesianPower!(doCopy, T)(items, repeat, buffer); } else { static immutable err = new Error("buffer.length < repeat"); throw err; } } void main() @nogc { import core.stdc.stdio; int[3] items = [10, 20, 30]; int[4] buf; foreach (p; cartesianPower!false(items, 4, buf)) printf("(%d, %d, %d, %d)\n", p[0], p[1], p[2], p[3]); } -- | ||||
March 01, 2016 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 greenify <greeenify@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |greeenify@gmail.com Assignee|nobody@puremagic.com |greeenify@gmail.com -- | ||||
March 01, 2016 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #24 from greenify <greeenify@gmail.com> --- As @quickfur linked to this: I put a PR for cartesianProduct together - so maybe this issue can be tackled :) https://github.com/D-Programming-Language/phobos/pull/4026 -- | ||||
October 15, 2016 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 Andrei Alexandrescu <andrei@erdani.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |bootcamp -- | ||||
October 15, 2016 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #25 from greenify <greeenify@gmail.com> --- @andralex: as there was no positive decision on the PR it's now part of Mir (http://docs.mir.dlang.io/latest/mir_combinatorics.html) with a couple of other goodies (more functions, allocator support). So there is no need to use this as bootcamp task as this is already done and reviewed and it was only blocked by your "No" a couple of months ago. If you want to have it in Phobos, I am happy to reopen the regarding PR again ;-) -- | ||||
October 15, 2016 [Issue 7128] Cartesian product of ranges | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=7128 --- Comment #26 from Andrei Alexandrescu <andrei@erdani.com> --- @greenify thanks, I'v bootcamped it so it gets looked at again :o). -- | ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply