// File: /workspace/github_cpp_makefile_star2_O3/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 0x6846
cmpb $0x1, (%r12)
jne 0x6846
testq %r15, %r15
je 0x6858
movl 0x81c(%r13), %eax
cmpl 0x81c(%r12), %eax
jne 0x686d
movq %r13, %rdi
callq 0x68d8
movq %r12, %rdi
callq 0x68d8
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 0x66ad
movl %ebx, 0xc(%rsp)
movl $0x1010, %edi           # imm = 0x1010
callq 0x3260
movq %rax, %rbp
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0xcdd0
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 0x6758
cmpl $-0x2, %ebp
jne 0x676e
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x6922
jmp 0x6830
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x6b30
jmp 0x6830
movq 0x808(%r14), %rax
movl %ebp, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpl %ecx, %eax
ja 0x6830
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 0xa0ff
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 0x6830
movl $0x20, %edx
movq 0x19819(%rip), %rbx     # 0x1fff0
movq %rbx, %rdi
leaq 0x11cb3(%rip), %rsi     # 0x18494
callq 0x32b0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3420
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x12070(%rip), %rsi     # 0x18872
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 0x673b
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 0x19791(%rip), %rbx     # 0x1fff0
leaq 0x11bc4(%rip), %rsi     # 0x1842a
movl $0x2e, %edx
jmp 0x6880
movq 0x1977c(%rip), %rbx     # 0x1fff0
leaq 0x11bde(%rip), %rsi     # 0x18459
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