// File: /workspace/github_cpp_makefile_star2_O2/adamnovak[P]rlcsa/sampler_test.cpp
// Function: main
int main(int argc, char** argv)
{
  std::cout << "Sampler test" << std::endl;
  std::cout << std::endl;

  // Handle command line parameters.
  double multiplier = 1.0, greediness = 0.0;
  bool greedy = false, forward = true, write = false;
  char* base_name = 0;
  char* weights_name = 0;
  usint threads = 1;
  for(int i = 1; i < argc; i++)
  {
    if(argv[i][0] == '-')
    {
      switch(argv[i][1])
      {
        case 'b':
          forward = false; break;
        case 'g':
          greediness = atof(argv[i] + 2); greedy = true; break;
        case 'm':
          multiplier = atof(argv[i] + 2); break;
        case 't':
          threads = atoi(argv[i] + 2); break;
        case 'w':
          write = true; break;
        default:
          std::cout << "Invalid option: " << argv[i] << std::endl << std::endl;
          printUsage();
          return 1;
      }
    }
    else if(base_name == 0)
    {
      base_name = argv[i];
    }
    else if(weights_name == 0)
    {
      weights_name = argv[i];
    }
  }
  if(weights_name == 0) { printUsage(); return 2; }

  std::cout << "Options:";
  greediness = std::min(1.0, std::max(0.0, greediness));
  threads = std::max(threads, (usint)1); threads = std::min(threads, (usint)64);
  if(!forward) { std::cout << " backward"; }
  if(greedy) { std::cout << " greediness=" << greediness; }
  if(multiplier < 0) { multiplier = 1.0; }
  if(multiplier != 1.0) { std::cout << " multiplier=" << multiplier; }
  std::cout << " threads=" << threads;
  if(write) { std::cout << " write"; }
  std::cout << std::endl;
  std::cout << "Base name: " << base_name << std::endl;
  std::cout << "Weights: " << weights_name << std::endl;
  std::cout << std::endl;

  // Determine index parameters.
  std::string parameters_name = std::string(base_name) + PARAMETERS_EXTENSION;
  Parameters parameters;
  parameters.set(RLCSA_BLOCK_SIZE);
  parameters.set(SAMPLE_RATE);
  parameters.read(parameters_name);
  parameters.set(SUPPORT_LOCATE.first, 1);
  parameters.set(SUPPORT_DISPLAY.first, 1);
  parameters.set(WEIGHTED_SAMPLES.first, 1);
  // parameters.print();


  std::ifstream weights_file(weights_name, std::ios_base::binary);
  if(!weights_file)
  {
    std::cerr << "Error opening weights file!" << std::endl;
    return 3;
  }

  usint n = fileSize(weights_file) / sizeof(weight_type);
  weight_type* weights = new weight_type[n];
  weights_file.read((char*)weights, n * sizeof(weight_type));
  weights_file.close();
  for(usint i = 0; i < n; i++)
  {
    if(weights[i] >= 0)
    {
      weights[i] = ceil(weights[i] * multiplier);
    }
    else
    {
      std::cerr << "Negative value found: weights[" << i << "] == " << weights[i] << "!" << std::endl;
      delete[] weights;
      return 4;
    }
  }
  std::cout << "Input size:   " << n << std::endl;
  std::cout << "Sample rate:  " << parameters.get(SAMPLE_RATE) << std::endl;
  std::cout << std::endl;


  // Do the actual sampling.
  double start = readTimer();
  Sampler* sampler = 0;
  if(!greedy)
  {
    WeightedSampler* wsampler = new WeightedSampler(weights, n, forward);
    if(!(wsampler->buildSamples(parameters.get(SAMPLE_RATE), parameters.get(INITIAL_ADJUSTMENT), threads)))
    {
      delete wsampler; wsampler = 0;
      return 5;
    }
    sampler = wsampler; wsampler = 0;
  }
  else
  {
    SemiGreedySampler* gsampler = new SemiGreedySampler(weights, n);
    if(!(gsampler->buildSamples(parameters.get(SAMPLE_RATE), greediness)))
    {
      delete gsampler; gsampler = 0;
      return 5;
    }
    sampler = gsampler; gsampler = 0;
  }
  double sampled = readTimer();
  usint memory = memoryUsage();


  // Write the samples or build RLCSA.
  double stop;
  if(write)
  {
    stop = readTimer();
    sampler->writeTo(base_name);
  }
  else
  {
    std::ifstream input_file(base_name, std::ios_base::binary);
    if(!input_file)
    {
      std::cerr << "Error opening input file " << base_name << "!" << std::endl;
      return 5;
    }
    uchar* data = new uchar[n];
    input_file.read((char*)data, n);
    input_file.close();
    RLCSA rlcsa(data, n, parameters.get(RLCSA_BLOCK_SIZE), parameters.get(SAMPLE_RATE), threads, sampler, true);
    stop = readTimer();
    if(rlcsa.isOk())
    {
      rlcsa.printInfo();
      rlcsa.reportSize(true);
      rlcsa.writeTo(base_name);
    }
  }
  usint total_memory = memoryUsage();


  double sampling_time = sampled - start;
  double time = stop - start;
  double megabytes = n / (double)MEGABYTE;
  std::cout << "Total: " << time << " seconds (" << (megabytes / time) << " MB/s), " << total_memory << " kB" << std::endl;
  std::cout << "Sampling: " << sampling_time << " seconds (" << (megabytes / sampling_time) << " MB/s), " << memory << " kB" << std::endl;
  std::cout << std::endl;

  delete sampler; sampler = 0;
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xcf8, %rsp            # imm = 0xCF8
movq %rsi, %r15
movl %edi, %ebx
movq 0x19004(%rip), %r14     # 0x1cfd0
leaq 0xf23a(%rip), %rsi      # 0x1320d
movq %r14, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x3100
movq %r14, %rdi
callq 0x3100
movslq %ebx, %r13
movsd 0xf012(%rip), %xmm0     # 0x13008
movapd %xmm0, 0x30(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
pushq $0x1
popq %r12
pushq $0x1
popq %rbp
xorl %r14d, %r14d
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
cmpq %r13, %rbp
jge 0x40be
movq (%r15,%rbp,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x4073
movzbl 0x1(%rdi), %eax
cmpl $0x62, %eax
je 0x40ae
cmpl $0x77, %eax
je 0x4086
cmpl $0x6d, %eax
je 0x409d
cmpl $0x74, %eax
je 0x408f
cmpl $0x67, %eax
jne 0x4572
addq $0x2, %rdi
callq 0x3280
movsd %xmm0, (%rsp)
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x40b6
testq %rbx, %rbx
je 0x4081
testq %r14, %r14
cmoveq %rdi, %r14
jmp 0x40b6
movq %rdi, %rbx
jmp 0x40b6
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x40b6
addq $0x2, %rdi
callq 0x3360
movl %eax, %r12d
jmp 0x40b6
addq $0x2, %rdi
callq 0x3280
movapd %xmm0, 0x30(%rsp)
jmp 0x40b6
movl $0x0, 0xc(%rsp)
incq %rbp
jmp 0x4029
testq %r14, %r14
je 0x435a
movq 0x18f02(%rip), %rdi     # 0x1cfd0
leaq 0xf156(%rip), %rsi      # 0x1322b
callq 0x3200
xorpd %xmm0, %xmm0
movsd (%rsp), %xmm1
maxsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
cmpl $0x1, %r12d
adcl $0x0, %r12d
cmpl $0x40, %r12d
pushq $0x40
popq %rbp
cmovbl %r12d, %ebp
testb $0x1, 0xc(%rsp)
jne 0x4119
movq 0x18ec3(%rip), %rdi     # 0x1cfd0
leaq 0xf120(%rip), %rsi      # 0x13234
callq 0x3200
movsd (%rsp), %xmm0
minsd 0xeee2(%rip), %xmm0     # 0x13008
movsd %xmm0, (%rsp)
testb $0x1, 0x10(%rsp)
movq 0x28(%rsp), %r13
je 0x4157
movq 0x18e92(%rip), %rdi     # 0x1cfd0
leaq 0xf0f9(%rip), %rsi      # 0x1323e
callq 0x3200
movq %rax, %rdi
movsd (%rsp), %xmm0
callq 0x3390
xorpd %xmm0, %xmm0
movapd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm3
cmpltsd %xmm0, %xmm3
movsd 0xee96(%rip), %xmm0     # 0x13008
movapd %xmm3, %xmm1
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm3
orpd %xmm1, %xmm3
ucomisd %xmm0, %xmm3
movapd %xmm3, 0x30(%rsp)
jne 0x4190
jnp 0x41b1
movq 0x18e39(%rip), %rdi     # 0x1cfd0
leaq 0xf0ad(%rip), %rsi      # 0x1324b
callq 0x3200
movq %rax, %rdi
movapd 0x30(%rsp), %xmm0
callq 0x3390
movq 0x18e18(%rip), %rdi     # 0x1cfd0
leaq 0xf099(%rip), %rsi      # 0x13258
callq 0x3200
movl %ebp, %esi
movq %rax, %rdi
callq 0x3160
testb $0x1, %r13b
je 0x41e7
movq 0x18df5(%rip), %rdi     # 0x1cfd0
leaq 0xf080(%rip), %rsi      # 0x13262
callq 0x3200
movq 0x18de2(%rip), %r15     # 0x1cfd0
movq %r15, %rdi
callq 0x3100
leaq 0xf06c(%rip), %rsi      # 0x13269
movq %r15, %rdi
callq 0x3200
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x3100
leaq 0xf056(%rip), %rsi      # 0x13275
movq %r15, %rdi
callq 0x3200
movq %rax, %rdi
movq %r14, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x3100
movq %r15, %rdi
callq 0x3100
leaq 0x4c8(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
movq %rbx, %rsi
callq 0x4944
leaq 0x1907f(%rip), %rdx     # 0x1d2e0
leaq 0x98(%rsp), %rdi
leaq 0x4c8(%rsp), %rsi
callq 0x4a30
leaq 0x4c8(%rsp), %rdi
callq 0x3478
leaq 0x50(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x190bc(%rip), %rsi     # 0x1d360
leaq 0x48(%rsp), %rdi
callq 0x10374
leaq 0x190d3(%rip), %rsi     # 0x1d388
leaq 0x48(%rsp), %rdi
callq 0x10374
leaq 0x48(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x105bc
leaq 0x190d8(%rip), %rsi     # 0x1d3b0
leaq 0x48(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x10368
leaq 0x190ec(%rip), %rsi     # 0x1d3d8
leaq 0x48(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x10368
leaq 0x19100(%rip), %rsi     # 0x1d400
leaq 0x48(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x10368
leaq 0xb8(%rsp), %rdi
pushq $0x4
popq %rdx
movq %r14, %rsi
callq 0x3430
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xd8(%rsp,%rax)
je 0x4366
movq 0x18cb3(%rip), %rdi     # 0x1cff0
leaq 0xef3b(%rip), %rsi      # 0x1327f
callq 0x3200
movq %rax, %rdi
callq 0x3100
pushq $0x3
popq %r15
jmp 0x4848
callq 0x3f1c
pushq $0x2
jmp 0x45a8
leaq 0xb8(%rsp), %rdi
callq 0x10d10
movq %rax, %r15
shrq $0x2, %r15
movl %r15d, %eax
movq %rax, 0x20(%rsp)
leaq (,%rax,4), %r14
movq %r14, %rdi
callq 0x3030
movq %rax, %r12
leaq 0xb8(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x3290
leaq 0xb8(%rsp), %rdi
callq 0x3090
movq %r15, 0x40(%rsp)
movl %r15d, %r14d
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x43e7
movl (%r12,%r15,4), %eax
cvtsi2sd %rax, %xmm0
mulsd 0x30(%rsp), %xmm0
callq 0x3070
cvttsd2si %xmm0, %rax
movl %eax, (%r12,%r15,4)
incq %r15
jmp 0x43c0
movq 0x18be2(%rip), %rdi     # 0x1cfd0
leaq 0xeea6(%rip), %rsi      # 0x1329b
callq 0x3200
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0x3160
movq %rax, %rdi
callq 0x3100
movq 0x18bba(%rip), %rdi     # 0x1cfd0
leaq 0xee8d(%rip), %rsi      # 0x132aa
callq 0x3200
movq %rax, %r14
leaq 0x18f5c(%rip), %rsi     # 0x1d388
leaq 0x48(%rsp), %rdi
callq 0x10362
movl %eax, %esi
movq %r14, %rdi
callq 0x3160
movq %rax, %rdi
callq 0x3100
movq 0x18b81(%rip), %rdi     # 0x1cfd0
callq 0x3100
callq 0x11014
movsd %xmm0, 0x30(%rsp)
pushq $0x5
popq %r15
testb $0x1, 0x10(%rsp)
je 0x44a7
pushq $0x30
popq %rdi
callq 0x3220
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
movq 0x40(%rsp), %rdx
callq 0xc47e
leaq 0x18efc(%rip), %rsi     # 0x1d388
leaq 0x48(%rsp), %rdi
callq 0x10362
movq %r14, %rdi
movl %eax, %esi
movsd (%rsp), %xmm0
callq 0xc582
jmp 0x44fe
pushq $0x58
popq %rdi
callq 0x3220
movq %rax, %r14
movzbl 0xc(%rsp), %ecx
andl $0x1, %ecx
movq %rax, %rdi
movq %r12, %rsi
movq 0x40(%rsp), %rdx
callq 0xb74e
leaq 0x18eb7(%rip), %rsi     # 0x1d388
leaq 0x48(%rsp), %rdi
callq 0x10362
movl %eax, %r12d
leaq 0x18f43(%rip), %rsi     # 0x1d428
leaq 0x48(%rsp), %rdi
callq 0x10362
movq %r14, %rdi
movl %r12d, %esi
movl %eax, %edx
movl %ebp, %ecx
callq 0xb9e4
testb %al, %al
je 0x4564
callq 0x11014
movsd %xmm0, (%rsp)
callq 0x11029
movl %eax, 0xc(%rsp)
testb $0x1, %r13b
je 0x45af
callq 0x11014
movsd %xmm0, 0x10(%rsp)
leaq 0x4c8(%rsp), %rdi
leaq 0x2c0(%rsp), %rdx
movq %rbx, %rsi
callq 0x4944
leaq 0x4c8(%rsp), %rsi
movq %r14, %rdi
callq 0xb610
leaq 0x4c8(%rsp), %rdi
callq 0x3478
jmp 0x471b
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
jmp 0x4848
movq 0x18a57(%rip), %rdi     # 0x1cfd0
leaq 0xec9a(%rip), %rsi      # 0x1321a
callq 0x3200
movq (%r15,%rbp,8), %rsi
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x3100
movq %rax, %rdi
callq 0x3100
callq 0x3f1c
pushq $0x1
popq %r15
jmp 0x486c
movl %ebp, %r13d
leaq 0x2c0(%rsp), %rdi
pushq $0x4
popq %rdx
movq %rbx, %rsi
callq 0x3430
movq 0x2c0(%rsp), %rax
movq -0x18(%rax), %rax
movl 0x2e0(%rsp,%rax), %ebp
testb $0x5, %bpl
je 0x461c
movq 0x18a0b(%rip), %rdi     # 0x1cff0
leaq 0xeccd(%rip), %rsi      # 0x132b9
callq 0x3200
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3200
leaq 0xf227(%rip), %rsi      # 0x1382a
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x3100
pushq $0x5
popq %r15
jmp 0x4704
movq 0x20(%rsp), %rdi
callq 0x3030
movq %rax, %r15
leaq 0x2c0(%rsp), %rdi
movq %rax, %rsi
movq 0x20(%rsp), %rdx
callq 0x3290
leaq 0x2c0(%rsp), %rdi
callq 0x3090
leaq 0x18d0e(%rip), %rsi     # 0x1d360
leaq 0x48(%rsp), %rdi
callq 0x10362
movl %eax, %r12d
leaq 0x18d22(%rip), %rsi     # 0x1d388
leaq 0x48(%rsp), %rdi
callq 0x10362
leaq 0x4c8(%rsp), %rdi
movq %r15, %rsi
movq 0x40(%rsp), %rdx
movl %r12d, %ecx
movl %eax, %r8d
movl %r13d, %r9d
pushq $0x1
pushq %r14
callq 0x5746
popq %rax
popq %rcx
callq 0x11014
movsd %xmm0, 0x10(%rsp)
cmpb $0x1, 0x4c8(%rsp)
jne 0x46f4
leaq 0x4c8(%rsp), %rdi
callq 0x7760
leaq 0x4c8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x759c
leaq 0x78(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
movq %rbx, %rsi
callq 0x4944
leaq 0x4c8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x5e8c
leaq 0x78(%rsp), %rdi
callq 0x3478
leaq 0x4c8(%rsp), %rdi
callq 0x5dd6
xorl %r15d, %r15d
leaq 0x2c0(%rsp), %rdi
callq 0x30a0
testb $0x5, %bpl
jne 0x4848
callq 0x11029
movl %eax, %ebx
movq 0x188a7(%rip), %rdi     # 0x1cfd0
leaq 0xeba3(%rip), %rsi      # 0x132d3
callq 0x3200
movsd 0x10(%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
movq %rax, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x3390
leaq 0xeb85(%rip), %rsi      # 0x132db
movq %rax, %rdi
callq 0x3200
cvtsi2sdq 0x20(%rsp), %xmm0
mulsd 0xe8a3(%rip), %xmm0     # 0x13010
movsd %xmm0, 0x28(%rsp)
divsd 0x10(%rsp), %xmm0
movq %rax, %rdi
callq 0x3390
leaq 0xeb5e(%rip), %rsi      # 0x132e6
movq %rax, %rdi
callq 0x3200
movl %ebx, %esi
movq %rax, %rdi
callq 0x3160
leaq 0xeb4e(%rip), %rsi      # 0x132ef
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x3100
movq 0x18818(%rip), %rdi     # 0x1cfd0
leaq 0xeb34(%rip), %rsi      # 0x132f3
callq 0x3200
movsd (%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
movq %rax, %rdi
movsd %xmm0, (%rsp)
callq 0x3390
leaq 0xeaf8(%rip), %rsi      # 0x132db
movq %rax, %rdi
callq 0x3200
movsd 0x28(%rsp), %xmm0
divsd (%rsp), %xmm0
movq %rax, %rdi
callq 0x3390
leaq 0xeae1(%rip), %rsi      # 0x132e6
movq %rax, %rdi
callq 0x3200
movl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x3160
leaq 0xeacf(%rip), %rsi      # 0x132ef
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x3100
movq 0x18799(%rip), %rdi     # 0x1cfd0
callq 0x3100
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
xorl %r15d, %r15d
leaq 0xb8(%rsp), %rdi
callq 0x30a0
leaq 0x48(%rsp), %rdi
callq 0x4a5a
leaq 0x98(%rsp), %rdi
callq 0x3478
movl %r15d, %eax
addq $0xcf8, %rsp            # imm = 0xCF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x3478
jmp 0x4895
jmp 0x4892
movq %rax, %rbx
leaq 0x4c8(%rsp), %rdi
callq 0x5dd6
jmp 0x48e0
movq %rax, %rbx
leaq 0x4c8(%rsp), %rdi
callq 0x3478
jmp 0x4912
jmp 0x490f
jmp 0x490f
jmp 0x490f
jmp 0x48dd
jmp 0x48dd
jmp 0x490f
jmp 0x490f
jmp 0x490f
movq %rax, %rbx
pushq $0x58
jmp 0x48d2
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r14, %rdi
callq 0x3230
jmp 0x4912
movq %rax, %rbx
leaq 0x2c0(%rsp), %rdi
callq 0x30a0
jmp 0x4912
jmp 0x490f
jmp 0x490f
jmp 0x490f
jmp 0x490f
jmp 0x4921
movq %rax, %rbx
leaq 0x4c8(%rsp), %rdi
jmp 0x4936
movq %rax, %rbx
jmp 0x493b
jmp 0x490f
jmp 0x490f
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x30a0
jmp 0x4924
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4a5a
leaq 0x98(%rsp), %rdi
callq 0x3478
movq %rbx, %rdi
callq 0x3410
nop