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

  usint value_samples = (this->number_of_blocks + BitVector::INDEX_RATE - 1) / BitVector::INDEX_RATE;
  this->rank_rate = (this->size + value_samples - 1) / value_samples;
  value_samples = (this->size + this->rank_rate - 1) / this->rank_rate + 1;
  WriteBuffer index_buffer(value_samples, length(this->number_of_blocks - 1));

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

  this->rank_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 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0x1ea5c
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 (%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, 0x30(%rax)
movl (%rax), %edi
movl 0x30(%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 0x1eaf0
jmp 0x1eaf2
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1d5c0
movb %al, -0x69(%rbp)
jmp 0x1eb04
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x1eb0d
jmp 0x1eb7e
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1fef0
jmp 0x1eb1c
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x14ff0
movl %eax, -0x70(%rbp)
jmp 0x1eb2e
movl -0x70(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jae 0x1eb70
movl -0x40(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x14e80
jmp 0x1eb4a
movq -0x60(%rbp), %rax
movl 0x30(%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x1eb34
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x1e570
jmp 0x1ebbf
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1eaf2
movq -0x60(%rbp), %rax
movl 0x14(%rax), %esi
decl %esi
leaq -0x38(%rbp), %rdi
callq 0x14e80
jmp 0x1eb92
leaq -0x38(%rbp), %rdi
callq 0x1e5b0
movq %rax, -0x78(%rbp)
jmp 0x1eba1
movq -0x60(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, 0x28(%rax)
leaq -0x38(%rbp), %rdi
callq 0x1e570
addq $0x80, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3530
nopl (%rax,%rax)