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

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

There is a newer version: 3.4.0
Show newest version
/**
 * Combinatorics Library 3
 * Copyright 2009-2016 Dmytro Paukov [email protected]
 */
package org.paukov.combinatorics3;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class PermutationWithRepetitionIterator  implements Iterator> {

    final PermutationWithRepetitionGenerator generator;

    private List currentPermutation = null;
    private long currentIndex = 0;

    private final int originalVectorSize;
    private final int permutationLength;

    // Internal data
    private int[] bitVector = null;

    PermutationWithRepetitionIterator(
            PermutationWithRepetitionGenerator generator) {
        this.generator = generator;
        originalVectorSize = generator.originalVector.size();
        permutationLength = generator.permutationLength;

        List list = new ArrayList<>(permutationLength);
        T defaultValue = generator.originalVector.get(0);
        for (int i = 0; i < permutationLength; i++) {
            list.add(defaultValue);
        }

        currentPermutation = new ArrayList<>(list);

        bitVector = new int[permutationLength + 2];
        currentIndex = 0;
    }


    @Override
    public boolean hasNext() {
        return (bitVector[permutationLength] != 1);
    }


    @Override
    public List next() {
        currentIndex++;

        for (int j = permutationLength - 1; j >= 0; j--) {
            currentPermutation.set(j, generator.originalVector.get(bitVector[j]));
        }

        int i = 0;
        while (bitVector[i] == originalVectorSize - 1) {
            if (i < permutationLength + 1)
                bitVector[i] = 0;
            else {
                bitVector[permutationLength] = 1;
                return new ArrayList<>(currentPermutation);
            }
            i++;
        }

        bitVector[i]++;
        return new ArrayList<>(currentPermutation);

    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }


    @Override
    public String toString() {
        return "PermutationWithRepetitionIterator=[#" + currentIndex + ", " + currentPermutation + "]";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy