// File: adamnovak[P]rlcsa/bits/bitvector.cpp
// Function: CSA::BitVector::indexForSelect()
void
BitVector::indexForSelect()
{
  delete this->select_index;

  usint index_samples = (this->number_of_blocks + BitVector::INDEX_RATE - 1) / BitVector::INDEX_RATE;
  this->select_rate = (this->items + index_samples - 1) / index_samples;
  index_samples = (this->items + this->select_rate - 1) / this->select_rate + 1;
  WriteBuffer index_buffer(index_samples, length(this->number_of_blocks - 1));

  // current is index, pointer is sample number.
  usint current = 0, pointer = 0;
  this->samples->goToItem(2);
  while(this->samples->hasNextItem())
  {
    usint limit = this->samples->readItem();  // Next sampled index.
    this->samples->skipItem();
    while(current < limit)
    {
      index_buffer.writeItem(pointer);
      current += this->select_rate;
    }
    pointer++;
  }
  index_buffer.writeItem(this->number_of_blocks - 1);

  this->select_index = index_buffer.getReadBuffer();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0x1ec0c
movq -0x58(%rbp), %rdi
callq 0x1e230
movq -0x58(%rbp), %rdi
movl $0x28, %esi
callq 0x32d0
movq -0x60(%rbp), %rax
movl 0x14(%rax), %ecx
addl $0x4, %ecx
movl $0xcccccccd, %edx       # imm = 0xCCCCCCCD
imulq %rdx, %rcx
shrq $0x22, %rcx
movl %ecx, -0xc(%rbp)
movl 0x4(%rax), %esi
movl -0xc(%rbp), %ecx
movl %ecx, %edx
movl %esi, %eax
leal -0x1(%rax,%rdx), %eax
xorl %edx, %edx
movl %edx, -0x68(%rbp)
divl %ecx
movl -0x68(%rbp), %edx
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x40(%rax)
movl 0x4(%rax), %edi
movl 0x40(%rax), %ecx
movl %ecx, %esi
movl %edi, %eax
leal -0x1(%rax,%rsi), %eax
divl %ecx
movl %eax, %ecx
movq -0x60(%rbp), %rax
incl %ecx
movl %ecx, -0xc(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, -0x64(%rbp)
movl 0x14(%rax), %edi
decl %edi
callq 0x14da0
movl -0x64(%rbp), %esi
movl %eax, %edx
leaq -0x38(%rbp), %rdi
callq 0x1e430
movq -0x60(%rbp), %rax
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x15070
jmp 0x1eca2
jmp 0x1eca4
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1d5c0
movb %al, -0x69(%rbp)
jmp 0x1ecb6
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x1ecbf
jmp 0x1ed32
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x14ff0
movl %eax, -0x70(%rbp)
jmp 0x1ecd1
movq -0x60(%rbp), %rax
movl -0x70(%rbp), %ecx
movl %ecx, -0x50(%rbp)
movq 0x18(%rax), %rdi
callq 0x1fef0
jmp 0x1ece6
jmp 0x1ece8
movl -0x3c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jae 0x1ed24
movl -0x40(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x14e80
jmp 0x1ecfe
movq -0x60(%rbp), %rax
movl 0x40(%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x1ece8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x1e570
jmp 0x1ed73
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1eca4
movq -0x60(%rbp), %rax
movl 0x14(%rax), %esi
decl %esi
leaq -0x38(%rbp), %rdi
callq 0x14e80
jmp 0x1ed46
leaq -0x38(%rbp), %rdi
callq 0x1e5b0
movq %rax, -0x78(%rbp)
jmp 0x1ed55
movq -0x60(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, 0x38(%rax)
leaq -0x38(%rbp), %rdi
callq 0x1e570
addq $0x80, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3530
nopl (%rax)