// File: /workspace/github_cpp_makefile_star2_O1/adamnovak[P]rlcsa/adaptive_samples.cpp
// Function: CSA::AdaptiveSamples::AdaptiveSamples(CSA::RLCSA const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
AdaptiveSamples::AdaptiveSamples(const RLCSA& rlcsa, const std::string& base_name) :
  index(rlcsa),
  samples(0), size(0), items(0),
  candidate_samples(0), c_items(0),
  regular_samples(0),
  text_start(0),
  promote_probability(0.0),
  pos(0), sum(0), window(0),
  ok(false)
{
  Parameters parameters;
  parameters.read(base_name + PARAMETERS_EXTENSION);
  this->use_candidates = parameters.get(CANDIDATE_SAMPLES);
  this->half_greedy = parameters.get(HALF_GREEDY_SAMPLES);
  this->window_size = parameters.get(SAMPLE_WINDOW_SIZE);

  if(parameters.get(SAMPLE_PROMOTE_RATE) > 0)
  {
    this->promote_probability = 1.0 / parameters.get(SAMPLE_PROMOTE_RATE);
  }

  std::string file_name = base_name + SA_SAMPLES_EXTENSION;
  std::ifstream sample_file(file_name.c_str(), std::ios_base::binary);
  if(!sample_file)
  {
    std::cerr << "AdaptiveSamples: Cannot open sample file!" << std::endl;
    return;
  }

  // Determine the number of adaptive, candidate, and regular samples.
  usint total_samples = 0;
  usint sample_rate = parameters.get(SAMPLE_RATE);
  sample_file.read((char*)&(total_samples), sizeof(total_samples)); // Actually text size.
  sample_file.read((char*)&(total_samples), sizeof(total_samples));
  this->size = (this->half_greedy ? total_samples / 2 : total_samples);
  if(this->use_candidates) { this->size /= 2; }

  // Initialize the samples.
  this->samples = new sample_type[this->size];
  for(key_type i = 0; i < this->size; i++)
  {
    this->samples[i] = sample_type(this->index.getSize(), this->index.getSize());
  }
  if(this->use_candidates)
  {
    this->candidate_samples = new sample_type[this->size];
    for(key_type i = 0; i < this->size; i++) { this->candidate_samples[i] = this->samples[i]; }
  }

  // Insert the initial samples.
  pair_type buffer;
  pair_type* regulars = 0;
  usint reg_samples = 0;
  if(this->half_greedy) { regulars = new pair_type[total_samples / 2]; }
  for(usint i = 0; i < total_samples; i++)
  {
    sample_file.read((char*)&buffer, sizeof(pair_type));
    if(this->half_greedy && buffer.second % (2 * sample_rate) == 0)
    {
      regulars[reg_samples++] = buffer;
    }
    else
    {
      this->addSample(sample_type(buffer.first, buffer.second));
    }
    if(buffer.second == 0) { this->text_start = buffer.first; }
  }
  if(this->half_greedy)
  {
    this->regular_samples = new SASamples(regulars, this->index.getSize(), 2 * sample_rate, 1);
    delete[] regulars; regulars = 0;
  }

  // Initialize the window.
  if(this->promote_probability > 0.0)
  {
    this->window = new key_type[this->window_size];
    for(usint i = 0; i < this->window_size; i++) { this->window[i] = sample_rate / 2; }
    this->sum = this->window_size * sample_rate / 2;
  }

  sample_file.close();
  this->ok = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, (%rdi)
xorl %eax, %eax
movq %rax, 0x28(%rdi)
movl %eax, 0x30(%rdi)
movq %rax, 0x38(%rdi)
movq %rax, 0x44(%rdi)
movq %rax, 0x50(%rdi)
movb $0x0, 0x58(%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rdi)
movupd %xmm0, 0x14(%rdi)
leaq 0x38(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x19dc9(%rip), %rdx     # 0x2d978
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0xdf8b
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1f272
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13bf2
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x19ec7(%rip), %rsi     # 0x2dac0
leaq 0x30(%rsp), %rdi
callq 0x1ef2e
testl %eax, %eax
setne 0x34(%rbx)
leaq 0x19ed8(%rip), %rsi     # 0x2dae8
leaq 0x30(%rsp), %rdi
callq 0x1ef2e
testl %eax, %eax
setne 0x35(%rbx)
leaq 0x19f11(%rip), %rsi     # 0x2db38
leaq 0x30(%rsp), %rdi
callq 0x1ef2e
movl %eax, 0x40(%rbx)
leaq 0x19ed5(%rip), %rsi     # 0x2db10
leaq 0x30(%rsp), %rdi
callq 0x1ef2e
testl %eax, %eax
je 0x13c75
leaq 0x19ec0(%rip), %rsi     # 0x2db10
leaq 0x30(%rsp), %rdi
callq 0x1ef2e
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0xe3cc(%rip), %xmm1     # 0x22038
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rbx)
leaq 0x19cdc(%rip), %rdx     # 0x2d958
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0xdf8b
movq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdi
movl $0x4, %edx
callq 0x34d0
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xa0(%rsp,%rax)
je 0x13d01
movq 0x19333(%rip), %rbx     # 0x2cff0
leaq 0xf06c(%rip), %rsi      # 0x22d30
movl $0x29, %edx
movq %rbx, %rdi
callq 0x32e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq 0x19301(%rip), %rdi     # 0x2cff0
callq 0x3040
movq %rax, %rdi
callq 0x31b0
jmp 0x13f7a
movl $0x0, 0xc(%rsp)
leaq 0x19d10(%rip), %rsi     # 0x2da20
leaq 0x30(%rsp), %rdi
callq 0x1ef2e
movq %rax, 0x20(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x3310
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x3310
movb 0x35(%rbx), %cl
movl 0xc(%rsp), %ebp
movl %ebp, %eax
movb %cl, 0x10(%rsp)
shrl %cl, %eax
movb 0x34(%rbx), %r13b
movl %eax, 0x10(%rbx)
cmpb $0x1, %r13b
jne 0x13d6e
shrl %eax
movl %eax, 0x10(%rbx)
movl 0x10(%rbx), %r14d
leaq (,%r14,8), %r12
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
movq %rbp, 0x28(%rsp)
leaq 0x8(%rbx), %rbp
testq %r14, %r14
je 0x13da0
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
movq %r15, (%rbp)
testq %r14, %r14
je 0x13dc2
movq (%rbx), %rax
movl 0x4(%rax), %eax
xorl %ecx, %ecx
movl %eax, (%r15,%rcx,8)
movl %eax, 0x4(%r15,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x13db1
testb %r13b, %r13b
movq 0x28(%rsp), %rbp
je 0x13e0f
movq %r12, %rdi
callq 0x3030
movq %rax, %r13
testq %r14, %r14
je 0x13de9
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
movq %r13, 0x18(%rbx)
testq %r14, %r14
je 0x13e0f
xorl %eax, %eax
movl (%r15,%rax,8), %ecx
movl %ecx, (%r13,%rax,8)
movl 0x4(%r15,%rax,8), %ecx
movl %ecx, 0x4(%r13,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x13df4
movq $0x0, 0x18(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0x13e4c
movl %ebp, %r12d
shrl %r12d
shlq $0x3, %r12
movq %r12, %rdi
callq 0x3030
movq %rax, 0x10(%rsp)
cmpl $0x2, %ebp
jb 0x13e55
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
jmp 0x13e55
movq $0x0, 0x10(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0x13eda
movq 0x20(%rsp), %rax
leal (%rax,%rax), %ebp
xorl %r14d, %r14d
leaq 0x80(%rsp), %r12
leaq 0x18(%rsp), %r13
xorl %r15d, %r15d
movl $0x8, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x3310
cmpb $0x1, 0x35(%rbx)
jne 0x13e9b
movl 0x1c(%rsp), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
testl %edx, %edx
je 0x13ec2
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x14040
cmpl $0x0, 0x1c(%rsp)
jne 0x13eb6
movl 0x18(%rsp), %eax
movl %eax, 0x30(%rbx)
incl %r14d
cmpl 0xc(%rsp), %r14d
jb 0x13e77
jmp 0x13eda
movl %r15d, %eax
incl %r15d
movl 0x18(%rsp), %edx
movq 0x10(%rsp), %rsi
movl %edx, (%rsi,%rax,8)
movl %ecx, 0x4(%rsi,%rax,8)
jmp 0x13ea8
cmpb $0x0, 0x35(%rbx)
je 0x13f24
movl $0x30, %edi
callq 0x3290
movq %rax, %r12
movq (%rbx), %rax
movl 0x4(%rax), %edx
movq 0x20(%rsp), %rax
leal (%rax,%rax), %ecx
movq %r12, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movl $0x1, %r8d
callq 0xf806
movq %r12, 0x28(%rbx)
testq %r14, %r14
je 0x13f24
movq 0x10(%rsp), %rdi
callq 0x3330
movsd 0x38(%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x13f69
movl 0x40(%rbx), %edi
shlq $0x2, %rdi
callq 0x3030
movq %rax, 0x50(%rbx)
movl 0x40(%rbx), %ecx
testq %rcx, %rcx
je 0x13f5f
movq 0x20(%rsp), %rdx
shrl %edx
xorl %esi, %esi
movl %edx, (%rax,%rsi,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x13f54
imull 0x20(%rsp), %ecx
shrl %ecx
movl %ecx, 0x48(%rbx)
leaq 0x80(%rsp), %rdi
callq 0x3080
movb $0x1, 0x58(%rbx)
leaq 0x80(%rsp), %rdi
callq 0x3090
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13fa2
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x30(%rsp), %rdi
callq 0xe384
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x30, %esi
movq %r12, %rdi
callq 0x32a0
jmp 0x14005
jmp 0x14002
movq %rax, %rbx
jmp 0x14012
jmp 0x13ffd
movq %rax, %rbx
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1402d
movq 0x90(%rsp), %rsi
jmp 0x14025
jmp 0x13ffd
jmp 0x14002
jmp 0x14002
movq %rax, %rbx
jmp 0x1402d
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x3090
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1402d
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x30(%rsp), %rdi
callq 0xe384
movq %rbx, %rdi
callq 0x34b0
nop