// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %r14
movb $0x0, (%rdi)
andq $0x0, 0x828(%rdi)
movq %rsi, %r13
leaq 0x808(%rdi), %rbp
xorps %xmm0, %xmm0
movups %xmm0, 0x808(%rdi)
andw $0x0, 0x818(%rdi)
pushq $0x1
popq %rax
cmpq $0x101, %rax            # imm = 0x101
je 0x58d6
andq $0x0, (%r14,%rax,8)
incq %rax
jmp 0x58c4
cmpb $0x0, (%r13)
je 0x5ad5
cmpb $0x1, (%r12)
jne 0x5ad5
testq %r15, %r15
je 0x5956
movl 0x81c(%r13), %eax
cmpl 0x81c(%r12), %eax
jne 0x5966
movl %r8d, 0xc(%rsp)
movq %r13, %rdi
callq 0x5b00
movq %r12, %rdi
callq 0x5b00
movq 0x808(%r13), %rax
movq 0x808(%r12), %rcx
xorl %edx, %edx
cmpq $0x100, %rdx            # imm = 0x100
je 0x5992
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
jmp 0x5928
movq 0x17693(%rip), %rdi     # 0x1cff0
leaq 0xda7e(%rip), %rsi      # 0x133e2
jmp 0x5974
movq 0x17683(%rip), %rdi     # 0x1cff0
leaq 0xda9d(%rip), %rsi      # 0x13411
callq 0x3200
movq %rax, %rdi
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3100
movl $0x1010, %edi           # imm = 0x1010
callq 0x3220
movq %rax, %rbx
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0xb32c
movq %rbx, 0x808(%r14)
movl 0x800(%rbx), %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, %al
movl %eax, 0x4(%rsp)
pushq $-0x2
popq %rbx
cmpq $0x100, %rbx            # imm = 0x100
je 0x5acc
cmpl $-0x1, %ebx
je 0x5a1a
cmpl $-0x2, %ebx
jne 0x5a30
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5b4c
jmp 0x5ac4
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x5d34
jmp 0x5ac4
movq (%rbp), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpl %ecx, %eax
ja 0x5ac4
movq 0x8(%r13,%rbx,8), %rdi
movq 0x8(%r12,%rbx,8), %rsi
movl 0x820(%r12), %ecx
addl 0x4(%r12), %ecx
movq %r15, %rdx
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %r9d
callq 0x8448
movq %rax, 0x8(%r14,%rbx,8)
andq $0x0, 0x8(%r13,%rbx,8)
andq $0x0, 0x8(%r12,%rbx,8)
xorl %eax, %eax
cmpq %rax, 0x8(%r14,%rbx,8)
jne 0x5ac4
movq 0x17561(%rip), %rdi     # 0x1cff0
leaq 0xd9b6(%rip), %rsi      # 0x1344c
callq 0x3200
movq %rax, %rdi
movl %ebx, %esi
callq 0x33e0
movq %rax, %rdi
leaq 0xdd7b(%rip), %rsi      # 0x1382a
callq 0x3200
movq %rax, %rdi
callq 0x3100
movl $0x0, 0x4(%rsp)
incq %rbx
jmp 0x59f0
movl 0x4(%rsp), %eax
andb $0x1, %al
movb %al, (%r14)
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x1010, %esi           # imm = 0x1010
movq %rbx, %rdi
callq 0x3230
movq %r14, %rdi
callq 0x3410
nop