// File: /workspace/github_cpp_makefile_star2_O1/adamnovak[P]rlcsa/rlcsa.cpp
// Function: CSA::RLCSA::RLCSA(CSA::RLCSA&, CSA::RLCSA&, unsigned int*, unsigned int, unsigned int)
RLCSA::RLCSA(RLCSA& index, RLCSA& increment, usint* positions, usint block_size, usint threads) :
  ok(false),
  alphabet(0),
  sa_samples(0), support_locate(false), support_display(false),
  end_points(0)
{
  for(usint c = 0; c < CHARS; c++) { this->array[c] = 0; }

  if(!index.isOk() || !increment.isOk())
  {
    return; // Fail silently. Actual error has already been reported.
  }
  if(positions == 0)
  {
    std::cerr << "RLCSA: Positions for insertions not available!" << std::endl;
    return;
  }
  if(index.sample_rate != increment.sample_rate)
  {
    std::cerr << "RLCSA: Cannot combine indexes with different sample rates!" << std::endl;
    return;
  }

  index.strip();
  increment.strip();

  // Build character tables etc.
  usint distribution[CHARS];
  for(usint c = 0; c < CHARS; c++)
  {
    distribution[c] = index.alphabet->countOf(c) + increment.alphabet->countOf(c);
  }
  this->alphabet = new Alphabet(distribution); this->data_size = this->alphabet->getDataSize();
  this->sample_rate = index.sample_rate;
  this->number_of_sequences = index.number_of_sequences + increment.number_of_sequences;


  // Merge end points, SA samples, and Psi.
  usint psi_size = this->data_size + this->number_of_sequences;
  bool should_be_ok = true;

  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  #endif
  #pragma omp parallel for schedule(dynamic, 1)
  for(int c = -2; c < (int)CHARS; c++)
  {
    if(c == -2)      { this->mergeEndPoints(index, increment); }
    else if(c == -1) { this->mergeSamples(index, increment, positions);  }
    else if(this->alphabet->hasChar(c) != 0)
    {
      this->array[c] = mergeVectors<PsiVector, PsiVector::Encoder, PsiVector::Iterator>(index.array[c], increment.array[c], positions, increment.data_size + increment.number_of_sequences, psi_size, block_size);
      index.array[c] = 0;
      increment.array[c] = 0;

      if(this->array[c] == 0)
      {
        std::cerr << "RLCSA: Merge failed for vectors " << c << "!" << std::endl;
        should_be_ok = false;
      }
    }
  }

  this->ok = should_be_ok;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movl %r8d, %ebx
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movb $0x0, (%rdi)
movq $0x0, 0x828(%rdi)
addq $0x8, %rdi
movl $0x812, %edx            # imm = 0x812
xorl %esi, %esi
callq 0x3150
cmpb $0x1, (%r13)
jne 0x68f4
cmpb $0x1, (%r12)
jne 0x68f4
testq %r15, %r15
je 0x6906
movl 0x81c(%r13), %eax
cmpl 0x81c(%r12), %eax
jne 0x691b
movq %r13, %rdi
callq 0x6986
movq %r12, %rdi
callq 0x6986
movq 0x808(%r13), %rax
movq 0x808(%r12), %rcx
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
movq (%rcx,%rdx,8), %rdi
leal (%rdi,%rsi), %r8d
shrq $0x20, %rsi
shrq $0x20, %rdi
subl %r8d, %esi
addl %edi, %esi
addl $0x2, %esi
movl %esi, 0x10(%rsp,%rdx,4)
incq %rdx
cmpq $0x100, %rdx            # imm = 0x100
jne 0x675b
movl %ebx, 0xc(%rsp)
movl $0x1010, %edi           # imm = 0x1010
callq 0x3260
movq %rax, %rbp
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0xcca8
movq %rbp, 0x808(%r14)
movl 0x800(%rbp), %eax
movl %eax, 0x4(%r14)
movl 0x81c(%r13), %ecx
movl %ecx, 0x81c(%r14)
movl 0x820(%r12), %ecx
addl 0x820(%r13), %ecx
movl %ecx, 0x820(%r14)
addl %eax, %ecx
movl %ecx, 0x8(%rsp)
movb $0x1, %bl
movq $-0x2, %rbp
cmpl $-0x1, %ebp
je 0x6806
cmpl $-0x2, %ebp
jne 0x681c
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x69d0
jmp 0x68de
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x6bde
jmp 0x68de
movq 0x808(%r14), %rax
movl %ebp, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpl %ecx, %eax
ja 0x68de
movq 0x8(%r13,%rbp,8), %rdi
movq 0x8(%r12,%rbp,8), %rsi
movl 0x820(%r12), %ecx
addl 0x4(%r12), %ecx
movq %r15, %rdx
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %r9d
callq 0x9fbd
movq %rax, 0x8(%r14,%rbp,8)
xorl %eax, %eax
movq %rax, 0x8(%r13,%rbp,8)
movq %rax, 0x8(%r12,%rbp,8)
cmpq %rax, 0x8(%r14,%rbp,8)
jne 0x68de
movl $0x20, %edx
movq 0x1876b(%rip), %rbx     # 0x1eff0
movq %rbx, %rdi
leaq 0x10bb8(%rip), %rsi     # 0x17447
callq 0x32b0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3420
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x10f75(%rip), %rsi     # 0x17825
callq 0x32b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3190
xorl %ebx, %ebx
incq %rbp
cmpq $0x100, %rbp            # imm = 0x100
jne 0x67e9
andb $0x1, %bl
movb %bl, (%r14)
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x186e3(%rip), %rbx     # 0x1eff0
leaq 0x10ac9(%rip), %rsi     # 0x173dd
movl $0x2e, %edx
jmp 0x692e
movq 0x186ce(%rip), %rbx     # 0x1eff0
leaq 0x10ae3(%rip), %rsi     # 0x1740c
movl $0x3a, %edx
movq %rbx, %rdi
callq 0x32b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3190
movq %rax, %rbx
movl $0x1010, %esi           # imm = 0x1010
movq %rbp, %rdi
callq 0x3270
movq %rbx, %rdi
callq 0x3460