Assign to Array Column
jmh530
john.michael.hall at gmail.com
Wed Feb 1 13:33:51 UTC 2023
On Wednesday, 1 February 2023 at 13:14:47 UTC, Siarhei Siamashka
wrote:
> On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote:
>> Greetings,
>>
>> for an array byte[3][3] myArr, I can code myArr[0] = 5 and
>> have:
>> 5,5,5
>> 0,0,0
>> 0,0,0
>>
>> Can I perform a similar assignment to the column? This,
>> myArr[][0] = 5, doesn't work.
>
> This works fine for small arrays, but for large arrays such
> access pattern is cache unfriendly. It's usually best to
> redesign the code to avoid assignments to columns if possible
> (for example, by working with a transposed array). The language
> is not providing a convenient shortcut for something that is
> usually undesirable and expensive. And I think that this is
> actually reasonable.
If the code is slow, then profile and try to speed up parts that
need it. The slowness may be due to a problem like this, but not
always.
The OP could also try mir's slices.
```d
/+dub.sdl:
dependency "mir-algorithm" version="*"
+/
import mir.ndslice.fuse;
import std.stdio: writeln;
void main() {
auto x = [[0, 0, 0], [0, 0, 0]].fuse;
x[0, 0 .. $] = 5;
x[0 .. $, 1] = 5;
writeln(x);
}
```
More information about the Digitalmars-d-learn
mailing list