// File: /workspace/github_cpp_makefile_star2_O1/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 0xd756
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
cmpl $0x1, 0xc(%rdi)
jne 0xd759
movl %edx, %ebp
movl %esi, %r14d
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0xd48d
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 0xd4d0
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 0x11af6(%rip), %r14     # 0x1efd0
leaq 0xa1f1(%rip), %rsi      # 0x176d2
movl $0x6, %edx
movq %r14, %rdi
callq 0x32b0
movl $0x1, %esi
movq %r14, %rdi
callq 0x3180
movq %rax, %r15
leaq 0xa1d4(%rip), %rsi      # 0x176d9
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 0xa180(%rip), %rsi      # 0x176e3
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 0xd852
movl %eax, %r12d
movl $0xc, %edx
movq %r14, %rdi
leaq 0xa132(%rip), %rsi      # 0x176f0
callq 0x32b0
movl %r12d, %esi
movq %r14, %rdi
callq 0x3180
movq %rax, %r15
movl $0x9, %edx
movq %rax, %rdi
leaq 0xa11d(%rip), %rsi      # 0x176fd
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 0xa137(%rip), %rsi      # 0x17742
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 0xd736
leal 0x1(%r12), %edi
shlq $0x2, %rdi
callq 0x3030
movq %rax, %r15
incl %r12d
je 0xd672
movl 0x48(%rbx), %eax
decl %eax
movq 0x30(%rbx), %rcx
movl %r12d, %edx
movl %eax, -0x4(%r15,%rdx,4)
movl %eax, %eax
movl (%rcx,%rax,4), %eax
decq %rdx
jne 0xd663
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xd852
movl %eax, %r12d
movl $0xc, %edx
movq %r14, %rdi
leaq 0xa077(%rip), %rsi      # 0x17707
callq 0x32b0
movl %r12d, %esi
movq %r14, %rdi
callq 0x3180
movq %rax, %r13
movl $0x9, %edx
movq %rax, %rdi
leaq 0xa04b(%rip), %rsi      # 0x176fd
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 0xa065(%rip), %rsi      # 0x17742
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 0xd760
movl 0x20(%rbx), %r13d
cmpl $0x2, %r13d
jb 0xd760
decl %r13d
movl %r13d, %eax
subl %ebp, %eax
shrl %eax
addl %ebp, %eax
movl %eax, 0x20(%rbx)
movq %r15, %rdi
callq 0x3300
jmp 0xd554
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 0xd554
xorl %eax, %eax
retq
xorl %eax, %eax
jmp 0xd842
leal 0x1(%r12), %edi
shlq $0x2, %rdi
callq 0x3030
movq %rax, %r13
movl %r12d, %edx
incl %edx
je 0xd792
movl 0x48(%rbx), %eax
decl %eax
movq 0x30(%rbx), %rcx
movl %edx, %edx
movl %eax, -0x4(%r13,%rdx,4)
movl %eax, %eax
movl (%rcx,%rax,4), %eax
decq %rdx
jne 0xd783
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 0xd3e4
movl 0x18(%rbx), %esi
cmpl %esi, %r12d
cmovbl %r12d, %esi
movl %esi, 0x18(%rbx)
movq %r13, %rdx
movl %r12d, %ecx
movq %r15, %r8
callq 0xdef6
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 0xd80d
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3150
movq %r14, 0x10(%rbx)
testq %r12, %r12
je 0xd831
movq 0x50(%rbx), %rax
xorl %ecx, %ecx
movl (%r13,%rcx,4), %edx
movl (%rax,%rdx,4), %edx
movl %edx, 0x4(%r14,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0xd81c
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