// File: /workspace/github_cpp_makefile_star2_O2/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 0xa5d8(%rip), %rdi      # 0xdfd8
leaq 0x5629(%rip), %rsi      # 0x9030
callq 0x3240
movl 0x4e4(%rbx), %esi
movq %rax, %rdi
callq 0x33d0
leaq 0x561b(%rip), %rsi      # 0x903c
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
callq 0x3130
movslq 0x4e4(%rbx), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x560c
cmpl $0x0, 0x4ec(%rbx)
jle 0x3a64
movq 0x50(%rbx), %rdi
movq 0x58(%rbx), %rsi
movq 0x98(%rbx), %rdx
callq 0x565d
notl %ebp
movl 0x4e4(%rbx), %eax
movl 0x4e8(%rbx), %ecx
imull %ecx, %ebp
addl 0x4dc(%rbx), %ebp
movslq %ebp, %rdx
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
xorl %edi, %edi
cmpq %rdi, %rax
je 0x3ab2
cmpq %rdx, %rdi
movl %ecx, %r8d
cmovll %esi, %r8d
addl %edi, %r8d
movslq %r8d, %r8
imulq $0x58, %r8, %r8
addq 0x50(%rbx), %r8
movq 0x10(%rsp), %r9
movq %r8, (%r9,%rdi,8)
incq %rdi
jmp 0x3a87
cmpl $0x0, 0x68(%rbx)
jg 0x3ac1
cmpl $0x0, 0x4ec(%rbx)
jle 0x3b2e
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r14
cmpq %r14, %r15
je 0x3b2e
movq %r14, %r12
subq %r15, %r12
movq %r12, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x532b
cmpq $0x81, %r12
jl 0x3b23
leaq 0x80(%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x5514
cmpq %r14, %r12
je 0x3b2e
movq %r12, %rdi
callq 0x5579
addq $0x8, %r12
jmp 0x3b10
movq %r15, %rdi
movq %r14, %rsi
callq 0x5514
leaq 0x38(%rbx), %r14
movq %r14, %rdi
callq 0x57ac
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq 0x28(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x30(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0xf(%rsp), %rcx
callq 0x57b4
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x7a42
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x7938
incl 0x4e0(%rbx)
pushq $0x1
popq %r13
movslq 0x4e4(%rbx), %rax
cmpq %rax, %r13
jge 0x3c33
pushq $-0x1
popq %rbp
pushq $-0x18
popq %r15
movslq 0x4e0(%rbx), %rax
incq %rbp
cmpq %rax, %rbp
jge 0x3bef
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq 0x38(%rbx), %rax
movq 0x18(%rax,%r15), %rax
movq (%rax), %rdx
movq %rbx, %rdi
callq 0x363e
addq $0x18, %r15
movsd 0x90(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3b97
addq (%r14), %r15
leaq (,%r13,8), %rsi
addq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x57ea
jmp 0x3c2b
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rax
movq %rax, 0x28(%rsp)
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
pushq $0x1
popq %rdx
leaq 0xf(%rsp), %rcx
callq 0x57b4
movq %r14, %rdi
movq %r12, %rsi
callq 0x7a42
movq %r12, %rdi
callq 0x7938
incl 0x4e0(%rbx)
incq %r13
jmp 0x3b80
leaq 0x10(%rsp), %rdi
callq 0x7938
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c65
jmp 0x3c54
jmp 0x3c65
jmp 0x3c65
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x7938
jmp 0x3c68
jmp 0x3c65
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7938
movq %rbx, %rdi
callq 0x33e0