// File: /workspace/github_cpp_makefile_star2_O2/cegme[P]vose/Vose.cc
// Function: Vose::init()
void Vose::init() {

  // Reset the random number generators
  reset_scores_die();
  reset_random_double();

  // clear alias array
  //vose_alias.clear();
  alias.resize(scores.size(), 0);

  // clear prob array
  //vose_prob.clear();
  prob.resize(scores.size(), 0);

  // Work lists
  std::list<size_t> small;
  std::list<size_t> large;

  size_t n = scores.size();

  std::vector<double> probs_list(n);
  for(size_t i = 0; i != n; ++i) {
    // Compute the weighted probabilies, times the size
    double prob = (double) n * scores[i].weight / total_weight;
    probs_list[i] = prob;
    //std:: cerr << "probs_list[" << i << "]: " << probs_list[i] << "\n";

    // Build work lists
    if(prob < 1) 
      small.push_back(i);
    else
      large.push_back(i);
  }

  while(!small.empty() && !large.empty()) {
    size_t from_small = small.front(); small.pop_front();
    size_t from_large = large.front(); large.pop_front();
    
    prob[from_small] = probs_list[from_small];
    alias[from_small] = from_large;

    probs_list[from_large] += probs_list[from_small] - 1;

    if(probs_list[from_large] < 1)
      small.push_front(from_large);
    else
      large.push_front(from_large);
  }

  while(!small.empty()) {
    size_t from_small = small.front(); small.pop_front();
    prob[from_small] = 1;
  }
  while (!large.empty()) {
    size_t from_large = large.front(); large.pop_front();
    prob[from_large] =  1;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r15
callq 0x63f2
movq %r15, %rdi
callq 0x6462
leaq 0x60(%r15), %rdi
movq 0x48(%r15), %rax
subq 0x40(%r15), %rax
pushq $0x30
popq %r13
cqto
idivq %r13
leaq -0x60(%rbp), %r12
andq $0x0, (%r12)
movq %rdi, -0x68(%rbp)
movq %rax, %rsi
movq %r12, %rdx
callq 0x6498
movq 0x48(%r15), %rax
subq 0x40(%r15), %rax
leaq 0x78(%r15), %rbx
cqto
idivq %r13
andq $0x0, (%r12)
leaq -0x60(%rbp), %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x64d2
movq %r14, 0x8(%r14)
movq %r14, (%r14)
andq $0x0, 0x10(%r14)
leaq -0x48(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq 0x48(%r15), %rax
subq 0x40(%r15), %rax
cqto
idivq %r13
movq %rax, %r13
leaq -0x80(%rbp), %rdi
leaq -0x30(%rbp), %rdx
movq %rax, %rsi
callq 0x650c
movq %r13, %xmm0
punpckldq 0x41ef(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x41f7(%rip), %xmm0     # 0xa030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x90(%rbp)
xorl %eax, %eax
leaq -0x30(%rbp), %r12
movq %rax, -0x30(%rbp)
cmpq %r13, %rax
je 0x5eab
movq 0x40(%r15), %rcx
imulq $0x30, %rax, %rdx
cvtsi2sdq (%rcx,%rdx), %xmm0
mulsd -0x90(%rbp), %xmm0
cvtsi2sdq 0x58(%r15), %xmm1
divsd %xmm1, %xmm0
movq -0x80(%rbp), %rcx
movsd %xmm0, (%rcx,%rax,8)
movsd 0x417b(%rip), %xmm1     # 0xa008
ucomisd %xmm0, %xmm1
movq %r14, %rdi
ja 0x5e9a
leaq -0x48(%rbp), %rdi
movq %r12, %rsi
callq 0x655e
movq -0x30(%rbp), %rax
incq %rax
jmp 0x5e53
leaq -0x48(%rbp), %r15
leaq -0x60(%rbp), %r12
leaq -0x30(%rbp), %r13
movq -0x60(%rbp), %rax
cmpq %r15, -0x48(%rbp)
sete %cl
cmpq %r12, %rax
je 0x5f6c
testb %cl, %cl
jne 0x5f6c
movq 0x10(%rax), %r14
movq %r12, %rdi
callq 0x656e
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq %r15, %rdi
callq 0x656e
movq -0x80(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
movq (%rbx), %rcx
movsd %xmm0, (%rcx,%r14,8)
movq -0x30(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq (%rdx), %rdx
movq %rcx, (%rdx,%r14,8)
addsd 0x42db(%rip), %xmm0     # 0xa1f8
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd 0x40d9(%rip), %xmm1     # 0xa008
ucomisd %xmm0, %xmm1
movq %r12, %rdi
ja 0x5f3b
movq %r15, %rdi
movq %r13, %rsi
callq 0x657c
jmp 0x5eb7
movq 0x10(%rax), %r15
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x656e
movq (%rbx), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax,%r15,8)
movq (%r14), %rax
leaq -0x60(%rbp), %rcx
cmpq %rcx, %rax
jne 0x5f48
leaq -0x48(%rbp), %r14
movabsq $0x3ff0000000000000, %r15 # imm = 0x3FF0000000000000
movq -0x48(%rbp), %rax
cmpq %r14, %rax
je 0x5fa1
movq 0x10(%rax), %r12
movq %r14, %rdi
callq 0x656e
movq (%rbx), %rax
movq %r15, (%rax,%r12,8)
jmp 0x5f83
leaq -0x80(%rbp), %rdi
callq 0x3a5e
leaq -0x48(%rbp), %rdi
callq 0x658c
leaq -0x60(%rbp), %rdi
callq 0x658c
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5fde
jmp 0x5fd2
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x3a5e
leaq -0x48(%rbp), %rdi
callq 0x658c
leaq -0x60(%rbp), %rdi
callq 0x658c
movq %rbx, %rdi
callq 0x22d0