// File: /workspace/github_cpp_makefile_star2_O2/CK7[P]abawaca/src/ClusterSeparator.cpp
// Function: ClusterSeparator::separate()
bool ClusterSeparator::separate()
{
	// Start by cleaning everything from previous runs
	if(this->cluster1 != NULL)
		delete(this->cluster1);
	if(this->cluster2 != NULL)
		delete(this->cluster2);
	this->cluster1 = this->cluster2 = NULL;
	this->best_separation->reset();
	this->raw_dps_cluster1.clear();
	this->raw_dps_cluster2.clear();

/*	for(size_t dimension_index=1; dimension_index<=clustering_db.ndimensions(); dimension_index++) {
		this->separate_dimension(dimension_index);
	}*/
	thread*	threads = new thread[clustering_db.ndimensions()];
	for(size_t dimension_index=1; dimension_index<=clustering_db.ndimensions(); dimension_index++) {
		threads[dimension_index-1] = thread(call_separate_dimension, this, dimension_index);
	}
	for(auto i=0; i<clustering_db.ndimensions(); i++)
		threads[i].join();
	delete[] (threads);

	this->reconstruct_best_clusters();

	// If no significant separation was found simply return false
	if(cluster1 == NULL)
		return false;

	// Before switching datapoints between the clusters keep the original sets
	for(auto sit = cluster1->dps_begin(); sit != cluster1->dps_end(); sit++)
		raw_dps_cluster1.insert(*sit);
	for(auto sit = cluster2->dps_begin(); sit != cluster2->dps_end(); sit++)
		raw_dps_cluster2.insert(*sit);

	// Now decide, for each scaffold, to which cluster it belongs. Cluster1 will contain all scaffolds that have at least half
	// of their datapoints in it, cluster2 will contain the rest
	for(auto mit = cluster1->scafs_begin(); mit != cluster1->scafs_end(); mit++)
		if(2*mit->second >= scaf_db.ndps(mit->first)) {
			cluster1->add_assigned_scaf(mit->first);
		}
	for(auto mit = cluster2->scafs_begin(); mit != cluster2->scafs_end(); mit++)
		if(2*mit->second > scaf_db.ndps(mit->first)) {
			cluster2->add_assigned_scaf(mit->first);
		}

	// Now move the datapoints based on their scaffold assignment
	set<size_t> dps;
	for(auto sit = cluster1->dps_begin(); sit != cluster1->dps_end(); sit++)
		if(!cluster1->assigned(scaf_db.dp2scaf(*sit)))
			dps.insert(*sit);

	for(auto sit = dps.begin(); sit != dps.end(); sit++) {
		*cluster1 -= *sit;
		*cluster2 += *sit;
	}

	dps.clear();
	for(auto sit = cluster2->dps_begin(); sit != cluster2->dps_end(); sit++)
		if(!cluster2->assigned(scaf_db.dp2scaf(*sit)))
			dps.insert(*sit);

	for(auto sit = dps.begin(); sit != dps.end(); sit++) {
		*cluster1 += *sit;
		*cluster2 -= *sit;
	}

	// Check again if the resulting clusters are sufficiently large.
	if((cluster1->ndps() < this->cluster_ndps_threshold) || (cluster2->ndps() < this->cluster_ndps_threshold)) {
		delete(cluster1);
		cluster1 = NULL;
		delete(cluster2);
		cluster2 = NULL;
		best_separation->reset();
		return false;
	}

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq 0x28(%rdi), %r14
testq %r14, %r14
je 0xf179
movq %r14, %rdi
callq 0x6d3e
movq %r14, %rdi
callq 0x4310
leaq 0x28(%rbx), %r15
movq 0x30(%rbx), %r14
testq %r14, %r14
je 0xf196
movq %r14, %rdi
callq 0x6d3e
movq %r14, %rdi
callq 0x4310
xorps %xmm0, %xmm0
movq %r15, -0x30(%rbp)
movups %xmm0, (%r15)
movq 0xa8(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
leaq 0x38(%rbx), %rdi
movq %rdi, -0x38(%rbp)
callq 0x767e
leaq 0x70(%rbx), %rdi
movq %rdi, -0x40(%rbp)
callq 0x767e
movq 0x18(%rbx), %r14
movq 0x8(%r14), %r15
subq (%r14), %r15
movq %r15, %rax
sarq $0x3, %rax
leaq -0x1(%rax), %rdx
movq %rdx, %rcx
movq %rdx, %r13
shrq $0x3d, %rcx
leaq -0x8(,%rax,8), %rax
addq $0x8, %rax
pushq $-0x1
popq %rdx
cmovaeq %rax, %rdx
xorl %edi, %edi
negq %rcx
sbbq %rdi, %rdi
orq %rdx, %rdi
callq 0x4030
movq %rax, %r12
movq %r13, %rax
movq %r13, (%r12)
leaq 0x8(%r12), %r13
testq %rax, %rax
je 0xf237
addq $-0x10, %r15
andq $-0x8, %r15
addq $0x8, %r15
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x41f0
pushq $0x1
popq %rcx
leaq -0x80(%rbp), %r15
movq %rcx, -0x78(%rbp)
movq 0x8(%r14), %rax
movq %rax, %rdx
subq (%r14), %rdx
sarq $0x3, %rdx
decq %rdx
cmpq %rdx, %rcx
ja 0xf298
movq %rbx, -0x48(%rbp)
movq %r15, %rdi
leaq -0x23d(%rip), %rsi      # 0xf029
leaq -0x48(%rbp), %rdx
leaq -0x78(%rbp), %rcx
callq 0xf580
movq -0x78(%rbp), %rax
leaq (%r12,%rax,8), %rdi
movq %r15, %rsi
callq 0xf60a
movq %r15, %rdi
callq 0xf628
movq -0x78(%rbp), %rcx
incq %rcx
movq 0x18(%rbx), %r14
jmp 0xf23e
xorl %r15d, %r15d
subq (%r14), %rax
sarq $0x3, %rax
decq %rax
cmpq %r15, %rax
jbe 0xf2c3
movq %r13, %rdi
callq 0x4610
incq %r15
movq 0x18(%rbx), %r14
movq 0x8(%r14), %rax
addq $0x8, %r13
jmp 0xf29b
movq (%r12), %r14
testq %r14, %r14
je 0xf2df
shlq $0x3, %r14
leaq (%r12,%r14), %rdi
callq 0xf628
addq $-0x8, %r14
jne 0xf2d0
movq %r12, %rdi
callq 0x4480
movq %rbx, %rdi
callq 0xf038
movq 0x28(%rbx), %r14
testq %r14, %r14
movq -0x30(%rbp), %r13
je 0xf54e
addq $0x48, %r14
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %r12
movq (%r14), %r14
testq %r14, %r14
je 0xf322
leaq 0x8(%r14), %rsi
movq %r12, %rdi
callq 0xde0e
jmp 0xf30c
movq 0x30(%rbx), %r14
addq $0x48, %r14
movq (%r14), %r14
testq %r14, %r14
je 0xf340
leaq 0x8(%r14), %rsi
movq %r15, %rdi
callq 0xde0e
jmp 0xf32a
movq 0x28(%rbx), %rax
movq 0x20(%rax), %r14
addq $0x10, %rax
cmpq %rax, %r14
je 0xf388
movq 0x20(%r14), %rsi
movq 0x28(%r14), %r15
addq %r15, %r15
movq 0x8(%rbx), %rdi
callq 0x9bec
cmpq %rax, %r15
jb 0xf377
movq (%r13), %rdi
movq 0x20(%r14), %rsi
callq 0x6fd2
movq %r14, %rdi
callq 0x43d0
movq %rax, %r14
movq (%r13), %rax
jmp 0xf348
movq 0x30(%rbx), %rax
movq 0x20(%rax), %r14
addq $0x10, %rax
cmpq %rax, %r14
je 0xf3d0
movq 0x20(%r14), %rsi
movq 0x28(%r14), %r15
addq %r15, %r15
movq 0x8(%rbx), %rdi
callq 0x9bec
cmpq %rax, %r15
jbe 0xf3bf
movq 0x30(%rbx), %rdi
movq 0x20(%r14), %rsi
callq 0x6fd2
movq %r14, %rdi
callq 0x43d0
movq %rax, %r14
movq 0x30(%rbx), %rax
jmp 0xf390
leaq -0x70(%rbp), %r12
andl $0x0, (%r12)
andq $0x0, 0x8(%r12)
movq %r12, 0x10(%r12)
movq %r12, 0x18(%r12)
andq $0x0, 0x20(%r12)
movq (%r13), %r13
addq $0x48, %r13
leaq -0x78(%rbp), %r14
movq (%r13), %r13
testq %r13, %r13
je 0xf432
movq 0x8(%rbx), %rdi
movq 0x28(%rbx), %r15
movq 0x8(%r13), %rsi
callq 0x9b48
movq %r15, %rdi
movq %rax, %rsi
callq 0xf638
testb %al, %al
jne 0xf3fb
leaq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x82e4
jmp 0xf3fb
movq -0x60(%rbp), %r14
movq -0x30(%rbp), %r15
cmpq %r12, %r14
je 0xf465
movq 0x20(%r14), %rsi
movq (%r15), %rdi
callq 0xea48
movq 0x20(%r14), %rsi
movq 0x30(%rbx), %rdi
callq 0xe984
movq %r14, %rdi
callq 0x43d0
movq %rax, %r14
jmp 0xf43a
leaq -0x78(%rbp), %r14
movq %r14, %rdi
callq 0xf708
movq 0x30(%rbx), %r13
addq $0x48, %r13
movq (%r13), %r13
testq %r13, %r13
je 0xf4b0
movq 0x8(%rbx), %rdi
movq 0x30(%rbx), %r15
movq 0x8(%r13), %rsi
callq 0x9b48
movq %r15, %rdi
movq %rax, %rsi
callq 0xf638
testb %al, %al
jne 0xf479
leaq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x82e4
jmp 0xf479
movq -0x60(%rbp), %r14
movq -0x30(%rbp), %r15
cmpq %r12, %r14
je 0xf4e3
movq 0x20(%r14), %rsi
movq (%r15), %rdi
callq 0xe984
movq 0x20(%r14), %rsi
movq 0x30(%rbx), %rdi
callq 0xea48
movq %r14, %rdi
callq 0x43d0
movq %rax, %r14
jmp 0xf4b8
movq 0x28(%rbx), %r15
movq 0xb0(%rbx), %rax
cmpq %rax, 0x50(%r15)
jb 0xf501
movq 0x30(%rbx), %rcx
movb $0x1, %r14b
cmpq %rax, 0x50(%rcx)
jae 0xf543
movq %r15, %rdi
callq 0x6d3e
movq %r15, %rdi
callq 0x4310
andq $0x0, 0x28(%rbx)
movq 0x30(%rbx), %r14
testq %r14, %r14
je 0xf527
movq %r14, %rdi
callq 0x6d3e
movq %r14, %rdi
callq 0x4310
andq $0x0, 0x30(%rbx)
movq 0xa8(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
xorl %r14d, %r14d
leaq -0x78(%rbp), %rdi
callq 0x7602
jmp 0xf551
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf56b
jmp 0xf56b
jmp 0xf56b
jmp 0xf56b
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x7602
movq %rbx, %rdi
callq 0x4660
nop