// File: /workspace/github_cpp_makefile_star2_O3/sebastienmeyer2[P]mst-clustering/src/kmeans.cpp
// Function: Cloud::init_forgy()
void Cloud::init_forgy()
{
    /*!
     * @brief Initializes the centers of this cloud using Forgy method,
	 * that is it samples the centers uniformly from the data points
     */

	// we will pick points from our data 
	int* availablePoints = new int [this->n] ();
	for (int i = 0; i < this->n; i ++){
		availablePoints[i] = 1;
	}

	// we must know what we've chosen so far
	int* chosenPoints = new int [this->k] ();
	int leftCenters = this->k;

	while (leftCenters > 0){
		int randomPoint = rand() % this->n;

		if (availablePoints[randomPoint] > 0){
			availablePoints[randomPoint] = 0;
			chosenPoints[this->k - leftCenters] = randomPoint;
			leftCenters -= 1;
		}
	}

	// 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[] availablePoints;
	availablePoints = NULL;
	delete[] chosenPoints;
	chosenPoints = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
movslq 0x4(%rdi), %r12
leaq (,%r12,4), %r15
testq %r12, %r12
movq $-0x1, %r13
cmovsq %r13, %r15
movq %r15, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3130
testq %r12, %r12
jle 0xee1a
leal 0x3(%r12), %eax
decq %r12
movq %r12, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %eax
shlq $0x4, %rax
movdqa 0x2bff(%rip), %xmm1     # 0x11940
movdqa 0x2c07(%rip), %xmm2     # 0x11950
xorl %ecx, %ecx
movdqa 0x2c0d(%rip), %xmm3     # 0x11960
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x2c0d(%rip), %xmm5     # 0x11970
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0xed9e
movl $0x1, (%rbx,%rcx)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0xedb4
movl $0x1, 0x4(%rbx,%rcx)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edx
testb $0x1, %dl
je 0xedf3
movl $0x1, 0x8(%rbx,%rcx)
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0xee05
movl $0x1, 0xc(%rbx,%rcx)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0xed63
movslq 0x8(%r14), %rbp
leaq (,%rbp,4), %r12
testq %rbp, %rbp
cmovsq %r13, %r12
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3130
testq %rbp, %rbp
jle 0xee78
callq 0x30a0
cltd
idivl 0x4(%r14)
movslq %edx, %rax
cmpl $0x0, (%rbx,%rax,4)
jle 0xee74
movl $0x0, (%rbx,%rax,4)
movslq 0x8(%r14), %rcx
movslq %ebp, %rbp
subq %rbp, %rcx
movl %eax, (%r15,%rcx,4)
decl %ebp
testl %ebp, %ebp
jg 0xee4a
leaq 0x8(%rsp), %rdi
callq 0x3a20
movslq 0x8(%r14), %rax
testq %rax, %rax
jle 0xeefc
movl (%r14), %ecx
movq 0x10(%r14), %rdx
movq 0x8(%rsp), %rsi
movq 0x18(%r14), %rdi
xorl %r9d, %r9d
movq %r9, %r8
testl %ecx, %ecx
jle 0xeeee
movslq (%r15,%r8,4), %r9
shlq $0x4, %r9
movq (%rdx,%r9), %r9
xorl %r10d, %r10d
movq (%r9,%r10,8), %xmm0
movq %xmm0, (%rsi,%r10,8)
incq %r10
cmpq %r10, %rcx
jne 0xeeb4
testl %ecx, %ecx
jle 0xeeee
movq %r8, %r9
shlq $0x4, %r9
movq (%rdi,%r9), %r9
xorl %r10d, %r10d
movq (%rsi,%r10,8), %xmm0
movq %xmm0, (%r9,%r10,8)
incq %r10
cmpq %r10, %rcx
jne 0xeeda
leaq 0x1(%r8), %r9
cmpq %rax, %r9
jne 0xee9e
movl %r8d, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3360
movq %r15, %rdi
callq 0x3360
leaq 0x8(%rsp), %rdi
callq 0x3a6e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop