// File: /workspace/github_cpp_makefile_star2_O3/adamnovak[P]rlcsa/sasamples.cpp
// Function: CSA::SASamples::SASamples(std::pair<unsigned int, unsigned int>*, CSA::DeltaVector*, unsigned int, unsigned int, unsigned int)
SASamples::SASamples(short_pair* sa, DeltaVector* end_points, usint data_size, usint sample_rate, usint threads) :
  weighted(false),
  rate(sample_rate),
  items(0)
{
  usint sequences = end_points->getNumberOfItems();

  DeltaVector::Iterator iter(*(end_points));

  // Determine the samples, insert them into a vector, and sort them.
  usint start = 0, end = iter.select(0);  // Closed range in padded collection.
  usint seq_start = 0, seq_end = end;     // Closed range in inverse SA.
  std::vector<pair_type>* vec = new std::vector<pair_type>();
  for(usint i = 0; i < sequences; i++)
  {
    for(usint j = seq_start; j <= seq_end; j += this->rate)
    {
      vec->push_back(pair_type(sa[j].second - sequences, this->items));
      this->items++;
    }
    start = nextMultipleOf(this->rate, end);
    end = iter.selectNext();
    seq_start = seq_end + 2;
    seq_end = seq_start + end - start;
  }
  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  #endif
  parallelSort(vec->begin(), vec->end());

  // Compress the samples.
  this->size = end + 1;
  WriteBuffer sample_buffer(this->items, length(this->items - 1));
  SAVector::Encoder encoder(INDEX_BLOCK_SIZE);
  for(usint i = 0; i < this->items; i++)
  {
    pair_type sample = (*vec)[i];
    encoder.setBit(sample.first);
    sample_buffer.writeItem(sample.second);
  }
  delete vec;

  this->indexes = new SAVector(encoder, this->size);
  this->samples = sample_buffer.getReadBuffer();
  this->buildInverseSamples();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movb $0x0, (%rdi)
movl %r8d, 0x4(%rdi)
movl $0x0, 0xc(%rdi)
movl 0x4(%rdx), %ebp
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
movq %rdx, %rsi
callq 0x12ac0
movq %r14, %rdi
xorl %esi, %esi
callq 0x12c6e
movl %eax, %r13d
movl $0x18, %edi
callq 0x3260
movq %rax, %r14
xorl %esi, %esi
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testl %ebp, %ebp
je 0xb6f5
movq %r14, %r15
xorl %r14d, %r14d
movl %r13d, %r12d
movq %rsi, 0x30(%rsp)
movl %r13d, 0x4(%rsp)
cmpl %r12d, %r14d
jbe 0xb66b
movl 0x4(%rbx), %r13d
jmp 0xb6a5
movl 0xc(%rbx), %eax
movl %r14d, %ecx
movq 0x38(%rsp), %rdx
movl 0x4(%rdx,%rcx,8), %ecx
subl %ebp, %ecx
movl %ecx, 0x40(%rsp)
movl %eax, 0x44(%rsp)
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0xc7da
movl 0x4(%rbx), %r13d
movl 0xc(%rbx), %eax
incl %eax
movl %eax, 0xc(%rbx)
addl %r13d, %r14d
cmpl %r12d, %r14d
jbe 0xb66e
leaq 0xb0(%rsp), %rdi
callq 0x12dce
movl %eax, %ecx
movl 0x4(%rsp), %eax
xorl %edx, %edx
divl %r13d
notl %eax
imull %r13d, %eax
movl %r12d, %r14d
addl $0x2, %r14d
addl %ecx, %r12d
addl $0x2, %r12d
addl %eax, %r12d
movq 0x30(%rsp), %rsi
incl %esi
movl %ecx, %r13d
cmpl %ebp, %esi
jne 0xb656
movq %r15, %r14
movq (%r15), %rdi
movq 0x8(%r15), %rsi
movl %ecx, %r13d
jmp 0xb6f7
xorl %edi, %edi
callq 0xc75d
incl %r13d
movl %r13d, 0x8(%rbx)
movl 0xc(%rbx), %esi
leal -0x1(%rsi), %eax
bsrl %eax, %edx
xorl $-0x20, %edx
addl $0x21, %edx
testl %eax, %eax
cmovel %eax, %edx
leaq 0x8(%rsp), %rdi
callq 0x113b2
leaq 0x40(%rsp), %rdi
movl $0x10, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x134f2
cmpl $0x0, 0xc(%rbx)
je 0xb7cd
xorl %r12d, %r12d
leaq 0x40(%rsp), %r15
movq (%r14), %rax
movl (%rax,%r12,8), %esi
movl 0x4(%rax,%r12,8), %ebp
movq %r15, %rdi
callq 0x13502
movl 0x14(%rsp), %ecx
movl 0x24(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
jae 0xb76d
movl %ecx, %eax
jmp 0xb798
movl %ebp, %esi
movl %eax, %ecx
shrl %cl, %esi
negb %dl
movl %edx, %ecx
shll %cl, %esi
shrl %cl, %esi
movq 0x8(%rsp), %rcx
movl 0x20(%rsp), %edx
orl %esi, (%rcx,%rdx,4)
incl 0x20(%rsp)
movl $0x20, 0x24(%rsp)
movl $0x20, %edx
testl %eax, %eax
je 0xb7ba
subl %eax, %edx
movl %edx, 0x24(%rsp)
negb %al
movl %eax, %ecx
shll %cl, %ebp
shrl %cl, %ebp
movl %edx, %ecx
shll %cl, %ebp
movq 0x8(%rsp), %rax
movl 0x20(%rsp), %ecx
orl %ebp, (%rax,%rcx,4)
incl 0x28(%rsp)
incq %r12
movl 0xc(%rbx), %eax
cmpq %rax, %r12
jb 0xb747
movq (%r14), %rdi
testq %rdi, %rdi
je 0xb7e1
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x3270
movl $0x18, %esi
movq %r14, %rdi
callq 0x3270
movl $0x48, %edi
callq 0x3260
movq %rax, %r14
movl 0x8(%rbx), %edx
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x12aa8
movq %r14, 0x10(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x11476
movq %rax, 0x18(%rbx)
movq %rbx, %rdi
callq 0xb2f2
leaq 0x40(%rsp), %rdi
callq 0x134fc
leaq 0x8(%rsp), %rdi
callq 0x11462
leaq 0xb0(%rsp), %rdi
callq 0x12ac6
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x48, %esi
movq %r14, %rdi
callq 0x3270
jmp 0xb87c
movq %rax, %rbx
jmp 0xb886
jmp 0xb892
jmp 0xb892
jmp 0xb892
jmp 0xb879
jmp 0xb892
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x134fc
leaq 0x8(%rsp), %rdi
callq 0x11462
jmp 0xb895
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x12ac6
movq %rbx, %rdi
callq 0x3460