// File: adamnovak[P]rlcsa/sampler.cpp
// Function: CSA::WeightedSampler::buildSamples(unsigned int, unsigned int, unsigned int)
bool
WeightedSampler::buildSamples(uint sample_rate, sum_type initial_adjustment, usint threads)
{
  if(sample_rate == 0 || this->status != READY) { return false; }

  usint force = 0;
  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  force = this->nodes / (threads * threads);
  #endif

  delete[] this->samples; this->samples = 0;
  this->number_of_samples = (this->size + sample_rate - 1) / sample_rate;

  sum_type average_weight;
  if(this->use_psi)
  {
    average_weight = this->edge_totals[this->nodes - 1] / this->size;
  }
  else
  {
    average_weight = this->edge_totals[0] / this->size;
  }
  average_weight = std::max(average_weight, (sum_type)1);

  // Should be -3 * max_weight, 3 * max_weight, if we allow negative weights.
  sum_type left = 1, right = std::numeric_limits<sum_type>::max();
  if(initial_adjustment > 0) { this->adjustment = initial_adjustment; }
  else { this->adjustment = average_weight; }
  uint shortest = 0, longest = 0;
  uint* shortest_path = 0;
  uint* longest_path = 0;

  // Find adjustment such that the shortest minimum weight path has at most
  // 'number' links, while the longest minimum weight path has at least 'number' links.
  // With adjustment 0, we can always find a path of weight 0: just take as many edges as
  // possible. Hence adjustment 0 will be used only when we can afford to sample every
  // suffix with a positive weight. This is equivalent to greedy sampling.
  std::cout << "left: " << left << ", right: " << right << std::endl;
  while(true)
  {
    std::cout << "Adjustment: " << this->adjustment << std::endl;

    shortest = this->minimumWeightPath(true, force);
    std::cout << "  shortest: " << shortest << " (weight " << this->path_weights[this->nodes - 1] << ")" << std::endl;
    if(shortest > this->number_of_samples)
    {
      left = this->adjustment + 1;
      this->adjustment = std::min(2 * this->adjustment, left + (right - left) / 2);
      continue;
    }
    shortest_path = new uint[shortest + 1];
    for(uint i = shortest + 1, cur = this->nodes - 1; i > 0; i--, cur = this->predecessors[cur])
    {
      shortest_path[i - 1] = cur;
    }

    longest = this->minimumWeightPath(false, force);
    std::cout << "  longest:  " << longest << " (weight " << this->path_weights[this->nodes - 1] << ")" << std::endl;
    if(longest < this->number_of_samples && this->adjustment > 1) // If adjustment will still be >= 1.
    {
      right = this->adjustment - 1;
      this->adjustment = left + (right - left) / 2;
      delete[] shortest_path; shortest_path = 0;
      continue;
    }
    longest_path = new uint[longest + 1];
    for(uint i = longest + 1, cur = this->nodes - 1; i > 0; i--, cur = this->predecessors[cur])
    {
      longest_path[i - 1] = cur;
    }

    break;
  }
  std::cout << std::endl;
  this->cleanUp();

  // Build a 'number'-link path from the saved paths.
  this->number_of_samples = std::min(this->number_of_samples, longest);
  this->buildPath(this->number_of_samples, longest_path, longest, shortest_path, shortest);
  delete[] shortest_path; shortest_path = 0;

  this->samples = new pair_type[this->number_of_samples];
  for(uint i = 0; i < this->number_of_samples; i++)
  {
    samples[i].second = this->node_positions[longest_path[i]];
  }
  delete[] longest_path; longest_path = 0;

  this->status = SAMPLED;
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x17b50
movq -0x78(%rbp), %rax
cmpl $0x1, 0xc(%rax)
je 0x17b59
movb $0x0, -0x1(%rbp)
jmp 0x180fa
movq -0x78(%rbp), %rax
movl $0x0, -0x20(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x17b7b
movq -0x80(%rbp), %rdi
callq 0x33e0
movq -0x78(%rbp), %rax
movq $0x0, 0x10(%rax)
movl 0x8(%rax), %eax
addl -0x14(%rbp), %eax
subl $0x1, %eax
xorl %edx, %edx
divl -0x14(%rbp)
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x18(%rax)
testb $0x1, 0x1c(%rax)
je 0x17bc1
movq -0x78(%rbp), %rcx
movq 0x40(%rcx), %rax
movl 0x48(%rcx), %edx
subl $0x1, %edx
movl %edx, %edx
movl (%rax,%rdx,4), %eax
xorl %edx, %edx
divl 0x8(%rcx)
movl %eax, -0x24(%rbp)
jmp 0x17bd3
movq -0x78(%rbp), %rcx
movq 0x40(%rcx), %rax
movl (%rax), %eax
xorl %edx, %edx
divl 0x8(%rcx)
movl %eax, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
leaq -0x24(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x6360
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movl $0x1, -0x2c(%rbp)
callq 0x19410
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x18(%rbp)
jbe 0x17c0d
movq -0x78(%rbp), %rax
movl -0x18(%rbp), %ecx
movl %ecx, 0x20(%rax)
jmp 0x17c17
movq -0x78(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, 0x20(%rax)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq 0x20394(%rip), %rdi     # 0x37fd0
leaq 0x11b2e(%rip), %rsi     # 0x29771
callq 0x32a0
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x3400
movq %rax, %rdi
leaq 0x11b1b(%rip), %rsi     # 0x29778
callq 0x32a0
movq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x3400
movq %rax, %rdi
movq 0x20339(%rip), %rsi     # 0x37fb0
callq 0x3300
movq 0x2034d(%rip), %rdi     # 0x37fd0
leaq 0x11af8(%rip), %rsi     # 0x29782
callq 0x32a0
movq %rax, %rdi
movq -0x78(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x3400
movq %rax, %rdi
movq 0x20308(%rip), %rsi     # 0x37fb0
callq 0x3300
movq -0x78(%rbp), %rdi
movl -0x20(%rbp), %edx
movl $0x1, %esi
callq 0x18120
movl %eax, -0x34(%rbp)
movq 0x20308(%rip), %rdi     # 0x37fd0
leaq 0x11ac0(%rip), %rsi     # 0x2978f
callq 0x32a0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x3400
movq %rax, %rdi
leaq 0x11ab3(%rip), %rsi     # 0x2979c
callq 0x32a0
movq -0x78(%rbp), %rcx
movq %rax, %rdi
movq 0x28(%rcx), %rax
movl 0x48(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %esi
callq 0x3400
movq %rax, %rdi
leaq 0x11ace(%rip), %rsi     # 0x297e1
callq 0x32a0
movq %rax, %rdi
movq 0x2028e(%rip), %rsi     # 0x37fb0
callq 0x3300
movq -0x78(%rbp), %rcx
movl -0x34(%rbp), %eax
cmpl 0x18(%rcx), %eax
jbe 0x17d76
movq -0x78(%rbp), %rax
movl 0x20(%rax), %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movl 0x20(%rax), %eax
shll %eax
movl %eax, -0x4c(%rbp)
movl -0x2c(%rbp), %eax
movl -0x30(%rbp), %ecx
subl -0x2c(%rbp), %ecx
shrl %ecx
addl %ecx, %eax
movl %eax, -0x50(%rbp)
leaq -0x4c(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x63a0
movq %rax, %rcx
movq -0x78(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, 0x20(%rax)
jmp 0x17c7c
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, -0x40(%rbp)
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x54(%rbp)
movl 0x48(%rax), %eax
subl $0x1, %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x54(%rbp)
jbe 0x17df0
movl -0x58(%rbp), %edx
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movq -0x78(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x54(%rbp)
movq 0x30(%rax), %rax
movl -0x58(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
jmp 0x17dbc
movq -0x78(%rbp), %rdi
movl -0x20(%rbp), %edx
xorl %esi, %esi
callq 0x18120
movl %eax, -0x38(%rbp)
movq 0x201c8(%rip), %rdi     # 0x37fd0
leaq 0x11997(%rip), %rsi     # 0x297a6
callq 0x32a0
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x3400
movq %rax, %rdi
leaq 0x11973(%rip), %rsi     # 0x2979c
callq 0x32a0
movq -0x78(%rbp), %rcx
movq %rax, %rdi
movq 0x28(%rcx), %rax
movl 0x48(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %esi
callq 0x3400
movq %rax, %rdi
leaq 0x1198e(%rip), %rsi     # 0x297e1
callq 0x32a0
movq %rax, %rdi
movq 0x2014e(%rip), %rsi     # 0x37fb0
callq 0x3300
movq -0x78(%rbp), %rcx
movl -0x38(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x17ec4
movq -0x78(%rbp), %rax
cmpl $0x1, 0x20(%rax)
jbe 0x17ec4
movq -0x78(%rbp), %rax
movl 0x20(%rax), %ecx
subl $0x1, %ecx
movl %ecx, -0x30(%rbp)
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %edx
subl -0x2c(%rbp), %edx
shrl %edx
addl %edx, %ecx
movl %ecx, 0x20(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x17eb7
movq -0x88(%rbp), %rdi
callq 0x33e0
movq $0x0, -0x40(%rbp)
jmp 0x17c7c
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, -0x48(%rbp)
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x5c(%rbp)
movl 0x48(%rax), %eax
subl $0x1, %eax
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x5c(%rbp)
jbe 0x17f3e
movl -0x60(%rbp), %edx
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movq -0x78(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x5c(%rbp)
movq 0x30(%rax), %rax
movl -0x60(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
jmp 0x17f0a
jmp 0x17f40
movq 0x20089(%rip), %rdi     # 0x37fd0
movq 0x20062(%rip), %rsi     # 0x37fb0
callq 0x3300
movq -0x78(%rbp), %rdi
callq 0x17a40
movq -0x78(%rbp), %rdi
addq $0x18, %rdi
leaq -0x38(%rbp), %rsi
callq 0x63a0
movq -0x78(%rbp), %rdi
movl (%rax), %eax
movl %eax, 0x18(%rdi)
movl 0x18(%rdi), %esi
movq -0x48(%rbp), %rdx
movl -0x38(%rbp), %ecx
movq -0x40(%rbp), %r8
movl -0x34(%rbp), %r9d
callq 0x18760
movq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x17faa
movq -0x90(%rbp), %rdi
callq 0x33e0
movq -0x78(%rbp), %rax
movq $0x0, -0x40(%rbp)
movl 0x18(%rax), %eax
movq %rax, -0xa0(%rbp)
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0xa8(%rbp)
callq 0x3030
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movq %rcx, -0x98(%rbp)
cmpq $0x0, %rax
je 0x18058
movq -0x98(%rbp), %rax
movq -0xa0(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0xe530
jmp 0x1803a
movq -0xb8(%rbp), %rcx
movq -0xc0(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0xb0(%rbp)
jne 0x18025
movq -0x78(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl $0x0, -0x70(%rbp)
movq -0x78(%rbp), %rcx
movl -0x70(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x180c6
movq -0x78(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x48(%rbp), %rdx
movl -0x70(%rbp), %esi
movl (%rdx,%rsi,4), %edx
movl (%rcx,%rdx,4), %edx
movq 0x10(%rax), %rax
movl -0x70(%rbp), %ecx
movl %edx, 0x4(%rax,%rcx,8)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x1806e
movq -0xa8(%rbp), %rsi
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
callq 0x3590
jmp 0x18108
movq -0x48(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x180e3
movq -0xc8(%rbp), %rdi
callq 0x33e0
movq -0x78(%rbp), %rax
movq $0x0, -0x48(%rbp)
movl $0x2, 0xc(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xd0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x3530
nopw %cs:(%rax,%rax)