// File: /workspace/github_cpp_makefile_star2_O1/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 0xb567
movq %rsi, %r14
movq %rdi, %rbx
callq 0xb24a
movq (%r14), %rsi
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movl $0x4, %edx
callq 0x3430
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x50(%rsp,%rax)
je 0xb579
leaq 0x30(%rsp), %rdi
callq 0x3090
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %rdi
callq 0x15d6c
movq %rax, %r14
movl %r14d, %r12d
movq %r12, %rdi
callq 0x3030
movq %rax, %r13
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x32a0
leaq 0x30(%rsp), %rdi
callq 0x3080
cmpq $0x0, (%rbx)
je 0xb739
xorpd %xmm0, %xmm0
movq %rsp, %rcx
movapd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0xb82a
movq %rax, %r15
callq 0x16167
movsd %xmm0, 0x28(%rsp)
testl %r14d, %r14d
je 0xb61a
movq 0x8(%rsp), %rax
subq (%rsp), %rax
shrq $0x2, %rax
movl %r14d, %ecx
xorl %edx, %edx
movzbl (%r13,%rdx), %esi
addl %eax, %esi
movl (%r15,%rdx,4), %edi
addl %edi, %esi
incl %esi
movl %esi, (%r15,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0xb5fe
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
je 0xb64b
sarq $0x2, %rcx
movq (%rbx), %rdx
xorl %esi, %esi
movl 0x820(%rdx), %edi
addl %esi, %edi
movl (%rax,%rsi,4), %r8d
movl %edi, (%r15,%r8,4)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0xb631
movl $0x830, %edi            # imm = 0x830
callq 0x3230
movq %rax, %rbp
movl 0x8(%rbx), %r8d
movl 0xc(%rbx), %r9d
movl 0x14(%rbx), %eax
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movl %r14d, %ecx
pushq $0x0
pushq %rax
callq 0x657c
addq $0x10, %rsp
testl %r14d, %r14d
je 0xb697
movl %r14d, %eax
xorl %ecx, %ecx
movzbl (%r13,%rcx), %edx
subl %edx, (%r15,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0xb685
movq %r13, %rdi
callq 0x32c0
callq 0x16167
movsd %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
subsd 0x28(%rsp), %xmm0
addsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x28(%rbx)
leaq (%r15,%r12,4), %rsi
movq %r15, %rdi
callq 0xba6e
movq 0x8(%rsp), %rax
subq (%rsp), %rax
shrq $0x2, %rax
cmpl %r14d, %eax
jae 0xb6f7
movl %eax, %edx
movl %r14d, %eax
leaq (%r15,%rdx,4), %rcx
subq %rdx, %rax
xorl %edx, %edx
addl %edx, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0xb6ec
callq 0x16167
subsd 0x20(%rsp), %xmm0
addsd 0x38(%rbx), %xmm0
movsd %xmm0, 0x38(%rbx)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xb91c
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb55d
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3240
jmp 0xb55d
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
movl $0x1, %ecx
callq 0xb79c
jmp 0xb55d
movq %rax, %rbx
movl $0x830, %esi            # imm = 0x830
movq %rbp, %rdi
callq 0x3240
jmp 0xb76e
jmp 0xb76b
jmp 0xb76b
jmp 0xb76b
jmp 0xb786
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb789
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3240
jmp 0xb789
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3410
nop