// File: xbai0624[P]dnn/src/DataInterface.cpp
// Function: DataInterface::loadFile(char const*, std::vector<Matrix, std::allocator<Matrix>>&)
void DataInterface::loadFile(const char* path, std::vector<Matrix> &contents)
{
    // this only for code development, it reads data in ./test_data/ directory
    fstream f(path, fstream::in);
    if(!f.is_open()) 
    {
        std::cout<<__func__<<" Error: Cannot open file: "<<path<<std::endl;
        exit(0);
    }

    string line;
    while(getline(f, line))
    {
        istringstream iss(line);
        string tmp;
        vector<double> vec;
        while(iss>>tmp)
        {
            if(tmp.size() > 0) 
            {
                double a = stod(tmp);
                vec.push_back(a);
            }
        }
        assert(vec.size() == 27);

        size_t horizontal = 10;
        size_t vertical = 10;
        if(__dataDimensionFromParameter.first > 0)
        {
            // if data dimension is set using the constructor parameter, then use it
            horizontal = sqrt((int)__dataDimensionFromParameter.first * (int)__dataDimensionFromParameter.second);
            vertical = horizontal;
        }
        Matrix m(horizontal, vertical); // 
        for(size_t i=0;i<vec.size();i+=3)
        {
            size_t ii = vec[i];
            size_t jj = vec[i+1];
            assert(ii<horizontal && jj < vertical);
            double val = vec[i+2];

            m[ii][jj] = val;
        }

        Matrix _mm = m.Reshape(__dataDimensionFromParameter.first, __dataDimensionFromParameter.second);
        //Matrix mm = _mm.Normalization();
        contents.push_back(_mm);
    }

    // *** implement the image dimension
    assert(__gLayerDimension != LayerDimension::Undefined); // make sure 1D or 2D interface
    auto dim = contents[0].Dimension();
    if(__gLayerDimension == LayerDimension::_1D)
    {
        __dataDimension.second = 1;
        __dataDimension.first = dim.first * dim.second;
    }
    else 
    {
        __dataDimension = dim;
    }
}

// Assembly:
subq $0x528, %rsp            # imm = 0x528
movq %rdi, 0x520(%rsp)
movq %rsi, 0x518(%rsp)
movq %rdx, 0x510(%rsp)
movq 0x520(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x518(%rsp), %rsi
leaq 0x300(%rsp), %rdi
movq %rdi, 0x90(%rsp)
movl $0x8, %edx
callq 0x42c0
movq 0x90(%rsp), %rdi
callq 0x43d0
movb %al, 0x9f(%rsp)
jmp 0x391d7
movb 0x9f(%rsp), %al
testb $0x1, %al
jne 0x3926c
jmp 0x391e8
movq 0x31de9(%rip), %rdi     # 0x6afd8
leaq 0x1ba06(%rip), %rsi     # 0x54bfc
callq 0x4230
movq %rax, 0x80(%rsp)
jmp 0x39205
movq 0x80(%rsp), %rdi
leaq 0x1b9f1(%rip), %rsi     # 0x54c05
callq 0x4230
movq %rax, 0x78(%rsp)
jmp 0x39220
movq 0x78(%rsp), %rdi
movq 0x518(%rsp), %rsi
callq 0x4230
movq %rax, 0x70(%rsp)
jmp 0x39239
movq 0x70(%rsp), %rdi
movq 0x31d6b(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x3924c
xorl %edi, %edi
callq 0x4290
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0x39843
leaq 0x2d0(%rsp), %rdi
callq 0x42f0
leaq 0x300(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
callq 0x4060
movq %rax, 0x68(%rsp)
jmp 0x39295
movq 0x68(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x42a0
movb %al, 0x67(%rsp)
jmp 0x392af
movb 0x67(%rsp), %al
testb $0x1, %al
jne 0x392bc
jmp 0x39750
leaq 0x150(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
movl $0x8, %edx
callq 0x43a0
jmp 0x392d8
leaq 0x130(%rsp), %rdi
callq 0x42f0
leaq 0x118(%rsp), %rdi
callq 0x1f090
leaq 0x150(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x42b0
movq %rax, 0x58(%rsp)
jmp 0x3930e
movq 0x58(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x42a0
movb %al, 0x57(%rsp)
jmp 0x39328
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x39335
jmp 0x393c0
leaq 0x130(%rsp), %rdi
callq 0x4180
cmpq $0x0, %rax
jbe 0x393bb
xorl %eax, %eax
movl %eax, %esi
leaq 0x130(%rsp), %rdi
callq 0x3a3b0
movsd %xmm0, 0x48(%rsp)
jmp 0x39361
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x21ce0
jmp 0x39387
jmp 0x393bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0x39836
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0x39724
jmp 0x392f2
leaq 0x118(%rsp), %rdi
callq 0x21e20
cmpq $0x1b, %rax
jne 0x393d5
jmp 0x393f4
leaq 0x1b844(%rip), %rdi     # 0x54c20
leaq 0x1b6b9(%rip), %rsi     # 0x54a9c
movl $0xf6, %edx
leaq 0x1b842(%rip), %rcx     # 0x54c31
callq 0x4170
movq 0x88(%rsp), %rax
movq $0xa, 0x108(%rsp)
movq $0xa, 0x100(%rsp)
cmpq $0x0, 0x80(%rax)
jbe 0x39486
movq 0x88(%rsp), %rax
movl 0x80(%rax), %edi
movl 0x88(%rax), %eax
imull %eax, %edi
callq 0x3a3f0
movsd %xmm0, 0x40(%rsp)
jmp 0x39442
movsd 0x40(%rsp), %xmm0
movsd 0x18c00(%rip), %xmm2    # 0x52050
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %rcx
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rsi
movq 0x100(%rsp), %rdx
leaq 0xe8(%rsp), %rdi
callq 0x3b3b0
jmp 0x394a5
movq $0x0, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x21e20
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0x3966c
movq 0xe0(%rsp), %rsi
leaq 0x118(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x1fd60
movq 0x20(%rsp), %rdi
movsd (%rax), %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x18b3a(%rip), %xmm1    # 0x52050
movsd %xmm1, 0x28(%rsp)
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, 0xd8(%rsp)
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x1fd60
movsd 0x28(%rsp), %xmm1
movsd (%rax), %xmm0
movaps %xmm0, %xmm2
cvttsd2si %xmm2, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, 0xd0(%rsp)
movq 0xd8(%rsp), %rcx
xorl %eax, %eax
cmpq 0x108(%rsp), %rcx
movb %al, 0x37(%rsp)
jae 0x395a2
movq 0xd0(%rsp), %rax
cmpq 0x100(%rsp), %rax
setb %al
movb %al, 0x37(%rsp)
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x395ac
jmp 0x395ae
jmp 0x395cd
leaq 0x1b6be(%rip), %rdi     # 0x54c73
leaq 0x1b4e0(%rip), %rsi     # 0x54a9c
movl $0x105, %edx            # imm = 0x105
leaq 0x1b669(%rip), %rcx     # 0x54c31
callq 0x4170
movq 0xe0(%rsp), %rsi
addq $0x2, %rsi
leaq 0x118(%rsp), %rdi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xd8(%rsp), %rsi
leaq 0xe8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x18(%rsp)
jmp 0x3961e
movq 0x18(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x1fd60
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0xe0(%rsp), %rax
addq $0x3, %rax
movq %rax, 0xe0(%rsp)
jmp 0x394b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0x39717
movq 0x88(%rsp), %rax
movq 0x80(%rax), %rdx
movq 0x88(%rax), %rcx
leaq 0xb0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x3df60
jmp 0x39699
movq 0x510(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1fa50
jmp 0x396b0
leaq 0xb0(%rsp), %rdi
callq 0x3bb80
leaq 0xe8(%rsp), %rdi
callq 0x3bb80
leaq 0x118(%rsp), %rdi
callq 0x1f190
leaq 0x130(%rsp), %rdi
callq 0x4160
leaq 0x150(%rsp), %rdi
callq 0x4110
jmp 0x39279
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x3bb80
leaq 0xe8(%rsp), %rdi
callq 0x3bb80
leaq 0x118(%rsp), %rdi
callq 0x1f190
leaq 0x130(%rsp), %rdi
callq 0x4160
leaq 0x150(%rsp), %rdi
callq 0x4110
jmp 0x39836
movq 0x88(%rsp), %rax
cmpl $0x2, 0xc0(%rax)
je 0x39763
jmp 0x39782
leaq 0x1b528(%rip), %rdi     # 0x54c92
leaq 0x1b32b(%rip), %rsi     # 0x54a9c
movl $0x111, %edx            # imm = 0x111
leaq 0x1b4b4(%rip), %rcx     # 0x54c31
callq 0x4170
movq 0x510(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f9e0
movq %rax, %rdi
callq 0x3bae0
movq %rdx, (%rsp)
movq %rax, 0x8(%rsp)
jmp 0x397a6
movq 0x88(%rsp), %rax
movq (%rsp), %rcx
movq 0x8(%rsp), %rdx
movq %rdx, 0xa0(%rsp)
movq %rcx, 0xa8(%rsp)
cmpl $0x0, 0xc0(%rax)
jne 0x397f7
movq 0x88(%rsp), %rax
movq $0x1, 0x78(%rax)
movq 0xa0(%rsp), %rcx
imulq 0xa8(%rsp), %rcx
movq %rcx, 0x70(%rax)
jmp 0x39814
movq 0x88(%rsp), %rdi
addq $0x70, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3a410
jmp 0x39812
jmp 0x39814
leaq 0x2d0(%rsp), %rdi
callq 0x4160
leaq 0x300(%rsp), %rdi
callq 0x4070
addq $0x528, %rsp            # imm = 0x528
retq
leaq 0x2d0(%rsp), %rdi
callq 0x4160
leaq 0x300(%rsp), %rdi
callq 0x4070
movq 0x2f8(%rsp), %rdi
callq 0x43f0
nopl (%rax)