// File: /workspace/github_cpp_makefile_star2_O2/MITRECND[P]snugglefish/src/nGramSearch.cpp
// Function: snugglefish::nGramSearch::searchAlpha(snugglefish::indexSet*, std::__cxx11::list<std::pair<unsigned long, unsigned long>, std::allocator<std::pair<unsigned long, unsigned long>>>&)
list<ngram_t_fidtype> nGramSearch::searchAlpha(indexSet* index, list< pair<uint64_t,size_t> > &queryList){
    list<ngram_t_fidtype> matchedIds;

    if(queryList.size() == 0){
        return matchedIds;
    }
    // this gets the ngram list for the first file, and places them 
    // into the matchedIds list
    ngram_t_numfiles count = 0;
    ngram_t_fidtype* ngrams = index->getNGrams((uint64_t) queryList.front().first, (size_t*) &count);
    for (ngram_t_numfiles j = 0; j < count; j++){
        matchedIds.push_back(ngrams[j]);
    }
        
    //Dont need the front element anymore so get rid of it
    queryList.pop_front();

    //For every id see if it's in the remaining Ngrams
    //Now for every subsequent NGram whittle down the list
    for(list< pair<uint64_t, size_t> >::iterator it = queryList.begin();
            it != queryList.end(); it++){

        ngram_t_numfiles ngram_elements = 0;
        ngram_t_fidtype* ngrams = index->getNGrams((uint64_t) (*it).first, (size_t*) &ngram_elements);
        uint32_t ngram_index = 0;

        list<ngram_t_fidtype>::iterator ft = matchedIds.begin();
        while(ft != matchedIds.end()){
            bool found = false;
            // Checks each element of the next fid array for
            // the current fid in matchedIds
            while(ngram_index < ngram_elements){
                if (ngrams[ngram_index] == (*ft)){
                    found = true;
                    break;
                }else if(ngrams[ngram_index] > (*ft)){
                    //update ngram location
                    // If the fid is too large, it is
                    // necessary to recheck the same index on the
                    // next pass, as to make sure it isn't missed
                    break;
                }
                ngram_index++;
            }

            if(!found){
                ft = matchedIds.erase(ft);
            }else{
                // ft++ is only needed if erase isn't called as 
                // erase moves the iterator ahead
                ft++;
            }
        }
    }

    return matchedIds;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
andq $0x0, 0x10(%rdi)
cmpq $0x0, 0x10(%rdx)
je 0x8666
movq %rdx, %r15
movq %rsi, %r13
leaq -0x30(%rbp), %rdx
andl $0x0, (%rdx)
movq (%r15), %rax
movq 0x10(%rax), %rsi
movq %r13, %rdi
callq 0xa5fe
movq %rax, %r12
xorl %r14d, %r14d
movl -0x30(%rbp), %eax
cmpq %rax, %r14
jae 0x85e7
movq %rbx, %rdi
movq %r12, %rsi
callq 0x86ea
incq %r14
addq $0x4, %r12
jmp 0x85cb
movq %r15, %rdi
callq 0x86fa
movq %r15, %r14
movq %r15, -0x38(%rbp)
movq (%r14), %r14
cmpq %r15, %r14
je 0x8666
andl $0x0, -0x2c(%rbp)
movq 0x10(%r14), %rsi
movq %r13, %r15
movq %r13, %rdi
leaq -0x2c(%rbp), %rdx
callq 0xa5fe
movq %rax, %r13
movq (%rbx), %rax
xorl %ecx, %ecx
cmpq %rbx, %rax
je 0x865d
movl -0x2c(%rbp), %edx
cmpl %edx, %ecx
movl %edx, %r12d
cmoval %ecx, %r12d
movl %ecx, %ecx
cmpl %edx, %ecx
jae 0x864d
movl 0x10(%rax), %esi
cmpl %esi, (%r13,%rcx,4)
je 0x8645
ja 0x864a
incq %rcx
jmp 0x8630
movq (%rax), %rax
jmp 0x861d
movl %ecx, %r12d
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8708
movl %r12d, %ecx
jmp 0x861d
movq %r15, %r13
movq -0x38(%rbp), %r15
jmp 0x85f6
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x867c
jmp 0x867c
movq %rax, %r14
movq %rbx, %rdi
callq 0x8720
movq %r14, %rdi
callq 0x64b0
nop