// File: /workspace/github_cpp_makefile_star2_O1/mapequation[P]partition-validation/./partition-validation.h
// Function: Partitions::clusterPartitions(int)
void Partitions::clusterPartitions(int fold) {
#ifdef _OPENMP
    // Initiate locks to keep track of best solutions
    omp_lock_t lock;
    omp_init_lock(&lock);
#endif

    cout << "Clustering " << nTrainingPartitions << " partitions:" << endl;

    auto partitionPtrs = vector<Partition *>(nTrainingPartitions);

    if (validationSamples > 0) {
        // Shuffle partitions for new training and validation set
        shuffle(begin(partitions), end(partitions), mtRands[0]);
    }

    for (auto i = 0; i < nTrainingPartitions; i++) {
        auto limit = nPartitions - (fold + 1) * nValidationPartitions;
        auto offset = i >= limit ? nValidationPartitions : 0;
        partitionPtrs[i] = &partitions[i + offset];
    }

    if (crossValidateK > 0 || validationSamples > 0) {
        // Order randomized partitions
        sort(begin(partitionPtrs), end(partitionPtrs),
             [](Partition *a, Partition *b) { return a->partitionId < b->partitionId; });
    }

    clusters.clear();
    clusters.push_back({partitionPtrs[0]});
    nClusters++;

    for (int i = 1; i < nTrainingPartitions; i++) {
        auto fitsInCluster = false;
        for (int j = 0; j < nClusters; j++) {
            if (wpJaccardDist(partitionPtrs[i], clusters[j][0]) < distThreshold) {
                fitsInCluster = true;
                clusters[j].push_back(partitionPtrs[i]);
                break;
            }
        }
        if (!fitsInCluster) {
            clusters.push_back({partitionPtrs[i]});
            nClusters++;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq 0xa39a(%rip), %r14      # 0xdfd8
leaq 0x63fb(%rip), %rsi      # 0xa040
movl $0xb, %edx
movq %r14, %rdi
callq 0x32a0
movl 0x4e4(%rbx), %esi
movq %r14, %rdi
callq 0x33d0
movq %rax, %r14
leaq 0x63e2(%rip), %rsi      # 0xa04c
movl $0xc, %edx
movq %rax, %rdi
callq 0x32a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
movslq 0x4e4(%rbx), %rsi
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x65fa
cmpl $0x0, 0x4ec(%rbx)
jle 0x3cd1
movq 0x50(%rbx), %rdi
movq 0x58(%rbx), %rsi
movq 0x98(%rbx), %rdx
callq 0x6684
movslq 0x4e4(%rbx), %rax
testq %rax, %rax
jle 0x3d1e
notl %ebp
movl 0x4e8(%rbx), %ecx
imull %ecx, %ebp
addl 0x4dc(%rbx), %ebp
movslq %ebp, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rdx, %rdi
movl %ecx, %r8d
cmovll %esi, %r8d
addl %edi, %r8d
movslq %r8d, %r8
imulq $0x58, %r8, %r8
addq 0x50(%rbx), %r8
movq 0x28(%rsp), %r9
movq %r8, (%r9,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x3cf5
cmpl $0x0, 0x68(%rbx)
jg 0x3d31
cmpl $0x0, 0x4ec(%rbx)
jle 0x3dbc
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %r14
cmpq %r14, %r12
je 0x3dbc
movq %r14, %r15
subq %r12, %r15
movq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x623f
cmpq $0x81, %r15
jl 0x3db1
leaq 0x80(%r12), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x6499
cmpq %r14, %r15
je 0x3dbc
movq -0x8(%r15), %rcx
movq (%r15), %rax
movl (%rax), %edx
movq %r15, %rsi
cmpl (%rcx), %edx
jge 0x3da8
movq %r15, %rsi
movq %rcx, (%rsi)
movq -0x10(%rsi), %rcx
addq $-0x8, %rsi
cmpl (%rcx), %edx
jl 0x3d99
movq %rax, (%rsi)
addq $0x8, %r15
jmp 0x3d81
movq %r12, %rdi
movq %r14, %rsi
callq 0x6499
leaq 0x38(%rbx), %r14
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x8962
movq 0x28(%rsp), %rax
movq (%rax), %rax
leaq 0x40(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movl $0x1, %edx
callq 0x6858
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x89ac
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3e14
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
incl 0x4e0(%rbx)
cmpl $0x2, 0x4e4(%rbx)
jl 0x3f11
movl $0x1, %ebp
leaq 0x10(%rsp), %r15
cmpl $0x0, 0x4e0(%rbx)
jle 0x3e7d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x28(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq 0x38(%rbx), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdx
movq %rbx, %rdi
callq 0x37fc
movsd 0x90(%rbx), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3ecc
incq %r13
movslq 0x4e0(%rbx), %rax
addq $0x18, %r12
cmpq %rax, %r13
jl 0x3e40
movq 0x28(%rsp), %rax
movq (%rax,%rbp,8), %rax
movq %rax, 0x40(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
leaq 0xf(%rsp), %rcx
callq 0x6858
movq %r14, %rdi
movq %r15, %rsi
callq 0x89ac
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3ec4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
incl 0x4e0(%rbx)
jmp 0x3efe
movq (%r14), %rax
leaq (,%rbp,8), %rdx
addq 0x28(%rsp), %rdx
leaq (%rax,%r12), %rdi
movq 0x8(%rax,%r12), %rsi
cmpq 0x10(%rax,%r12), %rsi
je 0x3ef9
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x3efe
callq 0x8c4e
incq %rbp
movslq 0x4e4(%rbx), %rax
cmpq %rax, %rbp
jl 0x3e31
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x3f28
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3f5d
jmp 0x3f41
jmp 0x3f5d
jmp 0x3f5d
jmp 0x3f5d
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3f60
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
jmp 0x3f60
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x3f77
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
movq %rbx, %rdi
callq 0x33e0
nop