How to best translate this C++ algorithm into D?
logicchains via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jun 7 06:04:51 PDT 2014
On Saturday, 7 June 2014 at 08:27:52 UTC, deadalnix wrote:
> This may ends up doing many allocations. you could use an array
> where you preallocate forests.length * 3 items via the reserve
> function and use it as an output range.
>
I've changed filter to partition, which speeds it up slightly,
but I'm not exactly sure how to use a pre-allocated array as an
output range. I've tried:
forest_t[] newForests;
newForests.reserve(forests.length*3);
map!(a => [forest_t(-1, -1, +1)+a, forest_t(-1, +1, -1)+a,
forest_t(+1, -1, -1)+a])(forests)
.join
.partition!(forest_invalid)
.sort.uniq.copy(newForests);
But this leaves newForests with length zero, even though indexing
it via for instance newForests[0] gives a correct forest.
> Try
> forests.map!(a => only(possible_meals[0]+a, possible_meals[1]+a,
> possible_meals[2]+a))
'only' doesn't seem to be working on my version of ldc (latest
Arch Linux community build); even a simple `auto tst = only("1",
"2", "3");` gives a compile error, even though the same thing
works fine on dmd. The best I can get is:
return map!(a => [forest_t(-1, -1, +1)+a, forest_t(-1, +1,
-1)+a, forest_t(+1, -1, -1)+a])(forests)
.join
.partition!(forest_invalid)
.sort.uniq.array;
More information about the Digitalmars-d
mailing list