/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 Florian Kohlmayer, Fabian Prasser
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.framework.data;

import java.util.HashMap;

/**
 * A dictionary mapping integers to strings for different dimensions.
 * 
 * @author Fabian Prasser
 * @author Florian Kohlmayer
 */
public class Dictionary {

    /** The resulting array mapping dimension->integer->string. */
    private final String[][]           mapping;

    /** Map used when building the dictionary. */
    private HashMap<String, Integer>[] maps;

    /**
     * Instantiates a new dictionary.
     * 
     * @param dimensions
     *            the dimensions
     */
    @SuppressWarnings("unchecked")
    public Dictionary(final int dimensions) {
        maps = new HashMap[dimensions];
        mapping = new String[dimensions][];
        for (int i = 0; i < dimensions; i++) {
            maps[i] = new HashMap<String, Integer>(10000);
        }
    }

    /**
     * Finalizes all dimensions. @see finalize()
     */
    public void finalizeAll() {
        for (int i = 0; i < maps.length; i++) {
            mapping[i] = new String[maps[i].size()];
            for (final String val : maps[i].keySet()) {
                mapping[i][maps[i].get(val)] = val;
            }
        }
        maps = null;
    }

    /**
     * Returns the mapping array.
     *
     * @return
     */
    public String[][] getMapping() {
        return mapping;
    }

    /**
     * Returns the number of dimensions in the dictionary.
     *
     * @return
     */
    public int getNumDimensions() {
        return mapping.length;
    }

    /**
     * Returns the number of unique values contained before finalizing the
     * dictionary.
     *
     * @param dimension
     * @return
     */
    public int getNumUniqueUnfinalizedValues(final int dimension) {
        return maps[dimension].size();
    }

    /**
     * Returns the registered value if present, null otherwise.
     *
     * @param dimension
     * @param string
     * @return
     */
    public Integer probe(final int dimension, final String string) {
        return maps[dimension].get(string);
    }

    /**
     * Registers a new string at the dictionary.
     * 
     * @param dimension
     *            the dimension
     * @param string
     *            the string
     * @return the int
     */
    public int register(final int dimension, final String string) {
        final Integer current = maps[dimension].get(string);
        if (current != null) { return current; }
        final int idx = maps[dimension].size();
        maps[dimension].put(string, idx);
        return idx;
    }

    /**
     * Merges this dictionary with another dictionary.
     *
     * @param targetDimension
     * @param dictionary
     * @param sourceDimension
     */
    public void registerAll(final int targetDimension,
                            final Dictionary dictionary,
                            final int sourceDimension) {
        final String[] vals = dictionary.mapping[sourceDimension];
        for (int id = 0; id < vals.length; id++) {
            maps[targetDimension].put(vals[id], id);
        }
    }
}

@@ -807,18 +807,43 @@
 ort 
-java.util.
+com.carrotsearch.hppc.ObjectIntOpen
 Hash
@@ -1114,24 +1114,28 @@
 %5D           
+    
 mapping;%0A%0A  
@@ -1194,16 +1194,29 @@
 private 
+ObjectIntOpen
 HashMap%3C
@@ -1221,25 +1221,16 @@
 p%3CString
-, Integer
 %3E%5B%5D maps
@@ -1448,24 +1448,37 @@
  maps = new 
+ObjectIntOpen
 HashMap%5Bdime
@@ -1603,16 +1603,29 @@
 %5D = new 
+ObjectIntOpen
 HashMap%3C
@@ -1634,24 +1634,10 @@
 ring
-, Integer%3E(10000
+%3E(
 );%0A 
@@ -1867,102 +1867,322 @@
    f
-or (final String val : maps%5Bi%5D.keySet()) %7B%0A                mapping%5Bi%5D%5Bmaps%5Bi%5D.get(val)%5D = val;
+inal Object%5B%5D keys = maps%5Bi%5D.keys;%0A            final int%5B%5D values = maps%5Bi%5D.values;%0A            final boolean%5B%5D allocated = maps%5Bi%5D.allocated;%0A            for (int j = 0; j %3C allocated.length; j++) %7B%0A                if (allocated%5Bj%5D) %7B%0A                    mapping%5Bi%5D%5Bvalues%5Bj%5D%5D = (String)keys%5Bj%5D;%0A                %7D
 %0A   
@@ -2184,32 +2184,33 @@
 %7D%0A            %7D%0A
+%0A
         %7D%0A      
@@ -3361,219 +3361,282 @@
     
-final Integer current = maps%5Bdimension%5D.get(string);%0A        if (current != null) %7B return current; %7D%0A        final int idx = maps%5Bdimension%5D.size();%0A        maps%5Bdimension%5D.put(string, idx);%0A        return idx;
+%0A        // Prepare%0A        ObjectIntOpenHashMap%3CString%3E map = maps%5Bdimension%5D;%0A        int size = map.size();%0A        %0A        // Return or store%0A        if (map.putIfAbsent(string, size)) %7B%0A            return size;%0A        %7D else %7B%0A            return map.lget();%0A        %7D
 %0A   

Improve performance of dictionary