// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq $0x0, 0x10(%rdi)
cmpq $0x0, 0x10(%rdx)
je 0x832e
movq %rdx, %r15
movq %rsi, %r13
leaq -0x30(%rbp), %rdx
movl $0x0, (%rdx)
movq (%r15), %rax
movq 0x10(%rax), %rsi
movq %r13, %rdi
callq 0xa220
cmpl $0x0, -0x30(%rbp)
je 0x8260
movq %rax, %r12
xorl %r14d, %r14d
movl $0x18, %edi
callq 0x5280
movl (%r12,%r14,4), %ecx
movl %ecx, 0x10(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5060
incq 0x10(%rbx)
incq %r14
movl -0x30(%rbp), %eax
cmpq %rax, %r14
jb 0x8235
movq (%r15), %r12
decq 0x10(%r15)
movq %r12, %rdi
callq 0x5450
movl $0x20, %esi
movq %r12, %rdi
callq 0x5290
movq (%r15), %rax
cmpq %r15, %rax
je 0x832e
movq %r15, -0x40(%rbp)
movq %r13, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
movq %rax, -0x48(%rbp)
movq 0x10(%rax), %rsi
movq %r13, %rdi
leaq -0x2c(%rbp), %rdx
callq 0xa220
movq (%rbx), %r12
cmpq %rbx, %r12
je 0x8316
movq %rax, %r13
xorl %r14d, %r14d
movl -0x2c(%rbp), %eax
cmpl %eax, %r14d
jae 0x82e9
movl 0x10(%r12), %ecx
movl %r14d, %edx
leaq (,%rdx,4), %rdx
addq %r13, %rdx
cmpl %ecx, (%rdx)
je 0x8310
ja 0x82e9
incl %r14d
addq $0x4, %rdx
cmpl %r14d, %eax
jne 0x82d4
movl %eax, %r14d
movq (%r12), %r15
decq 0x10(%rbx)
movq %r12, %rdi
callq 0x5450
movl $0x18, %esi
movq %r12, %rdi
callq 0x5290
movq %r15, %r12
cmpq %rbx, %r15
jne 0x82b9
jmp 0x8316
movq (%r12), %r15
jmp 0x8306
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %r15
cmpq %r15, %rax
movq -0x38(%rbp), %r13
jne 0x8290
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8344
jmp 0x8344
movq %rax, %r14
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x8364
movq (%rdi), %r15
movl $0x18, %esi
callq 0x5290
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x834f
movq %r14, %rdi
callq 0x54e0