// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movslq 0x4(%rdi), %r12
leaq (,%r12,8), %r13
testq %r12, %r12
movq $-0x1, %rax
cmovsq %rax, %r13
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, 0x18(%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
testq %r12, %r12
jle 0xf5fc
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq %rbx, 0x10(%rsp)
movslq 0x8(%rax), %rbx
leaq (,%rbx,8), %rbp
testq %rbx, %rbx
movq $-0x1, %rax
cmovsq %rax, %rbp
leaq (,%rbx,4), %r15
cmovsq %rax, %r15
movq %rbp, %rdi
callq 0x3030
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3130
movq %r15, %rdi
callq 0x3030
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3130
testl %r12d, %r12d
jle 0xf4e2
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x8(%rsp), %rcx
cmpq %rbx, %r14
je 0xf4ce
movq 0x10(%rcx), %rax
movslq 0x8(%rax,%r15), %r12
incl (%rbp,%r12,4)
leaq (%rax,%r15), %rsi
movq %r14, %rdi
shlq $0x4, %rdi
addq %rax, %rdi
callq 0x3b3a
ucomisd 0x21e6(%rip), %xmm0     # 0x11690
jb 0xf4b2
sqrtsd %xmm0, %xmm0
jmp 0xf4b7
callq 0x3210
addsd (%r13,%r12,8), %xmm0
movsd %xmm0, (%r13,%r12,8)
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %r12d
incq %rbx
movslq %r12d, %rax
addq $0x10, %r15
cmpq %rax, %rbx
jl 0xf47c
movl 0x8(%rcx), %ebx
jmp 0xf4e7
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movsd 0x2195(%rip), %xmm0     # 0x11688
testl %ebx, %ebx
jle 0xf54b
movq %r14, %rcx
shlq $0x4, %rcx
addq %rax, %rcx
addq $0x8, %rcx
movl %ebx, %edx
xorl %esi, %esi
movsd 0x2177(%rip), %xmm0     # 0x11688
movl (%rbp,%rsi,4), %edi
testl %edi, %edi
jle 0xf543
xorps %xmm2, %xmm2
cvtsi2sd %edi, %xmm2
movsd (%r13,%rsi,8), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, (%r13,%rsi,8)
movl (%rcx), %edi
cmpq %rdi, %rsi
je 0xf543
ucomisd %xmm1, %xmm0
jbe 0xf543
movapd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %rdx
jne 0xf511
movq %r14, %rcx
shlq $0x4, %rcx
movslq 0x8(%rax,%rcx), %rax
movsd (%r13,%rax,8), %xmm1
movsd %xmm1, 0x28(%rsp)
movq 0x20(%rsp), %rbx
movsd %xmm1, (%rbx,%r14,8)
movapd %xmm0, %xmm1
cmpltsd 0x210c(%rip), %xmm1     # 0x11688
andpd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
movq 0x18(%rsp), %r15
movlpd %xmm1, (%r15,%r14,8)
movq %r13, %rdi
callq 0x3360
movq %rbp, %rdi
callq 0x3360
movsd 0x28(%rsp), %xmm1
movapd 0x30(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq %rbx, %rax
cmovaq %r15, %rax
subsd %xmm1, %xmm0
divsd (%rax,%r14,8), %xmm0
movq 0x10(%rsp), %rbx
movsd %xmm0, (%rbx,%r14,8)
incq %r14
movq 0x8(%rsp), %rax
movslq 0x4(%rax), %r12
cmpq %r12, %r14
jl 0xf417
testl %r12d, %r12d
jle 0xf5fc
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%rbx,%rax,8), %xmm0
incq %rax
cmpq %rax, %r12
jne 0xf5ed
jmp 0xf600
xorpd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
divsd %xmm1, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop