// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/InputUtilSt.cpp
// Function: InputUtilSt::createConnectivities(task*)
void InputUtilSt::createConnectivities(struct task *a_task) {
    // init
    a_task->n_connectivities=2;
    a_task->n_connectivity_X = Calloc(NDAT, (size_t)a_task->n_connectivities);
    a_task->n_connectivity_X[0] = a_task->nK;
    a_task->n_connectivity_X[1] = a_task->nG;
    a_task->n_connectivity_Y = Calloc(NDAT, (size_t)a_task->n_connectivities);
    a_task->n_connectivity_Y[0] = a_task->nK;
    a_task->n_connectivity_Y[1] = a_task->nK;
    a_task->connectivities = Calloc(NPAR**, (size_t)a_task->n_connectivities);
    a_task->connectivities[0] = initToValue2D<NPAR>(a_task->n_connectivity_X[0], a_task->n_connectivity_Y[0], 0);
    a_task->connectivities[1] = initToValue2D<NPAR>(a_task->n_connectivity_X[1], a_task->n_connectivity_Y[1], 0);

    // create
    for(NDAT t=0; t<a_task->N; t++) { // vv for all non-stacked rows
        // skill-skill connectivity
        NCAT g = a_task->dat_group[t]; // -1, because in data they were 1-starting
        // grab skill array (if exists)
        NCAT *ar;
        NPAR n;
        getSkillsAtRow(a_task, t, &ar, &n);
        if(n>1) {
            for(NPAR i1=0;i1<(n-1);i1++) {
                for(NPAR i2=(NPAR)(i1+1);i2<n;i2++) {
                    a_task->connectivities[0][ ar[i1] ][ ar[i2] ] = 1;
                    a_task->connectivities[0][ ar[i2] ][ ar[i1] ] = 1;
                }
            }
        }
        // student-skill connectivity
        for(NPAR l=0; l<n; l++) {
            a_task->connectivities[1][g][l] = 1;
        }
    } // ^^ for all non-stacked rows
//    std::ofstream file; // DEBUG
//    file.open("connectivity.txt");  // DEBUG
//    for(NCAT i=0; i<a_task->n_connectivity_X[0]; i++) {  // DEBUG
//        std::stringstream ss;  // DEBUG
//        for(NDAT j=0; j<a_task->n_connectivity_Y[0]; j++) {  // DEBUG
//            ss << ((j>0)?" ":"") << std::to_string(a_task->connectivities[0][i][j]);  // DEBUG
//        }  // DEBUG
//        ss << "\n";  // DEBUG
//        file << ss.str();  // DEBUG
//    }  // DEBUG
//    file.close(); // DEBUG
    
    // turn connectivity[0] to reachability
    // vvv Warshall’s Algorithm $O(n^3)$ https://en.wikipedia.org/wiki/Reachability
    // https://cs.winona.edu/lin/cs440/ch08-2.pdf
    NDAT x1 = 0; // count 1's
    for(NCAT k=0; k<a_task->nK; k++) {
        x1 = 0;
        for(NCAT i=0; i<a_task->nK; i++) {
            for(NCAT j=0; j<a_task->nK; j++) {
                a_task->connectivities[0][i][j] = a_task->connectivities[0][i][j] || (a_task->connectivities[0][i][k] && a_task->connectivities[0][k][j]);
                x1+=a_task->connectivities[0][i][j];
            }
        }
    }// ^^^ Warshall’s Algorithm
//    std::ofstream file; // DEBUG
//    file.open("reachability.txt");  // DEBUG
//    for(NCAT i=0; i<a_task->n_connectivity_X[0]; i++) {  // DEBUG
//        std::stringstream ss;  // DEBUG
//        for(NDAT j=0; j<a_task->n_connectivity_Y[0]; j++) {  // DEBUG
//            ss << ((j>0)?" ":"") << std::to_string(a_task->connectivities[0][i][j]);  // DEBUG
//        }  // DEBUG
//        ss << "\n";  // DEBUG
//        file << ss.str();  // DEBUG
//    }  // DEBUG
//    file.close(); // DEBUG
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movb $0x2, 0x8d8(%rdi)
movl $0x2, %edi
movl $0x4, %esi
callq 0x2110
movq %rax, 0x8e0(%rbx)
movl 0x908(%rbx), %ebp
movl %ebp, (%rax)
movl 0x904(%rbx), %ecx
movl %ecx, 0x4(%rax)
movl $0x2, %edi
movl $0x4, %esi
callq 0x2110
movq %rax, 0x8e8(%rbx)
movl %ebp, (%rax)
movl %ebp, 0x4(%rax)
movl $0x2, %edi
movl $0x8, %esi
callq 0x2110
movq %rax, 0x8f0(%rbx)
movl %ebp, %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0xa6bd
movq 0x8f0(%rbx), %rcx
movq %rax, (%rcx)
movq 0x8e0(%rbx), %rax
movq 0x8e8(%rbx), %rcx
movl 0x4(%rax), %edi
movl 0x4(%rcx), %esi
xorl %edx, %edx
callq 0xa6bd
movq 0x8f0(%rbx), %rcx
movq %rax, 0x8(%rcx)
cmpl $0x0, 0x8f8(%rbx)
jle 0x96df
xorl %r14d, %r14d
leaq 0x10(%rsp), %r15
leaq 0xf(%rsp), %r12
movb $0x1, %bpl
movq 0x890(%rbx), %rax
movslq (%rax,%r14,4), %r13
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
movq %r12, %rcx
callq 0x76ed
cmpb $0x2, 0xf(%rsp)
jl 0x96a2
movb $0x1, %al
xorl %esi, %esi
leal 0x1(%rsi), %ecx
cmpb 0xf(%rsp), %cl
jge 0x9690
movsbq %al, %rdx
movsbq %sil, %rsi
movq 0x8f0(%rbx), %rdi
movq (%rdi), %rdi
movq 0x10(%rsp), %r8
movslq (%r8,%rsi,4), %r9
movq (%rdi,%r9,8), %rdi
movslq (%r8,%rdx,4), %r8
movb %bpl, (%rdi,%r8)
movq 0x8f0(%rbx), %rdi
movq (%rdi), %rdi
movq 0x10(%rsp), %r8
movslq (%r8,%rdx,4), %r9
movq (%rdi,%r9,8), %rdi
movslq (%r8,%rsi,4), %r8
movb %bpl, (%rdi,%r8)
incq %rdx
movsbq 0xf(%rsp), %rdi
cmpq %rdi, %rdx
jl 0x9644
movsbl %cl, %edx
movsbl 0xf(%rsp), %edi
decl %edi
incb %al
movl %ecx, %esi
cmpl %edx, %edi
jg 0x9633
cmpb $0x0, 0xf(%rsp)
jle 0x96cc
xorl %eax, %eax
movq 0x8f0(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r13,8), %rcx
movb $0x1, (%rcx,%rax)
incq %rax
movsbq 0xf(%rsp), %rcx
cmpq %rcx, %rax
jl 0x96ab
incq %r14
movslq 0x8f8(%rbx), %rax
cmpq %rax, %r14
jl 0x960c
cmpl $0x0, 0x908(%rbx)
jle 0x975e
xorl %eax, %eax
cmpl $0x0, 0x908(%rbx)
jle 0x974f
xorl %ecx, %ecx
cmpl $0x0, 0x908(%rbx)
jle 0x9740
xorl %edx, %edx
movq 0x8f0(%rbx), %rsi
movq (%rsi), %r8
movq (%r8,%rcx,8), %rsi
movb $0x1, %dil
cmpb $0x0, (%rsi,%rdx)
jne 0x972d
cmpb $0x0, (%rsi,%rax)
je 0x972b
movq (%r8,%rax,8), %rdi
cmpb $0x0, (%rdi,%rdx)
setne %dil
jmp 0x972d
xorl %edi, %edi
movb %dil, (%rsi,%rdx)
incq %rdx
movslq 0x908(%rbx), %rsi
cmpq %rsi, %rdx
jl 0x9700
incq %rcx
movslq 0x908(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x96f5
incq %rax
movslq 0x908(%rbx), %rcx
cmpq %rcx, %rax
jl 0x96ea
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop