// File: adamnovak[P]rlcsa/sasamples.cpp
// Function: CSA::SASamples::SASamples(std::pair<unsigned int, unsigned int>*, unsigned int, unsigned int, unsigned int)
SASamples::SASamples(pair_type* sample_pairs, usint data_size, usint sample_rate, usint threads) :
  weighted(false),
  rate(sample_rate), size(data_size), items((data_size + sample_rate - 1) / sample_rate)
{
  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  #endif
  parallelSort(sample_pairs, sample_pairs + this->items);

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movb $0x0, (%rax)
movl -0x18(%rbp), %ecx
movl %ecx, 0x4(%rax)
movl -0x14(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %ecx
movl %ecx, %edx
movl %esi, %eax
leal -0x1(%rax,%rdx), %eax
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movq -0xd8(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rdi
movl 0xc(%rax), %eax
leaq (%rdi,%rax,8), %rsi
callq 0x14f60
movq -0xd8(%rbp), %rax
movl 0xc(%rax), %edi
movl %edi, -0xcc(%rbp)
decl %edi
callq 0x14da0
movl -0xcc(%rbp), %esi
movl %eax, %edx
leaq -0x48(%rbp), %rdi
callq 0x1e430
leaq -0xb8(%rbp), %rdi
movl $0x10, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x206f0
jmp 0x13df5
movl $0x0, -0xc8(%rbp)
movq -0xd8(%rbp), %rcx
movl -0xc8(%rbp), %eax
cmpl 0xc(%rcx), %eax
jae 0x13e9a
movq -0x10(%rbp), %rax
movl -0xc8(%rbp), %ecx
movl (%rax,%rcx,8), %esi
leaq -0xb8(%rbp), %rdi
callq 0x20740
jmp 0x13e30
movq -0xd8(%rbp), %rcx
movq -0x10(%rbp), %rax
movl -0xc8(%rbp), %edx
movl 0x4(%rax,%rdx,8), %eax
movl 0x4(%rcx), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x14e80
jmp 0x13e59
jmp 0x13e5b
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x13dff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
jmp 0x13f56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
jmp 0x13f4a
movl $0x48, %edi
callq 0x32c0
movq %rax, -0xe0(%rbp)
jmp 0x13ead
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rax
movl 0x8(%rax), %edx
leaq -0xb8(%rbp), %rsi
callq 0x20020
jmp 0x13ecc
movq -0xd8(%rbp), %rax
movq -0xe0(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq -0x48(%rbp), %rdi
callq 0x1e5b0
movq %rax, -0xe8(%rbp)
jmp 0x13ef0
movq -0xd8(%rbp), %rdi
movq -0xe8(%rbp), %rax
movq %rax, 0x18(%rdi)
callq 0x13450
jmp 0x13f09
leaq -0xb8(%rbp), %rdi
callq 0x20720
leaq -0x48(%rbp), %rdi
callq 0x1e570
addq $0xf0, %rsp
popq %rbp
retq
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
movl $0x48, %esi
callq 0x32d0
leaq -0xb8(%rbp), %rdi
callq 0x20720
leaq -0x48(%rbp), %rdi
callq 0x1e570
movq -0xc0(%rbp), %rdi
callq 0x3530
nopl (%rax,%rax)