// File: /workspace/github_cpp_makefile_star2_O2/adamnovak[P]rlcsa/rlcsa_builder.cpp
// Function: CSA::RLCSABuilder::insertCollection(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void
RLCSABuilder::insertCollection(const std::string& base_name)
{
  if(!this->ok) { return; }

  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(this->threads);
  #endif

  this->flush();

  std::ifstream input(base_name.c_str(), std::ios_base::binary);
  if(!input) { return; }

  usint data_size = fileSize(input);
  uchar* data = new uchar[data_size];
  input.read((char*)data, data_size);
  input.close();

  if(this->index == 0)
  {
    this->addCollection(data, data_size, true);
    return;
  }

  std::vector<usint> end_markers;
  usint* ranks = this->getRanks(data, data_size, end_markers);

  double start = readTimer();
  #pragma omp parallel for schedule(static)
  for(usint i = 0; i < data_size; i++) { ranks[i] += end_markers.size() + 1 + data[i]; }
  for(usint i = 0; i < end_markers.size(); i++)
  {
    ranks[end_markers[i]] = this->index->getNumberOfSequences() + i;
  }
  RLCSA* increment = new RLCSA(data, ranks, data_size, this->block_size, this->sample_rate, this->threads, false);
  #pragma omp parallel for schedule(static)
  for(usint i = 0; i < data_size; i++) { ranks[i] -= data[i]; }
  delete[] data;
  double mark = readTimer();
  this->build_time += mark - start;

  parallelSort(ranks, ranks + data_size);
  #pragma omp parallel for schedule(static)
  for(usint i = end_markers.size(); i < data_size; i++) { ranks[i] += i - end_markers.size(); }
  this->sort_time += readTimer() - mark;

  this->mergeRLCSA(increment, ranks, data_size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
cmpb $0x1, 0x24(%rdi)
jne 0xa2cf
movq %rsi, %r14
movq %rdi, %rbx
callq 0x9e2c
movq (%r14), %rsi
leaq 0x30(%rsp), %r14
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x3420
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x50(%rsp,%rax)
jne 0xa2c5
leaq 0x30(%rsp), %rdi
callq 0x11bf4
movq %rax, %r13
movl %r13d, %r15d
movq %r15, %rdi
callq 0x3030
movq %rax, %r12
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x3280
leaq 0x30(%rsp), %rdi
callq 0x3080
cmpq $0x0, (%rbx)
je 0xa2b4
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rcx
movapd %xmm0, (%rcx)
andq $0x0, 0x10(%rcx)
movq %rbx, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0xa3ae
movq %rax, %r14
callq 0x11ef8
movsd %xmm0, 0x28(%rsp)
movq 0x18(%rsp), %rax
movq %rax, %rcx
subq 0x10(%rsp), %rcx
shrq $0x2, %rcx
movl %r13d, %ebp
xorl %edx, %edx
cmpq %rdx, %rbp
je 0xa1bb
movzbl (%r12,%rdx), %esi
addl %ecx, %esi
movl (%r14,%rdx,4), %edi
addl %edi, %esi
incl %esi
movl %esi, (%r14,%rdx,4)
incq %rdx
jmp 0xa19e
movq 0x10(%rsp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movq (%rbx), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rax
jbe 0xa1e7
movl 0x820(%rdx), %edi
addl %esi, %edi
movl (%rcx,%rsi,4), %r8d
movl %edi, (%r14,%r8,4)
incl %esi
jmp 0xa1cc
movl $0x830, %edi            # imm = 0x830
callq 0x3220
movl 0x8(%rbx), %r8d
movl 0xc(%rbx), %r9d
movl 0x14(%rbx), %r10d
movq %rax, (%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl %r13d, %ecx
pushq $0x0
pushq %r10
callq 0x5790
popq %rax
popq %rcx
xorl %eax, %eax
cmpq %rax, %rbp
je 0xa22d
movzbl (%r12,%rax), %ecx
subl %ecx, (%r14,%rax,4)
incq %rax
jmp 0xa21a
movq %r12, %rdi
callq 0x32a0
callq 0x11ef8
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
subsd 0x28(%rsp), %xmm0
addsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x28(%rbx)
leaq (%r14,%r15,4), %rsi
movq %r14, %rdi
callq 0xa5e6
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
shrq $0x2, %rax
movl %eax, %eax
xorl %ecx, %ecx
cmpq %rbp, %rax
jae 0xa284
addl %ecx, (%r14,%rax,4)
incq %rax
incl %ecx
jmp 0xa274
callq 0x11ef8
subsd 0x8(%rsp), %xmm0
addsd 0x38(%rbx), %xmm0
movsd %xmm0, 0x38(%rbx)
movq %rbx, %rdi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0xa47a
leaq 0x10(%rsp), %rdi
callq 0xa5c8
jmp 0xa2c5
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0xa320
leaq 0x30(%rsp), %rdi
callq 0x3090
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x830, %esi            # imm = 0x830
movq (%rsp), %rdi
callq 0x3230
jmp 0xa2ff
jmp 0xa2fc
jmp 0xa2fc
jmp 0xa2fc
jmp 0xa30b
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xa5c8
jmp 0xa30e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3400