// File: /workspace/github_cpp_makefile_star2_O3/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:
testl %esi, %esi
je 0xd8a3
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
cmpl $0x1, 0xc(%rdi)
jne 0xd8a6
movl %edx, %ebp
movl %esi, %r14d
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0xd5d9
callq 0x3300
movq $0x0, 0x10(%rbx)
movl 0x8(%rbx), %esi
leal (%r14,%rsi), %eax
decl %eax
xorl %edx, %edx
divl %r14d
movl %eax, 0x18(%rbx)
testl %ebp, %ebp
jne 0xd61c
movq 0x40(%rbx), %rax
movl 0x48(%rbx), %ecx
decl %ecx
xorl %edx, %edx
cmpb $0x0, 0x1c(%rbx)
cmoveq %rdx, %rcx
movl (%rax,%rcx,4), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %esi
cmpl %ecx, %esi
movl $0x1, %ebp
cmovbel %eax, %ebp
movl %ebp, 0x20(%rbx)
movq 0x129aa(%rip), %r14     # 0x1ffd0
leaq 0xb0f2(%rip), %rsi      # 0x1871f
movl $0x6, %edx
movq %r14, %rdi
callq 0x32b0
movl $0x1, %esi
movq %r14, %rdi
callq 0x3180
movq %rax, %r15
leaq 0xb0d5(%rip), %rsi      # 0x18726
movl $0x9, %edx
movq %rax, %rdi
callq 0x32b0
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r15, %rdi
callq 0x3180
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3190
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x1, %ebp
movl $0xc, %edx
movq %r14, %rdi
leaq 0xb081(%rip), %rsi      # 0x18730
callq 0x32b0
movl 0x20(%rbx), %esi
movq %r14, %rdi
callq 0x3180
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3190
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xd9a2
movl %eax, %r12d
movl $0xc, %edx
movq %r14, %rdi
leaq 0xb033(%rip), %rsi      # 0x1873d
callq 0x32b0
movl %r12d, %esi
movq %r14, %rdi
callq 0x3180
movq %rax, %r15
movl $0x9, %edx
movq %rax, %rdi
leaq 0xb01e(%rip), %rsi      # 0x1874a
callq 0x32b0
movq 0x28(%rbx), %rax
movl 0x48(%rbx), %ecx
decl %ecx
movl (%rax,%rcx,4), %esi
movq %r15, %rdi
callq 0x3180
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0xb038(%rip), %rsi      # 0x1878f
callq 0x32b0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3190
cmpl 0x18(%rbx), %r12d
ja 0xd883
leal 0x1(%r12), %r13d
leaq (,%r13,4), %rdi
callq 0x3030
movq %rax, %r15
incl %r12d
je 0xd7bf
movl 0x48(%rbx), %eax
decl %eax
movq 0x30(%rbx), %rcx
movl %eax, -0x4(%r15,%r13,4)
movl %eax, %eax
movl (%rcx,%rax,4), %eax
decq %r13
jne 0xd7b0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xd9a2
movl %eax, %r12d
movl $0xc, %edx
movq %r14, %rdi
leaq 0xaf77(%rip), %rsi      # 0x18754
callq 0x32b0
movl %r12d, %esi
movq %r14, %rdi
callq 0x3180
movq %rax, %r13
movl $0x9, %edx
movq %rax, %rdi
leaq 0xaf4b(%rip), %rsi      # 0x1874a
callq 0x32b0
movq 0x28(%rbx), %rax
movl 0x48(%rbx), %ecx
decl %ecx
movl (%rax,%rcx,4), %esi
movq %r13, %rdi
callq 0x3180
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xaf65(%rip), %rsi      # 0x1878f
callq 0x32b0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3190
cmpl 0x18(%rbx), %r12d
jae 0xd8ad
movl 0x20(%rbx), %r13d
cmpl $0x2, %r13d
jb 0xd8ad
decl %r13d
movl %r13d, %eax
subl %ebp, %eax
shrl %eax
addl %ebp, %eax
movl %eax, 0x20(%rbx)
movq %r15, %rdi
callq 0x3300
jmp 0xd6a0
movl 0x20(%rbx), %eax
leal 0x1(%rax), %ebp
movl %r13d, %ecx
subl %ebp, %ecx
shrl %ecx
addl %eax, %ecx
incl %ecx
addl %eax, %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0x20(%rbx)
jmp 0xd6a0
xorl %eax, %eax
retq
xorl %eax, %eax
jmp 0xd992
leal 0x1(%r12), %ebp
leaq (,%rbp,4), %rdi
callq 0x3030
movq %rax, %r13
movl %r12d, %eax
incl %eax
je 0xd8e1
movl 0x48(%rbx), %eax
decl %eax
movq 0x30(%rbx), %rcx
movl %eax, -0x4(%r13,%rbp,4)
movl %eax, %eax
movl (%rcx,%rax,4), %eax
decq %rbp
jne 0xd8d2
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3190
movq %rbx, %rdi
callq 0xd530
movl 0x18(%rbx), %esi
cmpl %esi, %r12d
cmovbl %r12d, %esi
movl %esi, 0x18(%rbx)
movq %r13, %rdx
movl %r12d, %ecx
movq %r15, %r8
callq 0xe0a4
movq %r15, %rdi
callq 0x3300
movl 0x18(%rbx), %r12d
leaq (,%r12,8), %r15
movq %r15, %rdi
callq 0x3030
movq %rax, %r14
testq %r12, %r12
je 0xd97d
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3150
movq %r14, 0x10(%rbx)
movq 0x50(%rbx), %rax
movl (%r13,%rbp,4), %ecx
movl (%rax,%rcx,4), %ecx
movl %ecx, 0x4(%r14,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0xd966
jmp 0xd981
movq %r14, 0x10(%rbx)
movq %r13, %rdi
callq 0x3300
movl $0x2, 0xc(%rbx)
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop