Exemplo n.º 1
0
 /**
  * Lists all items contained within a given transaction
  *
  * @param transaction the given transaction
  * @return items contained within the given transaction
  */
 public IndexedSet<I> itemsOf(T transaction) {
   IndexedSet<I> res = allItems.empty();
   res.indices().addAll(matrix.getRow(transactionToIndex(transaction)));
   return res;
 }
Exemplo n.º 2
0
  /**
   * Converts a generic collection of transaction-item pairs to a {@link PairSet} instance.
   *
   * @param matrix {@link IntSet} instance used to internally represent the set
   * @param pairs collection of {@link Pair} instances
   */
  public PairSet(BinaryMatrix matrix, Collection<? extends Pair<T, I>> pairs) {
    if (pairs == null) throw new RuntimeException("null pair set");
    if (pairs.isEmpty()) throw new RuntimeException("empty pair set");

    // identify all possible transactions and items and their frequencies
    final Map<T, Integer> ts = new HashMap<T, Integer>();
    final Map<I, Integer> is = new HashMap<I, Integer>();
    for (Pair<T, I> p : pairs) {
      Integer f;

      f = ts.get(p.transaction);
      f = f == null ? 1 : f + 1;
      ts.put(p.transaction, f);

      f = is.get(p.item);
      f = f == null ? 1 : f + 1;
      is.put(p.item, f);
    }

    // sort transactions and items by descending frequencies
    List<Pair<T, I>> sortedPairs = new ArrayList<Pair<T, I>>(pairs);
    Collections.sort(
        sortedPairs,
        new Comparator<Pair<T, I>>() {
          @Override
          public int compare(Pair<T, I> o1, Pair<T, I> o2) {
            int r = ts.get(o2.transaction).compareTo(ts.get(o1.transaction));
            if (r == 0) r = is.get(o2.item).compareTo(is.get(o1.item));
            return r;
          }
        });
    List<T> sortedTransactions = new ArrayList<T>(ts.keySet());
    Collections.sort(
        sortedTransactions,
        new Comparator<T>() {
          @Override
          public int compare(T o1, T o2) {
            return ts.get(o2).compareTo(ts.get(o1));
          }
        });
    List<I> sortedItems = new ArrayList<I>(is.keySet());
    Collections.sort(
        sortedItems,
        new Comparator<I>() {
          @Override
          public int compare(I o1, I o2) {
            return is.get(o2).compareTo(is.get(o1));
          }
        });

    // identify all transactions and items
    this.matrix = matrix;
    matrix.add(0, 0);
    allTransactions =
        new IndexedSet<T>(matrix.getRow(0), sortedTransactions).universe(); // .unmodifiable();
    allItems = new IndexedSet<I>(matrix.getRow(0), sortedItems).universe(); // .unmodifiable();
    matrix.clear();

    // create the matrix
    for (Pair<T, I> p : sortedPairs) add(p);
  }