/*
 * ARX: Powerful Data Anonymization
 * Copyright 2012 - 2015 Florian Kohlmayer, Fabian Prasser
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.deidentifier.arx.criteria;

import org.deidentifier.arx.framework.check.distribution.Distribution;
import org.deidentifier.arx.framework.check.groupify.HashGroupifyEntry;

/**
 * The entropy l-diversity criterion.
 *
 * @author Fabian Prasser
 * @author Florian Kohlmayer
 */
public class EntropyLDiversity extends LDiversity {

    /**  SVUID */
    private static final long   serialVersionUID = -354688551915634000L;

    /** Helper. */
    private final double        logL;

    /**
     * Creates a new instance of the entropy l-diversity criterion as proposed in:
     * Machanavajjhala A, Kifer D, Gehrke J.
     * l-diversity: Privacy beyond k-anonymity.
     * Transactions on Knowledge Discovery from Data (TKDD). 2007;1(1):3.
     *
     * @param attribute
     * @param l
     */
    public EntropyLDiversity(String attribute, double l){
        super(attribute, l, false, true);
        logL = Math.log(l);
    }

    @Override
    public boolean isAnonymous(HashGroupifyEntry entry) {

        Distribution d = entry.distributions[index];

        // If less than l values are present skip
        if (d.size() < minSize) { return false; }

        // Sum of the frequencies in distribution (=number of elements)
        int total = entry.count;
        double sum1 = 0d;

        final int[] buckets = d.getBuckets();
        for (int i = 0; i < buckets.length; i += 2) {
            if (buckets[i] != -1) { // bucket not empty
                final double frequency = buckets[i + 1];
                sum1 += frequency * Math.log(frequency);
            }
        }

        final double val = Math.log(total) - (sum1 / total);

        // check
        return val >= logL;
    }

	@Override
	public String toString() {
		return "entropy-"+l+"-diversity for attribute '"+attribute+"'";
	}

    @Override
    public EntropyLDiversity clone() {
        return new EntropyLDiversity(this.getAttribute(), this.getL());
    }
}

@@ -1090,66 +1090,8 @@
 L;%0A%0A
-    /** Helper. */%0A    private final double        logL;%0A%0A
     
@@ -1503,36 +1503,8 @@
 e);%0A
-        logL = Math.log(l);%0A
     
@@ -1814,16 +1814,22 @@
         
+final 
 int tota
@@ -1845,16 +1845,127 @@
 .count;%0A
+        // Sum must stay smaller than this constant term%0A        final double C = total * Math.log(total / l);%0A
         
@@ -2269,115 +2269,203 @@
     
-%7D%0A
     
-    %7D%0A%0A        final double val = Math.log(total) - (sum1 / total);%0A%0A        // check%0A       
+// If the sum grows over C, we can abort the loop earlier.%0A                if (C %3C sum1) %7B return false; %7D%0A            %7D%0A        %7D%0A%0A        // If we reach this point, the loop did not
  return 
 val 
@@ -2464,19 +2464,34 @@
 urn 
-val %3E= logL
+false.%0A        return true
 ;%0A  

Rewrite inequality so that loop can be aborted earlier. Makes helper variable logL obsolete.