// File: adamnovak[P]rlcsa/sasamples.cpp
// Function: CSA::SASamples::buildSamples(std::pair<unsigned int, unsigned int>*, bool, unsigned int)
void
SASamples::buildSamples(pair_type* sample_pairs, bool inverse, usint threads)
{
  if(inverse)
  {
    for(usint i = 0; i < this->items; i++)
    {
      std::swap(sample_pairs[i].first, sample_pairs[i].second);
    }
  }
  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  #endif
  parallelSort(sample_pairs, sample_pairs + this->items);

  SAVector::Encoder encoder(INDEX_BLOCK_SIZE);
  WriteBuffer sample_buffer(this->items, length(this->size - 1));
  for(usint i = 0; i < this->items; i++)
  {
    encoder.setBit(sample_pairs[i].first);
    sample_buffer.writeItem(sample_pairs[i].second);
/*    if(sample_pairs[i].first >= this->size || sample_pairs[i].second >= this->size)
    {
      std::cout << "Sample " << i << ": " << sample_pairs[i] << std::endl;
    }*/
  }

  if(inverse)
  {
    this->inverse_indexes = new SAVector(encoder, this->size);
    this->inverse_samples = sample_buffer.getReadBuffer();
  }
  else
  {
    this->indexes = new SAVector(encoder, this->size);
    this->samples = sample_buffer.getReadBuffer();
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
testb $0x1, -0x11(%rbp)
je 0x131d6
movl $0x0, -0x1c(%rbp)
movq -0xd0(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0xc(%rcx), %eax
jae 0x131d4
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rsi
addq $0x4, %rsi
callq 0x10700
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x13195
jmp 0x131d6
movq -0xd0(%rbp), %rax
movq -0x10(%rbp), %rdi
movl 0xc(%rax), %eax
leaq (%rdi,%rax,8), %rsi
callq 0x14f60
leaq -0x90(%rbp), %rdi
movl $0x10, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x206f0
movq -0xd0(%rbp), %rax
movl 0x8(%rax), %edi
movl 0xc(%rax), %eax
movl %eax, -0xd8(%rbp)
decl %edi
callq 0x14da0
movl %eax, -0xd4(%rbp)
jmp 0x13225
movl -0xd4(%rbp), %edx
movl -0xd8(%rbp), %esi
leaq -0xb8(%rbp), %rdi
callq 0x1e430
jmp 0x1323f
movl $0x0, -0xc8(%rbp)
movq -0xd0(%rbp), %rcx
movl -0xc8(%rbp), %eax
cmpl 0xc(%rcx), %eax
jae 0x132d3
movq -0x10(%rbp), %rax
movl -0xc8(%rbp), %ecx
movl (%rax,%rcx,8), %esi
leaq -0x90(%rbp), %rdi
callq 0x20740
jmp 0x13276
movq -0x10(%rbp), %rax
movl -0xc8(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %esi
leaq -0xb8(%rbp), %rdi
callq 0x14e80
jmp 0x13292
jmp 0x13294
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x13249
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
jmp 0x13434
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
jmp 0x13428
testb $0x1, -0x11(%rbp)
je 0x13375
movl $0x48, %edi
callq 0x32c0
movq %rax, -0xe0(%rbp)
jmp 0x132f0
movq -0xe0(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl 0x8(%rax), %edx
leaq -0x90(%rbp), %rsi
callq 0x20020
jmp 0x1330f
movq -0xd0(%rbp), %rax
movq -0xe0(%rbp), %rcx
movq %rcx, 0x20(%rax)
leaq -0xb8(%rbp), %rdi
callq 0x1e5b0
movq %rax, -0xe8(%rbp)
jmp 0x13336
movq -0xd0(%rbp), %rax
movq -0xe8(%rbp), %rcx
movq %rcx, 0x28(%rax)
jmp 0x13407
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
movl $0x48, %esi
callq 0x32d0
jmp 0x13428
movl $0x48, %edi
callq 0x32c0
movq %rax, -0xf0(%rbp)
jmp 0x13388
movq -0xf0(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl 0x8(%rax), %edx
leaq -0x90(%rbp), %rsi
callq 0x20020
jmp 0x133a7
movq -0xd0(%rbp), %rax
movq -0xf0(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq -0xb8(%rbp), %rdi
callq 0x1e5b0
movq %rax, -0xf8(%rbp)
jmp 0x133ce
movq -0xd0(%rbp), %rax
movq -0xf8(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0x13407
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
movl $0x48, %esi
callq 0x32d0
jmp 0x13428
leaq -0xb8(%rbp), %rdi
callq 0x1e570
leaq -0x90(%rbp), %rdi
callq 0x20720
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
leaq -0xb8(%rbp), %rdi
callq 0x1e570
leaq -0x90(%rbp), %rdi
callq 0x20720
movq -0xc0(%rbp), %rdi
callq 0x3530
nopl (%rax)