/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.set;

import java.io.IOException;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.Random;
import net.ranides.assira.collection.arrays.ArrayUtils;
import net.ranides.assira.collection.sets.IdentSet;
import static net.ranides.benchmark.app.BenchmarkRunner.run;
import net.ranides.benchmark.app.BenchmarkName;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.runner.RunnerException;


public class IdentitySetBenchmark {
    
    static final String[] ARRAY = new String[1000];
    
    static {
        Random rand = new Random(777);
        for(int i=0; i<ARRAY.length; i++) {
            ARRAY[i] = Integer.toString(rand.nextInt());
        }
        ArrayUtils.shuffle(new Random(777),ARRAY);
    }
    
    @Benchmark
    @BenchmarkName("Put: assira1")
    public boolean Put_Assira1() {
        IdentitySet1<String> is = new IdentitySet1<>(1500);
        for(String s : ARRAY) {
            is.add(s);
        }
        return is.contains(ARRAY[1]) || is.contains(ARRAY[2]);
    }
    
    @Benchmark
    @BenchmarkName("Put: assira2")
    public boolean Put_Assira2() {
        IdentSet<String> is = new IdentSet<>(1500);
        for(String s : ARRAY) {
            is.add(s);
        }
        return is.contains(ARRAY[1]) || is.contains(ARRAY[2]);
    }
    
    @Benchmark
    @BenchmarkName("Put: JDK")
    public boolean Put_JDK() {
        IdentityHashMap<String,Boolean> is = new IdentityHashMap<>(1500);
        for(String s : ARRAY) {
            is.put(s, true);
        }
        return is.containsKey(ARRAY[1]) || is.containsKey(ARRAY[2]);
    }
    
    @Benchmark
    @BenchmarkName("Get: assira1")
    public int Get_Assira1() {
        IdentitySet1<String> is = new IdentitySet1<>(1500);
        is.addAll(Arrays.asList(ARRAY).subList(0, 500));
        
        int ret = 0;
        for(int i=0; i<1000; i++) {
            ret += is.contains(ARRAY[i]) ? 7 : 19;
        }
        
        return ret;
    }
    
    @Benchmark
    @BenchmarkName("Get: assira2")
    public int Get_Assira2() {
        IdentSet<String> is = new IdentSet<>(1500);
        is.addAll(Arrays.asList(ARRAY).subList(0, 500));
        
        int ret = 0;
        for(int i=0; i<1000; i++) {
            ret += is.contains(ARRAY[i]) ? 7 : 19;
        }
        
        return ret;
    }
    
    @Benchmark
    @BenchmarkName("Get: JDK")
    public int Get_JDK() {
        IdentityHashMap<String,Boolean> is = new IdentityHashMap<>(1500);
        for(int i=0; i<500; i++) {
            is.put(ARRAY[i], true);
        }
        
        int ret = 0;
        for(int i=0; i<1000; i++) {
            ret += is.containsKey(ARRAY[i]) ? 7 : 19;
        }
        
        return ret;
    }
    
    public static void main(String[] args) throws RunnerException, IOException {
        run(IdentitySetBenchmark.class);
    }
    
}
