// File: 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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x458(%rbp)
movb $0x0, (%rax)
movq $0x0, 0x808(%rax)
movq $0x0, 0x810(%rax)
movb $0x0, 0x818(%rax)
movb $0x0, 0x819(%rax)
movq $0x0, 0x828(%rax)
movl $0x0, -0x2c(%rbp)
cmpl $0x100, -0x2c(%rbp)     # imm = 0x100
jae 0x847e
movq -0x458(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq $0x0, 0x8(%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x8457
movq -0x10(%rbp), %rdi
callq 0x6450
testb $0x1, %al
jne 0x848d
jmp 0x849a
movq -0x18(%rbp), %rdi
callq 0x6450
testb $0x1, %al
jne 0x849f
jmp 0x87d1
cmpq $0x0, -0x20(%rbp)
jne 0x84cd
movq 0x2fb43(%rip), %rdi     # 0x37ff0
leaq 0x20fb6(%rip), %rsi     # 0x2946a
callq 0x32a0
movq %rax, %rdi
movq 0x2faed(%rip), %rsi     # 0x37fb0
callq 0x3300
jmp 0x87d1
movq -0x10(%rbp), %rax
movl 0x81c(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x81c(%rcx), %eax
je 0x850a
movq 0x2fb06(%rip), %rdi     # 0x37ff0
leaq 0x20fa8(%rip), %rsi     # 0x29499
callq 0x32a0
movq %rax, %rdi
movq 0x2fab0(%rip), %rsi     # 0x37fb0
callq 0x3300
jmp 0x87d1
movq -0x10(%rbp), %rdi
callq 0x87f0
movq -0x18(%rbp), %rdi
callq 0x87f0
movl $0x0, -0x434(%rbp)
cmpl $0x100, -0x434(%rbp)    # imm = 0x100
jae 0x858a
movq -0x10(%rbp), %rax
movq 0x808(%rax), %rdi
movl -0x434(%rbp), %esi
callq 0xd520
movl %eax, -0x45c(%rbp)
movq -0x18(%rbp), %rax
movq 0x808(%rax), %rdi
movl -0x434(%rbp), %esi
callq 0xd520
movl -0x45c(%rbp), %ecx
addl %eax, %ecx
movl -0x434(%rbp), %eax
movl %ecx, -0x430(%rbp,%rax,4)
movl -0x434(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x434(%rbp)
jmp 0x8526
movl $0x1010, %edi           # imm = 0x1010
callq 0x32c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x468(%rbp)
leaq -0x430(%rbp), %rsi
callq 0x16dc0
jmp 0x85af
movq -0x458(%rbp), %rax
movq -0x468(%rbp), %rcx
movq %rcx, 0x808(%rax)
movq 0x808(%rax), %rdi
callq 0xd440
movq -0x458(%rbp), %rcx
movl %eax, 0x4(%rcx)
movq -0x10(%rbp), %rax
movl 0x81c(%rax), %eax
movl %eax, 0x81c(%rcx)
movq -0x10(%rbp), %rax
movl 0x820(%rax), %eax
movq -0x18(%rbp), %rdx
addl 0x820(%rdx), %eax
movl %eax, 0x820(%rcx)
movl 0x4(%rcx), %eax
addl 0x820(%rcx), %eax
movl %eax, -0x448(%rbp)
movb $0x1, -0x449(%rbp)
movl $0xfffffffe, -0x450(%rbp) # imm = 0xFFFFFFFE
cmpl $0x100, -0x450(%rbp)    # imm = 0x100
jge 0x87bf
cmpl $-0x2, -0x450(%rbp)
jne 0x867e
movq -0x458(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x8880
jmp 0x87a9
movq -0x468(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x440(%rbp)
movl %eax, -0x444(%rbp)
movl $0x1010, %esi           # imm = 0x1010
callq 0x32d0
jmp 0x87da
cmpl $-0x1, -0x450(%rbp)
jne 0x86a4
movq -0x458(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x8c50
jmp 0x87a7
movq -0x458(%rbp), %rax
movq 0x808(%rax), %rdi
movl -0x450(%rbp), %esi
callq 0xd460
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x87a5
movq -0x10(%rbp), %rax
movslq -0x450(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rax
movslq -0x450(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rsi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x18(%rbp), %rax
addl 0x820(%rax), %ecx
movl -0x448(%rbp), %r8d
movl -0x24(%rbp), %r9d
callq 0xd550
movq %rax, %rdx
movq -0x458(%rbp), %rax
movslq -0x450(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x10(%rbp), %rcx
movslq -0x450(%rbp), %rdx
movq $0x0, 0x8(%rcx,%rdx,8)
movq -0x18(%rbp), %rcx
movslq -0x450(%rbp), %rdx
movq $0x0, 0x8(%rcx,%rdx,8)
movslq -0x450(%rbp), %rcx
cmpq $0x0, 0x8(%rax,%rcx,8)
jne 0x87a3
movq 0x2f88c(%rip), %rdi     # 0x37ff0
leaq 0x20d69(%rip), %rsi     # 0x294d4
callq 0x32a0
movq %rax, %rdi
movl -0x450(%rbp), %esi
callq 0x3510
movq %rax, %rdi
leaq 0x2113c(%rip), %rsi     # 0x298c4
callq 0x32a0
movq %rax, %rdi
movq 0x2f819(%rip), %rsi     # 0x37fb0
callq 0x3300
movb $0x0, -0x449(%rbp)
jmp 0x87a5
jmp 0x87a7
jmp 0x87a9
jmp 0x87ab
movl -0x450(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x450(%rbp)
jmp 0x8624
movq -0x458(%rbp), %rax
movb -0x449(%rbp), %cl
andb $0x1, %cl
movb %cl, (%rax)
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
movq -0x440(%rbp), %rdi
callq 0x3530
nopw %cs:(%rax,%rax)