// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq 0x20f8c(%rip), %rdi     # 0x24fd0
leaq 0x13fd9(%rip), %rsi     # 0x18024
callq 0x32c0
movq %rax, %rdi
movq -0x108(%rbp), %rax
movl 0x4e4(%rax), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x13fc1(%rip), %rsi     # 0x18030
callq 0x32c0
movq %rax, %rdi
movq 0x20f42(%rip), %rsi     # 0x24fc0
callq 0x3310
movq -0x108(%rbp), %rax
movslq 0x4e4(%rax), %rax
movq %rax, -0x100(%rbp)
leaq -0x29(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x8770
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
leaq -0x28(%rbp), %rdi
callq 0x8790
jmp 0x40c1
leaq -0x29(%rbp), %rdi
callq 0x8800
movq -0x108(%rbp), %rax
cmpl $0x0, 0x4ec(%rax)
jle 0x417c
movq -0x108(%rbp), %rdi
addq $0x50, %rdi
callq 0x8a90
movq %rax, -0x110(%rbp)
jmp 0x40f7
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rax
movq %rax, -0x48(%rbp)
addq $0x50, %rdi
callq 0x8ac0
movq %rax, -0x118(%rbp)
jmp 0x411b
movq -0x108(%rbp), %rdi
movq -0x118(%rbp), %rax
movq %rax, -0x50(%rbp)
addq $0x98, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8af0
movq %rax, %rdx
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x8820
jmp 0x414f
jmp 0x417c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x8800
jmp 0x45e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x45da
movl $0x0, -0x54(%rbp)
movq -0x108(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x4e4(%rcx), %eax
jge 0x422d
movq -0x108(%rbp), %rdx
movl 0x4dc(%rdx), %eax
movl -0xc(%rbp), %ecx
addl $0x1, %ecx
imull 0x4e8(%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jl 0x41d5
movq -0x108(%rbp), %rax
movl 0x4e8(%rax), %eax
movl %eax, -0x11c(%rbp)
jmp 0x41df
xorl %eax, %eax
movl %eax, -0x11c(%rbp)
jmp 0x41df
movq -0x108(%rbp), %rdi
movl -0x11c(%rbp), %eax
movl %eax, -0x5c(%rbp)
addq $0x50, %rdi
movl -0x54(%rbp), %eax
addl -0x5c(%rbp), %eax
movslq %eax, %rsi
callq 0x8b10
movq %rax, -0x128(%rbp)
movslq -0x54(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x8b30
movq -0x128(%rbp), %rcx
movq %rcx, (%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4183
movq -0x108(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jg 0x424a
movq -0x108(%rbp), %rax
cmpl $0x0, 0x4ec(%rax)
jle 0x4295
leaq -0x28(%rbp), %rdi
callq 0x8b50
movq %rax, -0x130(%rbp)
jmp 0x425c
movq -0x130(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x8b80
movq %rax, -0x138(%rbp)
jmp 0x4279
movq -0x138(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x45f0
jmp 0x4293
jmp 0x4295
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
movq %rdi, -0x148(%rbp)
callq 0x8bb0
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0x8b30
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x1, -0x98(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x8770
movq -0x140(%rbp), %rcx
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x8c00
jmp 0x4312
movq -0x148(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x8bd0
jmp 0x4327
leaq -0x90(%rbp), %rdi
callq 0x8c80
leaq -0xa9(%rbp), %rdi
callq 0x8800
movq -0x108(%rbp), %rax
movl 0x4e0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4e0(%rax)
movl $0x1, -0xb0(%rbp)
movq -0x108(%rbp), %rcx
movl -0xb0(%rbp), %eax
cmpl 0x4e4(%rcx), %eax
jge 0x45c8
movb $0x0, -0xb1(%rbp)
movl $0x0, -0xb8(%rbp)
movq -0x108(%rbp), %rcx
movl -0xb8(%rbp), %eax
cmpl 0x4e0(%rcx), %eax
jge 0x44b1
movslq -0xb0(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x8b30
movq -0x108(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x158(%rbp)
addq $0x38, %rdi
movslq -0xb8(%rbp), %rsi
callq 0x8ce0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8b30
movq -0x108(%rbp), %rdi
movq -0x158(%rbp), %rsi
movq (%rax), %rdx
callq 0x37c0
movsd %xmm0, -0x150(%rbp)
jmp 0x43ff
movsd -0x150(%rbp), %xmm1
movq -0x108(%rbp), %rax
movsd 0x90(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x449b
movq -0x108(%rbp), %rdi
movb $0x1, -0xb1(%rbp)
addq $0x38, %rdi
movslq -0xb8(%rbp), %rsi
callq 0x8ce0
movq %rax, -0x160(%rbp)
movslq -0xb0(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x8b30
movq -0x160(%rbp), %rdi
movq %rax, %rsi
callq 0x8d00
jmp 0x4462
jmp 0x44b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x448a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x8c80
leaq -0xa9(%rbp), %rdi
callq 0x8800
jmp 0x45da
jmp 0x449d
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x4389
testb $0x1, -0xb1(%rbp)
jne 0x45b2
movq -0x108(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x170(%rbp)
movslq -0xb0(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x8b30
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
leaq -0xe8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0x1, -0xd8(%rbp)
leaq -0xe9(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x8770
movq -0x168(%rbp), %rcx
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x8c00
jmp 0x4539
movq -0x170(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x8bd0
jmp 0x454e
leaq -0xd0(%rbp), %rdi
callq 0x8c80
leaq -0xe9(%rbp), %rdi
callq 0x8800
movq -0x108(%rbp), %rax
movl 0x4e0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4e0(%rax)
jmp 0x45b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x45a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x8c80
leaq -0xe9(%rbp), %rdi
callq 0x8800
jmp 0x45da
jmp 0x45b4
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x435f
leaq -0x28(%rbp), %rdi
callq 0x8c80
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x8c80
movq -0x38(%rbp), %rdi
callq 0x34a0
nopl (%rax)