// File: /workspace/github_cpp_makefile_star2_O3/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 0x12296
movq %rsi, %r14
movq %rdi, %r15
xorl %eax, %eax
leaq 0xe4(%rsp), %rbx
movl %eax, (%rbx)
leaq 0x8c(%rsp), %rdx
movl %eax, (%rdx)
movq %rsi, %rdi
movq %rbx, %rsi
callq 0x11806
movq %r14, %rdi
callq 0x11a04
leaq 0x158(%rsp), %rdi
movl $0x20, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x19142
movl (%rbx), %r13d
leal 0x7(%r13), %edx
shrl $0x3, %edx
andl $-0x20, %edx
addl $0x20, %edx
leaq 0xe8(%rsp), %rdi
movl $0x20, %esi
callq 0x1b21c
xorps %xmm0, %xmm0
leaq 0x10(%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 0x65ce
leaq 0x8(%r15), %rax
movq %rax, 0x98(%rsp)
leaq 0x60(%rsp), %rbx
movq %r14, (%rbx)
leaq 0x10(%rsp), %r12
movq %r14, %rax
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11d67
movq %rax, (%rcx)
addq $0x8, 0x40(%rsp)
jmp 0x11d72
movq %r12, %rdi
movq %rbx, %rsi
callq 0x69a6
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x60(%rsp)
testq %rax, %rax
jne 0x11d49
movq %r13, 0xc8(%rsp)
movq %r15, 0x90(%rsp)
leaq 0x158(%rsp), %r12
leaq 0xe8(%rsp), %r13
leaq 0x10(%rsp), %rbp
leaq 0x60(%rsp), %rbx
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rdi
cmpq %rdi, %rax
je 0x11dd1
movq -0x8(%rax), %r15
cmpq %r14, %r15
je 0x11e96
addq $-0x8, %rax
jmp 0x11e1d
movq 0x58(%rsp), %rax
movq -0x8(%rax), %rax
movq 0x1f8(%rax), %r15
cmpq %r14, %r15
je 0x11e96
movl $0x200, %esi            # imm = 0x200
callq 0x3270
movq 0x58(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x58(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x48(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x50(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x40(%rsp)
movq 0x20(%r15), %rax
movq %rax, 0x60(%rsp)
testq %rax, %rax
je 0x11e64
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11e4e
movq %rax, (%rcx)
addq $0x8, 0x40(%rsp)
jmp 0x11e59
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x69a6
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rax
jmp 0x11e26
cmpq $0x0, 0x18(%r15)
jne 0x11e77
movl 0x4(%r15), %esi
movq %r12, %rdi
callq 0x191be
movq 0x10(%r15), %rax
cmpq 0x18(%rax), %r15
jne 0x11daf
movl 0x34(%r15), %esi
movq %r13, %rdi
callq 0x1b22a
jmp 0x11daf
movl $0x48, %edi
callq 0x3260
movq 0x90(%rsp), %rbx
movq 0xc8(%rsp), %r15
movq %rax, %r12
movl 0x8(%r14), %edx
incl %edx
leaq 0x158(%rsp), %rsi
movq %rax, %rdi
callq 0x186f8
movq 0x98(%rsp), %rax
movq %r12, (%rax)
movl $0x48, %edi
callq 0x3260
movq %rax, %r12
leaq 0xe8(%rsp), %rsi
movq %rax, %rdi
callq 0x1aa22
movq %r12, 0x10(%rbx)
movl 0x8c(%rsp), %ebp
subl %ebp, %r15d
leal -0x1(%r15), %eax
bsrl %eax, %edx
xorl $-0x20, %edx
addl $0x21, %edx
testl %eax, %eax
cmovel %eax, %edx
leaq 0x60(%rsp), %rdi
movl %r15d, %esi
callq 0x17002
bsrl %ebp, %edx
xorl $-0x20, %edx
addl $0x21, %edx
testl %ebp, %ebp
cmovel %ebp, %edx
leaq 0xa0(%rsp), %rdi
movl %r15d, %esi
callq 0x17002
movq 0x10(%rbx), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x1ab50
movq 0x20(%rsp), %rax
movq 0x40(%rsp), %rcx
cmpq %rax, %rcx
je 0x11faf
movq 0x48(%rsp), %rdi
cmpq %rdi, %rcx
je 0x11f6c
addq $-0x8, %rcx
jmp 0x11fa5
movl $0x200, %esi            # imm = 0x200
callq 0x3270
movq 0x20(%rsp), %rax
movq 0x58(%rsp), %rcx
leaq -0x8(%rcx), %rdx
movq %rdx, 0x58(%rsp)
movq -0x8(%rcx), %rdi
movq %rdi, 0x48(%rsp)
leaq 0x200(%rdi), %rcx
movq %rcx, 0x50(%rsp)
leaq 0x1f8(%rdi), %rcx
movq %rcx, 0x40(%rsp)
cmpq %rax, %rcx
jne 0x11f61
leaq 0x8(%rsp), %rbx
movq %r14, (%rbx)
leaq 0x10(%rsp), %r15
movq %r14, %rax
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11fdd
movq %rax, (%rcx)
addq $0x8, 0x40(%rsp)
jmp 0x11fe8
movq %r15, %rdi
movq %rbx, %rsi
callq 0x69a6
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x11fbf
leaq 0xd0(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rdi
cmpq %rdi, %rax
je 0x1202f
movq -0x8(%rax), %rbx
cmpq %r14, %rbx
je 0x12223
addq $-0x8, %rax
jmp 0x1207b
movq 0x58(%rsp), %rax
movq -0x8(%rax), %rax
movq 0x1f8(%rax), %rbx
cmpq %r14, %rbx
je 0x12223
movl $0x200, %esi            # imm = 0x200
callq 0x3270
movq 0x58(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x58(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x48(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x50(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x40(%rsp)
movq 0x20(%rbx), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x120c2
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x120ac
movq %rax, (%rcx)
addq $0x8, 0x40(%rsp)
jmp 0x120b7
movq %r12, %rdi
movq %r13, %rsi
callq 0x69a6
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
jmp 0x12084
movq 0x10(%rbx), %rax
cmpq 0x18(%rax), %rbx
jne 0x12174
movl 0x34(%rbx), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1ab5e
decl %eax
movl 0x6c(%rsp), %ecx
movl %ecx, %esi
imull %eax, %esi
movl %esi, %edi
shrl $0x5, %edi
movl %edi, 0x78(%rsp)
andl $0x1f, %esi
movl $0x20, %edx
subl %esi, %edx
movl %edx, 0x7c(%rsp)
movl %eax, 0x80(%rsp)
movq 0x10(%rbx), %rax
movl 0x34(%rax), %r8d
subl %ebp, %r8d
movl %ecx, %eax
subl %edx, %eax
jae 0x1211b
movl %ecx, %eax
jmp 0x12149
movl %r8d, %edx
movl %eax, %ecx
shrl %cl, %edx
movl %esi, %ecx
shll %cl, %edx
shrl %cl, %edx
movq 0x60(%rsp), %rcx
movl %edi, %esi
orl %edx, (%rcx,%rsi,4)
movl 0x78(%rsp), %edi
incl %edi
movl %edi, 0x78(%rsp)
movl $0x20, 0x7c(%rsp)
movl $0x20, %edx
testl %eax, %eax
je 0x1216d
subl %eax, %edx
movl %edx, 0x7c(%rsp)
negb %al
movl %eax, %ecx
shll %cl, %r8d
shrl %cl, %r8d
movl %edx, %ecx
shll %cl, %r8d
movq 0x60(%rsp), %rax
movl %edi, %ecx
orl %r8d, (%rax,%rcx,4)
incl 0x80(%rsp)
cmpl %ebp, 0x34(%rbx)
jb 0x1200d
movq 0x28(%rbx), %rax
leaq 0x34(%rax), %rcx
movq 0x98(%rsp), %rdx
movq (%rdx), %rdx
addq $0x4, %rdx
testq %rax, %rax
cmovneq %rcx, %rdx
movl (%rdx), %esi
movl 0xac(%rsp), %ecx
movl 0xbc(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
jae 0x121b5
movl %ecx, %eax
jmp 0x121ec
movl %esi, %edi
movl %eax, %ecx
shrl %cl, %edi
negb %dl
movl %edx, %ecx
shll %cl, %edi
shrl %cl, %edi
movq 0xa0(%rsp), %rcx
movl 0xb8(%rsp), %edx
orl %edi, (%rcx,%rdx,4)
incl 0xb8(%rsp)
movl $0x20, 0xbc(%rsp)
movl $0x20, %edx
testl %eax, %eax
je 0x12217
subl %eax, %edx
movl %edx, 0xbc(%rsp)
negb %al
movl %eax, %ecx
shll %cl, %esi
shrl %cl, %esi
movl %edx, %ecx
shll %cl, %esi
movq 0xa0(%rsp), %rax
movl 0xb8(%rsp), %ecx
orl %esi, (%rax,%rcx,4)
incl 0xc0(%rsp)
jmp 0x1200d
leaq 0x60(%rsp), %rdi
callq 0x170c6
movq 0x90(%rsp), %rbx
movq %rax, 0x18(%rbx)
leaq 0xa0(%rsp), %rdi
callq 0x170c6
movq %rax, 0x20(%rbx)
movb $0x1, 0x48(%rbx)
leaq 0xd0(%rsp), %rdi
callq 0x1ab5c
leaq 0xa0(%rsp), %rdi
callq 0x170b2
leaq 0x60(%rsp), %rdi
callq 0x170b2
leaq 0x10(%rsp), %rdi
callq 0x644a
leaq 0xe8(%rsp), %rdi
callq 0x1b224
leaq 0x158(%rsp), %rdi
callq 0x1914c
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x122ee
movq %rax, %rbx
jmp 0x122fb
jmp 0x12307
jmp 0x122b6
movq %rax, %rbx
movl $0x48, %esi
movq %r12, %rdi
callq 0x3270
jmp 0x1230a
movq %rax, %rbx
jmp 0x12314
movq %rax, %rbx
jmp 0x12321
jmp 0x122de
jmp 0x12307
jmp 0x122de
jmp 0x122de
jmp 0x12307
jmp 0x12307
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x1ab5c
leaq 0xa0(%rsp), %rdi
callq 0x170b2
leaq 0x60(%rsp), %rdi
callq 0x170b2
jmp 0x1230a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x644a
leaq 0xe8(%rsp), %rdi
callq 0x1b224
leaq 0x158(%rsp), %rdi
callq 0x1914c
movq %rbx, %rdi
callq 0x3460