/*
 * ARX: Efficient, Stable and Optimal Data Anonymization
 * Copyright (C) 2012 - 2013 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.criteria;

import org.deidentifier.arx.framework.check.groupify.HashGroupifyEntry;
import org.deidentifier.arx.framework.data.DataManager;

/**
 * The t-closeness criterion with equal-distance EMD
 * @author Prasser, Kohlmayer
 */
public class EqualDistanceTCloseness extends TCloseness {

    private static final long serialVersionUID = -1383357036299011323L;

    /** The original distribution*/
    private double[]          distribution;
    
    /** The original number of values*/
    private double            total;

    /**
     * Creates a new instance
     * @param t
     */
    public EqualDistanceTCloseness(double t) {
        super(t);
    }

    @Override
    public void initialize(DataManager manager) {
        distribution = manager.getDistribution();
        total = manager.getDataQI().getDataLength();
    }

    @Override
    public boolean isAnonymous(HashGroupifyEntry entry) {

        // Calculate EMD with equal distance
        int[] buckets = entry.distribution.getBuckets();
        int totalElements = 0;
        for (int i = 0; i < buckets.length; i += 2) {
            if (buckets[i] != -1) { // bucket not empty
                totalElements += buckets[i + 1];
            }
        }

        double val = total;
        for (int i = 0; i < buckets.length; i += 2) {
            if (buckets[i] != -1) { // bucket not empty
                double frequency = distribution[buckets[i]];
                // TODO: Can this be simplified (only one appearance of frequency)
                val += Math.abs((frequency - ((double) buckets[i + 1] / (double) totalElements))) - frequency;
            }
        }
        val /= 2;

        // check
        return val <= t;
    }
}

@@ -1252,86 +1252,8 @@
    %0A
-    /** The original number of values*/%0A    private double            total;%0A%0A
     
@@ -1500,61 +1500,8 @@
 ();%0A
-        total = manager.getDataQI().getDataLength();%0A
     
@@ -1914,21 +1914,20 @@
 e val = 
-total
+1.0d
 ;%0A      

Fix the bugfix in computation of equal-distance-EMD