// File: /workspace/github_cpp_makefile_star2_O3/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 %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 0x28(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x19bd5(%rip), %rdx     # 0x2d978
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0xe019
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1f79a
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13de0
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x19cd9(%rip), %rsi     # 0x2dac0
leaq 0x20(%rsp), %rdi
callq 0x1f46a
testl %eax, %eax
setne 0x34(%rbx)
leaq 0x19cea(%rip), %rsi     # 0x2dae8
leaq 0x20(%rsp), %rdi
callq 0x1f46a
testl %eax, %eax
setne 0x35(%rbx)
leaq 0x19d23(%rip), %rsi     # 0x2db38
leaq 0x20(%rsp), %rdi
callq 0x1f46a
movl %eax, 0x40(%rbx)
leaq 0x19ce7(%rip), %rsi     # 0x2db10
leaq 0x20(%rsp), %rdi
callq 0x1f46a
testl %eax, %eax
je 0x13e63
leaq 0x19cd2(%rip), %rsi     # 0x2db10
leaq 0x20(%rsp), %rdi
callq 0x1f46a
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0xe1de(%rip), %xmm1     # 0x22038
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rbx)
leaq 0x19aee(%rip), %rdx     # 0x2d958
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0xe019
movq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdi
movl $0x4, %edx
callq 0x34d0
movq 0x70(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x90(%rsp,%rax)
je 0x13ee9
movq 0x1914b(%rip), %rbx     # 0x2cff0
leaq 0xeecc(%rip), %rsi      # 0x22d78
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 0x19119(%rip), %rdi     # 0x2cff0
callq 0x3040
movq %rax, %rdi
callq 0x31b0
jmp 0x14244
movl $0x0, 0x4(%rsp)
leaq 0x19b28(%rip), %rsi     # 0x2da20
leaq 0x20(%rsp), %rdi
callq 0x1f46a
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
callq 0x3310
leaq 0x70(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
callq 0x3310
movb 0x35(%rbx), %cl
movl 0x4(%rsp), %ebp
movl %ebp, %eax
shrl %cl, %eax
movb 0x34(%rbx), %r13b
movl %eax, 0x10(%rbx)
cmpb $0x1, %r13b
jne 0x13f4c
shrl %eax
movl %eax, 0x10(%rbx)
movb %cl, 0x3(%rsp)
movl %eax, %r14d
leaq (,%r14,8), %r12
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
movq %rbp, 0x10(%rsp)
testq %r14, %r14
je 0x13f9c
xorl %ebp, %ebp
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
movq %r15, 0x8(%rbx)
movq (%rbx), %rax
movl 0x4(%rax), %eax
movl %eax, (%r15,%rbp,8)
movl %eax, 0x4(%r15,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x13f89
jmp 0x13fa3
leaq 0x8(%rbx), %rax
movq %r15, (%rax)
testb %r13b, %r13b
je 0x13fe2
movq %r12, %rdi
callq 0x3030
movq %rax, %r13
testl %r14d, %r14d
je 0x13fde
xorl %ebp, %ebp
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
movq %r13, 0x18(%rbx)
movq (%r15,%rbp,8), %rax
movq %rax, (%r13,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x13fcb
jmp 0x13fe2
movq %r13, 0x18(%rbx)
movq $0x0, 0x8(%rsp)
movb 0x3(%rsp), %bpl
testb %bpl, %bpl
je 0x14026
movq 0x10(%rsp), %r15
leaq (,%r15,4), %r12
andq $-0x8, %r12
movq %r12, %rdi
callq 0x3030
movq %rax, %r14
cmpl $0x2, %r15d
jb 0x14029
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
jmp 0x14035
xorl %r14d, %r14d
cmpq $0x0, 0x10(%rsp)
je 0x140bf
movq %r14, 0x10(%rsp)
movq 0x18(%rsp), %rax
leal (%rax,%rax), %ebp
xorl %r14d, %r14d
leaq 0x70(%rsp), %r12
leaq 0x8(%rsp), %r13
xorl %r15d, %r15d
movl $0x8, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x3310
cmpb $0x1, 0x35(%rbx)
jne 0x14076
movl 0xc(%rsp), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
testl %edx, %edx
je 0x1409e
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x14304
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
jne 0x14092
movl 0x8(%rsp), %eax
movl %eax, 0x30(%rbx)
incl %r14d
cmpl 0x4(%rsp), %r14d
jb 0x14052
jmp 0x140b6
movl %r15d, %eax
incl %r15d
movl 0x8(%rsp), %edx
movq 0x10(%rsp), %rsi
movl %edx, (%rsi,%rax,8)
movl %ecx, 0x4(%rsi,%rax,8)
jmp 0x14087
movb 0x35(%rbx), %bpl
movq 0x10(%rsp), %r14
testb $0x1, %bpl
je 0x14102
movl $0x30, %edi
callq 0x3290
movq %rax, %r12
movq (%rbx), %rax
movl 0x4(%rax), %edx
movq 0x18(%rsp), %rax
leal (%rax,%rax), %ecx
movq %r12, %rdi
movq %r14, %rsi
movl $0x1, %r8d
callq 0xf868
movq %r12, 0x28(%rbx)
testq %r14, %r14
je 0x14102
movq %r14, %rdi
callq 0x3330
movsd 0x38(%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x14236
movl 0x40(%rbx), %r14d
leaq (,%r14,4), %rdi
callq 0x3030
movq %rax, 0x50(%rbx)
testq %r14, %r14
je 0x14229
movq 0x18(%rsp), %rcx
shrl %ecx
leaq -0x1(%r14), %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq 0xc(,%r14,4), %rdx
andq $-0x10, %rdx
movdqa 0xe694(%rip), %xmm1     # 0x227f0
movdqa 0xe69c(%rip), %xmm2     # 0x22800
xorl %esi, %esi
movdqa 0xe6a2(%rip), %xmm3     # 0x22810
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0xe6a2(%rip), %xmm5     # 0x22820
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edi
notl %edi
testb $0x1, %dil
je 0x141b6
movl %ecx, (%rax,%rsi)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x141c9
movl %ecx, 0x4(%rax,%rsi)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edi
testb $0x1, %dil
je 0x14205
movl %ecx, 0x8(%rax,%rsi)
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x14214
movl %ecx, 0xc(%rax,%rsi)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x1417e
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 0x3090
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14269
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x20(%rsp), %rdi
callq 0xe41e
addq $0x278, %rsp            # imm = 0x278
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 0x142cc
jmp 0x142c9
movq %rax, %rbx
jmp 0x142d6
jmp 0x142c4
movq %rax, %rbx
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x142f1
movq 0x80(%rsp), %rsi
jmp 0x142e9
jmp 0x142c4
jmp 0x142c9
jmp 0x142c9
movq %rax, %rbx
jmp 0x142f1
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3090
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x142f1
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x20(%rsp), %rdi
callq 0xe41e
movq %rbx, %rdi
callq 0x34b0
nop