// File: /workspace/github_cpp_makefile_star2_O2/sebastienmeyer2[P]mst-clustering/src/kmeans.cpp
// Function: Cloud::silhouette()
double Cloud::silhouette()
{
    /*!
     * @brief Computes the silhouette of this cloud 
     * 
     * @return The silhouette of this cloud
     */

	double silhouette = 0.0;

	// 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
	double* a = new double [n] ();
	double* b = new double [n] ();
	double* s = new double [n] ();

	for (int i = 0; i < this->n; i ++){
		double* distPointClusters = new double [k] ();
		int* clustersSize = new int [k] ();
		
		for (int j = 0; j < this->n; j ++){
			if (j != i){
				int pointLabel = points[j].label;
				clustersSize[pointLabel] += 1;

				distPointClusters[pointLabel] += std::sqrt(points[i].distance(&(points[j])));
			}
		}

		double minDist = DBL_MAX;
		for (int j = 0; j < this->k; j ++){
			if (clustersSize[j] > 0){
				distPointClusters[j] /= clustersSize[j];

				if (j != points[i].label && distPointClusters[j] < minDist) {
					minDist = distPointClusters[j];
				}
			}
		}

		a[i] = distPointClusters[points[i].label];
		b[i] = 0.0;
		if (minDist < DBL_MAX){
			b[i] = minDist;
		}

		delete[] distPointClusters;
		distPointClusters = NULL;
		delete[] clustersSize;
		clustersSize = NULL;

		s[i] = (b[i] - a[i]) / (std::max(a[i], b[i]));
	}

	for (int i = 0; i < this->n; i ++){
		silhouette += s[i];
	}
	silhouette /= this->n;

	return silhouette;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, (%rsp)
movslq 0x4(%rdi), %r14
leaq (,%r14,8), %r13
testq %r14, %r14
pushq $-0x1
popq %rax
cmovsq %rax, %r13
movq %r13, %rdi
callq 0x3030
movq %rax, 0x20(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3170
movq %r13, %rdi
callq 0x3030
movq %rax, 0x10(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3170
movq %r13, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3170
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rbx, 0x18(%rsp)
movslq %r14d, %rax
cmpq %rax, %r12
jge 0xd527
movq %r12, 0x8(%rsp)
movq (%rsp), %rax
movslq 0x8(%rax), %rax
leaq (,%rax,8), %rbp
testq %rax, %rax
pushq $-0x1
popq %rcx
cmovsq %rcx, %rbp
leaq (,%rax,4), %rbx
cmovsq %rcx, %rbx
movq %rbp, %rdi
callq 0x3030
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3170
movq %rbx, %rdi
callq 0x3030
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x3170
xorl %r12d, %r12d
xorl %ebx, %ebx
movslq %r14d, %rax
cmpq %rax, %rbx
jge 0xd42f
cmpq %r12, %r15
je 0xd426
movq (%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x8(%rax,%r12), %r14
incl (%rbp,%r14,4)
leaq (%rax,%r12), %rsi
movq 0x8(%rsp), %rdi
shlq $0x4, %rdi
addq %rax, %rdi
callq 0x391a
ucomisd 0x225d(%rip), %xmm0     # 0xf660
jb 0xd40b
sqrtsd %xmm0, %xmm0
jmp 0xd410
callq 0x3240
addsd (%r13,%r14,8), %xmm0
movsd %xmm0, (%r13,%r14,8)
movq (%rsp), %rax
movl 0x4(%rax), %r14d
incq %rbx
addq $0x10, %r12
jmp 0xd3c7
movq (%rsp), %rcx
movl 0x8(%rcx), %eax
movq 0x10(%rcx), %rcx
movq 0x8(%rsp), %r12
movq %r12, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
addq $0x8, %rcx
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
xorl %edx, %edx
movsd 0x21f7(%rip), %xmm0     # 0xf658
cmpq %rdx, %rax
je 0xd49a
movl (%rbp,%rdx,4), %esi
testl %esi, %esi
jle 0xd495
cvtsi2sd %esi, %xmm2
movsd (%r13,%rdx,8), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, (%r13,%rdx,8)
movl (%rcx), %esi
cmpq %rsi, %rdx
je 0xd495
ucomisd %xmm1, %xmm0
jbe 0xd495
movapd %xmm1, %xmm0
incq %rdx
jmp 0xd461
movslq (%rcx), %rax
movsd (%r13,%rax,8), %xmm1
movsd %xmm1, 0x28(%rsp)
movq 0x20(%rsp), %rbx
movsd %xmm1, (%rbx,%r12,8)
movapd %xmm0, %xmm1
cmpltsd 0x2196(%rip), %xmm1     # 0xf658
andpd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
movq 0x10(%rsp), %r14
movlpd %xmm1, (%r14,%r12,8)
movq %r13, %rdi
callq 0x3370
movq %rbp, %rdi
callq 0x3370
movsd 0x28(%rsp), %xmm1
movapd 0x30(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq %rbx, %rax
cmovaq %r14, %rax
subsd %xmm1, %xmm0
divsd (%rax,%r12,8), %xmm0
movq 0x18(%rsp), %rbx
movsd %xmm0, (%rbx,%r12,8)
incq %r12
movq (%rsp), %rax
movl 0x4(%rax), %r14d
addq $0x10, %r15
jmp 0xd35b
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
xorpd %xmm0, %xmm0
cmpq %rax, %rcx
je 0xd548
addsd (%rbx,%rax,8), %xmm0
incq %rax
jmp 0xd539
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq