// File: /workspace/github_cpp_makefile_star2_O2/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, 0xa0(%rsp)
leaq 0x1d8(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rdx
movq %rdx, -0x28(%rax)
leaq 0x1b8(%rsp), %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rcx)
movl $0x3f800000, %eax       # imm = 0x3F800000
movl %eax, 0x10(%rcx)
movupd %xmm0, 0x18(%rcx)
leaq 0x1a0(%rsp), %rcx
movq %rcx, -0x30(%rcx)
movq %rdx, -0x28(%rcx)
movupd %xmm0, -0x20(%rcx)
movl %eax, -0x10(%rcx)
movupd %xmm0, -0x8(%rcx)
leaq 0x210(%rsp), %rcx
movq %rcx, -0x30(%rcx)
movq %rdx, -0x28(%rcx)
movupd %xmm0, -0x20(%rcx)
movl %eax, -0x10(%rcx)
movupd %xmm0, -0x8(%rcx)
leaq 0x130(%rsp), %rcx
movq %rcx, -0x30(%rcx)
movq %rdx, -0x28(%rcx)
leaq 0x110(%rsp), %rcx
movupd %xmm0, (%rcx)
movl %eax, 0x10(%rcx)
movupd %xmm0, 0x18(%rcx)
leaq 0x10(%rdi), %r13
leaq 0x100(%rsp), %r12
leaq 0x138(%rsp), %r14
leaq 0x1a8(%rsp), %r15
leaq 0x30(%rsp), %rbp
movq (%r13), %r13
testq %r13, %r13
je 0x8a27
movq 0x8(%r13), %rax
movq 0x10(%r13), %rcx
movq %rax, 0x138(%rsp)
movq %rcx, 0x30(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x593a
movq %r15, %rdi
movq %rbp, %rsi
callq 0x593a
leaq 0x170(%rsp), %rdi
movq %rbp, %rsi
callq 0x80ec
testq %rax, %rax
jne 0x8941
movq 0x30(%rsp), %rax
movq %rax, 0xc0(%rsp)
andl $0x0, 0xc8(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x8186
leaq 0x170(%rsp), %rdi
movq %rbp, %rsi
callq 0x8168
incl (%rax)
leaq 0x1e0(%rsp), %rdi
movq %rbp, %rsi
callq 0x551e
testq %rax, %rax
jne 0x8a07
movq %r15, %rbp
movq %r14, %r15
movq %r12, %r14
andq $0x0, 0x88(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0x68(%rsp)
movq $0x1, 0x70(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, 0x88(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq 0x30(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0x68(%rsp), %r12
movq %r12, %rsi
callq 0x49a8
leaq 0x1e0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x559a
leaq 0xc8(%rsp), %rdi
callq 0x4a48
movq %r12, %rdi
callq 0x4a48
movq %r14, %r12
movq %r15, %r14
movq %rbp, %r15
leaq 0x30(%rsp), %rbp
leaq 0x1e0(%rsp), %rdi
movq %rbp, %rsi
callq 0x58a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x593a
jmp 0x88ca
leaq 0xf0(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rdx
movq %rdx, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, %ecx       # imm = 0x3F800000
movl %ecx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x68(%rsp), %rsi
leaq 0x98(%rsp), %rax
movq %rax, (%rsi)
movq %rdx, 0x8(%rsi)
movupd %xmm0, 0x10(%rsi)
movl %ecx, 0x20(%rsi)
movupd %xmm0, 0x28(%rsi)
leaq 0x168(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rdx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %ecx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
movq 0x110(%rsp), %rbp
leaq 0xc0(%rsp), %r15
leaq 0x30(%rsp), %r12
leaq 0x138(%rsp), %r13
testq %rbp, %rbp
je 0x8afe
movq 0x8(%rbp), %r14
movq %r14, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x8fd4
movq %r14, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
leaq 0x68(%rsp), %rdi
movq %r12, %rsi
callq 0x8fd4
movq %r14, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x8fd4
movq (%rbp), %rbp
jmp 0x8aab
movq 0x110(%rsp), %rax
leaq 0x18(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x20(%rsp), %r13
testq %rax, %rax
je 0x8db4
movq %rax, 0xb0(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
movq $0x1, 0x38(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, 0x50(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
leaq 0x1b8(%rsp), %r12
movq (%r12), %r12
testq %r12, %r12
je 0x8b86
movq 0x8(%r12), %rax
movq %rax, 0x20(%rsp)
andq $0x0, 0x28(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x8fd4
jmp 0x8b60
leaq 0x110(%rsp), %r12
movq (%r12), %r12
testq %r12, %r12
je 0x8c08
movq 0x8(%r12), %rax
movq %rax, 0x20(%rsp)
cmpq 0x18(%rsp), %rax
je 0x8b8e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9310
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x847e
ucomisd 0x6a8b(%rip), %xmm0     # 0xf660
jb 0x8bdd
sqrtsd %xmm0, %xmm0
jmp 0x8be2
callq 0x3240
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x93aa
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x8b8e
leaq 0x1b8(%rsp), %r12
movsd 0x6a40(%rip), %xmm0     # 0xf658
movsd %xmm0, 0x8(%rsp)
movq (%r12), %r12
testq %r12, %r12
je 0x8c9e
movq 0x8(%r12), %rax
movq %rax, 0x20(%rsp)
leaq 0x170(%rsp), %rdi
movq %r13, %rsi
callq 0x8168
movl (%rax), %ebp
movq %r15, %rdi
movq %r13, %rsi
callq 0x93aa
cvtsi2sd %ebp, %xmm0
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax)
movq 0x20(%rsp), %rbp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9310
cmpq (%rax), %rbp
je 0x8c1e
movq %r15, %rdi
movq %r13, %rsi
callq 0x93aa
movsd 0x8(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x8c1e
movq %r15, %rdi
movq %r13, %rsi
callq 0x93aa
movsd (%rax), %xmm0
jmp 0x8c18
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9310
movq %r15, %rdi
movq %rax, %rsi
callq 0x93aa
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x93aa
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x6978(%rip), %xmm0     # 0xf658
ucomisd 0x8(%rsp), %xmm0
jbe 0x8cff
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x93aa
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x93aa
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x93aa
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x93aa
movq %rax, %r12
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x93aa
movsd (%r12), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x138(%rsp), %rdi
movq %r14, %rsi
callq 0x93aa
movsd 0x10(%rsp), %xmm1
maxsd 0xa8(%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq %r15, %rdi
callq 0x8f2c
movq 0xb0(%rsp), %rax
movq (%rax), %rax
jmp 0x8b15
xorpd %xmm1, %xmm1
leaq 0x138(%rsp), %rbx
leaq 0x30(%rsp), %r14
leaq 0x110(%rsp), %r15
movq (%r15), %r15
testq %r15, %r15
je 0x8dfb
movsd %xmm1, 0x8(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x93aa
movsd 0x8(%rsp), %xmm1
addsd (%rax), %xmm1
jmp 0x8dcd
cvtsi2sdl 0xa0(%rsp), %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x8f2c
leaq 0x68(%rsp), %rdi
callq 0x8f2c
leaq 0xc0(%rsp), %rdi
callq 0x8f2c
leaq 0x100(%rsp), %rdi
callq 0x4a48
leaq 0x1e0(%rsp), %rdi
callq 0x4af0
leaq 0x170(%rsp), %rdi
callq 0x7dd0
leaq 0x1a8(%rsp), %rdi
callq 0x4a48
movsd 0x8(%rsp), %xmm0
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8eab
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x4a48
leaq 0x68(%rsp), %rdi
callq 0x4a48
jmp 0x8ee7
jmp 0x8ea2
jmp 0x8ea2
jmp 0x8ea2
movq %rax, %rbx
jmp 0x8ec3
jmp 0x8eab
jmp 0x8eab
movq %rax, %rbx
jmp 0x8ee7
jmp 0x8eb6
jmp 0x8eb6
jmp 0x8eb6
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x8f2c
leaq 0x138(%rsp), %rdi
callq 0x8f2c
leaq 0x68(%rsp), %rdi
callq 0x8f2c
leaq 0xc0(%rsp), %rdi
callq 0x8f2c
leaq 0x100(%rsp), %rdi
callq 0x4a48
leaq 0x1e0(%rsp), %rdi
callq 0x4af0
leaq 0x170(%rsp), %rdi
callq 0x7dd0
leaq 0x1a8(%rsp), %rdi
callq 0x4a48
movq %rbx, %rdi
callq 0x34b0
nop