// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x1
popq %rdi
callq 0x20a0
leaq 0x30(%rsp), %rax
movq 0x51cb(%rip), %rbx      # 0x7fe0
leaq 0x20(%rsp), %r14
leaq 0x60(%rsp), %r15
leaq 0x40(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x3127b0(%rip), %rbp    # 0x3155e0
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2050
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2e9d
movq 0x20(%rsp), %rax
cmpb $0x53, (%rax)
je 0x2e9d
movq %r15, %rdi
movq %r14, %rsi
pushq $0x18
popq %rdx
callq 0x22b0
movq %r15, %rdi
movq %r12, %rsi
callq 0x22f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x22f0
movl 0x10(%rsp), %edx
movl 0x40(%rsp), %esi
movq %rbp, %rdi
callq 0x25b8
movq %r15, %rdi
callq 0x2160
jmp 0x2e3c
movq 0x512c(%rip), %rdi      # 0x7fd0
leaq 0x22dd(%rip), %rsi      # 0x5188
callq 0x21f0
movq %rax, %rdi
callq 0x20d0
movq 0x5121(%rip), %r14      # 0x7fe0
leaq 0x10(%rsp), %rbx
leaq 0x31297d(%rip), %r15    # 0x315848
leaq 0x3129a6(%rip), %rbp    # 0x315878
leaq 0x312a77(%rip), %r12    # 0x315950
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x2050
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x304e
movq 0x20(%rsp), %rax
cmpb $0x46, (%rax)
jne 0x2f3b
leaq 0x312947(%rip), %rsi    # 0x315850
movq %rbx, %rdi
callq 0x328e
leaq 0x60(%rsp), %r13
movb $0x1, 0x31298b(%rip)    # 0x3158a8
movq %r15, %rdi
callq 0x32a4
orq 0x31291c(%rip), %rax    # 0x315848
je 0x2f9f
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2070
jmp 0x2f1d
leaq 0x60(%rsp), %r13
leaq 0x20(%rsp), %rsi
movq %r13, %rdi
pushq $0x18
popq %rdx
callq 0x22b0
leaq 0xf(%rsp), %rsi
movq %r13, %rdi
callq 0x2230
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22f0
movzbl 0xf(%rsp), %eax
pushq $0x1
popq %rsi
cmpl $0x41, %eax
je 0x302f
cmpl $0x44, %eax
je 0x302c
cmpl $0x51, %eax
jne 0x303f
xorl %esi, %esi
jmp 0x302f
movq %r13, %rdi
callq 0x2140
movq 0x3129b2(%rip), %r13    # 0x315960
cmpq %r12, %r13
je 0x2fd6
movq 0x28(%r13), %rsi
leaq 0x70(%rsp), %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x20d0
movq %r13, %rdi
callq 0x2130
movq %rax, %r13
jmp 0x2fae
leaq 0x40(%rsp), %r14
leaq 0x78(%rsp), %rsi
movq %r14, %rdi
callq 0x22a0
movq 0x4fe1(%rip), %rdi      # 0x7fd0
movq %r14, %rsi
callq 0x21b0
movq 0x4fe2(%rip), %r13      # 0x7fe0
movq %r14, %rdi
callq 0x20b0
leaq 0x31293b(%rip), %rdi    # 0x315948
callq 0x42b6
leaq 0x60(%rsp), %rdi
callq 0x2160
movq %rbx, %rdi
callq 0x32d4
movq %r13, %r14
jmp 0x2ed9
pushq $0x2
popq %rsi
movl 0x40(%rsp), %edx
movl 0x10(%rsp), %ecx
movq %r15, %rdi
callq 0x32ea
leaq 0x60(%rsp), %rdi
callq 0x2160
jmp 0x2ed9
leaq 0x20(%rsp), %rdi
callq 0x20b0
xorl %eax, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3089
jmp 0x3089
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
callq 0x20b0
jmp 0x30a7
jmp 0x30a4
jmp 0x309f
jmp 0x3089
jmp 0x3089
jmp 0x3089
movq %rax, %rbx
jmp 0x30bb
jmp 0x3090
leaq 0x60(%rsp), %rdi
movq %rax, %rbx
callq 0x2160
jmp 0x30bb
movq %rax, %rbx
jmp 0x30b1
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x2160
leaq 0x10(%rsp), %rdi
callq 0x32d4
leaq 0x20(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2300
nop