All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.paukov.combinatorics3.PermutationGenerator Maven / Gradle / Ivy

Go to download

Simple java library to generate permutations, combinations and other combinatorial sequences

The newest version!
/*
 * Combinatorics Library 3
 * Copyright 2009-2016 Dmytro Paukov [email protected]
 */
package org.paukov.combinatorics3;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.paukov.combinatorics3.EmptyGenerator.emptyGenerator;

/**
 * A generator for iterating over the permutations.
 * @param  Type of the elements in the permutations.
 */
public class PermutationGenerator {

  final Collection originalVector;

  PermutationGenerator(Collection originalVector) {
    this.originalVector = originalVector;
  }

  public static  boolean hasDuplicates(Collection collection) {
    if (collection.size() <= 1) {
      return false;
    }
    Set set = new HashSet<>(collection);
    return set.size() < collection.size();
  }

  public IGenerator> simple() {
    return new SimplePermutationGenerator<>(originalVector, false);
  }

  public IGenerator> simple(TreatDuplicatesAs treatAsIdentical) {
    return new SimplePermutationGenerator<>(originalVector,
        TreatDuplicatesAs.IDENTICAL.equals(treatAsIdentical));
  }

  public IGenerator> k(int length) {
    return new KPermutationGenerator<>(originalVector, length, false);
  }

  public IGenerator> k(int length, TreatDuplicatesAs treatAsIdentical) {
    return new KPermutationGenerator<>(originalVector, length,
        TreatDuplicatesAs.IDENTICAL.equals(treatAsIdentical));
  }

  public IGenerator> withRepetitions(int permutationLength) {
    return originalVector.isEmpty()
        ? emptyGenerator()
        : new PermutationWithRepetitionGenerator<>(originalVector, permutationLength);
  }

  public enum TreatDuplicatesAs {
    DIFFERENT,
    IDENTICAL
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy