// File: adamnovak[P]rlcsa/bits/bitvector.cpp
// Function: CSA::BitVector::BitVector(CSA::VectorEncoder&, unsigned int)
BitVector::BitVector(VectorEncoder& encoder, usint universe_size) :
  size(universe_size), items(encoder.items),
  block_size(encoder.block_size),
  number_of_blocks(encoder.blocks),
  rank_index(0), select_index(0)
{
  if(this->items == 0)
  {
    std::cerr << "BitVector: Cannot create a bit vector with no 1-bits!" << std::endl;
    return;
  }
  this->copyArray(encoder);

  this->integer_bits = length(this->size);
  WriteBuffer sample_buffer(2 * (this->number_of_blocks + 1), this->integer_bits);

  // Compress the samples.
  for(std::list<usint*>::iterator iter = encoder.sample_blocks.begin(); iter != encoder.sample_blocks.end(); iter++)
  {
    usint* buf = *iter;
    for(usint i = 0; i < 2 * encoder.samples_in_superblock; i++)
    {
      sample_buffer.writeItem(buf[i]);
    }
  }
  for(usint i = 0; i < 2 * encoder.current_samples; i++)
  {
    sample_buffer.writeItem(encoder.samples[i]);
  }
  sample_buffer.writeItem(this->items);
  sample_buffer.writeItem(this->size);

  this->samples = sample_buffer.getReadBuffer();

  this->indexForRank();
  this->indexForSelect();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, 0x14(%rax)
movq $0x0, 0x28(%rax)
movq $0x0, 0x38(%rax)
cmpl $0x0, 0x4(%rax)
jne 0x1f021
movq 0x18fef(%rip), %rdi     # 0x37ff0
leaq 0xa920(%rip), %rsi      # 0x29928
callq 0x32a0
movq %rax, %rdi
movq 0x18f99(%rip), %rsi     # 0x37fb0
callq 0x3300
jmp 0x1f1b7
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x1f1d0
movq -0x88(%rbp), %rax
movl (%rax), %edi
callq 0x14da0
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x20(%rax)
movl 0x14(%rax), %esi
addl $0x1, %esi
shll %esi
movl 0x20(%rax), %edx
leaq -0x40(%rbp), %rdi
callq 0x1e430
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
callq 0x1d4e0
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
callq 0x1d540
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1d510
testb $0x1, %al
jne 0x1f096
jmp 0x1f10f
leaq -0x48(%rbp), %rdi
callq 0x1d570
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x68(%rcx), %ecx
shll %ecx
cmpl %ecx, %eax
jae 0x1f0f9
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
leaq -0x40(%rbp), %rdi
callq 0x14e80
jmp 0x1f0d2
jmp 0x1f0d4
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1f0ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x1e570
jmp 0x1f1c0
jmp 0x1f0fb
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1d590
movq %rax, -0x78(%rbp)
jmp 0x1f072
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x6c(%rcx), %ecx
shll %ecx
cmpl %ecx, %eax
jae 0x1f14c
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
leaq -0x40(%rbp), %rdi
callq 0x14e80
jmp 0x1f13f
jmp 0x1f141
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x1f116
movq -0x88(%rbp), %rax
movl 0x4(%rax), %esi
leaq -0x40(%rbp), %rdi
callq 0x14e80
jmp 0x1f161
movq -0x88(%rbp), %rax
movl (%rax), %esi
leaq -0x40(%rbp), %rdi
callq 0x14e80
jmp 0x1f175
leaq -0x40(%rbp), %rdi
callq 0x1e5b0
movq %rax, -0x90(%rbp)
jmp 0x1f187
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rax
movq %rax, 0x18(%rdi)
callq 0x1ea20
jmp 0x1f1a0
movq -0x88(%rbp), %rdi
callq 0x1ebd0
jmp 0x1f1ae
leaq -0x40(%rbp), %rdi
callq 0x1e570
addq $0x90, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x3530
nopl (%rax)