// File: /workspace/github_cpp_makefile_star2_O3/sebastienmeyer2[P]mst-clustering/src/node.cpp
// Function: intracluster_variance(std::unordered_map<Node*, Node*, std::hash<Node*>, std::equal_to<Node*>, std::allocator<std::pair<Node* const, Node*>>>)
double intracluster_variance(std::unordered_map<Node*, Node*> clusters)
{
    /*!
     * @brief Returns the intracluster variance by computing the centers of 
     * the different clusters first 
     * 
     * @param clusters The different clusters computed, that is, a map between 
     * nodes and their representatives
     * 
     * @return The intracluster variance of these clusters
     * 
     */

    double intracluster_variance = 0.0;

    // firstly, we compute the clusters in the reverse mode, that is, 
    // we take for each representative, all its members
    std::unordered_set<Node*> representatives;
    std::unordered_map<Node*, std::unordered_set<Node*> > members;

    for (auto kv : clusters) {
        Node* node = kv.first;
        Node* rep = kv.second;

        representatives.insert(rep);
        
        if (members.find(rep) == members.end()) {
            members.insert(std::pair<Node*, std::unordered_set<Node*> >(rep, std::unordered_set<Node*>()));
        }

        members.at(rep).insert(node);
    }

    for (Node* rep : representatives) {
        int d = rep->get_point()->get_dim();
        int cluster_size = members.at(rep).size();

        // secondly, we compute centers as in the M-step of kmeans 
        Node* center = new Node(rep->label, new Point());

        for (Node* member : members.at(rep)) {
            for (int j = 0; j < d; j ++) {
                center->get_point()->coords[j] += member->get_point()->coords[j];
            }
        }

        for (int j = 0; j < d; j ++) {
            center->get_point()->coords[j] /= cluster_size;
        }

        // finally, we compute the intracluster variance out of these centers
        double cluster_variance = 0.0;
        for (Node* member : members.at(rep)) {
            cluster_variance += member->get_point()->distance(center->get_point());
        }

        intracluster_variance += cluster_variance;
    }
    intracluster_variance /= clusters.size();

    return intracluster_variance;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %r13
leaq 0xd0(%rsp), %rax
movq %rax, -0x30(%rax)
movl $0x1, %ecx
movq %rcx, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x60(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
movq 0x10(%rdi), %r12
xorpd %xmm2, %xmm2
testq %r12, %r12
je 0x990d
movq %r13, 0x18(%rsp)
leaq 0x98(%rsp), %rbx
leaq 0xe0(%rsp), %r14
leaq 0xa0(%rsp), %r15
leaq 0xd8(%rsp), %r13
leaq 0x68(%rsp), %rbp
movq 0x8(%r12), %rax
movq 0x10(%r12), %rcx
movq %rax, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
movq %r15, 0xd8(%rsp)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x5ccc
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r9
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq (%r8,%rdx,8), %rax
testq %rax, %rax
je 0x965d
movq %rdx, %rsi
movq (%rax), %r10
cmpq 0x8(%r10), %rcx
je 0x96dd
movq (%r10), %r10
testq %r10, %r10
je 0x965d
movq 0x8(%r10), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r9
cmpq %rsi, %rdx
jne 0x965d
cmpq %rdi, %rcx
jne 0x963a
jmp 0x96dd
movq $0x0, 0x88(%rsp)
movq %rbx, 0x68(%rsp)
movq $0x1, 0x70(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rbx)
movl $0x3f800000, 0x88(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rbx)
movq %rcx, 0xd8(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x503c
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x5996
movq %r14, %rdi
callq 0x50b2
movq %rbp, %rdi
callq 0x50b2
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r9
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq %rdx, %rsi
movq (%r8,%rsi,8), %rax
testq %rax, %rax
je 0x9967
movq (%rax), %rdi
cmpq 0x8(%rdi), %rcx
je 0x9719
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x9967
movq 0x8(%rdi), %r8
movq %r8, %rax
xorl %edx, %edx
divq %r9
cmpq %rsi, %rdx
jne 0x9967
cmpq %r8, %rcx
jne 0x96f3
addq $0x10, %rdi
movq %rdi, 0xd8(%rsp)
leaq 0x28(%rsp), %rsi
movq %r13, %rdx
callq 0x5ccc
movq (%r12), %r12
testq %r12, %r12
jne 0x95de
movq 0xb0(%rsp), %r12
testq %r12, %r12
xorpd %xmm2, %xmm2
movq 0x18(%rsp), %r13
je 0x990d
movsd %xmm2, 0x8(%rsp)
movq 0x8(%r12), %r14
movq 0x8(%r14), %rdi
callq 0x3a7c
movl %eax, %ebx
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdi
movq %r14, %rax
xorl %edx, %edx
divq %rdi
movq (%rsi,%rdx,8), %rax
testq %rax, %rax
je 0x9973
movq %rdx, %rcx
movq (%rax), %r8
cmpq 0x8(%r8), %r14
je 0x97c0
movq (%r8), %r8
testq %r8, %r8
je 0x9973
movq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x9973
cmpq %rsi, %r14
jne 0x979a
movl 0x28(%r8), %ebp
movl $0x10, %edi
callq 0x3280
movq %rax, %r15
movq %rax, %rdi
callq 0x3a20
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdi
movq %r14, %rax
xorl %edx, %edx
divq %rdi
movq (%rsi,%rdx,8), %rax
testq %rax, %rax
je 0x997f
movq %rdx, %rcx
movq (%rax), %r8
movq 0x8(%r8), %r9
movq %r8, %r10
cmpq %r9, %r14
je 0x9830
movq (%r10), %r10
testq %r10, %r10
je 0x997f
movq 0x8(%r10), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x997f
cmpq %rsi, %r14
jne 0x980a
movq 0x20(%r10), %rdx
movl %ebx, %eax
testq %rdx, %rdx
je 0x986f
testl %ebx, %ebx
jle 0x986a
movq 0x8(%rdx), %rsi
movq 0x8(%rsi), %rsi
movq (%rsi), %rsi
movq (%r15), %r10
xorl %r11d, %r11d
movsd (%rsi,%r11,8), %xmm0
addsd (%r10,%r11,8), %xmm0
movsd %xmm0, (%r10,%r11,8)
incq %r11
cmpq %r11, %rax
jne 0x9850
movq (%rdx), %rdx
jmp 0x9836
testl %ebx, %ebx
jle 0x9895
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movq (%r15), %rdx
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x987f
cmpq %r9, %r14
je 0x98c0
movq (%r8), %r8
testq %r8, %r8
je 0x998b
movq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x998b
cmpq %rsi, %r14
jne 0x989a
movq 0x20(%r8), %rbx
xorpd %xmm0, %xmm0
testq %rbx, %rbx
je 0x98f6
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rdi
movq %r15, %rsi
callq 0x3b3a
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movq (%rbx), %rbx
jmp 0x98c8
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movq (%r12), %r12
testq %r12, %r12
jne 0x9759
movsd 0x18(%r13), %xmm0
unpcklps 0x77a6(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x77ae(%rip), %xmm0     # 0x110d0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x5122
leaq 0xa0(%rsp), %rdi
callq 0x50b2
movsd 0x8(%rsp), %xmm0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x781c(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0x7810(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0x7804(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0x77f8(%rip), %rdi      # 0x1118a
callq 0x33e0
jmp 0x99ce
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x3290
jmp 0x99d1
jmp 0x99ce
movq %rax, %rbx
movq %r14, %rdi
callq 0x50b2
leaq 0x68(%rsp), %rdi
callq 0x50b2
jmp 0x99d1
jmp 0x99ce
jmp 0x99ce
jmp 0x99ce
jmp 0x99ce
jmp 0x99ce
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5122
leaq 0xa0(%rsp), %rdi
callq 0x50b2
movq %rbx, %rdi
callq 0x34a0