// File: /workspace/github_cpp_makefile_star2_O2/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 $0x278, %rsp            # imm = 0x278
movq %rdi, %rbx
movq %rsi, (%rdi)
andq $0x0, 0x28(%rdi)
andl $0x0, 0x30(%rdi)
andq $0x0, 0x38(%rdi)
andq $0x0, 0x44(%rdi)
movq %rdx, %r14
andq $0x0, 0x50(%rdi)
movb $0x0, 0x58(%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rdi)
movupd %xmm0, 0x14(%rdi)
leaq 0x28(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x1858b(%rip), %rdx     # 0x28960
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0xae80
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x17c6c
leaq 0x70(%rsp), %rdi
callq 0x34e8
leaq 0x186a6(%rip), %rsi     # 0x28aa8
leaq 0x20(%rsp), %rdi
callq 0x17a12
testl %eax, %eax
setne 0x34(%rbx)
leaq 0x186b7(%rip), %rsi     # 0x28ad0
leaq 0x20(%rsp), %rdi
callq 0x17a12
testl %eax, %eax
setne 0x35(%rbx)
leaq 0x186f0(%rip), %rsi     # 0x28b20
leaq 0x20(%rsp), %rdi
callq 0x17a12
movl %eax, 0x40(%rbx)
leaq 0x186b4(%rip), %rsi     # 0x28af8
leaq 0x20(%rsp), %rdi
callq 0x17a12
testl %eax, %eax
je 0x1047b
leaq 0x1869f(%rip), %rsi     # 0x28af8
leaq 0x20(%rsp), %rdi
callq 0x17a12
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd 0x9b9e(%rip), %xmm1     # 0x1a010
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rbx)
leaq 0x184be(%rip), %rdx     # 0x28940
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0xae80
movq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x3490
movq 0x70(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x90(%rsp,%rax)
je 0x104d4
movq 0x17b35(%rip), %rdi     # 0x27ff0
leaq 0xa86e(%rip), %rsi      # 0x1ad30
callq 0x3240
movq %rax, %rdi
callq 0x3110
jmp 0x1071c
andl $0x0, 0x4(%rsp)
leaq 0x18528(%rip), %rsi     # 0x28a08
leaq 0x20(%rsp), %rdi
callq 0x17a12
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x32d0
leaq 0x70(%rsp), %rdi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x32d0
movb 0x35(%rbx), %cl
movl 0x4(%rsp), %ebp
movl %ebp, %eax
movb %cl, (%rsp)
shrl %cl, %eax
movb 0x34(%rbx), %r12b
movl %eax, 0x10(%rbx)
cmpb $0x1, %r12b
jne 0x10533
shrl %eax
movl %eax, 0x10(%rbx)
movl %eax, %r13d
leaq (,%r13,8), %r14
movq %r14, %rdi
callq 0x3030
movq %rax, %r15
testq %r13, %r13
je 0x1055b
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3150
movq %r15, 0x8(%rbx)
movq (%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x1057a
movl 0x4(%rax), %edx
movl %edx, (%r15,%rcx,8)
movl %edx, 0x4(%r15,%rcx,8)
incq %rcx
jmp 0x10564
testb %r12b, %r12b
je 0x105b4
movq %r14, %rdi
callq 0x3030
movq %rax, %r12
testl %r13d, %r13d
je 0x1059c
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3150
movq %r12, 0x18(%rbx)
xorl %eax, %eax
cmpq %rax, %r13
je 0x105b4
movq (%r15,%rax,8), %rcx
movq %rcx, (%r12,%rax,8)
incq %rax
jmp 0x105a2
andq $0x0, 0x8(%rsp)
cmpb $0x0, (%rsp)
je 0x105e7
leaq (,%rbp,4), %r14
andq $-0x8, %r14
movq %r14, %rdi
callq 0x3030
movq %rax, %r12
cmpl $0x2, %ebp
jae 0x105f5
movq $0x0, 0x10(%rsp)
jmp 0x1060b
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
jmp 0x1060b
movq $0x0, 0x10(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3150
movq 0x18(%rsp), %rax
addl %eax, %eax
movl %eax, (%rsp)
leaq 0x8(%rsp), %r13
pushq $0x8
popq %r14
xorl %r15d, %r15d
cmpl %ebp, %r15d
jae 0x1068d
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x32d0
cmpb $0x1, 0x35(%rbx)
jne 0x1064b
movl 0xc(%rsp), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl (%rsp)
testl %edx, %edx
je 0x10670
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x107a4
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
jne 0x10667
movl 0x8(%rsp), %eax
movl %eax, 0x30(%rbx)
incl %r15d
movl 0x4(%rsp), %ebp
jmp 0x10621
movq 0x10(%rsp), %rdx
movl %edx, %eax
incl %edx
movq %rdx, 0x10(%rsp)
movl 0x8(%rsp), %edx
movl %edx, (%r12,%rax,8)
movl %ecx, 0x4(%r12,%rax,8)
jmp 0x1065c
cmpb $0x0, 0x35(%rbx)
je 0x106c7
pushq $0x30
popq %rdi
callq 0x3260
movq %rax, %r14
movq (%rbx), %rax
movl 0x4(%rax), %edx
pushq $0x1
popq %r8
movq %r14, %rdi
movq %r12, %rsi
movl (%rsp), %ecx
callq 0xccb2
movq %r14, 0x28(%rbx)
testq %r12, %r12
je 0x106c7
movq %r12, %rdi
callq 0x32f0
movsd 0x38(%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1070e
movl 0x40(%rbx), %r14d
leaq (,%r14,4), %rdi
callq 0x3030
movq %rax, 0x50(%rbx)
movq 0x18(%rsp), %rcx
shrl %ecx
xorl %edx, %edx
cmpq %rdx, %r14
je 0x10701
movl %ecx, (%rax,%rdx,4)
incq %rdx
jmp 0x106f4
imull 0x18(%rsp), %r14d
shrl %r14d
movl %r14d, 0x48(%rbx)
leaq 0x70(%rsp), %rdi
callq 0x3080
movb $0x1, 0x58(%rbx)
leaq 0x70(%rsp), %rdi
callq 0x30a0
leaq 0x50(%rsp), %rdi
callq 0x34e8
leaq 0x20(%rsp), %rdi
callq 0xb612
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r14, %rdi
callq 0x3270
jmp 0x1077d
jmp 0x1077a
movq %rax, %rbx
jmp 0x10787
jmp 0x10775
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x1078c
jmp 0x10775
jmp 0x1077a
jmp 0x1077a
movq %rax, %rbx
jmp 0x10791
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x30a0
leaq 0x50(%rsp), %rdi
callq 0x34e8
leaq 0x20(%rsp), %rdi
callq 0xb612
movq %rbx, %rdi
callq 0x3470
nop