<commit_before>/*
 * ARX: Powerful Data Anonymization
 * Copyright 2012 - 2015 <NAME>, <NAME>
 * 
 * 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.framework.check.groupify;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.deidentifier.arx.risk.RiskModelEquivalenceClasses;

import com.carrotsearch.hppc.IntIntOpenHashMap;

/**
 * A distribution of equivalence classes
 * @author <NAME>
 */
public class HashGroupifyDistribution {

    /** The backing map */
    private IntIntOpenHashMap   distribution = new IntIntOpenHashMap();
    /** The number of suppressed tuples */
    private int                 suppressed   = 0;
    /** Entries that can be suppressed */
    private HashGroupifyEntry[] entries;
    /** Offset into the array of entries that can be suppressed */
    private int                 offset       = 0;
    /** Number of tuples in the data set */
    private double              numTuples;
    /** Number of classes in the data set */
    private double              numClasses   = 0;

    /**
     * Creates a new instance
     * @param entry
     */
    HashGroupifyDistribution(HashGroupifyEntry entry) {
        
        // Initialize
        List<HashGroupifyEntry> list = new ArrayList<HashGroupifyEntry>();
        while(entry != null) {
            if (entry.isNotOutlier) {
                this.numClasses++;
                addToDistribution(entry.count);
                list.add(entry);
            } else {
                this.suppressed += entry.count;
            }
            numTuples += entry.count;
            entry = entry.nextOrdered;
        }
        
        // Suppressed tuples form one equivalence class
        if (suppressed != 0) {
            this.distribution.putOrAdd(suppressed, 1, 1);
            this.numClasses++;
        }
        
        // Sort & store suppressible entries
        Collections.sort(list, new Comparator<HashGroupifyEntry>(){
            public int compare(HashGroupifyEntry o1, HashGroupifyEntry o2) {
                return Integer.compare(o1.representant, o2.representant);
            }
        });
        this.entries = list.toArray(new HashGroupifyEntry[list.size()]);<commit_msg>Bugfixes: (1) exclude groups that are only contained in the public table, and, (2) order groups by size and representant<commit_after>/*
 * ARX: Powerful Data Anonymization
 * Copyright 2012 - 2015 <NAME>, <NAME>
 * 
 * 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.framework.check.groupify;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.deidentifier.arx.risk.RiskModelEquivalenceClasses;

import com.carrotsearch.hppc.IntIntOpenHashMap;

/**
 * A distribution of equivalence classes
 * @author <NAME>
 */
public class HashGroupifyDistribution {

    /** The backing map */
    private IntIntOpenHashMap   distribution = new IntIntOpenHashMap();
    /** The number of suppressed tuples */
    private int                 suppressed   = 0;
    /** Entries that can be suppressed */
    private HashGroupifyEntry[] entries;
    /** Offset into the array of entries that can be suppressed */
    private int                 offset       = 0;
    /** Number of tuples in the data set */
    private double              numTuples;
    /** Number of classes in the data set */
    private double              numClasses   = 0;

    /**
     * Creates a new instance
     * @param entry
     */
    HashGroupifyDistribution(HashGroupifyEntry entry) {
        
        // Initialize
        List<HashGroupifyEntry> list = new ArrayList<HashGroupifyEntry>();
        while(entry != null) {
            if (entry.isNotOutlier && entry.count > 0) {
                this.numClasses++;
                addToDistribution(entry.count);
                list.add(entry);
            } else {
                this.suppressed += entry.count;
            }
            numTuples += entry.count;
            entry = entry.nextOrdered;
        }
        
        // Suppressed tuples form one equivalence class
        if (suppressed != 0) {
            this.distribution.putOrAdd(suppressed, 1, 1);
            this.numClasses++;
        }
        
        // Sort & store suppressible entries
        Collections.sort(list, new Comparator<HashGroupifyEntry>(){
            public int compare(HashGroupifyEntry o1, HashGroupifyEntry o2) {
                int cmp = Integer.compare(o1.count, o2.count);
                return cmp != 0 ? cmp : Integer.compare(o1.representant, o2.representant);
            }
        });
        this.entries = list.toArray(new HashGroupifyEntry[list.size()]);
{'fraction_non_alphanumeric': 0.09004163775156142, 'fraction_numerical': 0.008674531575294934, 'mean_word_length': 2.3189407023603916, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, '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': '6893197', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1475, 'n_words': 489}