// File: /workspace/github_cpp_makefile_star2_O1/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 0x12cee
leaq 0x70(%r14), %rdi
callq 0x9c80
leaq 0x38(%r14), %rdi
callq 0x9cd8
leaq 0x8(%r14), %rdi
callq 0x9d4e
movq %r14, %rdi
callq 0x5340
leaq 0x28(%rbx), %r15
movq 0x30(%rbx), %r14
testq %r14, %r14
je 0x12d1e
leaq 0x70(%r14), %rdi
callq 0x9c80
leaq 0x38(%r14), %rdi
callq 0x9cd8
leaq 0x8(%r14), %rdi
callq 0x9d4e
movq %r14, %rdi
callq 0x5340
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, -0x68(%rbp)
callq 0x9d00
leaq 0x70(%rbx), %rdi
movq %rdi, -0x70(%rbp)
callq 0x9d00
movq 0x18(%rbx), %rax
movq 0x8(%rax), %r14
subq (%rax), %r14
movq %r14, %rax
sarq $0x3, %rax
leaq -0x1(%rax), %r15
movq %r15, %rcx
shrq $0x3d, %rcx
leaq -0x8(,%rax,8), %rax
addq $0x8, %rax
movq $-0x1, %rdx
cmovaeq %rax, %rdx
xorl %edi, %edi
negq %rcx
sbbq %rdi, %rdi
orq %rdx, %rdi
callq 0x5030
movq %rax, %r12
movq %r15, (%rax)
leaq 0x8(%rax), %r13
testq %r15, %r15
je 0x12dbb
addq $-0x10, %r14
andq $-0x8, %r14
addq $0x8, %r14
movq %r13, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x51f0
movq $0x1, -0x60(%rbp)
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
cmpq $0x8, %rcx
je 0x12e2b
leaq -0x78(%rbp), %r14
leaq -0x60(%rbp), %r15
movq %rbx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x31a(%rip), %rsi      # 0x12ad1
movq %r14, %rdx
movq %r15, %rcx
callq 0x13220
movq -0x60(%rbp), %rax
cmpq $0x0, (%r12,%rax,8)
jne 0x131ff
movq -0x80(%rbp), %rcx
movq %rcx, (%r12,%rax,8)
incq %rax
movq %rax, -0x60(%rbp)
movq 0x18(%rbx), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
sarq $0x3, %rdx
decq %rdx
cmpq %rdx, %rax
jbe 0x12ddc
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
cmpq $0x8, %rcx
je 0x12e65
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x5650
incq %r14
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
decq %rcx
addq $0x8, %r13
cmpq %r14, %rcx
ja 0x12e3f
movq (%r12), %rax
testq %rax, %rax
movq -0x30(%rbp), %r13
je 0x12e87
shlq $0x3, %rax
cmpq $0x0, (%r12,%rax)
jne 0x131ff
addq $-0x8, %rax
jne 0x12e76
movq %r12, %rdi
callq 0x54d0
movq %rbx, %rdi
callq 0x12ae0
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x131ea
movq 0x48(%rax), %r15
testq %r15, %r15
movq -0x68(%rbp), %r12
je 0x12ed0
leaq -0x60(%rbp), %r14
leaq 0x8(%r15), %rsi
movq %r12, -0x60(%rbp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x11712
movq (%r15), %r15
testq %r15, %r15
jne 0x12eb5
movq 0x30(%rbx), %rax
movq 0x48(%rax), %r15
testq %r15, %r15
movq -0x70(%rbp), %r12
je 0x12f00
leaq -0x60(%rbp), %r14
leaq 0x8(%r15), %rsi
movq %r12, -0x60(%rbp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x11712
movq (%r15), %r15
testq %r15, %r15
jne 0x12ee5
movq (%r13), %rax
movq 0x20(%rax), %r14
addq $0x10, %rax
cmpq %rax, %r14
je 0x12f5e
leaq -0x60(%rbp), %r15
movq 0x20(%r14), %rsi
movq 0x28(%r14), %r12
addq %r12, %r12
movq 0x8(%rbx), %rdi
callq 0xcdbe
cmpq %rax, %r12
jb 0x12f46
movq (%r13), %rdi
movq 0x20(%r14), %rax
movq %rax, -0x60(%rbp)
addq $0x70, %rdi
movq %r15, %rsi
callq 0xb14e
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
movq (%r13), %rax
addq $0x10, %rax
cmpq %rax, %r14
jne 0x12f15
movq 0x30(%rbx), %rax
movq 0x20(%rax), %r14
addq $0x10, %rax
cmpq %rax, %r14
je 0x12fbc
leaq -0x60(%rbp), %r15
movq 0x20(%r14), %rsi
movq 0x28(%r14), %r12
addq %r12, %r12
movq 0x8(%rbx), %rdi
callq 0xcdbe
cmpq %rax, %r12
jbe 0x12fa4
movq 0x30(%rbx), %rdi
movq 0x20(%r14), %rax
movq %rax, -0x60(%rbp)
addq $0x70, %rdi
movq %r15, %rsi
callq 0xb14e
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
movq 0x30(%rbx), %rax
addq $0x10, %rax
cmpq %rax, %r14
jne 0x12f73
leaq -0x58(%rbp), %r15
movl $0x0, (%r15)
xorl %eax, %eax
movq %rax, 0x8(%r15)
movq %r15, 0x10(%r15)
movq %r15, 0x18(%r15)
movq %rax, 0x20(%r15)
movq (%r13), %rax
movq 0x48(%rax), %r12
testq %r12, %r12
je 0x13058
leaq -0x60(%rbp), %r14
movq 0x8(%rbx), %rdi
movq 0x28(%rbx), %r13
movq 0x8(%r12), %rsi
callq 0xcd08
movq 0x80(%r13), %rdx
addq $0x78, %r13
movq %r13, %rcx
testq %rdx, %rdx
je 0x1302a
movq %r13, %rcx
xorl %esi, %esi
cmpq %rax, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x13012
movq %r13, %rdx
cmpq %r13, %rcx
je 0x1303d
cmpq %rax, 0x20(%rcx)
cmovaq %r13, %rcx
movq %rcx, %rdx
cmpq %r13, %rdx
jne 0x1304f
leaq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0xb14e
movq (%r12), %r12
testq %r12, %r12
jne 0x12fea
movq -0x48(%rbp), %r14
cmpq %r15, %r14
movq -0x30(%rbp), %r12
je 0x1308f
movq 0x20(%r14), %rsi
movq (%r12), %rdi
callq 0x1248a
movq 0x20(%r14), %rsi
movq 0x30(%rbx), %rdi
callq 0x12330
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
cmpq %r15, %rax
jne 0x13065
leaq -0x60(%rbp), %rdi
callq 0x13306
movq 0x30(%rbx), %rax
movq 0x48(%rax), %r12
testq %r12, %r12
je 0x13117
leaq -0x60(%rbp), %r14
movq 0x8(%rbx), %rdi
movq 0x30(%rbx), %r13
movq 0x8(%r12), %rsi
callq 0xcd08
movq 0x80(%r13), %rdx
addq $0x78, %r13
movq %r13, %rcx
testq %rdx, %rdx
je 0x130e9
movq %r13, %rcx
xorl %esi, %esi
cmpq %rax, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x130d1
movq %r13, %rdx
cmpq %r13, %rcx
je 0x130fc
cmpq %rax, 0x20(%rcx)
cmovaq %r13, %rcx
movq %rcx, %rdx
cmpq %r13, %rdx
jne 0x1310e
leaq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0xb14e
movq (%r12), %r12
testq %r12, %r12
jne 0x130a9
movq -0x48(%rbp), %r14
cmpq %r15, %r14
movq -0x30(%rbp), %r12
je 0x1314e
movq 0x20(%r14), %rsi
movq (%r12), %rdi
callq 0x12330
movq 0x20(%r14), %rsi
movq 0x30(%rbx), %rdi
callq 0x1248a
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
cmpq %r15, %rax
jne 0x13124
movq 0x28(%rbx), %r14
movq 0xb0(%rbx), %rax
cmpq %rax, 0x50(%r14)
jb 0x1316c
movq 0x30(%rbx), %rcx
movb $0x1, %r15b
cmpq %rax, 0x50(%rcx)
jae 0x131df
testq %r14, %r14
je 0x13194
leaq 0x70(%r14), %rdi
callq 0x9c80
leaq 0x38(%r14), %rdi
callq 0x9cd8
leaq 0x8(%r14), %rdi
callq 0x9d4e
movq %r14, %rdi
callq 0x5340
movq $0x0, 0x28(%rbx)
movq 0x30(%rbx), %r14
testq %r14, %r14
je 0x131c8
leaq 0x70(%r14), %rdi
callq 0x9c80
leaq 0x38(%r14), %rdi
callq 0x9cd8
leaq 0x8(%r14), %rdi
callq 0x9d4e
movq %r14, %rdi
callq 0x5340
movq $0x0, 0x30(%rbx)
movq 0xa8(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
xorl %r15d, %r15d
leaq -0x60(%rbp), %rdi
callq 0x9c80
jmp 0x131ed
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x50e0
jmp 0x1320c
jmp 0x1320c
jmp 0x1320c
jmp 0x1320c
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x9c80
movq %rbx, %rdi
callq 0x56a0