// File: /workspace/github_cpp_makefile_star2_O3/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 0xb6e1
movq %rsi, %r14
movq %rdi, %rbx
callq 0xb3c4
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 0xb6f3
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 0x16398
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 0xb8b7
xorpd %xmm0, %xmm0
movq %rsp, %rcx
movapd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0xb9a8
movq %rax, %r15
callq 0x16787
movsd %xmm0, 0x28(%rsp)
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
testl %r14d, %r14d
je 0xb7a1
movq %rcx, %rsi
shrq $0x2, %rsi
movl %r14d, %edi
xorl %r8d, %r8d
movzbl (%r13,%r8), %r9d
addl %esi, %r9d
movl (%r15,%r8,4), %r10d
addl %r10d, %r9d
incl %r9d
movl %r9d, (%r15,%r8,4)
incq %r8
cmpq %r8, %rdi
jne 0xb782
cmpq %rax, %rdx
je 0xb7c9
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 0xb7af
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 0x6546
addq $0x10, %rsp
testl %r14d, %r14d
je 0xb815
movl %r14d, %eax
xorl %ecx, %ecx
movzbl (%r13,%rcx), %edx
subl %edx, (%r15,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0xb803
movq %r13, %rdi
callq 0x32c0
callq 0x16787
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 0xbbfa
movq 0x8(%rsp), %rax
subq (%rsp), %rax
shrq $0x2, %rax
cmpl %r14d, %eax
jae 0xb875
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 0xb86a
callq 0x16787
subsd 0x20(%rsp), %xmm0
addsd 0x38(%rbx), %xmm0
movsd %xmm0, 0x38(%rbx)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xbaa8
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb6d7
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3240
jmp 0xb6d7
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
movl $0x1, %ecx
callq 0xb91a
jmp 0xb6d7
movq %rax, %rbx
movl $0x830, %esi            # imm = 0x830
movq %rbp, %rdi
callq 0x3240
jmp 0xb8ec
jmp 0xb8e9
jmp 0xb8e9
jmp 0xb8e9
jmp 0xb904
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb907
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3240
jmp 0xb907
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3410
nop