<commit_before>/*
 * ARX: Powerful Data Anonymization
 * Copyright 2012 - 2016 <NAME>, <NAME> and contributors
 * 
 * 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.groupify.HashGroupifyEntry;
import org.deidentifier.arx.framework.data.DataManager;

import com.carrotsearch.hppc.IntDoubleOpenHashMap;

/**
 * The t-closeness criterion for ordered attributes.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class OrderedDistanceTCloseness extends TCloseness {

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

    /** The original distribution. */
    private double[]          distribution;

    /** The order of the elements. */
    private int[]             order;
    
    /**
     * Creates a new instance of the t-closeness criterion for ordered attributes as proposed in:
     * <NAME>, <NAME>, <NAME>.
     * t-Closeness: Privacy beyond k-anonymity and l-diversity.
     * 23rd International Conference on Data Engineering. 2007:106-115.
     *
     * @param attribute
     * @param t
     */
    public OrderedDistanceTCloseness(String attribute, double t) {
        super(attribute, t);
    }

    @Override
    public OrderedDistanceTCloseness clone() {
        return new OrderedDistanceTCloseness(this.getAttribute(), this.getT());
    }
    
    @Override
    public void initialize(DataManager manager) {
        super.initialize(manager);
        this.distribution = manager.getDistribution(attribute);
        this.order = manager.getOrder(attribute);
    }

    @Override
    public boolean isAnonymous(HashGroupifyEntry entry) {

        // Init
        int[] buckets = entry.distributions[index].getBuckets();
        double count = entry.count;
        
        // Prepare
        IntDoubleOpenHashMap map = new IntDoubleOpenHashMap(buckets.length/2);
        for (int i = 0; i < buckets.length; i += 2) {
            if (buckets[i] != -1) { // bucket not empty
                int value = buckets[i];
                double frequency = ((double) buckets[i + 1] / count);
                map.put(value, frequency);
            }
        }
        double threshold = t * (order.length - 1d);
        double distance = 0d;
        double sum_i = 0d;
        
        // Calculate and check
        for (int i=0; i<order.length; i++) {
            
            // Compute summands and distance
            int value = order[i];
            sum_i += (map.getOrDefault(value, 0d) - distribution[value]);
            distance += Math.abs(sum_i);
            
            // Early abort
            if (distance > threshold) {
                return false;
            }
        }
        
        // Yes
        return true;
    }

	@Override
    public boolean isLocalRecodingSupported() {
        return true;
    }

    @Override
	public String toString() {
		return t+"-closeness with ordered distance for attribute '"+attribute+"'";
	}
}<commit_msg>Code formatting<commit_after>/*
 * ARX: Powerful Data Anonymization
 * Copyright 2012 - 2016 <NAME>, <NAME> and contributors
 * 
 * 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.groupify.HashGroupifyEntry;
import org.deidentifier.arx.framework.data.DataManager;

import com.carrotsearch.hppc.IntDoubleOpenHashMap;

/**
 * The t-closeness criterion for ordered attributes.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class OrderedDistanceTCloseness extends TCloseness {

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

    /** The original distribution. */
    private double[]          distribution;

    /** The order of the elements. */
    private int[]             order;
    
    /**
     * Creates a new instance of the t-closeness criterion for ordered attributes as proposed in:
     * <NAME>, <NAME>, <NAME>.
     * t-Closeness: Privacy beyond k-anonymity and l-diversity.
     * 23rd International Conference on Data Engineering. 2007:106-115.
     *
     * @param attribute
     * @param t
     */
    public OrderedDistanceTCloseness(String attribute, double t) {
        super(attribute, t);
    }

    @Override
    public OrderedDistanceTCloseness clone() {
        return new OrderedDistanceTCloseness(this.getAttribute(), this.getT());
    }
    
    @Override
    public void initialize(DataManager manager) {
        super.initialize(manager);
        this.distribution = manager.getDistribution(attribute);
        this.order = manager.getOrder(attribute);
    }

    @Override
    public boolean isAnonymous(HashGroupifyEntry entry) {

        // Init
        int[] buckets = entry.distributions[index].getBuckets();
        double count = entry.count;
        
        // Prepare
        IntDoubleOpenHashMap map = new IntDoubleOpenHashMap(buckets.length/2);
        for (int i = 0; i < buckets.length; i += 2) {
            if (buckets[i] != -1) { // bucket not empty
                int value = buckets[i];
                double frequency = ((double) buckets[i + 1] / count);
                map.put(value, frequency);
            }
        }
        double threshold = t * (order.length - 1d);
        double distance = 0d;
        double sum_i = 0d;
        
        // Calculate and check
        for (int i=0; i<order.length; i++) {
            
            // Compute summands and distance
            int value = order[i];
            sum_i += (map.getOrDefault(value, 0d) - distribution[value]);
            distance += Math.abs(sum_i);
            
            // Early abort
            if (distance > threshold) {
                return false;
            }
        }
        
        // Yes
        return true;
    }

    @Override
    public boolean isLocalRecodingSupported() {
        return true;
    }

    @Override
    public String toString() {
        return t+"-closeness with ordered distance for attribute '"+attribute+"'";
    }
}
{'fraction_non_alphanumeric': 0.09879003558718862, 'fraction_numerical': 0.015088967971530249, 'mean_word_length': 2.272473218444341, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3403870', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1884, 'n_words': 591}