// File: GAStudyGroup[P]GPX2/src/cross/gpx/sources/GPX2Fusion.cpp
// Function: GPX2Fusion::generateFusionPairs(std::vector<GPX2Structs::UnfeasibleConnection, std::allocator<GPX2Structs::UnfeasibleConnection>>&, std::map<int, Partition*, std::less<int>, std::allocator<std::pair<int const, Partition*>>>&)
void GPX2Fusion::generateFusionPairs(vector<GPX2Structs::UnfeasibleConnection> &fuseWith,GPX2Structs::PartitionMap &partition)
{ // Percorre o mapa com as partições unfeasible para gerar a lista com fusões a serem feitas

    for (auto uF : partition) {

        // Pega o retorno da função, IDs das partições e número de conexões entre elas, e salva na estrutura
        GPX2Structs::UnfeasibleConnection data = whichPartitionToFuseWith(partition[uF.first]);

        // Caso seja -1 ele está ligado a uma partição feasible, logo é ignorado
        if (data.partitionId1 != -1) {
            // Carrega o par de partições para fundir
            fuseWith.push_back(GPX2Structs::UnfeasibleConnection(uF.first, data.partitionId1, data.numberOfConnections));
        }
    }

    /* 
        Irá percorrer a lista fuseWith retirando as entradas iguais

        Ex: 1,2 e 2,1
            É uma situação de igualdade na qual é retirada, sobrando apenas uma
    */
    for (auto itOut = fuseWith.begin(); itOut != fuseWith.end(); itOut++) {
        for (auto itIn = fuseWith.begin(); itIn != fuseWith.end();) {
            if (itOut != itIn) {

                // Método para comparação, caso ocorra a igualdade então é retirado da lista
                if (comparePairInt(*itOut, *itIn)) {
                    itIn = fuseWith.erase(itIn);
                } else {
                    itIn++;
                }
            } else {
                itIn++;
            }
        }
    }

    for (const auto& p : partition) {
        // Pega o retorno do método, todas as possíveis fusões daquela partição
        vector<GPX2Structs::UnfeasibleConnection> tmp = fusionsWithPartition(p.first, fuseWith);

        // Se for menor que 2, logo não é possível que ela seja utilizada em mais de uma fusão.
        if (fuseWith.size() < 2) {
            break;
        }

        // Bloco para validação em caso de "melhor" fusão, compara entre as possíveis fusões e irá manter apenas a com maior número de conexões entre as partições.
        if (!tmp.empty()) {
            int maxCon{ -1 }, maxPos{ -1 };
            for (unsigned i = 0; i < tmp.size(); i++) {
                if (tmp[i].numberOfConnections > maxCon) {
                    maxCon = tmp[i].numberOfConnections;
                    maxPos = i;
                }
            }

            tmp.erase(tmp.begin() + maxPos);

            
            /*vector<UnfeasibleConnection> sameCon;
            int maxCon{ -1 }, maxPos{ -1 };
            for (unsigned i = 0; i < tmp.size(); i++) {
                if (tmp[i].numberOfConnections > maxCon) {
                    sameCon.clear();
                    maxCon = tmp[i].numberOfConnections;
                    maxPos = i;
                    sameCon.push_back(tmp[i]);
                }else if(tmp[i].numberOfConnections == maxCon){
                    sameCon.push_back(tmp[i]);
                }
            }

            if(sameCon.size()!=1){
                
                unsigned numberOfNodes{999999};
                unsigned numberOfNodes{0}; 
                UnfeasibleConnection best{0,0,0};
                for(UnfeasibleConnection uC : sameCon){
                    unsigned p1Nodes{unfeasiblePartitions[uC.partitionId1]->getNodes().size()},p2Nodes{unfeasiblePartitions[uC.partitionId2]->getNodes().size()};
                    unsigned sum{p1Nodes+p2Nodes};
                    //fusão que produzi o maior numero de nodes
                    if(sum>numberOfNodes){
                        numberOfNodes=sum;
                        best = uC;
                    } 

                    //fusão que produzi o maior numero de nodes
                    if(sum<numberOfNodes){
                        numberOfNodes=sum;
                        best = uC;
                    }
                }
                std::cout<<"escolheu a fusion com mais nodes "<<numberOfNodes<<endl; 
                std::cout<<"escolheu a fusion com menos nodes "<<numberOfNodes<<endl;
                tmp.erase(find_if(tmp.begin(),tmp.end(),[&best](UnfeasibleConnection const &uC){return((uC.partitionId1==best.partitionId1) && (uC.partitionId2==best.partitionId2));}));

            }else{
                tmp.erase(tmp.begin() + maxPos);
            } */

            for (auto& unfCon : tmp) {
                for (auto it = fuseWith.begin(); it != fuseWith.end();) {

                    if (compareUnfeasibleConnections(unfCon,(*it))) {
                        it = fuseWith.erase(it);
                    } else {
                        it++;
                    }
                }
            }
        }
    }
}

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1b0(%rsp)
movq %rsi, 0x1a8(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x54cc0
movq %rax, 0x198(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x54d10
movq %rax, 0x190(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x54cf0
testb $0x1, %al
jne 0x53c9f
jmp 0x53d60
leaq 0x198(%rsp), %rdi
callq 0x54d40
movups (%rax), %xmm0
movaps %xmm0, 0x180(%rsp)
movq 0x1a8(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x55370
movq (%rax), %rdi
callq 0x54280
movl %edx, 0x168(%rsp)
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x174(%rsp)
movl 0x168(%rsp), %eax
movl %eax, 0x17c(%rsp)
cmpl $-0x1, 0x174(%rsp)
je 0x53d4c
movq 0x1b0(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x180(%rsp), %esi
movl 0x174(%rsp), %edx
movl 0x17c(%rsp), %ecx
leaq 0x154(%rsp), %rdi
callq 0x55960
movq 0x40(%rsp), %rdi
leaq 0x154(%rsp), %rsi
callq 0x55930
jmp 0x53d4e
leaq 0x198(%rsp), %rdi
callq 0x552a0
jmp 0x53c81
movq 0x1b0(%rsp), %rdi
callq 0x552d0
movq %rax, 0x148(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x55340
movq %rax, 0x140(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x55300
testb $0x1, %al
jne 0x53da8
jmp 0x53ee1
movq 0x1b0(%rsp), %rdi
callq 0x552d0
movq %rax, 0x138(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x55340
movq %rax, 0x130(%rsp)
leaq 0x138(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x55300
testb $0x1, %al
jne 0x53df0
jmp 0x53ec3
leaq 0x148(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x55300
testb $0x1, %al
jne 0x53e0e
jmp 0x53ea7
leaq 0x148(%rsp), %rdi
callq 0x55460
movq %rax, 0x38(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x55460
movq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x52c10
testb $0x1, %al
jne 0x53e40
jmp 0x53e8e
movq 0x1b0(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x120(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x557f0
movq 0x30(%rsp), %rdi
movq 0x120(%rsp), %rsi
callq 0x55780
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x138(%rsp)
jmp 0x53ea5
leaq 0x138(%rsp), %rdi
xorl %esi, %esi
callq 0x55990
movq %rax, 0x118(%rsp)
jmp 0x53ebe
leaq 0x138(%rsp), %rdi
xorl %esi, %esi
callq 0x55990
movq %rax, 0x110(%rsp)
jmp 0x53dbd
jmp 0x53ec5
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x55990
movq %rax, 0x108(%rsp)
jmp 0x53d75
movq 0x1a8(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rdi
callq 0x54cc0
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rdi
callq 0x54d10
movq %rax, 0xf0(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x54cf0
testb $0x1, %al
jne 0x53f39
jmp 0x5425d
leaq 0xf8(%rsp), %rdi
callq 0x54d40
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rax
movl (%rax), %esi
movq 0x1b0(%rsp), %rdx
leaq 0xd0(%rsp), %rdi
callq 0x53b30
movq 0x1b0(%rsp), %rdi
callq 0x559d0
cmpq $0x2, %rax
jae 0x53f90
movl $0x9, 0xcc(%rsp)
jmp 0x5422d
leaq 0xd0(%rsp), %rdi
callq 0x559f0
testb $0x1, %al
jne 0x54222
movl $0xffffffff, 0xc8(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xc4(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
movq %rax, 0x28(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x559d0
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x54054
movl 0xc0(%rsp), %eax
movl %eax, %esi
leaq 0xd0(%rsp), %rdi
callq 0x55a40
movl 0x8(%rax), %eax
cmpl 0xc8(%rsp), %eax
jle 0x5403c
movl 0xc0(%rsp), %eax
movl %eax, %esi
leaq 0xd0(%rsp), %rdi
callq 0x55a40
movl 0x8(%rax), %eax
movl %eax, 0xc8(%rsp)
movl 0xc0(%rsp), %eax
movl %eax, 0xc4(%rsp)
jmp 0x5403e
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x53fc6
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x552d0
movq %rax, 0xa8(%rsp)
movslq 0xc4(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x55a60
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x557f0
movq 0x18(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x55780
movq %rax, 0x20(%rsp)
jmp 0x540b9
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0x552d0
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rdi
callq 0x55340
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x55300
testb $0x1, %al
jne 0x54118
jmp 0x54220
leaq 0x80(%rsp), %rdi
callq 0x55460
movq %rax, 0x70(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x552d0
movq %rax, 0x68(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x55340
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x55300
testb $0x1, %al
jne 0x54166
jmp 0x5420c
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x55460
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x52cb0
testb $0x1, %al
jne 0x5418d
jmp 0x541f6
movq 0x1b0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x557f0
movq (%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x55780
movq %rax, 0x8(%rsp)
jmp 0x541bd
movq 0x8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x68(%rsp)
jmp 0x54207
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x558d0
jmp 0x54265
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x55990
movq %rax, 0x48(%rsp)
jmp 0x5413c
jmp 0x5420e
leaq 0x80(%rsp), %rdi
callq 0x558b0
jmp 0x540fd
jmp 0x54222
movl $0x0, 0xcc(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x558d0
movl 0xcc(%rsp), %eax
testl %eax, %eax
je 0x54249
jmp 0x54247
jmp 0x5425d
jmp 0x5424b
leaq 0xf8(%rsp), %rdi
callq 0x552a0
jmp 0x53f1b
addq $0x1b8, %rsp            # imm = 0x1B8
retq
movq 0xa0(%rsp), %rdi
callq 0x6850
nopw %cs:(%rax,%rax)