// File: /workspace/github_cpp_makefile_star2_O2/sebastienmeyer2[P]mst-clustering/src/kmeans.cpp
// Function: Cloud::init_plusplus()
void Cloud::init_plusplus()
{
    /*!
     * @brief Initializes the centers of this cloud using kmeans++
	 * which consists in drawing a distribution of points
     */

	// step of initialization for kmeans++
	int* chosenPoints = new int [this->k] ();
	int randomPoint = rand() % this->n;
	chosenPoints[0] = randomPoint;

	// loop 
	double* distances = new double [this->n] ();
	for (int i = 1; i < this->k; i ++){

		// we compute the nearest center for each data point
		double totalDist = 0.0;

		for (int j = 0; j < this->n; j ++){
			distances[j] = DBL_MAX;

			for (int l = 0; l < i; l ++){
				double dist = points[j].distance(&(points[chosenPoints[l]]));

				if (dist < distances[j]){
					distances[j] = dist;
				}

				totalDist += distances[j];
			}
		}

		for (int j = 0; j < this->n; j ++){
			distances[j] /= totalDist; 
			if (j > 0){
				distances[j] += distances[j-1]; // we create a cumulative sum
			}
		}

		// we find the next center from a uniform distribution
		double randomUnif = (double)rand() / RAND_MAX;

		for (int j = 0; j < this->n; j ++){
			if ( (j == 0 && distances[j] > randomUnif && randomUnif >= 0) \
					|| (j > 0 && distances[j] > randomUnif && randomUnif >= distances[j-1]) ){
				chosenPoints[i] = j;
			}
		}
	}

	// we just have to build our centers now!
	Point c;
	for (int i = 0; i < this->k; i ++){
		c.label = i;

		for (int j = 0; j < this->d; j ++){
			c.coords[j] = points[chosenPoints[i]].coords[j];
		}

		this->set_center(c, i);
	}		

	delete[] chosenPoints;
	chosenPoints = NULL;
	delete[] distances;
	distances = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movslq 0x8(%rdi), %rax
leaq (,%rax,4), %r15
testq %rax, %rax
pushq $-0x1
popq %rbp
cmovsq %rbp, %r15
movq %r15, %rdi
callq 0x3030
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3170
callq 0x30b0
movslq 0x4(%rbx), %r13
cltd
idivl %r13d
leaq (,%r13,8), %r12
testq %r13, %r13
movq %r14, (%rsp)
movl %edx, (%r14)
cmovsq %rbp, %r12
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
movq %rbx, %rdx
pushq $0x1
popq %r12
movq %rbx, %r14
movslq 0x8(%rdx), %rax
cmpq %rax, %r12
jge 0xd202
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
movslq %r13d, %rax
cmpq %rax, %rbp
jge 0xd157
movq %r15, %rbx
movabsq $0x7fefffffffffffff, %rax # imm = 0x7FEFFFFFFFFFFFFF
movq %rax, (%r15,%rbp,8)
movq %rbp, %r13
shlq $0x4, %r13
movsd 0x2567(%rip), %xmm2     # 0xf658
xorl %r15d, %r15d
cmpq %r15, %r12
je 0xd148
movsd %xmm1, 0x10(%rsp)
movq 0x10(%rdx), %rax
leaq (%rax,%r13), %rdi
movq (%rsp), %rcx
movslq (%rcx,%r15,4), %rsi
shlq $0x4, %rsi
addq %rax, %rsi
movsd %xmm2, 0x8(%rsp)
callq 0x391a
movsd 0x8(%rsp), %xmm2
ucomisd %xmm0, %xmm2
jbe 0xd136
movsd %xmm0, (%rbx,%rbp,8)
movapd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
addsd %xmm2, %xmm1
incq %r15
movq %r14, %rdx
jmp 0xd0f4
incq %rbp
movl 0x4(%rdx), %r13d
movq %rbx, %r15
jmp 0xd0c5
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %eax, %eax
cmpq %rax, %r13
je 0xd191
movsd (%r15,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r15,%rax,8)
testq %rax, %rax
je 0xd18c
addsd -0x8(%r15,%rax,8), %xmm0
movsd %xmm0, (%r15,%rax,8)
incq %rax
jmp 0xd165
callq 0x30b0
movq %r14, %rdx
cvtsi2sd %eax, %xmm0
divsd 0x276b(%rip), %xmm0     # 0xf910
movl 0x4(%r14), %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0xd1fa
testq %rcx, %rcx
je 0xd1d8
movsd (%r15,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xd1f5
ucomisd -0x8(%r15,%rcx,8), %xmm0
jae 0xd1ed
jmp 0xd1f5
ucomisd 0x2480(%rip), %xmm0     # 0xf660
jb 0xd1f5
movsd (%r15), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xd1f5
movq (%rsp), %rsi
movl %ecx, (%rsi,%r12,4)
incq %rcx
jmp 0xd1b7
incq %r12
jmp 0xd0b2
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rdx, %rbx
callq 0x3860
xorl %ebp, %ebp
xorl %r13d, %r13d
movslq 0x8(%rbx), %rax
cmpq %rax, %r13
jge 0xd272
movq %rbx, %rdi
movl %r13d, 0x20(%rsp)
movl (%rbx), %eax
movq 0x10(%rbx), %rcx
movq 0x18(%rsp), %rdx
testl %eax, %eax
cmovlel %ebp, %eax
xorl %esi, %esi
cmpq %rsi, %rax
je 0xd25f
movq (%rsp), %r8
movslq (%r8,%r13,4), %r8
shlq $0x4, %r8
movq (%rcx,%r8), %r8
movsd (%r8,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
jmp 0xd23a
movq %r12, %rsi
movl %r13d, %edx
movq %rdi, %rbx
callq 0xc9c4
incq %r13
jmp 0xd217
movq (%rsp), %rdi
callq 0x3370
movq %r15, %rdi
callq 0x3370
leaq 0x18(%rsp), %rdi
callq 0x38a8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq