// File: /workspace/github_cpp_makefile_star2_O2/adamnovak[P]rlcsa/docarray.cpp
// Function: CSA::DocArray::DocArray(CSA::STNode*, CSA::RLCSA const&)
DocArray::DocArray(STNode* root, const RLCSA& _rlcsa) :
  rlcsa(_rlcsa),
  leaf_ranges(0), first_children(0), parents(0), next_leaves(0),
  rule_borders(0), rules(0),
  block_borders(0), blocks(0),
  ok(false), has_grammar(false), uses_rle(false)
{
  if(root == 0) { return; }

  uint nodes = 0, leaves = 0;
  root->determineSize(nodes, leaves);
  root->setNext();

  DeltaVector::Encoder range_encoder(RANGE_BLOCK_SIZE);
  SuccinctVector::Encoder child_encoder(PARENT_BLOCK_SIZE,
    nextMultipleOf(PARENT_BLOCK_SIZE, BITS_TO_BYTES(nodes)));

  std::stack<STNode*> nodestack;
  for(STNode* curr = root; curr != 0; curr = curr->child) { nodestack.push(curr); }
  while(nodestack.top() != root)
  {
    STNode* curr = nodestack.top(); nodestack.pop();
    for(STNode* temp = curr->sibling; temp != 0; temp = temp->child) { nodestack.push(temp); }
    if(curr->child == 0) { range_encoder.addBit(curr->range.first); }
    if(curr == curr->parent->child) { child_encoder.setBit(curr->id); }
  }
  this->leaf_ranges = new DeltaVector(range_encoder, root->range.second + 1);
  this->first_children = new SuccinctVector(child_encoder);

  // Pointers to the parents for first children and to the next leaf for internal nodes.
  WriteBuffer par_buffer(nodes - leaves, length(nodes - leaves - 1));
  WriteBuffer next_buffer(nodes - leaves, length(leaves));
  SuccinctVector::Iterator iter(*(this->first_children));
  while(!(nodestack.empty())) { nodestack.pop(); }
  for(STNode* curr = root; curr != 0; curr = curr->child) { nodestack.push(curr); }
  while(nodestack.top() != root)
  {
    STNode* curr = nodestack.top(); nodestack.pop();
    for(STNode* temp = curr->sibling; temp != 0; temp = temp->child) { nodestack.push(temp); }
    if(curr == curr->parent->child)
    {
      par_buffer.goToItem(iter.rank(curr->id) - 1);
      par_buffer.writeItem(curr->parent->id - leaves);
    }
    if(curr->id >= leaves)
    {
      next_buffer.writeItem(curr->next == 0 ? this->getNumberOfLeaves() : curr->next->id);
    }
  }
  this->parents = par_buffer.getReadBuffer();
  this->next_leaves = next_buffer.getReadBuffer();

  this->ok = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdx, (%rdi)
andl $0x0, 0x47(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x38(%rdi)
testq %rsi, %rsi
je 0xf4b7
movq %rdi, %r15
movq %rsi, %r14
leaq 0x6c(%rsp), %rbx
andl $0x0, (%rbx)
leaq 0x14(%rsp), %rdx
andl $0x0, (%rdx)
movq %rsi, %rdi
movq %rbx, %rsi
callq 0xed9a
movq %r14, %rdi
callq 0xef6e
leaq 0x158(%rsp), %rdi
pushq $0x20
popq %rbp
movl %ebp, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x13f3a
movl (%rbx), %r13d
leal 0x7(%r13), %edx
shrl $0x3, %edx
andl $-0x20, %edx
addl $0x20, %edx
leaq 0xe8(%rsp), %rdi
movl %ebp, %esi
callq 0x14e98
leaq 0x70(%rsp), %rdi
callq 0x53d2
leaq 0x8(%r15), %rax
movq %rax, 0x20(%rsp)
leaq 0x70(%rsp), %rbx
leaq 0x28(%rsp), %r12
movq %r14, %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xf1b9
movq %rbx, %rdi
movq %r12, %rsi
callq 0x59d4
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rax
jmp 0xf199
movq %r13, 0x50(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x70(%rsp), %rbx
leaq 0x158(%rsp), %r12
leaq 0xe8(%rsp), %r13
leaq 0x28(%rsp), %rbp
movq 0xa0(%rsp), %rax
cmpq 0xa8(%rsp), %rax
je 0xf1f5
movq -0x8(%rax), %r15
jmp 0xf208
movq 0xb8(%rsp), %rax
movq -0x8(%rax), %rax
movq 0x1f8(%rax), %r15
cmpq %r14, %r15
je 0xf26a
movq %rbx, %rdi
callq 0x597e
leaq 0x20(%r15), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xf23c
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x59d4
movq 0x28(%rsp), %rax
addq $0x18, %rax
jmp 0xf219
cmpq $0x0, 0x18(%r15)
jne 0xf24f
movl 0x4(%r15), %esi
movq %r12, %rdi
callq 0x13faa
movq 0x10(%r15), %rax
cmpq 0x18(%rax), %r15
jne 0xf1dd
movl 0x34(%r15), %esi
movq %r13, %rdi
callq 0x14ea6
jmp 0xf1dd
pushq $0x48
popq %rdi
callq 0x3230
movq 0x18(%rsp), %rbx
movq 0x50(%rsp), %r15
movq %rax, %r12
movl 0x8(%r14), %edx
incl %edx
leaq 0x158(%rsp), %rsi
movq %rax, %rdi
callq 0x13c44
movq 0x20(%rsp), %rax
movq %r12, (%rax)
pushq $0x48
popq %rdi
callq 0x3230
movq %rax, %r12
leaq 0xe8(%rsp), %rsi
movq %rax, %rdi
callq 0x149ee
movq %r12, 0x10(%rbx)
movl 0x14(%rsp), %r12d
subl %r12d, %r15d
leal -0x1(%r15), %edi
callq 0xbad4
leaq 0x28(%rsp), %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x12c3a
movl %r12d, %edi
callq 0xbad4
leaq 0xc0(%rsp), %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x12c3a
movq 0x10(%rbx), %rsi
leaq 0x58(%rsp), %rdi
callq 0x14ae6
leaq 0x70(%rsp), %rbx
movq 0xa0(%rsp), %rax
cmpq 0x80(%rsp), %rax
je 0xf325
movq %rbx, %rdi
callq 0x597e
jmp 0xf309
leaq 0x70(%rsp), %rbx
leaq 0x8(%rsp), %r15
movq %r14, %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xf352
movq %rbx, %rdi
movq %r15, %rsi
callq 0x59d4
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
jmp 0xf332
leaq 0x70(%rsp), %r15
leaq 0xc0(%rsp), %rbx
leaq 0x8(%rsp), %r13
movq 0xa0(%rsp), %rax
cmpq 0xa8(%rsp), %rax
je 0xf37c
movq -0x8(%rax), %rbp
jmp 0xf38f
movq 0xb8(%rsp), %rax
movq -0x8(%rax), %rax
movq 0x1f8(%rax), %rbp
cmpq %r14, %rbp
je 0xf44a
movq %r15, %rdi
callq 0x597e
leaq 0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xf3c7
movq %r15, %rdi
movq %r13, %rsi
callq 0x59d4
movq 0x8(%rsp), %rax
addq $0x18, %rax
jmp 0xf3a4
movq 0x10(%rbp), %rax
cmpq 0x18(%rax), %rbp
jne 0xf416
movl 0x34(%rbp), %esi
leaq 0x58(%rsp), %rdi
xorl %edx, %edx
callq 0x14af0
decl %eax
movl 0x34(%rsp), %ecx
imull %eax, %ecx
movl %ecx, %edx
shrl $0x5, %edx
movl %edx, 0x40(%rsp)
andl $0x1f, %ecx
pushq $0x20
popq %rdx
subl %ecx, %edx
movl %edx, 0x44(%rsp)
movl %eax, 0x48(%rsp)
movq 0x10(%rbp), %rax
movl 0x34(%rax), %esi
subl %r12d, %esi
leaq 0x28(%rsp), %rdi
callq 0xbae2
cmpl %r12d, 0x34(%rbp)
jb 0xf364
movq 0x28(%rbp), %rax
leaq 0x34(%rax), %rcx
movq 0x20(%rsp), %rdx
movq (%rdx), %rdx
addq $0x4, %rdx
testq %rax, %rax
cmovneq %rcx, %rdx
movl (%rdx), %esi
movq %rbx, %rdi
callq 0xbae2
jmp 0xf364
leaq 0x28(%rsp), %rdi
callq 0x12cf6
movq 0x18(%rsp), %rbx
movq %rax, 0x18(%rbx)
leaq 0xc0(%rsp), %rdi
callq 0x12cf6
movq %rax, 0x20(%rbx)
movb $0x1, 0x48(%rbx)
leaq 0x58(%rsp), %rdi
callq 0x14aee
leaq 0xc0(%rsp), %rdi
callq 0x12ce2
leaq 0x28(%rsp), %rdi
callq 0x12ce2
leaq 0x70(%rsp), %rdi
callq 0x5452
leaq 0xe8(%rsp), %rdi
callq 0x14ea0
leaq 0x158(%rsp), %rdi
callq 0x13f42
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xf50a
movq %rax, %rbx
jmp 0xf517
jmp 0xf523
jmp 0xf4d7
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r12, %rdi
callq 0x3240
jmp 0xf526
movq %rax, %rbx
jmp 0xf530
movq %rax, %rbx
jmp 0xf53d
jmp 0xf4fd
jmp 0xf523
jmp 0xf523
jmp 0xf4fd
jmp 0xf523
jmp 0xf4fd
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x14aee
leaq 0xc0(%rsp), %rdi
callq 0x12ce2
leaq 0x28(%rsp), %rdi
callq 0x12ce2
jmp 0xf526
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5452
leaq 0xe8(%rsp), %rdi
callq 0x14ea0
leaq 0x158(%rsp), %rdi
callq 0x13f42
movq %rbx, %rdi
callq 0x3430