// File: /workspace/github_cpp_makefile_star2_O2/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 0xbc0f
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
cmpl $0x1, 0xc(%rdi)
jne 0xbc12
movl %edx, %ebp
movl %esi, %r14d
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0xba17
callq 0x32b0
andq $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 0xba55
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
pushq $0x1
popq %rbp
cmovbel %eax, %ebp
pushq $0x1
popq %r13
movl %ebp, 0x20(%rbx)
movq 0x1156d(%rip), %r14     # 0x1cfd0
leaq 0x7c6d(%rip), %rsi      # 0x136d7
movq %r14, %rdi
callq 0x3200
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x3160
leaq 0x7c5a(%rip), %rsi      # 0x136de
movq %rax, %rdi
callq 0x3200
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
callq 0x3100
pushq $-0x1
popq %rbp
movq %r14, %rdi
leaq 0x7c3a(%rip), %rsi      # 0x136e8
callq 0x3200
movl 0x20(%rbx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
callq 0x3100
movq %rbx, %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0xbce8
movl %eax, %r12d
movq %r14, %rdi
leaq 0x7c15(%rip), %rsi      # 0x136f5
callq 0x3200
movl %r12d, %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x7c08(%rip), %rsi      # 0x13702
callq 0x3200
movq 0x28(%rbx), %rcx
movl 0x48(%rbx), %edx
decl %edx
movl (%rcx,%rdx,4), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x7c2a(%rip), %rsi      # 0x13747
callq 0x3200
movq %rax, %rdi
callq 0x3100
cmpl 0x18(%rbx), %r12d
ja 0xbbee
incl %r12d
leaq (,%r12,4), %rdi
callq 0x3030
movq %rax, %r15
movl 0x48(%rbx), %eax
decl %eax
movq 0x30(%rbx), %rcx
subq $0x1, %r12
jb 0xbb61
movl %eax, (%r15,%r12,4)
movl %eax, %eax
movl (%rcx,%rax,4), %eax
jmp 0xbb50
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xbce8
movl %eax, %r12d
movq %r14, %rdi
leaq 0x7b92(%rip), %rsi      # 0x1370c
callq 0x3200
movl %r12d, %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x7b6e(%rip), %rsi      # 0x13702
callq 0x3200
movq 0x28(%rbx), %rcx
movl 0x48(%rbx), %edx
decl %edx
movl (%rcx,%rdx,4), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x7b90(%rip), %rsi      # 0x13747
callq 0x3200
movq %rax, %rdi
callq 0x3100
cmpl 0x18(%rbx), %r12d
jae 0xbc19
movl 0x20(%rbx), %ebp
cmpl $0x2, %ebp
jb 0xbc19
decl %ebp
movl %ebp, %eax
subl %r13d, %eax
shrl %eax
addl %r13d, %eax
movl %eax, 0x20(%rbx)
movq %r15, %rdi
callq 0x32b0
jmp 0xbaa4
movl 0x20(%rbx), %eax
leal 0x1(%rax), %r13d
movl %ebp, %ecx
subl %r13d, %ecx
shrl %ecx
addl %eax, %ecx
incl %ecx
addl %eax, %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0x20(%rbx)
jmp 0xbaa4
xorl %eax, %eax
retq
xorl %eax, %eax
jmp 0xbcd9
leal 0x1(%r12), %r13d
leaq (,%r13,4), %rdi
callq 0x3030
movq %rax, %r14
movl 0x48(%rbx), %eax
decl %eax
movq 0x30(%rbx), %rcx
subq $0x1, %r13
jb 0xbc48
movl %eax, (%r14,%r13,4)
movl %eax, %eax
movl (%rcx,%rax,4), %eax
jmp 0xbc37
movq 0x11381(%rip), %rdi     # 0x1cfd0
callq 0x3100
movq %rbx, %rdi
callq 0xb97c
movl 0x18(%rbx), %esi
cmpl %esi, %r12d
cmovbl %r12d, %esi
movl %esi, 0x18(%rbx)
movq %r14, %rdx
movl %r12d, %ecx
movq %r15, %r8
callq 0xc04e
movq %r15, %rdi
callq 0x32b0
movl 0x18(%rbx), %r13d
leaq (,%r13,8), %r12
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
testq %r13, %r13
je 0xbca8
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3130
movq %r15, 0x10(%rbx)
movq 0x50(%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0xbcc8
movl (%r14,%rcx,4), %edx
movl (%rax,%rdx,4), %edx
movl %edx, 0x4(%r15,%rcx,8)
incq %rcx
jmp 0xbcb2
movq %r14, %rdi
callq 0x32b0
movl $0x2, 0xc(%rbx)
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq