maedhroz commented on code in PR #3835:
URL: https://github.com/apache/cassandra/pull/3835#discussion_r1932632830


##########
test/unit/accord/utils/Gens.java:
##########
@@ -336,24 +507,70 @@ public static <T> Gen<Gen<T>> mixedDistribution(T... list)
     public static <T> Gen<Gen<T>> mixedDistribution(List<T> list)
     {
         return rs -> {
-            switch (rs.nextInt(0, 2))
+            switch (rs.nextInt(0, 4))
             {
                 case 0: // uniform
                     return r -> list.get(rs.nextInt(0, list.size()));
-                case 1: // zipf
+                case 1: // median biased
+                    int median = rs.nextInt(0, list.size());
+                    return r -> list.get(r.nextBiasedInt(0, median, 
list.size()));
+                case 2: // zipf
                     List<T> array = list;
                     if (rs.nextBoolean())
                     {
                         array = new ArrayList<>(list);
                         Collections.reverse(array);
                     }
                     return pickZipf(array);
+                case 3: // random weight
+                    return randomWeights(list).next(rs);
                 default:
                     throw new AssertionError();
             }
         };
     }
 
+    public static <T> Gen<Gen.IntGen> mixedDistribution(int[] list)
+    {
+        return rs -> {
+            switch (rs.nextInt(0, 4))
+            {
+                case 0: // uniform
+                    return r -> list[rs.nextInt(0, list.length)];
+                case 1: // median biased
+                    int median = rs.nextInt(0, list.length);
+                    return r -> list[r.nextBiasedInt(0, median, list.length)];
+                case 2: // zipf
+                    int[] array = list;
+                    if (rs.nextBoolean())
+                    {
+                        array = Arrays.copyOf(array, array.length);
+                        reverse(array);
+                    }
+                    return pickZipf(array);
+                case 3: // random weight
+                    return randomWeights(list).next(rs);
+                default:
+                    throw new AssertionError();
+            }
+        };
+    }

Review Comment:
   There are some things in this class that we might able to slim down with 
some tweaking of types. This method might be able to go away with this patch, 
for instance:
   
   ```
   Subject: [PATCH] DRY up mixedDistribution()
   ---
   Index: test/unit/accord/utils/Gens.java
   IDEA additional info:
   Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
   <+>UTF-8
   ===================================================================
   diff --git a/test/unit/accord/utils/Gens.java 
b/test/unit/accord/utils/Gens.java
   --- a/test/unit/accord/utils/Gens.java       (revision 
7bc06a322c211f52fbb6df60f2058a32cd57dafc)
   +++ b/test/unit/accord/utils/Gens.java       (date 1738086893079)
   @@ -331,15 +331,16 @@
                throw new IllegalArgumentException("Range is too large; min=" + 
minInclusive + ", max=" + maxExclusive);
            if (numBuckets <= 0 || numBuckets > domainSize)
                throw new IllegalArgumentException("Num buckets must be between 
1 and " + domainSize + "; given " + numBuckets);
   -        int[] bucket, indexes;
   -        bucket = new int[numBuckets];
   +        Integer[] bucket, indexes;
   +        bucket = new Integer[numBuckets];
            int delta = domainSize / numBuckets;
            for (int i = 0; i < numBuckets; i++)
                bucket[i] = minInclusive + i * delta;
   -        indexes = IntStream.range(0, bucket.length).toArray();
   -        Gen<Gen.IntGen> indexDistro = mixedDistribution(indexes);
   +
   +        indexes = (Integer[]) IntStream.range(0, 
bucket.length).boxed().toArray();
   +        Gen<Gen<Integer>> indexDistro = mixedDistribution(indexes);
            return rs -> {
   -            Gen.IntGen indexGen = indexDistro.next(rs);
   +            Gen<Integer> indexGen = indexDistro.next(rs);
                switch (rs.nextInt(0, 2))
                {
                    case 0: // uniform
   @@ -530,30 +531,9 @@
            };
        }
    
   -    public static <T> Gen<Gen.IntGen> mixedDistribution(int[] list)
   +    public static Gen<Gen<Integer>> mixedDistribution(Integer[] list)
        {
   -        return rs -> {
   -            switch (rs.nextInt(0, 4))
   -            {
   -                case 0: // uniform
   -                    return r -> list[rs.nextInt(0, list.length)];
   -                case 1: // median biased
   -                    int median = rs.nextInt(0, list.length);
   -                    return r -> list[r.nextBiasedInt(0, median, 
list.length)];
   -                case 2: // zipf
   -                    int[] array = list;
   -                    if (rs.nextBoolean())
   -                    {
   -                        array = Arrays.copyOf(array, array.length);
   -                        reverse(array);
   -                    }
   -                    return pickZipf(array);
   -                case 3: // random weight
   -                    return randomWeights(list).next(rs);
   -                default:
   -                    throw new AssertionError();
   -            }
   -        };
   +        return mixedDistribution(Arrays.asList(list));
        }
    
        /**
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to