/*
 * HPPC
 *
 * Copyright (C) 2010-2020 Carrot Search s.c.
 * All rights reserved.
 *
 * Refer to the full license file "LICENSE.txt":
 * https://github.com/carrotsearch/hppc/blob/master/LICENSE.txt
 */
package com.carrotsearch.hppc.benchmarks;

import com.carrotsearch.hppc.XorShift128P;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

// To normalize the measure, run B009_Random to know how much time XorShift128P.nextInt() takes.

@Fork(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@State(Scope.Benchmark)
public class B005_HashSet_Add_Random {
  @Param("0.75")
  public double loadFactor;

  @Param public Library library;

  @Param({"3", "6", "12", "24"})
  public int mbOfKeys;

  public int keyCount;
  public IntIntMapOps[] ops;
  public XorShift128P rnd;
  public int randomRange;

  @Setup(Level.Trial)
  public void prepare() {
    keyCount = mbOfKeys * (1024 * 1024) / 8;
    ops = new IntIntMapOps[10];
    for (int i = 0; i < ops.length; i++) {
      ops[i] = library.newIntIntMap(keyCount, loadFactor);
    }
    rnd = new XorShift128P(0xdeadbeefL);
    randomRange = 2 * keyCount;
    if (Integer.bitCount(randomRange) == 1) {
      // Avoid power-of-2 because XorShift128P.nextInt() is twice faster in this case and that
      // perturbs the measure.
      randomRange++;
    }
  }

  @Benchmark()
  @BenchmarkMode(Mode.SingleShotTime)
  public Object put() {
    final XorShift128P rnd = this.rnd;
    final int randomRange = this.randomRange;
    for (final IntIntMapOps ops : ops) {
      for (int i = 0, numLoops = keyCount; i < numLoops; i++) {
        ops.put(rnd.nextInt(randomRange), i);
      }
    }
    return ops;
  }

  public static void main(String[] args) throws RunnerException {
    Options opt =
        new OptionsBuilder().include(B005_HashSet_Add_Random.class.getSimpleName()).build();
    new Runner(opt).run();
  }
}

@@ -899,30 +899,27 @@
   public Int
-IntMap
+Set
 Ops%5B%5D ops;%0A 
@@ -1081,22 +1081,19 @@
  new Int
-IntMap
+Set
 Ops%5B10%5D;
@@ -1165,22 +1165,19 @@
 y.newInt
-IntMap
+Set
 (keyCoun
@@ -1544,19 +1544,19 @@
  Object 
-put
+add
 () %7B%0A   
@@ -1659,14 +1659,11 @@
  Int
-IntMap
+Set
 Ops 
@@ -1755,11 +1755,11 @@
 ops.
-put
+add
 (rnd
@@ -1783,11 +1783,8 @@
 nge)
-, i
 );%0A 

Fix B005_HashSet_Add_Random to properly use IntSetOps.