/** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof PairSet<?, ?>)) return false; final PairSet<?, ?> other = (PairSet<?, ?>) obj; return hasSameIndices(other) && matrix.equals(other.matrix); }
/** * Checks if the given transaction-item pair is contained within the set * * @param transaction the transaction of the pair * @param item the item of the pair * @return <code>true</code> if the given transaction-item pair is contained within the set */ public boolean contains(T transaction, I item) { int t = transactionToIndex(transaction); if (t < 0) return false; int i = itemToIndex(item); if (i < 0) return false; return matrix.contains(t, i); }
/** * Wraps a {@link BinaryMatrix} instance with a {@link PairSet} instance. * * <p><b>NOTE:</b> the maximum item and transaction IDs are those existing in the binary matrix * when the wrapping take place * * @param b a {@link BinaryMatrix} instance to wrap * @return a new {@link PairSet} instance, indexed by the given matrix */ public static PairSet<Integer, Integer> createFromBinaryMatrix(BinaryMatrix b) { // TODO this is a little bit costly since PairSet will allocate an array // and a HashMap of Integers to map elements of BinaryMatrix... // Think about a IntegerPairSet class or to an "fake" IntegerIndexedSet // just for this purpose. IntegerSet t = new IntegerSet(b.emptyRow()); t.intSet().add(b.maxRow() + 1); t.intSet().complement(); IntegerSet i = new IntegerSet(b.emptyRow()); i.intSet().add(b.maxCol() + 1); i.intSet().complement(); return new PairSet<Integer, Integer>(b, t, i); }
/** * Retains the pairs obtained from the Cartesian product of transactions and items * * @param trans collection of transactions * @param item the item * @return <code>true</code> if the set set has been changed */ public boolean retainAll(Collection<T> trans, I item) { if (isEmpty()) return false; if (trans == null || trans.isEmpty() || item == null) { clear(); return true; } return matrix.retainAll(allTransactions.convert(trans).indices(), itemToIndex(item)); }
/** * Retains the pairs obtained from the Cartesian product of transactions and items * * @param trans the transaction * @param items collection of items * @return <code>true</code> if the set set has been changed */ public boolean retainAll(T trans, Collection<I> items) { if (isEmpty()) return false; if (trans == null || items == null || items.isEmpty()) { clear(); return true; } return matrix.retainAll(transactionToIndex(trans), allItems.convert(items).indices()); }
/** {@inheritDoc} */ @Override public void fill(Pair<T, I> from, Pair<T, I> to) { matrix.fill( transactionToIndex(from.transaction), itemToIndex(from.item), transactionToIndex(to.transaction), itemToIndex(to.item)); }
/** {@inheritDoc} */ @Override public String debugInfo() { StringBuilder s = new StringBuilder(); s.append("possible transactions: "); s.append(allTransactions); s.append('\n'); s.append("possible items: "); s.append(allItems); s.append('\n'); s.append("pairs:\n"); s.append(matrix.toString()); s.append("info: " + matrix.debugInfo()); return s.toString(); }
/** * Initializes the set by specifying all possible transactions and items. * * @param matrix {@link BinaryMatrix} instance used to internally represent the matrix * @param transactions collection of <i>all</i> possible transactions. The specified order will be * preserved within when iterating over the {@link PairSet} instance. * @param items collection of <i>all</i> possible items. The specified order will be preserved * within each transaction {@link PairSet}. */ public PairSet(BinaryMatrix matrix, Collection<T> transactions, Collection<I> items) { if (transactions == null || items == null) throw new NullPointerException(); this.matrix = matrix; IntSet tmp = matrix.emptyRow(); if (transactions instanceof IndexedSet<?>) allTransactions = (IndexedSet<T>) transactions; else allTransactions = new IndexedSet<T>(tmp.empty(), transactions).universe(); // .unmodifiable(); if (items instanceof IndexedSet<?>) allItems = (IndexedSet<I>) items; else allItems = new IndexedSet<I>(tmp.empty(), items).universe(); // .unmodifiable(); }
/** * Creates a new {@link PairSet} instance with the union of all possible transactions and items as * result for {@link #allTransactions()} and {@link #allItems()}, respectively, and the union of * pairs. * * @param other the other {@link PairSet} instance to merge * @return the merged {@link PairSet} instance */ public PairSet<T, I> merged(PairSet<T, I> other) { if (other == null) return clone(); // compute the new universe Set<T> newAllTransactions = new LinkedHashSet<T>(allTransactions); Set<I> newAllItems = new LinkedHashSet<I>(allItems); newAllTransactions.addAll(other.allTransactions); newAllItems.addAll(other.allItems); // compute the union of pairs PairSet<T, I> res = new PairSet<T, I>(matrix.clone(), newAllTransactions, newAllItems); if (!other.isEmpty()) res.addAll(other); return res; }
/** * Creates a new {@link PairSet} instance with only non-empty transactions and items. * * @return the compacted {@link PairSet} instance */ public PairSet<T, I> compacted() { // trivial case if (isEmpty()) return empty(); // compute the new universe final Set<T> newAllTransactions = new LinkedHashSet<T>(involvedTransactions()); final Set<I> newAllItems = new LinkedHashSet<I>(involvedItems()); if (newAllTransactions.size() == allTransactions.size() && newAllItems.size() == allItems.size()) return clone(); // compute the union of pairs PairSet<T, I> res = new PairSet<T, I>(matrix.empty(), newAllTransactions, newAllItems); res.addAll(this); return res; }
/** {@inheritDoc} */ @Override public void flip(Pair<T, I> e) { matrix.flip(transactionToIndex(e.transaction), itemToIndex(e.item)); }
/** * Lists all transactions involved with a specified item * * @param item the given item * @return transactions involved with a specified item */ public IndexedSet<T> transactionsOf(I item) { IndexedSet<T> res = allTransactions.empty(); res.indices().addAll(matrix.getCol(itemToIndex(item))); return res; }
/** {@inheritDoc} */ @Override public boolean containsAtLeast(Collection<? extends Pair<T, I>> other, int minElements) { return other != null && !other.isEmpty() && matrix.containsAtLeast(convert(other).matrix, minElements); }
/** {@inheritDoc} */ @Override public boolean containsAny(Collection<? extends Pair<T, I>> other) { return other == null || matrix.containsAny(convert(other).matrix); }
/** {@inheritDoc} */ @Override public PairSet<T, I> complemented() { return createFromIndices(matrix.complemented()); }
/** {@inheritDoc} */ @Override public int complementSize() { return matrix.complementSize(); }
/** * Gets the set of items in {@link #allItems()} that are contained in at least one transaction * * @return the set of items in {@link #allItems()} that are contained in at least one transaction */ public IndexedSet<I> involvedItems() { IndexedSet<I> res = allItems.empty(); res.indices().addAll(matrix.involvedCols()); return res; }
/** {@inheritDoc} */ @Override public PairSet<T, I> empty() { return createFromIndices(matrix.empty()); }
/** {@inheritDoc} */ @Override public int unionSize(Collection<? extends Pair<T, I>> other) { return other == null ? (int) size() : (int) matrix.unionSize(convert(other).matrix); }
/** {@inheritDoc} */ @Override public int symmetricDifferenceSize(Collection<? extends Pair<T, I>> other) { return other == null ? (int) size() : (int) matrix.symmetricDifferenceSize(convert(other).matrix); }
/** * Provides position of element within the set. * * <p>It returns -1 if the element does not exist within the set. * * @param element element of the set * @return the element position */ @Override public int indexOf(Pair<T, I> element) { return matrix.indexOf(transactionToIndex(element.transaction), itemToIndex(element.item)); }
/** * Gets the <code>i</code><sup>th</sup> element of the set * * @param index position of the element in the sorted set * @return the <code>i</code><sup>th</sup> element of the set * @throws IndexOutOfBoundsException if <code>i</code> is less than zero, or greater or equal to * {@link #size()} */ @Override public Pair<T, I> get(int index) { return indexToPair(matrix.get(index)); }
/** {@inheritDoc} */ @Override public PairSet<T, I> intersection(Collection<? extends Pair<T, I>> c) { return c == null ? empty() : createFromIndices(matrix.intersection(convert(c).matrix)); }
/** {@inheritDoc} */ @Override public void complement() { matrix.complement(); }
/** {@inheritDoc} */ @Override public PairSet<T, I> symmetricDifference(Collection<? extends Pair<T, I>> other) { return other == null ? clone() : createFromIndices(matrix.symmetricDifference(convert(other).matrix)); }
/** * Gets the set of transactions in {@link #allTransactions()} that contains at least one item * * @return the set of transactions in {@link #allTransactions()} that contains at least one item */ public IndexedSet<T> involvedTransactions() { IndexedSet<T> res = allTransactions.empty(); res.indices().addAll(matrix.involvedRows()); return res; }
/** {@inheritDoc} */ @Override public PairSet<T, I> union(Collection<? extends Pair<T, I>> other) { return other == null ? clone() : createFromIndices(matrix.union(convert(other).matrix)); }
/** {@inheritDoc} */ @Override public double bitmapCompressionRatio() { return matrix.bitmapCompressionRatio(); }
/** {@inheritDoc} */ @Override public Pair<T, I> first() { return indexToPair(matrix.first()); }
/** {@inheritDoc} */ @Override public double collectionCompressionRatio() { return matrix.collectionCompressionRatio(); }