This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch feature_NUMBERS-215
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git


The following commit(s) were added to refs/heads/feature_NUMBERS-215 by this 
push:
     new dcee12e7 NUMBERS-215: Iterate over k-partitions.
dcee12e7 is described below

commit dcee12e775bae241608103e824c7746dfbe2f091
Author: Gilles Sadowski <[email protected]>
AuthorDate: Sat Sep 19 13:55:00 2026 +0200

    NUMBERS-215: Iterate over k-partitions.
    
    Added documentation.
    One method changed to "static".
---
 .../commons/numbers/combinatorics/Stirling.java    | 57 ++++++++++++++++------
 .../numbers/combinatorics/StirlingTest.java        |  3 +-
 2 files changed, 42 insertions(+), 18 deletions(-)

diff --git 
a/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/Stirling.java
 
b/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/Stirling.java
index df04d8a6..2fc8a4a4 100644
--- 
a/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/Stirling.java
+++ 
b/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/Stirling.java
@@ -305,11 +305,33 @@ public final class Stirling {
 
     /**
      * From a collection of {@code n} items, generates all partitions that 
contains {@code k} subsets.
-     * The number of partitions is {@link #stirlingS2(int,int) stirlingS2(n, 
k)}.
+     * For example:
+     * <pre>{@code
+     * Stirling.S2.of(4, 2)
+     *     .stream()
+     *     .forEach(p -> System.out.println(java.util.Arrays.deepToString(p)));
+     * }</pre>
+     * will output
+     * <pre>
+     * [[0, 1, 2], [3]]
+     * [[0, 1, 3], [2]]
+     * [[0, 1], [2, 3]]
+     * [[0, 2, 3], [1]]
+     * [[0, 2], [1, 3]]
+     * [[0, 3], [1, 2]]
+     * [[0], [1, 2, 3]]
+     * </pre>
+     *
+     * <p>
+     * Method {@link #get() S2.of(n, k).get()} returns the number of 
partitions.
+     * </p>
+     *
+     * <p>
      * A <a 
href="https://mathworld.wolfram.com/RestrictedGrowthString.html";>restrictive 
growth string
      * (RGS)</a> is used internally.  RGS uses integers to represent items:  
Position (index) in the
      * RGS array is the same as in the original list to be partitioned, value 
is the "group" to which
      * this element belongs in a given partition.
+     * </p>
      */
     public static final class S2 {
         /** Number of sublists in every partition (aka "k"). */
@@ -393,23 +415,18 @@ public final class Stirling {
         }
 
         /**
-         * Iteration wrapped in a stream, where each element is a partition
-         * of the given {@code items}.
+         * Factory method for iterating on the partitions of the given list
+         * of {@code items}.
          *
+         * @param k Number of sublists in each partition.
          * @param items Items to be partitioned.
          * @return a stream (without duplicate or "null" elements).
-         * @throws IllegalArgumentException if the number of {@code items} does
-         * not match the {@link #of(int,int) first argument of the factory 
method}.
          *
          * @param <T> Item type.
          */
-        public <T> Stream<List<List<T>>> stream(List<T> items) {
-            if (items.size() != numberOfElements) {
-                throw new 
CombinatoricsException(CombinatoricsException.MISMATCH,
-                                                 numberOfElements, 
items.size());
-            }
-
-            return stream().map(o -> mapPartition(o, items));
+        public static <T> Stream<List<List<T>>> stream(List<T> items,
+                                                       int k) {
+            return of(items.size(), k).stream().map(o -> mapPartition(o, 
items, k));
         }
 
         /**
@@ -423,7 +440,13 @@ public final class Stirling {
          * @param <T> Item type.
          */
         public <T> Stream<List<List<T>>> stream(T... items) {
-            return stream(Arrays.asList(items));
+            if (items.length != numberOfElements) {
+                throw new 
CombinatoricsException(CombinatoricsException.MISMATCH,
+                                                 numberOfElements, 
items.length);
+            }
+
+            final List<T> list = Arrays.asList(items);
+            return stream().map(o -> mapPartition(o, list, numberOfSubsets));
         }
 
         /**
@@ -465,13 +488,15 @@ public final class Stirling {
          *
          * @param p Partition.
          * @param items List of objects.
+         * @param k Number of sublists.
          * @return the mapped partition.
          *
          * @param <T> Item type.
          */
-        private <T> List<List<T>> mapPartition(int[][] p,
-                                               List<T> items) {
-            final List<List<T>> out = new ArrayList<>(numberOfSubsets);
+        private static <T> List<List<T>> mapPartition(int[][] p,
+                                                      List<T> items,
+                                                      int k) {
+            final List<List<T>> out = new ArrayList<>(k);
 
             for (int[] subset : p) {
                 final List<T> customSubset = new ArrayList<>(subset.length);
diff --git 
a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/StirlingTest.java
 
b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/StirlingTest.java
index bab5c8a9..e53ae9a3 100644
--- 
a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/StirlingTest.java
+++ 
b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/StirlingTest.java
@@ -401,8 +401,7 @@ class StirlingTest {
         items.add(a);
         items.add(b);
         items.add(c);
-        final List<List<List<String>>> out = Stirling.S2.of(3, 2)
-            .stream(items)
+        final List<List<List<String>>> out = Stirling.S2.stream(items, 2)
             .collect(Collectors.toList());
 
         Assertions.assertEquals(3, out.size());

Reply via email to