On 12/15/2016 10:47 PM, KaattuPoochi wrote:
> On Tuesday, 13 December 2016 at 21:13:26 UTC, Ali wrote:
>>
>> And extending Ali's solution you can actually get the data in
>> to a two dimentional array at compile time and have it in static
>> memory with a small adjustment:
>>
>> static immutable matrix = import("data.txt")
>>     .split("\n")
>>     .map!(a => a.split(",").map!(to!int).array)
>>     .array;
>>
>> void main() {
>>     writeln(matrix);
>> }
>
> 1. For any non-trivial matrices (with 500 lines) runs DMD 2.072.1 out of
> memory (2GB). Not sure if this is a known bug. Works fine with LDC 1.0.0.

Just to note, there are 501 arrays generated at compile. You can reduce that number to 1 if you know the width and the height of the matrix. The following solution reads everything into a single array and then uses it as an int[N][]:

import std.algorithm;
import std.conv;
import std.array;
import std.stdio;

static immutable buffer = import("deneme.txt")
                          .splitter
                          .map!(a => a.splitter(',').map!(to!int))
                          .joiner
                          .array;

// Assume these are known or are calculated at compile time:
enum width = 4;
enum height = 3;

immutable int[width][] matrix;

shared static this() {
    matrix = (cast(immutable(int[width])*)buffer.ptr)[0..height];

    // Make sure we did not copy into matrix
    assert(cast(void*)matrix.ptr == cast(void*)buffer.ptr);
}

void main() {
    writeln(matrix);
}

> 2. The EOL on the last line results in an empty row in the end. Is there
> a way to overcome this?

As an added bonus, the solution above does not suffer from empty lines.

Ali

Reply via email to