PermutationsIterator.java

  1. /*
  2.  * Licensed to the Hipparchus project under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.hipparchus.util;

  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.NoSuchElementException;

  22. /** Iterator for generating permutations.
  23.  * <p>
  24.  * This class implements the Steinhaus–Johnson–Trotter algorithm
  25.  * with Even's speedup
  26.  * <a href="https://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm">Steinhaus–Johnson–Trotter algorithm</a>
  27.  * </p>
  28.  * @param <T> type of the elements
  29.  * @since 2.2
  30.  */
  31. class PermutationsIterator<T> implements Iterator<List<T>> {

  32.     /** Current permuted list. */
  33.     private final List<T> permuted;

  34.     /** Value markers. */
  35.     private final int[] value;

  36.     /** Directions markers. */
  37.     private final int[] direction;

  38.     /** Indicator for exhausted partitions. */
  39.     private boolean exhausted;

  40.     /** Simple constructor.
  41.      * @param list list to permute (will not be touched)
  42.      */
  43.     PermutationsIterator(final List<T> list) {

  44.         this.permuted  = new ArrayList<>(list);

  45.         this.value     = new int[list.size()];
  46.         this.direction = new int[list.size()];
  47.         for (int i = 0; i < value.length; ++i) {
  48.             value[i]     = i;
  49.             direction[i] = i == 0 ? 0 : -1;
  50.         }

  51.         exhausted = false;

  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public boolean hasNext() {
  56.         return !exhausted;
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public List<T> next() {

  61.         if (exhausted) {
  62.             throw new NoSuchElementException();
  63.         }

  64.         // the value that will be returned at the end
  65.         final List<T> current = new ArrayList<>(permuted);

  66.         // select element to swap for next permutation
  67.         int selectedIndex     = -1;
  68.         int selectedValue     = -1;
  69.         int selectedDirection = -1;
  70.         for (int i = 0; i < value.length; ++i) {
  71.             if (direction[i] != 0 && value[i] > selectedValue) {
  72.                 selectedIndex     = i;
  73.                 selectedValue     = value[i];
  74.                 selectedDirection = direction[i];
  75.             }
  76.         }
  77.         if (selectedIndex < 0) {
  78.             exhausted = true;
  79.         } else {

  80.             // prepare next permutation

  81.             // swap selected and peer elements
  82.             final int selectedPeer   = selectedIndex + selectedDirection;
  83.             final T tmp              = permuted.get(selectedIndex);
  84.             permuted.set(selectedIndex, permuted.get(selectedPeer));
  85.             permuted.set(selectedPeer, tmp);
  86.             value[selectedIndex]     = value[selectedPeer];
  87.             value[selectedPeer]      = selectedValue;
  88.             direction[selectedIndex] = direction[selectedPeer];
  89.             if (selectedPeer == 0 || selectedPeer == permuted.size() - 1 ||
  90.                 value[selectedPeer + selectedDirection] > selectedValue) {
  91.                 // we cannot move anymore
  92.                 direction[selectedPeer] = 0;
  93.             } else {
  94.                 // we will continue moving in the same direction
  95.                 direction[selectedPeer] = selectedDirection;
  96.             }

  97.             // enable motion again for greater elements, towards selected one
  98.             for (int i = 0; i < selectedIndex; ++i) {
  99.                 if (value[i] > selectedValue) {
  100.                     direction[i] = +1;
  101.                 }
  102.             }
  103.             for (int i = selectedIndex + 1; i < value.length; ++i) {
  104.                 if (value[i] > selectedValue) {
  105.                     direction[i] = -1;
  106.                 }
  107.             }

  108.         }

  109.         return current;

  110.     }

  111. }