// File: /workspace/github_cpp_makefile_star2_O1/sebastienmeyer2[P]mst-clustering/src/node.cpp
// Function: silhouette(std::unordered_map<Node*, Node*, std::hash<Node*>, std::equal_to<Node*>, std::allocator<std::pair<Node* const, Node*>>>)
double silhouette(std::unordered_map<Node*, Node*> clusters)
{
    /*!
     * @brief Computes the silhouette of the clusters given in argument
     *
     * @param clusters A mapping between nodes and their representatives 
     * in clusters
     * 
     * @return The silhouette of these clusters
     */

    int n = clusters.size(); // number of nodes

	double silhouette = 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*, int> clusters_size;
    std::unordered_map<Node*, std::unordered_set<Node*> > members;
    std::unordered_set<Node*> nodes;

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

        nodes.insert(node);

        representatives.insert(rep);
        if (clusters_size.find(rep) == clusters_size.end()) {
            clusters_size.insert(std::pair<Node*, int>(rep, 0));
        }
        clusters_size.at(rep) += 1;
        
        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);
    }

    int nb_clusters = representatives.size();

	// we compute a and b which are respectively the 
	// average distance to points of the same cluster 
	// and min of average distances to foreign clusters
    std::unordered_map<Node*, double> a;
    std::unordered_map<Node*, double> b;
    std::unordered_map<Node*, double> s;

    for (Node* node : nodes) {
        a.insert(std::pair<Node*, double>(node, 0.0));
        b.insert(std::pair<Node*, double>(node, 0.0));
        s.insert(std::pair<Node*, double>(node, 0.0));
    }

	for (Node* node : nodes){
        std::unordered_map<Node*, double> dist_to_clusters;
        for (Node* rep : representatives) {
            dist_to_clusters.insert(std::pair<Node*, double>(rep, 0.0));
        }

        for (Node* other_node : nodes) {
            if (other_node != node) {
                Node* other_node_cluster = clusters.at(other_node);

                dist_to_clusters.at(other_node_cluster) += std::sqrt(node->dist(other_node));
            }
        }

        double minDist = DBL_MAX;
        for (Node* rep : representatives) {
            dist_to_clusters.at(rep) /= clusters_size.at(rep);

            if (rep != clusters.at(node) && dist_to_clusters.at(rep) < minDist) {
                minDist = dist_to_clusters.at(rep);
            }
        }

        a.at(node) = dist_to_clusters.at(clusters.at(node));
        if (minDist < DBL_MAX) {
            b.at(node) = minDist;
        }
        s.at(node) = (b.at(node) - a.at(node)) / (std::max(a.at(node), b.at(node)));
	}

    for (Node* node : nodes) {
        silhouette += s.at(node);
    }
	silhouette /= n;

	return silhouette;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rdi, %rbx
movq 0x18(%rdi), %rax
movq %rax, 0xe0(%rsp)
leaq 0x210(%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 0x1d8(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x1a0(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x168(%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), %r15
testq %r15, %r15
je 0x9e2b
leaq 0x100(%rsp), %r12
leaq 0xa0(%rsp), %r13
leaq 0x28(%rsp), %r14
leaq 0x1a8(%rsp), %rbp
movq 0x8(%r15), %rax
movq 0x10(%r15), %rcx
movq %rax, 0x100(%rsp)
movq %rcx, 0x28(%rsp)
leaq 0x138(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
movq %r12, %rsi
movq %r13, %rdx
callq 0x5d94
leaq 0x1e0(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
movq %r14, %rsi
movq %r13, %rdx
callq 0x5d94
movq 0x28(%rsp), %rcx
movq 0x1a8(%rsp), %rdi
movq 0x1b0(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x9c98
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0x9c98
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x9c96
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x9c98
movq %r10, %rax
cmpq %rdi, %rcx
jne 0x9c6b
jmp 0x9c98
xorl %eax, %eax
testq %rax, %rax
je 0x9ca3
cmpq $0x0, (%rax)
jne 0x9cc1
movq %rcx, 0xa0(%rsp)
movl $0x0, 0xa8(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x940e
movq %rbp, %rdi
movq %r14, %rsi
callq 0x93cc
incl (%rax)
movq 0x28(%rsp), %rcx
movq 0x170(%rsp), %rdi
movq 0x178(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x9d35
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0x9d35
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x9d33
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x9d35
movq %r10, %rax
cmpq %rdi, %rcx
jne 0x9d08
jmp 0x9d35
xorl %eax, %eax
testq %rax, %rax
je 0x9d44
cmpq $0x0, (%rax)
jne 0x9df9
movq %r15, (%rsp)
movq %r13, %r15
movq %r14, %r13
movq %rbp, %r14
movq %r12, %rbp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movq $0x0, 0x90(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x60(%rsp)
movq $0x1, 0x68(%rsp)
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, 0x80(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq %rcx, 0xa0(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0x60(%rsp), %r12
movq %r12, %rsi
movq %r12, %rdx
callq 0x5052
leaq 0x170(%rsp), %rdi
movq %r15, %rsi
callq 0x5a2e
leaq 0xa8(%rsp), %rdi
callq 0x50cc
movq %r12, %rdi
callq 0x50cc
movq %rbp, %r12
movq %r14, %rbp
movq %r13, %r14
movq %r15, %r13
movq (%rsp), %r15
leaq 0x170(%rsp), %rdi
movq %r14, %rsi
callq 0x5d0a
movq %rax, 0xa0(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5d94
movq (%r15), %r15
testq %r15, %r15
jne 0x9be6
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 0x90(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x130(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
movq 0x148(%rsp), %rbp
testq %rbp, %rbp
je 0x9f0c
leaq 0x28(%rsp), %r15
leaq 0x60(%rsp), %r12
leaq 0x100(%rsp), %r13
movq 0x8(%rbp), %r14
movq %r14, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r15, %rsi
callq 0xa3c2
movq %r14, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0xa3c2
movq %r14, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xa3c2
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x9eaf
movq 0x148(%rsp), %rbp
testq %rbp, %rbp
je 0xa1dd
leaq 0x10(%rsp), %r14
leaq 0x28(%rsp), %r15
leaq 0x18(%rsp), %r13
movq 0x8(%rbp), %rax
movq %rax, 0x10(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
movq $0x1, 0x30(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, 0x48(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq 0x1f0(%rsp), %r12
testq %r12, %r12
je 0x9f8f
movq 0x8(%r12), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0xa3c2
movq (%r12), %r12
jmp 0x9f66
movq 0x148(%rsp), %r12
testq %r12, %r12
je 0xa00f
movq 0x8(%r12), %rax
movq %rax, 0x18(%rsp)
cmpq 0x10(%rsp), %rax
je 0xa009
movq %rbx, %rdi
movq %r13, %rsi
callq 0xa6b4
movq (%rax), %rax
movq %rax, 0xf8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x96d4
ucomisd 0x7686(%rip), %xmm0     # 0x11660
jb 0x9fe2
sqrtsd %xmm0, %xmm0
jmp 0x9fe7
callq 0x3210
movsd %xmm0, (%rsp)
movq %r15, %rdi
leaq 0xf8(%rsp), %rsi
callq 0xa73e
movsd (%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq (%r12), %r12
jmp 0x9f97
movq %rbp, 0xf0(%rsp)
movq 0x1f0(%rsp), %r12
movsd 0x7631(%rip), %xmm0     # 0x11658
movsd %xmm0, (%rsp)
testq %r12, %r12
je 0xa0c8
movsd 0x761b(%rip), %xmm0     # 0x11658
movsd %xmm0, (%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x18(%rsp)
leaq 0x1a8(%rsp), %rdi
movq %r13, %rsi
callq 0x93cc
movl (%rax), %ebp
movq %r15, %rdi
movq %r13, %rsi
callq 0xa73e
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax)
movq 0x18(%rsp), %rbp
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa6b4
cmpq (%rax), %rbp
je 0xa0bb
movq %r15, %rdi
movq %r13, %rsi
callq 0xa73e
movsd (%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0xa0bb
movq %r15, %rdi
movq %r13, %rsi
callq 0xa73e
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq (%r12), %r12
testq %r12, %r12
jne 0xa042
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa6b4
movq 0xf0(%rsp), %rbp
movq %r15, %rdi
movq %rax, %rsi
callq 0xa73e
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0xa73e
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x7546(%rip), %xmm0     # 0x11658
ucomisd (%rsp), %xmm0
jbe 0xa12f
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0xa73e
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0xa73e
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0xa73e
movsd (%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, (%rsp)
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0xa73e
movq %rax, %r12
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0xa73e
movsd (%r12), %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x100(%rsp), %rdi
movq %r14, %rsi
callq 0xa73e
movsd 0x8(%rsp), %xmm1
maxsd 0xe8(%rsp), %xmm1
movsd (%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq %r15, %rdi
callq 0xa352
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x9f2c
movq 0x148(%rsp), %r15
xorpd %xmm1, %xmm1
testq %r15, %r15
je 0xa225
leaq 0x100(%rsp), %rbx
leaq 0x28(%rsp), %r14
movsd %xmm1, (%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa73e
movsd (%rsp), %xmm1
addsd (%rax), %xmm1
movq (%r15), %r15
testq %r15, %r15
jne 0xa1fb
xorps %xmm0, %xmm0
cvtsi2sdl 0xe0(%rsp), %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
leaq 0x100(%rsp), %rdi
callq 0xa352
leaq 0x60(%rsp), %rdi
callq 0xa352
leaq 0xa0(%rsp), %rdi
callq 0xa352
leaq 0x138(%rsp), %rdi
callq 0x50cc
leaq 0x170(%rsp), %rdi
callq 0x513c
leaq 0x1a8(%rsp), %rdi
callq 0x9158
leaq 0x1e0(%rsp), %rdi
callq 0x50cc
movsd (%rsp), %xmm0
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x50cc
leaq 0x60(%rsp), %rdi
callq 0x50cc
jmp 0xa30e
jmp 0xa2d2
jmp 0xa2cd
jmp 0xa2cd
jmp 0xa2cd
movq %rax, %rbx
jmp 0xa2ea
movq %rax, %rbx
jmp 0xa30e
jmp 0xa2dd
jmp 0xa2dd
jmp 0xa2dd
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xa352
leaq 0x100(%rsp), %rdi
callq 0xa352
leaq 0x60(%rsp), %rdi
callq 0xa352
leaq 0xa0(%rsp), %rdi
callq 0xa352
leaq 0x138(%rsp), %rdi
callq 0x50cc
leaq 0x170(%rsp), %rdi
callq 0x513c
leaq 0x1a8(%rsp), %rdi
callq 0x9158
leaq 0x1e0(%rsp), %rdi
callq 0x50cc
movq %rbx, %rdi
callq 0x34a0