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

com.github.dakusui.combinatoradix.Enumerator Maven / Gradle / Ivy

The newest version!
package com.github.dakusui.combinatoradix;

import com.github.dakusui.combinatoradix.utils.InternalUtils;
import com.github.dakusui.combinatoradix.utils.Predicates;

import java.util.List;
import java.util.NoSuchElementException;

import static com.github.dakusui.combinatoradix.utils.ExceptionFactory.Utils.indexOutOfBounds;
import static com.github.dakusui.combinatoradix.utils.ExceptionFactory.Utils.npe;
import static com.github.dakusui.combinatoradix.utils.InternalUtils.check;
import static com.github.dakusui.combinatoradix.utils.Predicates.inRange;

public interface Enumerator extends Iterable> {
  List get(long index);

  long size();

  long indexOf(List entry);

  class Iterator implements java.util.Iterator> {
    private final Enumerator enumerator;
    private       long          index;

    @SuppressWarnings("WeakerAccess")
    public Iterator(long offset, Enumerator enumerator) {
      this.enumerator = enumerator;
      this.index = offset;
    }

    @Override
    public boolean hasNext() {
      return this.index < enumerator.size();
    }

    @Override
    public List next() {
      if (!hasNext()) {
        String message = "No more element in this enumberator.";
        throw new NoSuchElementException(message);
      }
      return enumerator.get(this.index++);
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException("This operation is not supported.");
    }
  }

  abstract class Base implements Enumerator, Iterable> {

    private final long enumSize;

    protected final int k;

    protected final List symbols;

    /**
     * Creates an object of this class.
     *
     * @param symbols A list of elements from which returned value of {@code get(int)} will be chosen.
     * @param k       Number of elements chosen from {@code items}
     * @param size    Number of lists this object can return.
     */
    protected Base(List symbols, int k, long size) {
      this.symbols = check(
          symbols,
          Predicates.>notNull(),
          npe(),
          "'symbols' mustn't be null"
      );
      this.k = k;
      this.enumSize = size;
    }

    @Override
    public List get(long index) {
      return getElement(check(
          index,
          inRange(0L, enumSize),
          indexOutOfBounds(),
          "Index (%d) must be less than %d", index, this.enumSize
      ));
    }

    protected abstract List getElement(long index);

    @Override
    public long indexOf(List element) {
      InternalUtils.checkCondition(
          element.size() == k,
          "Size of element:%d is not valid (expected=%d)",
          element.size(),
          k
      );
      InternalUtils.checkCondition(
          symbols.containsAll(element),
          "Element %s contained invalid value(s): (valid values=%s)",
          element,
          symbols
      );
      return calculateIndexOf(element);
    }

    abstract protected long calculateIndexOf(List element);


    final public long size() {
      return this.enumSize;
    }

    @Override
    public java.util.Iterator> iterator() {
      return new Iterator(0, this);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy