// File: GAStudyGroup[P]GPX2/src/cross/gpx/sources/GPX2Fusion.cpp
// Function: GPX2Fusion::fusePartitions(std::vector<GPX2Structs::UnfeasibleConnection, std::allocator<GPX2Structs::UnfeasibleConnection>>&, std::map<int, Partition*, std::less<int>, std::allocator<std::pair<int const, Partition*>>>&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Node*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, Node*>>>&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Node*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, Node*>>>&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Node*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, Node*>>>&)
void GPX2Fusion::fusePartitions(vector<GPX2Structs::UnfeasibleConnection> &fuseWith,GPX2Structs::PartitionMap &partitions, GPX2Structs::NodeMap &unitedGraph, GPX2Structs::NodeMap& red, GPX2Structs::NodeMap& blue)
{ // Realiza a fusão entre as partições, de acordo com as validações já realizadas
    // Início da execução da fusão em si
    for (auto it = fuseWith.begin(); it != fuseWith.end();) {
        // Não precisa mudar o custo pois a checkPartition não verifica isso
        Partition* p1Ptr = partitions[(*it).partitionId1];
        Partition* p2Ptr = partitions[(*it).partitionId2];
        vector<string> intermediariesNodes, accessNodesToRemove;

        // Deletar as entradas de conexões (connectTo) diferentes da partição que será fundida
        for (auto itIn = p1Ptr->getConnectedTo().begin(); itIn != p1Ptr->getConnectedTo().end();) {
            if ((*itIn).connectedPartition != (*it).partitionId2) {
                p1Ptr->getConnectedTo().erase(itIn);
            } else {
                itIn++;
            }
        }
        for (auto itIn = p2Ptr->getConnectedTo().begin(); itIn != p2Ptr->getConnectedTo().end();) {
            if ((*itIn).connectedPartition != (*it).partitionId1) {
                p2Ptr->getConnectedTo().erase(itIn);
            } else {
                itIn++;
            }
        }

        for (string nodes : p2Ptr->getNodes()) {
            p1Ptr->getNodes().push_back(nodes);
        }
        for (string access : p2Ptr->getAccessNodes()) {
            p1Ptr->getAccessNodes().push_back(access);
        }

        // Percorrer os nós com ligação na partição que será feita a fusão
        for (auto connectedNode : p2Ptr->getConnectedTo()) {

            // Pegar os nós intermediários (o "caminho" da conexão)
            pair<GPX2Structs::SearchResult, vector<string>> tmp = GPX2Support::DFS_outside(connectedNode.node, partitions,unitedGraph);

            // Apagar os access nodes da lista da partição
            accessNodesToRemove.push_back(connectedNode.node);
            accessNodesToRemove.push_back(connectedNode.connectedNode);

            // Apagar o primeiro pois é o nó access que da origem a busca
            if (!tmp.second.empty()) {
                tmp.second.erase(tmp.second.begin());

                // Apagar o último nó pois é onde ele se conecta na partição que será unida
                if (!tmp.second.empty()) {
                    tmp.second.erase(tmp.second.end());
                }
            }

            // Carrega os nós intermediarios
            intermediariesNodes.insert(intermediariesNodes.end(), tmp.second.begin(), tmp.second.end());
        }

        /*  
            Inserir os nós intermediários na nova partição fundida
            Os nós são a ligação entre a P1 e P2, sendo a conexão de fusão das duas
        */
        p1Ptr->getNodes().insert(p1Ptr->getNodes().end(), intermediariesNodes.begin(), intermediariesNodes.end());

        // Os access nodes que ligavam as duas partições não serão mais access nodes, eles serão removidos
        GPX2Support::eraseSubVector(p1Ptr->getAccessNodes(), accessNodesToRemove);

        //atualizar a lista de entradas e saídas
        p1Ptr->setEntryAndExits(GPX2Support::getEntryAndExitList(p1Ptr,red,blue));

        // Apagar tudo
        delete p2Ptr;
        partitions.erase((*it).partitionId2);

        //apaga a entrada na fuseWith pois a fusão já foi realizada
        it = fuseWith.erase(it);
    }
}

// Assembly:
subq $0x3a8, %rsp            # imm = 0x3A8
movq %rdi, 0x3a0(%rsp)
movq %rsi, 0x398(%rsp)
movq %rdx, 0x390(%rsp)
movq %rcx, 0x388(%rsp)
movq %r8, 0x380(%rsp)
movq 0x3a0(%rsp), %rdi
callq 0x552d0
movq %rax, 0x378(%rsp)
movq 0x3a0(%rsp), %rdi
callq 0x55340
movq %rax, 0x370(%rsp)
leaq 0x378(%rsp), %rdi
leaq 0x370(%rsp), %rsi
callq 0x55300
testb $0x1, %al
jne 0x530d7
jmp 0x53b1b
movq 0x398(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0x378(%rsp), %rdi
movq %rdi, 0x100(%rsp)
callq 0x55460
movq 0xf8(%rsp), %rdi
movq %rax, %rsi
callq 0x55370
movq 0x100(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x368(%rsp)
movq 0x398(%rsp), %rax
movq %rax, 0x108(%rsp)
callq 0x55460
movq 0x108(%rsp), %rdi
movq %rax, %rsi
addq $0x4, %rsi
callq 0x55370
movq (%rax), %rax
movq %rax, 0x360(%rsp)
leaq 0x348(%rsp), %rdi
callq 0x1e3e0
leaq 0x330(%rsp), %rdi
callq 0x1e3e0
movq 0x368(%rsp), %rdi
callq 0x67ee0
movq %rax, 0x110(%rsp)
jmp 0x53184
movq 0x110(%rsp), %rdi
callq 0x54e70
movq %rax, 0x328(%rsp)
movq 0x368(%rsp), %rdi
callq 0x67ee0
movq %rax, 0xf0(%rsp)
jmp 0x531b0
movq 0xf0(%rsp), %rdi
callq 0x54ea0
movq %rax, 0x310(%rsp)
leaq 0x328(%rsp), %rdi
leaq 0x310(%rsp), %rsi
callq 0x54ed0
testb $0x1, %al
jne 0x531e3
jmp 0x532a7
leaq 0x328(%rsp), %rdi
callq 0x54f10
movl (%rax), %eax
movl %eax, 0xec(%rsp)
leaq 0x378(%rsp), %rdi
callq 0x55460
movq %rax, %rcx
movl 0xec(%rsp), %eax
cmpl 0x4(%rcx), %eax
je 0x5328b
movq 0x368(%rsp), %rdi
callq 0x67ee0
movq %rax, 0xe0(%rsp)
jmp 0x5322c
leaq 0x308(%rsp), %rdi
leaq 0x328(%rsp), %rsi
callq 0x554e0
movq 0xe0(%rsp), %rdi
movq 0x308(%rsp), %rsi
callq 0x55470
movq %rax, 0xd8(%rsp)
jmp 0x53260
movq 0xd8(%rsp), %rax
movq %rax, 0x300(%rsp)
jmp 0x532a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0x53aff
leaq 0x328(%rsp), %rdi
xorl %esi, %esi
callq 0x55520
movq %rax, 0x2f8(%rsp)
jmp 0x53199
movq 0x360(%rsp), %rdi
callq 0x67ee0
movq %rax, 0xd0(%rsp)
jmp 0x532be
movq 0xd0(%rsp), %rdi
callq 0x54e70
movq %rax, 0x2f0(%rsp)
movq 0x360(%rsp), %rdi
callq 0x67ee0
movq %rax, 0xc8(%rsp)
jmp 0x532ea
movq 0xc8(%rsp), %rdi
callq 0x54ea0
movq %rax, 0x2e8(%rsp)
leaq 0x2f0(%rsp), %rdi
leaq 0x2e8(%rsp), %rsi
callq 0x54ed0
testb $0x1, %al
jne 0x5331d
jmp 0x533c7
leaq 0x2f0(%rsp), %rdi
callq 0x54f10
movl (%rax), %eax
movl %eax, 0xc4(%rsp)
leaq 0x378(%rsp), %rdi
callq 0x55460
movq %rax, %rcx
movl 0xc4(%rsp), %eax
cmpl (%rcx), %eax
je 0x533ab
movq 0x360(%rsp), %rdi
callq 0x67ee0
movq %rax, 0xb8(%rsp)
jmp 0x53365
leaq 0x2e0(%rsp), %rdi
leaq 0x2f0(%rsp), %rsi
callq 0x554e0
movq 0xb8(%rsp), %rdi
movq 0x2e0(%rsp), %rsi
callq 0x55470
movq %rax, 0xb0(%rsp)
jmp 0x53399
movq 0xb0(%rsp), %rax
movq %rax, 0x2d8(%rsp)
jmp 0x533c2
leaq 0x2f0(%rsp), %rdi
xorl %esi, %esi
callq 0x55520
movq %rax, 0x2d0(%rsp)
jmp 0x532d3
movq 0x360(%rsp), %rdi
callq 0x67e70
movq %rax, 0xa8(%rsp)
jmp 0x533de
movq 0xa8(%rsp), %rax
movq %rax, 0x2c8(%rsp)
movq 0x2c8(%rsp), %rdi
callq 0x1e310
movq %rax, 0x2c0(%rsp)
movq 0x2c8(%rsp), %rdi
callq 0x1e340
movq %rax, 0x2b8(%rsp)
leaq 0x2c0(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
callq 0x1e370
testb $0x1, %al
jne 0x53436
jmp 0x534c8
leaq 0x2c0(%rsp), %rdi
callq 0x1e3b0
movq %rax, %rsi
leaq 0x298(%rsp), %rdi
callq 0x6200
jmp 0x53455
movq 0x368(%rsp), %rdi
callq 0x67e70
movq %rax, 0xa0(%rsp)
jmp 0x5346c
movq 0xa0(%rsp), %rdi
leaq 0x298(%rsp), %rsi
callq 0x37210
jmp 0x53483
leaq 0x298(%rsp), %rdi
callq 0x6988
leaq 0x2c0(%rsp), %rdi
callq 0x1e3c0
jmp 0x53418
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x298(%rsp), %rdi
callq 0x6988
jmp 0x53aff
movq 0x360(%rsp), %rdi
callq 0x67e80
movq %rax, 0x98(%rsp)
jmp 0x534df
movq 0x98(%rsp), %rax
movq %rax, 0x290(%rsp)
movq 0x290(%rsp), %rdi
callq 0x1e310
movq %rax, 0x288(%rsp)
movq 0x290(%rsp), %rdi
callq 0x1e340
movq %rax, 0x280(%rsp)
leaq 0x288(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x1e370
testb $0x1, %al
jne 0x53537
jmp 0x535c9
leaq 0x288(%rsp), %rdi
callq 0x1e3b0
movq %rax, %rsi
leaq 0x260(%rsp), %rdi
callq 0x6200
jmp 0x53556
movq 0x368(%rsp), %rdi
callq 0x67e80
movq %rax, 0x90(%rsp)
jmp 0x5356d
movq 0x90(%rsp), %rdi
leaq 0x260(%rsp), %rsi
callq 0x37210
jmp 0x53584
leaq 0x260(%rsp), %rdi
callq 0x6988
leaq 0x288(%rsp), %rdi
callq 0x1e3c0
jmp 0x53519
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x260(%rsp), %rdi
callq 0x6988
jmp 0x53aff
movq 0x360(%rsp), %rdi
callq 0x67ee0
movq %rax, 0x88(%rsp)
jmp 0x535e0
movq 0x88(%rsp), %rax
movq %rax, 0x258(%rsp)
movq 0x258(%rsp), %rdi
callq 0x54e70
movq %rax, 0x250(%rsp)
movq 0x258(%rsp), %rdi
callq 0x54ea0
movq %rax, 0x248(%rsp)
leaq 0x250(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x54ed0
testb $0x1, %al
jne 0x53638
jmp 0x538ea
leaq 0x250(%rsp), %rdi
callq 0x54f10
movq %rax, %rsi
leaq 0x200(%rsp), %rdi
callq 0x54f20
jmp 0x53657
leaq 0x208(%rsp), %rsi
leaq 0x1c0(%rsp), %rdi
callq 0x6200
jmp 0x5366e
movq 0x398(%rsp), %rdx
movq 0x390(%rsp), %rcx
leaq 0x1e0(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x6be60
jmp 0x53695
leaq 0x1c0(%rsp), %rdi
callq 0x6988
leaq 0x208(%rsp), %rsi
leaq 0x330(%rsp), %rdi
callq 0x37210
jmp 0x536b9
leaq 0x228(%rsp), %rsi
leaq 0x330(%rsp), %rdi
callq 0x37210
jmp 0x536d0
leaq 0x1e0(%rsp), %rdi
addq $0x8, %rdi
callq 0x49f70
testb $0x1, %al
jne 0x5381b
leaq 0x1e8(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x1e310
movq %rax, 0x1b0(%rsp)
leaq 0x1b8(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x555d0
movq 0x78(%rsp), %rdi
movq 0x1b8(%rsp), %rsi
callq 0x55560
movq %rax, 0x80(%rsp)
jmp 0x53734
movq 0x80(%rsp), %rax
movq %rax, 0x1a8(%rsp)
leaq 0x1e0(%rsp), %rdi
addq $0x8, %rdi
callq 0x49f70
testb $0x1, %al
jne 0x53819
leaq 0x1e8(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x1e340
movq %rax, 0x198(%rsp)
leaq 0x1a0(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x555d0
movq 0x68(%rsp), %rdi
movq 0x1a0(%rsp), %rsi
callq 0x55560
movq %rax, 0x70(%rsp)
jmp 0x537a5
movq 0x70(%rsp), %rax
movq %rax, 0x190(%rsp)
jmp 0x53819
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0x538d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x6988
jmp 0x538d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x556d0
jmp 0x538d8
jmp 0x5381b
leaq 0x348(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x1e340
movq %rax, 0x180(%rsp)
leaq 0x188(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x555d0
leaq 0x1e8(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x1e310
movq 0x50(%rsp), %rdi
movq %rax, 0x178(%rsp)
callq 0x1e340
movq 0x58(%rsp), %rdi
movq %rax, 0x170(%rsp)
movq 0x188(%rsp), %rsi
movq 0x178(%rsp), %rdx
movq 0x170(%rsp), %rcx
callq 0x55610
movq %rax, 0x60(%rsp)
jmp 0x5389f
movq 0x60(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x556d0
leaq 0x200(%rsp), %rdi
callq 0x55110
leaq 0x250(%rsp), %rdi
callq 0x55140
jmp 0x5361a
leaq 0x200(%rsp), %rdi
callq 0x55110
jmp 0x53aff
movq 0x368(%rsp), %rdi
callq 0x67e70
movq %rax, 0x48(%rsp)
jmp 0x538fe
movq 0x368(%rsp), %rdi
callq 0x67e70
movq %rax, 0x40(%rsp)
jmp 0x53912
movq 0x40(%rsp), %rdi
callq 0x1e340
movq %rax, 0x158(%rsp)
leaq 0x160(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x555d0
leaq 0x348(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x1e310
movq 0x30(%rsp), %rdi
movq %rax, 0x150(%rsp)
callq 0x1e340
movq 0x48(%rsp), %rdi
movq %rax, 0x148(%rsp)
movq 0x160(%rsp), %rsi
movq 0x150(%rsp), %rdx
movq 0x148(%rsp), %rcx
callq 0x55610
movq %rax, 0x38(%rsp)
jmp 0x5398e
movq 0x38(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x368(%rsp), %rdi
callq 0x67e80
movq %rax, 0x28(%rsp)
jmp 0x539af
movq 0x28(%rsp), %rdi
leaq 0x330(%rsp), %rsi
callq 0x6b5b0
jmp 0x539c3
movq 0x368(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq 0x388(%rsp), %rdx
movq 0x380(%rsp), %rcx
leaq 0x128(%rsp), %rdi
callq 0x6cda0
jmp 0x539ef
movq 0x20(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x67fd0
jmp 0x53a03
leaq 0x128(%rsp), %rdi
callq 0x31b40
movq 0x360(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x53a3c
movq 0x18(%rsp), %rdi
callq 0x556f0
movq 0x18(%rsp), %rdi
movl $0x80, %esi
callq 0x64c0
movq 0x398(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x378(%rsp), %rdi
callq 0x55460
movq 0x10(%rsp), %rdi
movq %rax, %rsi
addq $0x4, %rsi
callq 0x55750
jmp 0x53a69
movq 0x3a0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x378(%rsp), %rsi
callq 0x557f0
movq (%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x55780
movq %rax, 0x8(%rsp)
jmp 0x53aa2
movq 0x8(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x378(%rsp)
leaq 0x330(%rsp), %rdi
callq 0xa2c0
leaq 0x348(%rsp), %rdi
callq 0xa2c0
jmp 0x530a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x31b40
leaq 0x330(%rsp), %rdi
callq 0xa2c0
leaq 0x348(%rsp), %rdi
callq 0xa2c0
jmp 0x53b23
addq $0x3a8, %rsp            # imm = 0x3A8
retq
movq 0x320(%rsp), %rdi
callq 0x6850