// File: /workspace/github_cpp_makefile_star2_O3/Kobzol[P]sigmod-2016/src/main.cpp
// Function: main
int main()
{
    std::ios::sync_with_stdio(true);

#ifdef REDIRECT_TEST_FILE_TO_INPUT
    std::ifstream soubor("test/test-data.txt", std::ios::in);
    std::istream& vstup = soubor;
#else
    std::istream& vstup = std::cin;
#endif

    std::string line;
    while (std::getline(vstup, line))
    {
        if (line[0] == 'S')
        {
            break;
        }
        else
        {
            std::stringstream ss(line);
            sigint from, to;
            ss >> from >> to;
            graph.add_edge(from, to);
        }
    }

    std::cout << "R" << std::endl;  // TIMER STARTS
    size_t query_id = 1;

    while (std::getline(vstup, line))
    {
        if (line[0] == 'F')
        {
#ifdef USE_THREADS
            std::unique_lock<std::mutex> lock(jobQueue.jobMutex);
            jobQueue.batchEnded = true;

            while (jobQueue.get_size() > 0 || jobQueue.jobs_in_work > 0)
            {
                jobQueue.batchEndCV.wait(lock);
            }

            std::map<size_t, int64_t>& results = jobQueue.get_results();
            std::stringstream ss;
            for (auto& item : results)
            {
                ss << item.second << std::endl;
            }

            std::cout << ss.str();

            jobQueue.clear_results();
#endif
            continue;
        }

        std::stringstream ss(line);
        char action;
        sigint from, to;
        ss >> action >> from >> to;

        switch (action)
        {
            case 'A':
#ifdef USE_THREADS
                jobQueue.add_job(JobType::AddEdge, from, to);
#else
                graph.add_edge(from, to);
#endif
                break;
            case 'D':
#ifdef USE_THREADS
                jobQueue.add_job(JobType::RemoveEdge, from, to);
#else
                graph.remove_edge(from, to);
#endif
                break;
            case 'Q':
            {
#ifdef USE_THREADS
                jobQueue.add_job(JobType::Query, from, to);
#else
                int64_t result = GraphEvaluator::query(from, to, query_id++, 0);

#ifdef COLLECT_STATS
                QUERY_COUNT++;
                if (QUERY_RESULTS.count(result))
                {
                    QUERY_RESULTS[result]++;
                }
                else QUERY_RESULTS.insert({result, 1});
#else
                std::cout << result << std::endl;
#endif
#endif
            }
                break;
            default:
                break;
        }
    }

#ifdef COLLECT_STATS
    std::cout << "BFS queue max size: " << BFS_QUEUE_MAX_SIZE << std::endl;
    std::cout << "Pocet dotazu: " << QUERY_COUNT << std::endl;
    std::cout << "Pocet union hitu: " << UNION_HITS << std::endl;

    std::cout << "Histogram vysledku dotazu: " << std::endl;
    for (auto& pair : QUERY_RESULTS)
    {
        std::cout << "Vysledek " << pair.first << " byl nalezen " << pair.second << "x" << std::endl;
    }
#endif

    // threads are detached, so they die with the main thread
    //threadPool.terminate();
    //jobQueue.quit();
    //jobQueue.bufferEmpty.notify_all();
    //threadPool.join();

#ifdef REDIRECT_TEST_FILE_TO_INPUT
    soubor.close();
#endif

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movl $0x1, %edi
callq 0x20b0
movq 0x51d6(%rip), %r14      # 0x7fe0
movq 0x51a7(%rip), %r13      # 0x7fb8
leaq 0x50(%rsp), %rax
leaq 0x40(%rsp), %r12
leaq 0x10(%rsp), %rbp
leaq 0x3127b9(%rip), %r15    # 0x3155e0
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2320
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2ecc
movq 0x40(%rsp), %rax
cmpb $0x53, (%rax)
je 0x2ecc
leaq 0x60(%rsp), %rbx
movl $0x18, %edx
movq %r12, %rsi
movq %rbx, %rdi
callq 0x22b0
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x22f0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x22f0
movl 0x10(%rsp), %edx
movl 0x20(%rsp), %esi
movq %r15, %rdi
callq 0x262a
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
leaq 0xe0(%rsp), %rdi
callq 0x2080
jmp 0x2e36
movq 0x50fd(%rip), %rbx      # 0x7fd0
leaq 0x2302(%rip), %rsi      # 0x51dc
movl $0x1, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movl $0xa, %esi
movq %rbx, %rdi
callq 0x21e0
movq 0x50ce(%rip), %rdi      # 0x7fd0
movsbl %al, %esi
callq 0x2040
movq %rax, %rdi
callq 0x2120
leaq 0x60(%rsp), %rbx
leaq 0x10(%rsp), %r13
leaq 0x312925(%rip), %rbp    # 0x315848
leaq 0x31294e(%rip), %r15    # 0x315878
leaq 0x80(%rbx), %rax
movq %rax, 0x8(%rsp)
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %edx
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x2320
movq (%rax), %rcx
movq 0x40(%rsp), %rdi
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3127
cmpb $0x46, (%rdi)
jne 0x2fb9
leaq 0x3128d3(%rip), %rax    # 0x315850
movq %rax, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
movq %r13, %rdi
callq 0x4420
movb $0x1, %al
movb %al, 0x18(%rsp)
movb %al, 0x31290d(%rip)     # 0x3158a8
movq %rbp, %rdi
callq 0x3416
orq 0x31289e(%rip), %rax    # 0x315848
je 0x3017
movq %r15, %rdi
movq %r13, %rsi
callq 0x2070
jmp 0x2f9b
leaq 0x40(%rsp), %rsi
movl $0x18, %edx
movq %rbx, %rdi
callq 0x22b0
leaq 0x7(%rsp), %rsi
movq %rbx, %rdi
callq 0x2230
leaq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x22f0
movzbl 0x7(%rsp), %eax
cmpl $0x41, %eax
je 0x30f4
cmpl $0x44, %eax
je 0x30ed
cmpl $0x51, %eax
jne 0x3109
xorl %esi, %esi
jmp 0x30f9
movq %rbx, %rdi
movq %r14, %r12
callq 0x2140
movq 0x312937(%rip), %r14    # 0x315960
leaq 0x312920(%rip), %rax    # 0x315950
cmpq %rax, %r14
je 0x307a
movq 0x28(%r14), %rsi
leaq 0x70(%rsp), %rdi
callq 0x21d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq %r14, %rdi
callq 0x2130
movq %rax, %r14
jmp 0x3029
leaq 0x20(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x22a0
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x4f36(%rip), %rdi      # 0x7fd0
callq 0x2210
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
leaq 0x60(%rsp), %rbx
movq %r12, %r14
cmpq %rax, %rdi
je 0x30bb
callq 0x21c0
leaq 0x312886(%rip), %rdi    # 0x315948
callq 0x43f2
movq 0x4eea(%rip), %rsi      # 0x7fb8
movq %rbx, %rdi
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x2080
movq %r13, %rdi
callq 0x3452
jmp 0x2f36
movl $0x2, %esi
jmp 0x30f9
movl $0x1, %esi
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %ecx
movq %rbp, %rdi
callq 0x3468
movq 0x4ea8(%rip), %rsi      # 0x7fb8
movq %rbx, %rdi
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x2080
jmp 0x2f36
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3136
callq 0x21c0
xorl %eax, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31af
jmp 0x31af
movq 0x20(%rsp), %rdi
movq %rax, %rbx
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x31bc
callq 0x21c0
jmp 0x31bc
jmp 0x31b9
jmp 0x31b4
jmp 0x31af
jmp 0x31af
jmp 0x31af
movq 0x4e40(%rip), %rsi      # 0x7fb8
leaq 0x60(%rsp), %rdi
movq %rax, %rbx
callq 0x2160
leaq 0xe0(%rsp), %rdi
jmp 0x31a8
movq 0x4e22(%rip), %rsi      # 0x7fb8
leaq 0x60(%rsp), %rdi
movq %rax, %rbx
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x2080
jmp 0x31e1
movq %rax, %rbx
jmp 0x31e1
movq %rax, %rbx
jmp 0x31d7
movq %rax, %rbx
movq 0x4df5(%rip), %rsi      # 0x7fb8
leaq 0x60(%rsp), %rdi
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x2080
leaq 0x10(%rsp), %rdi
callq 0x3452
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x31f5
callq 0x21c0
movq %rbx, %rdi
callq 0x2300
nop