// File: isaacbrodsky[P]K-means-plus-plus/k-means-multi.cpp
// Function: Cluster_set::Initialize_plus_plus()
void Cluster_set::Initialize_plus_plus(void) {
	// Initializes using K-means++.

	// Number of instances in the input data
	size_t szData = vclInput_data.size();
	size_t uIndex;
	// Points that have already been selected as starting points.
	// true indicates already selected, false not selected
	// as a starting point.
	vector<bool> vbSkipPoints;
	vector<float> vfDistance;
	int iSelectedPoints;
	int iAttribute_index;
	float fTotalDistance;
	float fRandomDistance;

	vbSkipPoints.resize(szData);
	vfDistance.resize(szData);

	// Select the first data instance as the initial mean
	// (by copying it into the list of means and marking
	// it as selected in our list of points to skip.)
	for (iAttribute_index = 0; iAttribute_index < iAttribute_ct; iAttribute_index++){ // read attributes
		vvfMeans[0][iAttribute_index]
			= vclInput_data[0].vfAttribute[iAttribute_index];
	} // for
	vbSkipPoints[0] = true;
	iSelectedPoints = 1;

	// While we don't have enough starting clusters
	for (; iSelectedPoints < iK_count; iSelectedPoints++) {
		fTotalDistance = 0;

		if (iNumPlusPlusThreads == 1)
		{
			//Don't bother creating more threads.
			fTotalDistance = Initialize_plus_plus_process(0, szData, iSelectedPoints, vfDistance, vbSkipPoints);
		}
		else
		{
			// local variables
			unsigned uPerThread;
			unsigned uDataStart;
			int iThread_index;
			vector<future<float>> vtThreads;

			uDataStart = 0;
			uPerThread = szData / iNumThreads;

			//Split the dataset into parts and launch as individual
			//threads.
			for (iThread_index = 0; iThread_index < iNumPlusPlusThreads; iThread_index++)
			{
				//Force the last thread to take remaining data
				if (iThread_index == iNumPlusPlusThreads - 1)
				{
					uPerThread = szData - uDataStart;
				}

				vtThreads.push_back(async(launch::async, [=, &vfDistance, &vbSkipPoints](unsigned uStart, unsigned uLength) {
					return Initialize_plus_plus_process(uStart, uLength, iSelectedPoints, vfDistance, vbSkipPoints); }, uDataStart, uPerThread));

				uDataStart += uPerThread;
			} // Launch all threads

			//Wait for all threads to complete.
			for (iThread_index = 0; iThread_index < iNumPlusPlusThreads; iThread_index++)
			{
				fTotalDistance += vtThreads[iThread_index].get();
			} // Wait for all threads
		}

		// Determine which instance to take as a new starting cluster
		// First generate a random number uniformly between [0, fTotalDistance)
		// using mtRandom as the PRNG.
		fRandomDistance = uniform_real_distribution<float>(0, fTotalDistance)(mtRandom);
		for (uIndex = 0; uIndex < szData; uIndex++) {
			// Don't consider points already selected as starting points.
			if (!vbSkipPoints[uIndex]) {
				// Data instances with a larger distance will have a higher effect
				// here, biasing the k-means++ algorithm toward selecting them.
				fRandomDistance -= vfDistance[uIndex];
				if (fRandomDistance <= 0) {
					// Select this point as a starting point
					// (by copying it into the means vector
					// and marking it as used in our own
					// vector of points to skip.)
					for (iAttribute_index = 0; iAttribute_index < iAttribute_ct; iAttribute_index++){
						vvfMeans[iSelectedPoints][iAttribute_index]
							= vclInput_data[uIndex].vfAttribute[iAttribute_index];
					} // for
					vbSkipPoints[uIndex] = true;
					
					// Stop selecting the next starting point
					break;
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x118(%rbp)
subq $-0x80, %rdi
callq 0xa5b0
movq %rax, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0xa6c0
leaq -0x58(%rbp), %rdi
callq 0xa0a0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0xa6e0
jmp 0x6b84
movq -0x10(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xa330
jmp 0x6b93
movl $0x0, -0x60(%rbp)
movq -0x118(%rbp), %rcx
movl -0x60(%rbp), %eax
cmpl 0x9c(%rcx), %eax
jge 0x6c1f
movq -0x118(%rbp), %rdi
addq $0x80, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xa5f0
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0xa3c0
movq -0x118(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x11c(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0xa4e0
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0xa3c0
movss -0x11c(%rbp), %xmm0
movss %xmm0, (%rax)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x6b9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x7080
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xa800
movq %rdx, -0x130(%rbp)
movq %rax, -0x128(%rbp)
jmp 0x6c3c
movq -0x130(%rbp), %rax
movq -0x128(%rbp), %rcx
movq %rcx, -0x88(%rbp)
movq %rax, -0x80(%rbp)
leaq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0xa860
movl $0x1, -0x5c(%rbp)
movq -0x118(%rbp), %rcx
movl -0x5c(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x7065
movq -0x118(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, -0x64(%rbp)
cmpl $0x1, 0x1430(%rax)
jne 0x6cd0
movq -0x118(%rbp), %rdi
movl -0x10(%rbp), %edx
movl -0x5c(%rbp), %ecx
xorl %esi, %esi
leaq -0x58(%rbp), %r8
leaq -0x40(%rbp), %r9
callq 0x6990
movss %xmm0, -0x134(%rbp)
jmp 0x6cbe
movss -0x134(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
jmp 0x6ea0
leaq -0xb0(%rbp), %rdi
callq 0xa8b0
movq -0x118(%rbp), %rcx
movl $0x0, -0x90(%rbp)
movq -0x10(%rbp), %rax
movslq 0x1434(%rcx), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, -0x8c(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x118(%rbp), %rcx
movl -0x94(%rbp), %eax
cmpl 0x1430(%rcx), %eax
jge 0x6e1a
movq -0x118(%rbp), %rcx
movl -0x94(%rbp), %eax
movl 0x1430(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x6d53
movq -0x10(%rbp), %rax
movl -0x90(%rbp), %ecx
subq %rcx, %rax
movl %eax, -0x8c(%rbp)
movq -0x118(%rbp), %rax
leaq -0x58(%rbp), %rcx
movq %rcx, -0xe0(%rbp)
leaq -0x40(%rbp), %rcx
movq %rcx, -0xd8(%rbp)
movq %rax, -0xd0(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
movl $0x1, %esi
leaq -0xe0(%rbp), %rdx
leaq -0x90(%rbp), %rcx
leaq -0x8c(%rbp), %r8
callq 0x70a0
jmp 0x6da8
leaq -0xb0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0xa8d0
jmp 0x6dbd
leaq -0xc0(%rbp), %rdi
callq 0xa900
movl -0x8c(%rbp), %eax
addl -0x90(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x6d0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x6e8f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0xa900
jmp 0x6e8f
movl $0x0, -0x94(%rbp)
movq -0x118(%rbp), %rcx
movl -0x94(%rbp), %eax
cmpl 0x1430(%rcx), %eax
jge 0x6e81
movslq -0x94(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0xa920
movq %rax, %rdi
callq 0xa940
movss %xmm0, -0x138(%rbp)
jmp 0x6e5e
movss -0x138(%rbp), %xmm0
addss -0x64(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x6e24
leaq -0xb0(%rbp), %rdi
callq 0xa9b0
jmp 0x6ea0
leaq -0xb0(%rbp), %rdi
callq 0xa9b0
jmp 0x7080
movss -0x64(%rbp), %xmm1
leaq -0xe8(%rbp), %rdi
xorps %xmm0, %xmm0
callq 0xaa10
jmp 0x6eb6
movq -0x118(%rbp), %rsi
addq $0xa8, %rsi
leaq -0xe8(%rbp), %rdi
callq 0xaa40
movss %xmm0, -0x13c(%rbp)
jmp 0x6eda
movss -0x13c(%rbp), %xmm0
movss %xmm0, -0x68(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x7055
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xa800
movq %rdx, -0x150(%rbp)
movq %rax, -0x148(%rbp)
jmp 0x6f1a
movq -0x150(%rbp), %rax
movq -0x148(%rbp), %rcx
movq %rcx, -0xf8(%rbp)
movq %rax, -0xf0(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xaa70
xorb $-0x1, %al
testb $0x1, %al
jne 0x6f4d
jmp 0x7042
movq -0x18(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xa3c0
movss (%rax), %xmm1
movss -0x68(%rbp), %xmm0
subss %xmm1, %xmm0
movss %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x68(%rbp), %xmm0
jb 0x7040
movl $0x0, -0x60(%rbp)
movq -0x118(%rbp), %rcx
movl -0x60(%rbp), %eax
cmpl 0x9c(%rcx), %eax
jge 0x6ff4
movq -0x118(%rbp), %rdi
addq $0x80, %rdi
movq -0x18(%rbp), %rsi
callq 0xa5f0
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0xa3c0
movq -0x118(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x154(%rbp)
movslq -0x5c(%rbp), %rsi
callq 0xa4e0
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0xa3c0
movss -0x154(%rbp), %xmm0
movss %xmm0, (%rax)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x6f80
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xa800
movq %rdx, -0x168(%rbp)
movq %rax, -0x160(%rbp)
jmp 0x7011
movq -0x168(%rbp), %rax
movq -0x160(%rbp), %rcx
movq %rcx, -0x108(%rbp)
movq %rax, -0x100(%rbp)
leaq -0x108(%rbp), %rdi
movl $0x1, %esi
callq 0xa860
jmp 0x7055
jmp 0x7042
jmp 0x7044
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x6eef
jmp 0x7057
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x6c6d
leaq -0x58(%rbp), %rdi
callq 0x4c40
leaq -0x40(%rbp), %rdi
callq 0xaaa0
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x4c40
leaq -0x40(%rbp), %rdi
callq 0xaaa0
movq -0x70(%rbp), %rdi
callq 0x4530
nopl (%rax,%rax)