// File: /workspace/github_cpp_makefile_star2_O1/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)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x38(%rdi)
movl $0x0, 0x47(%rdi)
testq %rsi, %rsi
je 0x11fff
movq %rsi, %r14
movq %rdi, %rbx
xorl %eax, %eax
leaq 0x1c(%rsp), %r15
movl %eax, (%r15)
leaq 0xc(%rsp), %rdx
movl %eax, (%rdx)
movq %rsi, %rdi
movq %r15, %rsi
callq 0x11554
movq %r14, %rdi
callq 0x11750
leaq 0x158(%rsp), %rdi
movl $0x20, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x18d26
movl (%r15), %edx
addl $0x7, %edx
shrl $0x3, %edx
andl $-0x20, %edx
addl $0x20, %edx
leaq 0xe8(%rsp), %rdi
movl $0x20, %esi
callq 0x1acfa
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x6694
leaq 0x8(%rbx), %rax
movq %rax, 0xa0(%rsp)
leaq 0x78(%rsp), %r15
movq %r14, (%r15)
leaq 0x20(%rsp), %r12
movq %r14, %rax
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11ab9
movq %rax, (%rcx)
addq $0x8, 0x50(%rsp)
jmp 0x11ac4
movq %r12, %rdi
movq %r15, %rsi
callq 0x6a66
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x78(%rsp)
testq %rax, %rax
jne 0x11a9b
movq %rbx, 0xd0(%rsp)
movl $0x200, %ebx            # imm = 0x200
leaq 0xe8(%rsp), %r12
leaq 0x20(%rsp), %r13
leaq 0x78(%rsp), %rbp
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rdi
movq %rax, %rcx
cmpq %rdi, %rax
jne 0x11b14
movq 0x68(%rsp), %rcx
movq -0x8(%rcx), %rcx
addq %rbx, %rcx
cmpq %r14, -0x8(%rcx)
je 0x11bf2
movq %rax, %rcx
cmpq %rdi, %rax
jne 0x11b32
movq 0x68(%rsp), %rcx
movq -0x8(%rcx), %rcx
addq %rbx, %rcx
movq -0x8(%rcx), %r15
cmpq %rdi, %rax
je 0x11b41
addq $-0x8, %rax
jmp 0x11b74
movl $0x200, %esi            # imm = 0x200
callq 0x3270
movq 0x68(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x68(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x58(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x60(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x50(%rsp)
movq 0x20(%r15), %rax
movq %rax, 0x78(%rsp)
testq %rax, %rax
je 0x11bbb
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11ba5
movq %rax, (%rcx)
addq $0x8, 0x50(%rsp)
jmp 0x11bb0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6a66
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
jmp 0x11b7d
cmpq $0x0, 0x18(%r15)
jne 0x11bd3
movl 0x4(%r15), %esi
leaq 0x158(%rsp), %rdi
callq 0x18da2
movq 0x10(%r15), %rax
cmpq 0x18(%rax), %r15
jne 0x11af6
movl 0x34(%r15), %esi
movq %r12, %rdi
callq 0x1ad08
jmp 0x11af6
movl $0x48, %edi
callq 0x3260
movq 0xd0(%rsp), %rbx
movq %rax, %r15
movl 0x8(%r14), %edx
incl %edx
leaq 0x158(%rsp), %rsi
movq %rax, %rdi
callq 0x1842c
movq 0xa0(%rsp), %rax
movq %r15, (%rax)
movl $0x48, %edi
callq 0x3260
movq %rax, %r15
leaq 0xe8(%rsp), %rsi
movq %rax, %rdi
callq 0x1a508
movq %r15, 0x10(%rbx)
movl 0x1c(%rsp), %esi
subl 0xc(%rsp), %esi
leal -0x1(%rsi), %eax
bsrl %eax, %edx
xorl $-0x20, %edx
addl $0x21, %edx
testl %eax, %eax
cmovel %eax, %edx
leaq 0x78(%rsp), %rdi
callq 0x16d92
movl 0x1c(%rsp), %esi
movl 0xc(%rsp), %eax
subl %eax, %esi
bsrl %eax, %edx
xorl $-0x20, %edx
addl $0x21, %edx
testl %eax, %eax
cmovel %eax, %edx
leaq 0xa8(%rsp), %rdi
callq 0x16d92
movq 0x10(%rbx), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x1a636
movq 0x50(%rsp), %rax
cmpq 0x30(%rsp), %rax
je 0x11cf8
movq 0x58(%rsp), %rdi
cmpq %rdi, %rax
je 0x11cbe
addq $-0x8, %rax
jmp 0x11cf1
movl $0x200, %esi            # imm = 0x200
callq 0x3270
movq 0x68(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x68(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x58(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x60(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x50(%rsp)
jmp 0x11ca7
leaq 0x10(%rsp), %r15
movq %r14, (%r15)
leaq 0x20(%rsp), %r12
movq %r14, %rax
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11d26
movq %rax, (%rcx)
addq $0x8, 0x50(%rsp)
jmp 0x11d31
movq %r12, %rdi
movq %r15, %rsi
callq 0x6a66
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x11d08
movl $0x200, %ebp            # imm = 0x200
leaq 0x20(%rsp), %r12
leaq 0x10(%rsp), %r13
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rdi
movq %rax, %rcx
cmpq %rdi, %rax
jne 0x11d71
movq 0x68(%rsp), %rcx
movq -0x8(%rcx), %rcx
addq %rbp, %rcx
cmpq %r14, -0x8(%rcx)
je 0x11f94
movq %rax, %rcx
cmpq %rdi, %rax
jne 0x11d8f
movq 0x68(%rsp), %rcx
movq -0x8(%rcx), %rcx
addq %rbp, %rcx
movq -0x8(%rcx), %r15
cmpq %rdi, %rax
je 0x11d9e
addq $-0x8, %rax
jmp 0x11dd1
movl $0x200, %esi            # imm = 0x200
callq 0x3270
movq 0x68(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x68(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x58(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x60(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x50(%rsp)
movq 0x20(%r15), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x11e18
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11e02
movq %rax, (%rcx)
addq $0x8, 0x50(%rsp)
jmp 0x11e0d
movq %r12, %rdi
movq %r13, %rsi
callq 0x6a66
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
jmp 0x11dda
movq 0x10(%r15), %rax
cmpq 0x18(%rax), %r15
jne 0x11ee2
movl 0x34(%r15), %esi
leaq 0xd8(%rsp), %rdi
xorl %edx, %edx
callq 0x1a644
decl %eax
movl 0x84(%rsp), %ecx
movl %ecx, %esi
imull %eax, %esi
movl %esi, %r8d
shrl $0x5, %r8d
movl %r8d, 0x90(%rsp)
andl $0x1f, %esi
movl $0x20, %r9d
subl %esi, %r9d
movl %r9d, 0x94(%rsp)
movl %eax, 0x98(%rsp)
movq 0x10(%r15), %rax
movl 0x34(%rax), %edi
subl 0xc(%rsp), %edi
movl %ecx, %edx
subl %r9d, %edx
jb 0x11eae
movl %edi, %eax
movl %edx, %ecx
shrl %cl, %eax
movl %esi, %ecx
shll %cl, %eax
shrl %cl, %eax
movq 0x78(%rsp), %rcx
movl %r8d, %esi
orl %eax, (%rcx,%rsi,4)
incl 0x90(%rsp)
movl $0x20, 0x94(%rsp)
movl %edx, %ecx
testl %ecx, %ecx
je 0x11edb
movl 0x94(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x94(%rsp)
negb %cl
shll %cl, %edi
shrl %cl, %edi
movl %eax, %ecx
shll %cl, %edi
movq 0x78(%rsp), %rax
movl 0x90(%rsp), %ecx
orl %edi, (%rax,%rcx,4)
incl 0x98(%rsp)
movl 0x34(%r15), %eax
cmpl 0xc(%rsp), %eax
jb 0x11d53
movq 0x28(%r15), %rax
leaq 0x34(%rax), %rcx
movq 0xa0(%rsp), %rdx
movq (%rdx), %rdx
addq $0x4, %rdx
testq %rax, %rax
cmovneq %rcx, %rdx
movl (%rdx), %esi
movl 0xb4(%rsp), %ecx
movl 0xc4(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
jb 0x11f58
movl %esi, %edi
movl %eax, %ecx
shrl %cl, %edi
negb %dl
movl %edx, %ecx
shll %cl, %edi
shrl %cl, %edi
movq 0xa8(%rsp), %rcx
movl 0xc0(%rsp), %edx
orl %edi, (%rcx,%rdx,4)
incl 0xc0(%rsp)
movl $0x20, 0xc4(%rsp)
movl %eax, %ecx
testl %ecx, %ecx
je 0x11f88
movl 0xc4(%rsp), %eax
subl %ecx, %eax
movl %eax, 0xc4(%rsp)
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %eax, %ecx
shll %cl, %esi
movq 0xa8(%rsp), %rax
movl 0xc0(%rsp), %ecx
orl %esi, (%rax,%rcx,4)
incl 0xc8(%rsp)
jmp 0x11d53
leaq 0x78(%rsp), %rdi
callq 0x16e56
movq %rax, 0x18(%rbx)
leaq 0xa8(%rsp), %rdi
callq 0x16e56
movq %rax, 0x20(%rbx)
movb $0x1, 0x48(%rbx)
leaq 0xd8(%rsp), %rdi
callq 0x1a642
leaq 0xa8(%rsp), %rdi
callq 0x16e42
leaq 0x78(%rsp), %rdi
callq 0x16e42
leaq 0x20(%rsp), %rdi
callq 0x6524
leaq 0xe8(%rsp), %rdi
callq 0x1ad02
leaq 0x158(%rsp), %rdi
callq 0x18d30
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x12057
movq %rax, %rbx
jmp 0x12064
jmp 0x12070
jmp 0x1201f
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0x3270
jmp 0x12073
movq %rax, %rbx
jmp 0x1207d
movq %rax, %rbx
jmp 0x1208a
jmp 0x12047
jmp 0x12070
jmp 0x12047
jmp 0x12047
jmp 0x12070
jmp 0x12070
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x1a642
leaq 0xa8(%rsp), %rdi
callq 0x16e42
leaq 0x78(%rsp), %rdi
callq 0x16e42
jmp 0x12073
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x6524
leaq 0xe8(%rsp), %rdi
callq 0x1ad02
leaq 0x158(%rsp), %rdi
callq 0x18d30
movq %rbx, %rdi
callq 0x3460
nop