// File: 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
movq %rsp, %rbp
subq $0x320, %rsp            # imm = 0x320
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x290(%rbp)
testb $0x1, 0x24(%rax)
jne 0x14239
jmp 0x14726
movq -0x290(%rbp), %rdi
callq 0x13d90
movq -0x10(%rbp), %rdi
callq 0x3120
movq %rax, %rsi
leaq -0x218(%rbp), %rdi
movl $0x4, %edx
callq 0x3550
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x3150
movb %al, -0x291(%rbp)
jmp 0x14282
movb -0x291(%rbp), %al
testb $0x1, %al
jne 0x1428e
jmp 0x142b4
movl $0x1, -0x228(%rbp)
jmp 0x1471a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x1473b
leaq -0x218(%rbp), %rdi
callq 0x25e10
movq %rax, -0x2a0(%rbp)
jmp 0x142c9
movq -0x2a0(%rbp), %rax
movl %eax, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movl %eax, %edi
callq 0x3030
movq %rax, -0x2a8(%rbp)
jmp 0x142ec
movq -0x2a8(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rsi
movl -0x22c(%rbp), %eax
movl %eax, %edx
leaq -0x218(%rbp), %rdi
callq 0x33b0
jmp 0x14317
leaq -0x218(%rbp), %rdi
callq 0x30b0
jmp 0x14325
movq -0x290(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x14361
movq -0x290(%rbp), %rdi
movq -0x238(%rbp), %rsi
movl -0x22c(%rbp), %edx
movl $0x1, %ecx
callq 0x14760
jmp 0x14352
movl $0x1, -0x228(%rbp)
jmp 0x1471a
leaq -0x250(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0xee90
movq -0x290(%rbp), %rdi
movq -0x2b8(%rbp), %rcx
movq -0x238(%rbp), %rsi
movl -0x22c(%rbp), %edx
callq 0x14820
movq %rax, -0x2b0(%rbp)
jmp 0x1439d
movq -0x2b0(%rbp), %rax
movq %rax, -0x258(%rbp)
callq 0x263b0
movsd %xmm0, -0x2c0(%rbp)
jmp 0x143ba
movsd -0x2c0(%rbp), %xmm0
movsd %xmm0, -0x260(%rbp)
movl $0x0, -0x264(%rbp)
movl -0x264(%rbp), %eax
cmpl -0x22c(%rbp), %eax
jae 0x14447
leaq -0x250(%rbp), %rdi
callq 0x11730
movq %rax, %rsi
addq $0x1, %rsi
movq -0x238(%rbp), %rax
movl -0x264(%rbp), %ecx
movzbl (%rax,%rcx), %eax
addq %rax, %rsi
movq -0x258(%rbp), %rax
movl -0x264(%rbp), %ecx
movl (%rax,%rcx,4), %edx
addq %rsi, %rdx
movl %edx, (%rax,%rcx,4)
movl -0x264(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x264(%rbp)
jmp 0x143d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x1472f
movl $0x0, -0x268(%rbp)
movl -0x268(%rbp), %eax
movq %rax, -0x2c8(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x11730
movq %rax, %rcx
movq -0x2c8(%rbp), %rax
cmpq %rcx, %rax
jae 0x144ed
movq -0x290(%rbp), %rax
movq (%rax), %rdi
callq 0x14c30
movl %eax, -0x2cc(%rbp)
jmp 0x14490
movl -0x2cc(%rbp), %eax
addl -0x268(%rbp), %eax
movl %eax, -0x2dc(%rbp)
movq -0x258(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movl -0x268(%rbp), %eax
movl %eax, %esi
leaq -0x250(%rbp), %rdi
callq 0x14c50
movl -0x2dc(%rbp), %edx
movq %rax, %rcx
movq -0x2d8(%rbp), %rax
movl (%rcx), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x268(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x268(%rbp)
jmp 0x14451
movl $0x830, %edi            # imm = 0x830
callq 0x32c0
movq %rax, -0x2e8(%rbp)
jmp 0x14500
movq -0x2e8(%rbp), %rdi
movq -0x290(%rbp), %rax
movq -0x238(%rbp), %rsi
movq -0x258(%rbp), %rdx
movl -0x22c(%rbp), %ecx
movl 0x8(%rax), %r8d
movl 0xc(%rax), %r9d
movl 0x14(%rax), %r10d
movq %rsp, %rax
movl %r10d, (%rax)
movl $0x0, 0x8(%rax)
callq 0x8b60
jmp 0x14542
movq -0x2e8(%rbp), %rax
movq %rax, -0x270(%rbp)
movl $0x0, -0x274(%rbp)
movl -0x274(%rbp), %eax
cmpl -0x22c(%rbp), %eax
jae 0x145c7
movq -0x238(%rbp), %rax
movl -0x274(%rbp), %ecx
movzbl (%rax,%rcx), %esi
movq -0x258(%rbp), %rax
movl -0x274(%rbp), %ecx
movl (%rax,%rcx,4), %edx
subl %esi, %edx
movl %edx, (%rax,%rcx,4)
movl -0x274(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x274(%rbp)
jmp 0x1455a
movq -0x2e8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
movl $0x830, %esi            # imm = 0x830
callq 0x32d0
jmp 0x1472f
movq -0x238(%rbp), %rax
movq %rax, -0x2f0(%rbp)
cmpq $0x0, %rax
je 0x145e7
movq -0x2f0(%rbp), %rdi
callq 0x33d0
callq 0x263b0
movsd %xmm0, -0x2f8(%rbp)
jmp 0x145f6
movq -0x290(%rbp), %rax
movsd -0x2f8(%rbp), %xmm0
movsd %xmm0, -0x280(%rbp)
movsd -0x280(%rbp), %xmm1
movsd -0x260(%rbp), %xmm0
subsd %xmm0, %xmm1
movsd 0x28(%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rax)
movq -0x258(%rbp), %rdi
movl -0x22c(%rbp), %eax
leaq (%rdi,%rax,4), %rsi
callq 0x14c70
jmp 0x14647
leaq -0x250(%rbp), %rdi
callq 0x11730
movl %eax, -0x284(%rbp)
movl -0x284(%rbp), %eax
cmpl -0x22c(%rbp), %eax
jae 0x146b1
movl -0x284(%rbp), %eax
movq %rax, -0x300(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x11730
movq -0x300(%rbp), %rsi
subq %rax, %rsi
movq -0x258(%rbp), %rax
movl -0x284(%rbp), %ecx
movl (%rax,%rcx,4), %edx
addq %rsi, %rdx
movl %edx, (%rax,%rcx,4)
movl -0x284(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x284(%rbp)
jmp 0x14659
callq 0x263b0
movsd %xmm0, -0x308(%rbp)
jmp 0x146c0
movq -0x290(%rbp), %rdi
movsd -0x308(%rbp), %xmm1
movsd -0x280(%rbp), %xmm0
subsd %xmm0, %xmm1
movsd 0x38(%rdi), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rdi)
movq -0x270(%rbp), %rsi
movq -0x258(%rbp), %rdx
movl -0x22c(%rbp), %ecx
callq 0x14980
jmp 0x14704
leaq -0x250(%rbp), %rdi
callq 0x14ca0
movl $0x0, -0x228(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x30d0
addq $0x320, %rsp            # imm = 0x320
popq %rbp
retq
leaq -0x250(%rbp), %rdi
callq 0x14ca0
leaq -0x218(%rbp), %rdi
callq 0x30d0
movq -0x220(%rbp), %rdi
callq 0x3520
nopw %cs:(%rax,%rax)