// File: 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
movq %rsp, %rbp
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2f8(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movl $0x0, 0x14(%rax)
movq $0x0, 0x18(%rax)
movl $0x0, 0x20(%rax)
movq $0x0, 0x28(%rax)
movl $0x0, 0x30(%rax)
movq $0x0, 0x38(%rax)
movl $0x0, 0x44(%rax)
movl $0x0, 0x48(%rax)
movq $0x0, 0x50(%rax)
movb $0x0, 0x58(%rax)
leaq -0x48(%rbp), %rdi
callq 0x152a0
movq -0x18(%rbp), %rsi
leaq 0x2d8ad(%rip), %rdx     # 0x51a30
leaq -0x68(%rbp), %rdi
callq 0x15200
jmp 0x2418e
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x34ff0
jmp 0x2419d
leaq -0x68(%rbp), %rdi
callq 0x4688
leaq 0x2d9cb(%rip), %rsi     # 0x51b78
leaq -0x48(%rbp), %rdi
callq 0x34c40
movl %eax, -0x2fc(%rbp)
jmp 0x241be
movq -0x2f8(%rbp), %rax
movl -0x2fc(%rbp), %ecx
testl %ecx, %ecx
setne 0x34(%rax)
leaq 0x2d9c8(%rip), %rsi     # 0x51ba0
leaq -0x48(%rbp), %rdi
callq 0x34c40
movl %eax, -0x300(%rbp)
jmp 0x241e9
movq -0x2f8(%rbp), %rax
movl -0x300(%rbp), %ecx
testl %ecx, %ecx
setne 0x35(%rax)
leaq 0x2d9ed(%rip), %rsi     # 0x51bf0
leaq -0x48(%rbp), %rdi
callq 0x34c40
movl %eax, -0x304(%rbp)
jmp 0x24214
movq -0x2f8(%rbp), %rax
movl -0x304(%rbp), %ecx
movl %ecx, 0x40(%rax)
leaq 0x2d99d(%rip), %rsi     # 0x51bc8
leaq -0x48(%rbp), %rdi
callq 0x34c40
movl %eax, -0x308(%rbp)
jmp 0x2423c
movl -0x308(%rbp), %eax
cmpl $0x0, %eax
jbe 0x242b1
leaq 0x2d97a(%rip), %rsi     # 0x51bc8
leaq -0x48(%rbp), %rdi
callq 0x34c40
movl %eax, -0x30c(%rbp)
jmp 0x2425f
movq -0x2f8(%rbp), %rax
movl -0x30c(%rbp), %ecx
movl %ecx, %ecx
cvtsi2sd %rcx, %xmm1
movsd 0x17d95(%rip), %xmm0    # 0x3c010
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rax)
jmp 0x242b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x24a99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4688
jmp 0x24a99
movq -0x18(%rbp), %rsi
leaq 0x2d754(%rip), %rdx     # 0x51a10
leaq -0x98(%rbp), %rdi
callq 0x15200
jmp 0x242ca
leaq -0x98(%rbp), %rdi
callq 0x4150
movq %rax, %rsi
leaq -0x2a0(%rbp), %rdi
movl $0x4, %edx
callq 0x45f0
jmp 0x242ec
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x2a0(%rbp,%rax), %rdi
callq 0x4180
movb %al, -0x30d(%rbp)
jmp 0x2430c
movb -0x30d(%rbp), %al
testb $0x1, %al
jne 0x24318
jmp 0x2437a
movq 0x2ccd1(%rip), %rdi     # 0x50ff0
leaq 0x18a92(%rip), %rsi     # 0x3cdb8
callq 0x4300
movq %rax, -0x318(%rbp)
jmp 0x24334
movq -0x318(%rbp), %rdi
movq 0x2cc6e(%rip), %rsi     # 0x50fb0
callq 0x4370
jmp 0x24349
movl $0x1, -0x2a4(%rbp)
jmp 0x24a57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x24a8d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x24a81
movl $0x0, -0x2a8(%rbp)
leaq 0x2d74d(%rip), %rsi     # 0x51ad8
leaq -0x48(%rbp), %rdi
callq 0x34c40
movl %eax, -0x31c(%rbp)
jmp 0x2439c
movl -0x31c(%rbp), %eax
movl %eax, -0x2ac(%rbp)
leaq -0x2a0(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
movl $0x4, %edx
callq 0x4430
jmp 0x243c2
leaq -0x2a0(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
movl $0x4, %edx
callq 0x4430
jmp 0x243dc
movq -0x2f8(%rbp), %rax
testb $0x1, 0x35(%rax)
je 0x243f9
movl -0x2a8(%rbp), %eax
shrl %eax
movl %eax, -0x320(%rbp)
jmp 0x24405
movl -0x2a8(%rbp), %eax
movl %eax, -0x320(%rbp)
movq -0x2f8(%rbp), %rax
movl -0x320(%rbp), %ecx
movl %ecx, 0x10(%rax)
testb $0x1, 0x34(%rax)
je 0x2442a
movq -0x2f8(%rbp), %rax
movl 0x10(%rax), %ecx
shrl %ecx
movl %ecx, 0x10(%rax)
movq -0x2f8(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edi
movq %rdi, -0x338(%rbp)
shlq $0x3, %rdi
movq %rdi, -0x330(%rbp)
callq 0x4030
movq %rax, -0x328(%rbp)
jmp 0x24456
movq -0x338(%rbp), %rax
cmpq $0x0, %rax
je 0x244bc
movq -0x328(%rbp), %rax
movq -0x338(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x348(%rbp)
movq %rax, -0x340(%rbp)
movq -0x340(%rbp), %rdi
movq %rdi, -0x350(%rbp)
callq 0x162a0
jmp 0x2449e
movq -0x348(%rbp), %rcx
movq -0x350(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x340(%rbp)
jne 0x24489
movq -0x2f8(%rbp), %rax
movq -0x328(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, -0x2b0(%rbp)
movq -0x2f8(%rbp), %rcx
movl -0x2b0(%rbp), %eax
cmpl 0x10(%rcx), %eax
jae 0x245ac
movq -0x2f8(%rbp), %rax
movq (%rax), %rdi
callq 0xa410
movl %eax, -0x354(%rbp)
jmp 0x24505
movq -0x2f8(%rbp), %rax
movl -0x354(%rbp), %ecx
movl %ecx, -0x2bc(%rbp)
movq (%rax), %rdi
callq 0xa410
movl %eax, -0x358(%rbp)
jmp 0x24528
movl -0x358(%rbp), %eax
movl %eax, -0x2c0(%rbp)
leaq -0x2b8(%rbp), %rdi
leaq -0x2bc(%rbp), %rsi
leaq -0x2c0(%rbp), %rdx
callq 0x1cf20
jmp 0x24550
movq -0x2f8(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x2b0(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0x2b8(%rbp), %rsi
callq 0xa8a0
movl -0x2b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b0(%rbp)
jmp 0x244d8
movq -0x330(%rbp), %rsi
movq -0x328(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
callq 0x4630
jmp 0x24a81
movq -0x2f8(%rbp), %rax
testb $0x1, 0x34(%rax)
je 0x246da
movq -0x2f8(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edi
movq %rdi, -0x370(%rbp)
shlq $0x3, %rdi
movq %rdi, -0x368(%rbp)
callq 0x4030
movq %rax, -0x360(%rbp)
jmp 0x245e9
movq -0x370(%rbp), %rax
cmpq $0x0, %rax
je 0x2464f
movq -0x360(%rbp), %rax
movq -0x370(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x380(%rbp)
movq %rax, -0x378(%rbp)
movq -0x378(%rbp), %rdi
movq %rdi, -0x388(%rbp)
callq 0x162a0
jmp 0x24631
movq -0x380(%rbp), %rcx
movq -0x388(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x378(%rbp)
jne 0x2461c
movq -0x2f8(%rbp), %rax
movq -0x360(%rbp), %rcx
movq %rcx, 0x18(%rax)
movl $0x0, -0x2c4(%rbp)
movq -0x2f8(%rbp), %rcx
movl -0x2c4(%rbp), %eax
cmpl 0x10(%rcx), %eax
jae 0x246d8
movq -0x2f8(%rbp), %rax
movl -0x2c4(%rbp), %ecx
movq 0x8(%rax), %rdx
movq 0x18(%rax), %rax
leaq (%rdx,%rcx,8), %rsi
leaq (%rax,%rcx,8), %rdi
callq 0xcad0
jmp 0x246a1
jmp 0x246a3
movl -0x2c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c4(%rbp)
jmp 0x2466b
movq -0x368(%rbp), %rsi
movq -0x360(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
callq 0x4630
jmp 0x24a81
jmp 0x246da
leaq -0x2cc(%rbp), %rdi
callq 0x162a0
jmp 0x246e8
movq -0x2f8(%rbp), %rax
movq $0x0, -0x2d8(%rbp)
movl $0x0, -0x2dc(%rbp)
testb $0x1, 0x35(%rax)
je 0x247d2
movl -0x2a8(%rbp), %eax
shrl %eax
movl %eax, %edi
movq %rdi, -0x3a0(%rbp)
shlq $0x3, %rdi
movq %rdi, -0x398(%rbp)
callq 0x4030
movq %rax, -0x390(%rbp)
jmp 0x24738
movq -0x3a0(%rbp), %rax
cmpq $0x0, %rax
je 0x2479e
movq -0x390(%rbp), %rax
movq -0x3a0(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x3b0(%rbp)
movq %rax, -0x3a8(%rbp)
movq -0x3a8(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
callq 0x162a0
jmp 0x24780
movq -0x3b0(%rbp), %rcx
movq -0x3b8(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x3a8(%rbp)
jne 0x2476b
movq -0x390(%rbp), %rax
movq %rax, -0x2d8(%rbp)
jmp 0x247d2
movq -0x398(%rbp), %rsi
movq -0x390(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
callq 0x4630
jmp 0x24a81
movl $0x0, -0x2e0(%rbp)
movl -0x2e0(%rbp), %eax
cmpl -0x2a8(%rbp), %eax
jae 0x248b9
leaq -0x2a0(%rbp), %rdi
leaq -0x2cc(%rbp), %rsi
movl $0x8, %edx
callq 0x4430
jmp 0x24808
movq -0x2f8(%rbp), %rax
testb $0x1, 0x35(%rax)
je 0x24857
movl -0x2c8(%rbp), %eax
movl -0x2ac(%rbp), %ecx
shll %ecx
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
jne 0x24857
movq -0x2d8(%rbp), %rax
movl -0x2dc(%rbp), %ecx
movl %ecx, %edx
incl %edx
movl %edx, -0x2dc(%rbp)
leaq (%rax,%rcx,8), %rdi
leaq -0x2cc(%rbp), %rsi
callq 0xcad0
jmp 0x24855
jmp 0x2488a
leaq -0x2c8(%rbp), %rdx
leaq -0x2e8(%rbp), %rdi
leaq -0x2cc(%rbp), %rsi
callq 0x15cd0
jmp 0x24873
movq -0x2f8(%rbp), %rdi
movq -0x2e8(%rbp), %rsi
callq 0x24ab0
jmp 0x24888
jmp 0x2488a
cmpl $0x0, -0x2c8(%rbp)
jne 0x248a3
movq -0x2f8(%rbp), %rax
movl -0x2cc(%rbp), %ecx
movl %ecx, 0x30(%rax)
jmp 0x248a5
movl -0x2e0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2e0(%rbp)
jmp 0x247dc
movq -0x2f8(%rbp), %rax
testb $0x1, 0x35(%rax)
je 0x2498c
movl $0x30, %edi
callq 0x4320
movq %rax, -0x3c0(%rbp)
jmp 0x248dd
movq -0x2f8(%rbp), %rax
movq -0x2d8(%rbp), %rcx
movq %rcx, -0x3d0(%rbp)
movq (%rax), %rdi
callq 0xa410
movl %eax, -0x3c4(%rbp)
jmp 0x24902
movl -0x3c4(%rbp), %edx
movq -0x3d0(%rbp), %rsi
movq -0x3c0(%rbp), %rdi
movl -0x2ac(%rbp), %ecx
addl %ecx, %ecx
movl $0x1, %r8d
callq 0x1bd10
jmp 0x2492b
movq -0x2f8(%rbp), %rax
movq -0x3c0(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x2d8(%rbp), %rax
movq %rax, -0x3d8(%rbp)
cmpq $0x0, %rax
je 0x2495d
movq -0x3d8(%rbp), %rdi
callq 0x4450
movq $0x0, -0x2d8(%rbp)
jmp 0x2498c
movq -0x3c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x30, %esi
callq 0x4330
jmp 0x24a81
movq -0x2f8(%rbp), %rax
movsd 0x38(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x24a34
movq -0x2f8(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x4030
movq %rax, -0x3e0(%rbp)
jmp 0x249c3
movq -0x2f8(%rbp), %rax
movq -0x3e0(%rbp), %rcx
movq %rcx, 0x50(%rax)
movl $0x0, -0x2ec(%rbp)
movq -0x2f8(%rbp), %rcx
movl -0x2ec(%rbp), %eax
cmpl 0x40(%rcx), %eax
jae 0x24a1e
movq -0x2f8(%rbp), %rax
movl -0x2ac(%rbp), %edx
shrl %edx
movq 0x50(%rax), %rax
movl -0x2ec(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x2ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ec(%rbp)
jmp 0x249df
movq -0x2f8(%rbp), %rax
movl 0x40(%rax), %ecx
imull -0x2ac(%rbp), %ecx
shrl %ecx
movl %ecx, 0x48(%rax)
leaq -0x2a0(%rbp), %rdi
callq 0x40c0
jmp 0x24a42
movq -0x2f8(%rbp), %rax
movb $0x1, 0x58(%rax)
movl $0x0, -0x2a4(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x40f0
leaq -0x98(%rbp), %rdi
callq 0x4688
leaq -0x48(%rbp), %rdi
callq 0x15360
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
leaq -0x2a0(%rbp), %rdi
callq 0x40f0
leaq -0x98(%rbp), %rdi
callq 0x4688
leaq -0x48(%rbp), %rdi
callq 0x15360
movq -0x70(%rbp), %rdi
callq 0x45c0
nopl (%rax,%rax)