// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movslq 0x4(%rdi), %rax
leaq (,%rax,8), %r13
testq %rax, %rax
movq $-0x1, %rax
cmovsq %rax, %r13
movq %r13, %rdi
callq 0x3030
movq %rax, 0x28(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3130
movq %r13, %rdi
callq 0x3030
movq %rax, 0x20(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3130
movq %r13, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3130
movl 0x4(%r14), %eax
testl %eax, %eax
jle 0xf169
xorl %r15d, %r15d
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movslq 0x8(%r14), %rax
leaq (,%rax,8), %rbp
testq %rax, %rax
movq $-0x1, %rcx
cmovsq %rcx, %rbp
leaq (,%rax,4), %r14
cmovsq %rcx, %r14
movq %rbp, %rdi
callq 0x3030
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3130
movq %r14, %rdi
callq 0x3030
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq 0x10(%rsp), %r14
callq 0x3130
cmpl $0x0, 0x4(%r14)
jle 0xf06c
movq %r15, %rax
shlq $0x4, %rax
movq %rax, 0x8(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0xf05c
movq 0x10(%r14), %rdi
leaq (%rdi,%r12), %rsi
movslq 0x8(%rdi,%r12), %r14
incl (%rbp,%r14,4)
addq 0x8(%rsp), %rdi
callq 0x3b3a
ucomisd 0x2624(%rip), %xmm0     # 0x11660
jb 0xf044
sqrtsd %xmm0, %xmm0
jmp 0xf049
callq 0x3210
addsd (%r13,%r14,8), %xmm0
movsd %xmm0, (%r13,%r14,8)
movq 0x10(%rsp), %r14
incq %rbx
movslq 0x4(%r14), %rax
addq $0x10, %r12
cmpq %rax, %rbx
jl 0xf013
movslq 0x8(%r14), %rax
movsd 0x25e0(%rip), %xmm0     # 0x11658
testq %rax, %rax
jle 0xf0d3
movq 0x10(%r14), %rcx
movq %r15, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
addq $0x8, %rcx
xorl %edx, %edx
movsd 0x25bf(%rip), %xmm0     # 0x11658
movl (%rbp,%rdx,4), %esi
testl %esi, %esi
jle 0xf0cb
xorps %xmm2, %xmm2
cvtsi2sd %esi, %xmm2
movsd (%r13,%rdx,8), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, (%r13,%rdx,8)
movl (%rcx), %esi
cmpq %rsi, %rdx
je 0xf0cb
ucomisd %xmm1, %xmm0
jbe 0xf0cb
movapd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0xf099
movq 0x10(%r14), %rax
movq %r15, %rcx
shlq $0x4, %rcx
movslq 0x8(%rax,%rcx), %rax
movsd (%r13,%rax,8), %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0x28(%rsp), %r12
movsd %xmm1, (%r12,%r15,8)
movapd %xmm0, %xmm1
cmpltsd 0x2550(%rip), %xmm1     # 0x11658
andpd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
movq 0x20(%rsp), %rbx
movlpd %xmm1, (%rbx,%r15,8)
movq %r13, %rdi
callq 0x3360
movq %rbp, %rdi
callq 0x3360
movsd 0x8(%rsp), %xmm1
movapd 0x30(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq %r12, %rax
cmovaq %rbx, %rax
subsd %xmm1, %xmm0
divsd (%rax,%r15,8), %xmm0
movq 0x18(%rsp), %rbx
movsd %xmm0, (%rbx,%r15,8)
incq %r15
movslq 0x4(%r14), %rax
cmpq %rax, %r15
jl 0xefa0
testl %eax, %eax
jle 0xf184
movl %eax, %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
addsd (%rbx,%rdx,8), %xmm0
incq %rdx
cmpq %rdx, %rcx
jne 0xf175
jmp 0xf188
xorpd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop