// File: /workspace/github_cpp_makefile_star2_O1/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
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x51ea(%rip), %r14      # 0x7fe0
leaq 0x40(%rsp), %r12
leaq 0x8(%rsp), %rbp
leaq 0x3127d9(%rip), %r15    # 0x3155e0
movq 0x51aa(%rip), %r13      # 0x7fb8
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 0x2ea4
movq 0x40(%rsp), %rax
cmpb $0x53, (%rax)
je 0x2ea4
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movl $0x18, %edx
callq 0x22b0
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x22f0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x22f0
movl 0x20(%rsp), %esi
movl 0x8(%rsp), %edx
movq %r15, %rdi
callq 0x262a
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
leaq 0xe0(%rsp), %rdi
callq 0x2080
jmp 0x2e0e
movq 0x5125(%rip), %rbx      # 0x7fd0
leaq 0x232a(%rip), %rsi      # 0x51dc
movl $0x1, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq 0x50f3(%rip), %rdi      # 0x7fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
leaq 0x60(%rsp), %rbx
leaq 0x80(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x8(%rsp), %r13
leaq 0x312941(%rip), %rbp    # 0x315848
leaq 0x31296a(%rip), %r15    # 0x315878
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %edx
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2320
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
movq 0x40(%rsp), %rdi
jne 0x30ff
cmpb $0x46, (%rdi)
jne 0x2f91
leaq 0x3128fb(%rip), %rax    # 0x315850
movq %rax, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq %r13, %rdi
callq 0x442c
movb $0x1, %al
movb %al, 0x10(%rsp)
movb %al, 0x312935(%rip)     # 0x3158a8
movq %rbp, %rdi
callq 0x33d8
orq 0x3128c6(%rip), %rax    # 0x315848
je 0x2fef
movq %r15, %rdi
movq %r13, %rsi
callq 0x2070
jmp 0x2f73
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
movl $0x18, %edx
callq 0x22b0
movq %rbx, %rdi
leaq 0x7(%rsp), %rsi
callq 0x2230
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x22f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x22f0
movzbl 0x7(%rsp), %eax
cmpl $0x41, %eax
je 0x30cc
cmpl $0x44, %eax
je 0x30c5
cmpl $0x51, %eax
jne 0x30e1
xorl %esi, %esi
jmp 0x30d1
movq %r14, %r12
movq %rbx, %rdi
callq 0x2140
movq 0x31295f(%rip), %r14    # 0x315960
leaq 0x312948(%rip), %rax    # 0x315950
cmpq %rax, %r14
je 0x3052
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 0x3001
leaq 0x20(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x22a0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x4f5e(%rip), %rdi      # 0x7fd0
callq 0x2210
movq %r12, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
leaq 0x60(%rsp), %rbx
je 0x3093
callq 0x21c0
leaq 0x3128ae(%rip), %rdi    # 0x315948
callq 0x43fe
movq %rbx, %rdi
movq 0x4f0f(%rip), %rsi      # 0x7fb8
callq 0x2160
movq 0x18(%rsp), %rdi
callq 0x2080
movq %r13, %rdi
callq 0x3414
jmp 0x2f0e
movl $0x2, %esi
jmp 0x30d1
movl $0x1, %esi
movl 0x8(%rsp), %ecx
movl 0x20(%rsp), %edx
movq %rbp, %rdi
callq 0x342a
movq %rbx, %rdi
movq 0x4ecd(%rip), %rsi      # 0x7fb8
callq 0x2160
movq 0x18(%rsp), %rdi
callq 0x2080
jmp 0x2f0e
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x310e
callq 0x21c0
xorl %eax, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x316e
jmp 0x316e
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x317b
callq 0x21c0
jmp 0x317b
jmp 0x3178
jmp 0x3173
jmp 0x316e
jmp 0x316e
jmp 0x316e
jmp 0x314b
movq %rax, %rbx
movq 0x4e63(%rip), %rsi      # 0x7fb8
leaq 0x60(%rsp), %rdi
callq 0x2160
leaq 0xe0(%rsp), %rdi
callq 0x2080
jmp 0x31a3
movq %rax, %rbx
jmp 0x31a3
movq %rax, %rbx
jmp 0x3199
movq %rax, %rbx
movq 0x4e36(%rip), %rsi      # 0x7fb8
leaq 0x60(%rsp), %rdi
callq 0x2160
leaq 0xe0(%rsp), %rdi
callq 0x2080
leaq 0x8(%rsp), %rdi
callq 0x3414
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x31b7
callq 0x21c0
movq %rbx, %rdi
callq 0x2300
nop