// File: 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:
subq $0x5d8, %rsp            # imm = 0x5D8
movl $0x0, 0x5d4(%rsp)
movl $0x1, %edi
callq 0x20b0
movq 0xbafd(%rip), %rax      # 0x10fe0
movq %rax, 0x5c8(%rsp)
leaq 0x5a8(%rsp), %rdi
callq 0x2270
movq 0xbae1(%rip), %rdi      # 0x10fe0
leaq 0x5a8(%rsp), %rsi
callq 0x2060
movq %rax, 0x80(%rsp)
jmp 0x5516
movq 0x80(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2230
movb %al, 0x7f(%rsp)
jmp 0x5533
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0x5540
jmp 0x5639
xorl %eax, %eax
movl %eax, %esi
leaq 0x5a8(%rsp), %rdi
callq 0x2340
movq %rax, 0x70(%rsp)
jmp 0x5558
movq 0x70(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x53, %eax
jne 0x5583
jmp 0x5639
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5a0(%rsp)
movl %eax, 0x59c(%rsp)
jmp 0x5a73
movl $0x10, %edi
movl $0x8, %esi
callq 0x5e20
movl %eax, 0x6c(%rsp)
jmp 0x5598
movl 0x6c(%rsp), %edx
leaq 0x410(%rsp), %rdi
leaq 0x5a8(%rsp), %rsi
callq 0x22d0
jmp 0x55b3
leaq 0x410(%rsp), %rdi
leaq 0x40c(%rsp), %rsi
callq 0x2040
movq %rax, 0x60(%rsp)
jmp 0x55cf
movq 0x60(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x2040
jmp 0x55e3
movl 0x40c(%rsp), %esi
movl 0x408(%rsp), %edx
leaq 0x318ff8(%rip), %rdi    # 0x31e5f0
callq 0x25f0
jmp 0x55ff
leaq 0x410(%rsp), %rdi
callq 0x2160
jmp 0x5634
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5a0(%rsp)
movl %eax, 0x59c(%rsp)
leaq 0x410(%rsp), %rdi
callq 0x2160
jmp 0x5a73
jmp 0x54f8
movq 0xb990(%rip), %rdi      # 0x10fd0
leaq 0x59e9(%rip), %rsi      # 0xb030
callq 0x21f0
movq %rax, 0x58(%rsp)
jmp 0x5653
movq 0x58(%rsp), %rdi
movq 0xb959(%rip), %rsi      # 0x10fb8
callq 0x2220
jmp 0x5666
movq $0x1, 0x400(%rsp)
movq 0xb967(%rip), %rdi      # 0x10fe0
leaq 0x5a8(%rsp), %rsi
callq 0x2060
movq %rax, 0x50(%rsp)
jmp 0x568d
movq 0x50(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2230
movb %al, 0x4f(%rsp)
jmp 0x56a7
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x56b4
jmp 0x5a4c
xorl %eax, %eax
movl %eax, %esi
leaq 0x5a8(%rsp), %rdi
callq 0x2340
movq %rax, 0x40(%rsp)
jmp 0x56cc
movq 0x40(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x46, %eax
jne 0x5907
leaq 0x31917c(%rip), %rsi    # 0x31e860
leaq 0x3f0(%rsp), %rdi
callq 0x5e40
jmp 0x56f3
movb $0x1, 0x3191be(%rip)    # 0x31e8b8
leaq 0x319157(%rip), %rdi    # 0x31e858
callq 0x5e80
movq %rax, 0x38(%rsp)
jmp 0x570d
movq 0x38(%rsp), %rcx
movb $0x1, %al
cmpq $0x0, %rcx
movb %al, 0x37(%rsp)
ja 0x572d
cmpq $0x0, 0x319132(%rip)    # 0x31e858
seta %al
movb %al, 0x37(%rsp)
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x5737
jmp 0x576a
leaq 0x31911a(%rip), %rdi    # 0x31e858
addq $0x30, %rdi
leaq 0x3f0(%rsp), %rsi
callq 0x2080
jmp 0x56fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5a0(%rsp)
movl %eax, 0x59c(%rsp)
jmp 0x58f5
leaq 0x3190e7(%rip), %rdi    # 0x31e858
callq 0x5ed0
movq %rax, 0x28(%rsp)
jmp 0x577d
movq 0x28(%rsp), %rax
movq %rax, 0x3e8(%rsp)
leaq 0x260(%rsp), %rdi
callq 0x2140
jmp 0x5799
movq 0x3e8(%rsp), %rax
movq %rax, 0x258(%rsp)
movq 0x258(%rsp), %rdi
callq 0x5ef0
movq %rax, 0x250(%rsp)
movq 0x258(%rsp), %rdi
callq 0x5f20
movq %rax, 0x248(%rsp)
leaq 0x250(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x5f50
testb $0x1, %al
jne 0x57ee
jmp 0x5860
leaq 0x250(%rsp), %rdi
callq 0x5f70
movq %rax, 0x240(%rsp)
leaq 0x270(%rsp), %rdi
movq 0x240(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x2330
movq %rax, 0x20(%rsp)
jmp 0x5823
movq 0x20(%rsp), %rdi
movq 0xb789(%rip), %rsi      # 0x10fb8
callq 0x2220
jmp 0x5836
jmp 0x5838
leaq 0x250(%rsp), %rdi
callq 0x5f90
jmp 0x57d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5a0(%rsp)
movl %eax, 0x59c(%rsp)
jmp 0x58e8
leaq 0x220(%rsp), %rdi
leaq 0x260(%rsp), %rsi
callq 0x21e0
jmp 0x5877
movq 0xb752(%rip), %rdi      # 0x10fd0
leaq 0x220(%rsp), %rsi
callq 0x21b0
jmp 0x588d
leaq 0x220(%rsp), %rdi
callq 0x2100
leaq 0x318fb7(%rip), %rdi    # 0x31e858
callq 0x5fc0
jmp 0x58a8
leaq 0x260(%rsp), %rdi
callq 0x2160
leaq 0x3f0(%rsp), %rdi
callq 0x5fe0
jmp 0x5672
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5a0(%rsp)
movl %eax, 0x59c(%rsp)
leaq 0x220(%rsp), %rdi
callq 0x2100
leaq 0x260(%rsp), %rdi
callq 0x2160
leaq 0x3f0(%rsp), %rdi
callq 0x5fe0
jmp 0x5a73
movl $0x10, %edi
movl $0x8, %esi
callq 0x5e20
movl %eax, 0x1c(%rsp)
jmp 0x591c
movl 0x1c(%rsp), %edx
leaq 0x98(%rsp), %rdi
leaq 0x5a8(%rsp), %rsi
callq 0x22d0
jmp 0x5937
leaq 0x98(%rsp), %rdi
leaq 0x97(%rsp), %rsi
callq 0x2250
movq %rax, 0x10(%rsp)
jmp 0x5953
movq 0x10(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x2040
movq %rax, 0x8(%rsp)
jmp 0x596c
movq 0x8(%rsp), %rdi
leaq 0x8c(%rsp), %rsi
callq 0x2040
jmp 0x5980
movsbl 0x97(%rsp), %eax
movl %eax, 0x4(%rsp)
subl $0x41, %eax
je 0x59d2
jmp 0x5993
movl 0x4(%rsp), %eax
subl $0x44, %eax
je 0x59f5
jmp 0x599e
movl 0x4(%rsp), %eax
subl $0x51, %eax
je 0x5a18
jmp 0x5a38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5a0(%rsp)
movl %eax, 0x59c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x2160
jmp 0x5a73
movl 0x90(%rsp), %edx
movl 0x8c(%rsp), %ecx
leaq 0x318e71(%rip), %rdi    # 0x31e858
movl $0x1, %esi
callq 0x6020
jmp 0x59f3
jmp 0x5a3a
movl 0x90(%rsp), %edx
movl 0x8c(%rsp), %ecx
leaq 0x318e4e(%rip), %rdi    # 0x31e858
movl $0x2, %esi
callq 0x6020
jmp 0x5a16
jmp 0x5a3a
movl 0x90(%rsp), %edx
movl 0x8c(%rsp), %ecx
leaq 0x318e2b(%rip), %rdi    # 0x31e858
xorl %esi, %esi
callq 0x6020
jmp 0x5a36
jmp 0x5a3a
jmp 0x5a3a
leaq 0x98(%rsp), %rdi
callq 0x2160
jmp 0x5672
movl $0x0, 0x5d4(%rsp)
leaq 0x5a8(%rsp), %rdi
callq 0x2100
movl 0x5d4(%rsp), %eax
addq $0x5d8, %rsp            # imm = 0x5D8
retq
leaq 0x5a8(%rsp), %rdi
callq 0x2100
movq 0x5a0(%rsp), %rdi
callq 0x2310
nopl (%rax)