<commit_before>package com.kevlindev.karnaugh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Partitions implements Iterable<MinTermList> {
	public static final Comparator<String> IMPLICANT_COMPARATOR = new Comparator<String>() {
		@Override
		public int compare(String arg0, String arg1) {
			int result = 0;

			for (int i = 0; i < arg0.length(); i++) {
				char c1 = arg0.charAt(i);
				char c2 = arg1.charAt(i);

				if (c1 != c2) {
					// do nothing
					if (c1 == '-') {
						result = 1;
					} else if (c2 == '-') {
						result = -1;
					} else if (c1 == '0') {
						result = -1;
					} else if (c2 == '1') {
						result = 1;
					}
					break;
				}
			}

			return result;
		}
	};

	private List<MinTermList> partitions = new ArrayList<MinTermList>();

	public void add(MinTermList terms) {
		partitions.add(terms);
	}

	public Set<MinTerm> flatten() {
		Set<MinTerm> result = new HashSet<MinTerm>();

		for (MinTermList partition : partitions) {
			result.addAll(partition.getMinTerms());
		}

		return result;
	}

	public MinTermList get(int index) {
		return partitions.get(index);
	}

	// TODO: bad name here
	public List<String> getPrimeImplicants() {
		Set<String> primeImplicantSet = new HashSet<String>();

		for (MinTermList partition : partitions) {
			for (MinTerm term : partition) {
				primeImplicantSet.add(Util.join("", term.getBits()));
			}
		}

		ArrayList<String> primeImplicants = new ArrayList<String>(primeImplicantSet);
		Collections.sort(primeImplicants, IMPLICANT_COMPARATOR);

		return primeImplicants;
	}

	@Override
	public Iterator<MinTermList> iterator() {
		return partitions.iterator();
	}

	public int size() {
		return partitions.size();
	}

	public void sort() {
		Collections.sort(partitions, new Comparator<MinTermList>() {
			@Override
			public int compare(MinTermList arg0, MinTermList arg1) {
				int result = arg0.size() - arg1.size();

				if (result == 0) {
					int indexCount0 = 0;
					int indexCount1 = 0;

					for (MinTerm term : arg0) {
						indexCount0 += term.getIndexes().size();
					}

					for (MinTerm term : arg1) {
						indexCount1 += term.getIndexes().size();
					}

					result = indexCount0 - indexCount1;
				}

				return result;
			}
		});
	}
	
	public String toString() {
		StringBuilder buffer = new StringBuilder();

		for (int i = 0; i < partitions.size(); i++) {
			MinTermList partition = partitions.get(i);
			List<String> v = new ArrayList<String>();

			for (MinTerm minterm : partition) {
				String indexes = "[" + minterm.toString() + "]";

				v.add(indexes);
			}

			buffer.append(i).append(" : ");
			buffer.append(Util.join(", ", v));<commit_msg>Remove now-unused comparator and now-unused code. Sort members. Add equals/hashCode implementations.<commit_after>package com.kevlindev.karnaugh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Partitions implements Iterable<MinTermList> {
	private List<MinTermList> partitions = new ArrayList<MinTermList>();

	/**
	 * Add a new MinTermList to this collection
	 * 
	 * @param terms
	 */
	public void add(MinTermList terms) {
		partitions.add(terms);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		boolean match = false;

		if (obj instanceof Partitions) {
			Partitions other = (Partitions) obj;

			if (size() == other.size()) {
				match = true;

				for (int i = 0; i < size(); i++) {
					if (partitions.get(i).equals(other.partitions.get(i)) == false) {
						match = false;
						break;
					}
				}
			}
		}

		return match;
	}

	/**
	 * Create a unique set of all MinTerms contained within this collection
	 * 
	 * @return
	 */
	public Set<MinTerm> flatten() {
		Set<MinTerm> result = new HashSet<MinTerm>();

		for (MinTermList partition : partitions) {
			result.addAll(partition.getMinTerms());
		}

		return result;
	}

	/**
	 * Return the MinTermList at the specified index
	 * 
	 * @param index
	 * @return
	 */
	public MinTermList get(int index) {
		return partitions.get(index);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return partitions.hashCode();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Iterable#iterator()
	 */
	@Override
	public Iterator<MinTermList> iterator() {
		return partitions.iterator();
	}

	/**
	 * Return the number of MinTermLists in this collection
	 * 
	 * @return
	 */
	public int size() {
		return partitions.size();
	}

	/**
	 * Sort the list of MinTermLists, shortest first, then by fewest states
	 */
	public void sort() {
		Collections.sort(partitions, new Comparator<MinTermList>() {
			@Override
			public int compare(MinTermList arg0, MinTermList arg1) {
				int result = arg0.size() - arg1.size();

				if (result == 0) {
					int indexCount0 = 0;
					int indexCount1 = 0;

					for (MinTerm term : arg0) {
						indexCount0 += term.getIndexes().size();
					}

					for (MinTerm term : arg1) {
						indexCount1 += term.getIndexes().size();
					}

					result = indexCount0 - indexCount1;
				}

				return result;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuilder buffer = new StringBuilder();

		for (int i = 0; i < partitions.size(); i++) {
			MinTermList partition = partitions.get(i);
			List<String> v = new ArrayList<String>();

			for (MinTerm minterm : partition) {
				String indexes = "[" + minterm.toString() + "]";

				v.add(indexes);
			}

			buffer.append(i).append(" : ");
			buffer.append(Util.join(", ", v));
{'fraction_non_alphanumeric': 0.1412285179513357, 'fraction_numerical': 0.009188361408882083, 'mean_word_length': 3.0904662491301322, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4658217', 'n_tokens_mistral': 2350, 'n_tokens_neox': 1924, 'n_words': 476}