// File: /workspace/github_cpp_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movq %rsi, %r15
movq %rdi, %rbx
movb $0x0, (%rdi)
movl %r8d, 0x4(%rdi)
andl $0x0, 0xc(%rdi)
movl 0x4(%rdx), %r13d
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movq %rdx, %rsi
callq 0xf8d8
movq %r14, %rdi
xorl %esi, %esi
callq 0xf976
movl %eax, %r12d
pushq $0x18
popq %rdi
callq 0x3220
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %ebp, %ebp
xorl %eax, %eax
movl %r12d, (%rsp)
movl %eax, 0x4(%rsp)
cmpl %r13d, %eax
je 0xa053
cmpl %r12d, %ebp
ja 0xa01a
movl %ebp, %eax
movl 0x4(%r15,%rax,8), %eax
subl %r13d, %eax
movl %eax, 0x30(%rsp)
movl 0xc(%rbx), %eax
movl %eax, 0x34(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0xac2e
incl 0xc(%rbx)
addl 0x4(%rbx), %ebp
jmp 0x9feb
movl 0x4(%rbx), %ebp
leaq 0xa0(%rsp), %rdi
callq 0xf9d8
movl %eax, %ecx
movl (%rsp), %eax
xorl %edx, %edx
divl %ebp
notl %eax
imull %ebp, %eax
movl %r12d, %ebp
addl $0x2, %ebp
addl %ecx, %r12d
addl $0x2, %r12d
addl %eax, %r12d
movl 0x4(%rsp), %eax
incl %eax
movl %ecx, (%rsp)
jmp 0x9fe2
movq (%r14), %rdi
movq 0x8(%r14), %rsi
callq 0xadc1
movl (%rsp), %eax
incl %eax
movl %eax, 0x8(%rbx)
movl 0xc(%rbx), %r15d
leal -0x1(%r15), %edi
callq 0xab34
leaq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0xe8b6
leaq 0x30(%rsp), %rdi
pushq $0x10
popq %rsi
movl $0x100000, %edx         # imm = 0x100000
callq 0xfbb6
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
leaq 0x8(%rsp), %r12
movl 0xc(%rbx), %eax
cmpq %rax, %r13
jae 0xa0cd
movq (%r14), %rax
movl (%rax,%r13,8), %esi
movl 0x4(%rax,%r13,8), %ebp
movq %r15, %rdi
callq 0xfbc4
movq %r12, %rdi
movl %ebp, %esi
callq 0xab42
incq %r13
jmp 0xa0a2
movq %r14, %rdi
callq 0xb30c
pushq $0x18
popq %rsi
movq %r14, %rdi
callq 0x3230
pushq $0x48
popq %rdi
callq 0x3220
movq %rax, %r14
movl 0x8(%rbx), %edx
leaq 0x30(%rsp), %rsi
movq %rax, %rdi
callq 0xf8c0
movq %r14, 0x10(%rbx)
leaq 0x8(%rsp), %rdi
callq 0xe972
movq %rax, 0x18(%rbx)
movq %rbx, %rdi
callq 0x9d6e
leaq 0x30(%rsp), %rdi
callq 0xfbbe
leaq 0x8(%rsp), %rdi
callq 0xe95e
leaq 0xa0(%rsp), %rdi
callq 0xf8de
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r14, %rdi
callq 0x3230
jmp 0xa16a
movq %rax, %rbx
jmp 0xa174
jmp 0xa180
jmp 0xa180
jmp 0xa180
jmp 0xa167
jmp 0xa180
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xfbbe
leaq 0x8(%rsp), %rdi
callq 0xe95e
jmp 0xa183
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0xf8de
movq %rbx, %rdi
callq 0x3410